작업중
This commit is contained in:
@@ -0,0 +1,254 @@
|
||||
import ky from 'ky';
|
||||
import type { APIExecuter, APINamespace, HttpMethod, InvalidResponse, RawArgType, ValidResponse } from '../api/base.js';
|
||||
import isArray from 'lodash-es/isArray.js';
|
||||
import isEmpty from 'lodash-es/isEmpty.js';
|
||||
|
||||
const apiPath = process.env.API_ROOT_PATH ?? process.env.VITE_API_ROOT_PATH ?? '/api';
|
||||
|
||||
interface BasicAPICallT<
|
||||
ArgType extends RawArgType,
|
||||
ResultType extends ValidResponse,
|
||||
ErrorType extends InvalidResponse
|
||||
> {
|
||||
(args: ArgType): Promise<ResultType>;
|
||||
(args: ArgType, returnError: false): Promise<ResultType>;
|
||||
(args: ArgType, returnError: true): Promise<ResultType | ErrorType>;
|
||||
}
|
||||
|
||||
interface EmptyAPICallT<ResultType extends ValidResponse, ErrorType extends InvalidResponse> {
|
||||
(): Promise<ResultType>;
|
||||
(args: undefined): Promise<ResultType>;
|
||||
(args: undefined, returnError: false): Promise<ResultType>;
|
||||
(args: undefined, returnError: true): Promise<ResultType | ErrorType>;
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
export type ArgTypeOf<T> = T extends APICallT<infer A, any, any> ? A : never;
|
||||
|
||||
|
||||
export type APICallT<
|
||||
ArgType extends RawArgType,
|
||||
ResultType extends ValidResponse = ValidResponse,
|
||||
ErrorType extends InvalidResponse = InvalidResponse
|
||||
> = ArgType extends undefined ? EmptyAPICallT<ResultType, ErrorType> : BasicAPICallT<ArgType, ResultType, ErrorType>;
|
||||
|
||||
export type APITail = typeof GET | typeof POST | typeof PUT | typeof PATCH | typeof HEAD | typeof DELETE;
|
||||
|
||||
const httpMethodMap = new Map<APITail, HttpMethod>([
|
||||
[GET, "get"],
|
||||
[POST, "post"],
|
||||
[PUT, "put"],
|
||||
[PATCH, "patch"],
|
||||
[HEAD, "head"],
|
||||
[DELETE, "delete"],
|
||||
]);
|
||||
|
||||
export function extractHttpMethod(tail: APITail): HttpMethod {
|
||||
return httpMethodMap.get(tail) ?? "post";
|
||||
}
|
||||
|
||||
|
||||
export async function GET<ResultType extends ValidResponse, ArgType extends undefined = undefined>(
|
||||
args?: ArgType
|
||||
): Promise<ResultType>;
|
||||
export async function GET<ResultType extends ValidResponse, ArgType extends undefined = undefined>(
|
||||
args: ArgType | undefined,
|
||||
returnError: false
|
||||
): Promise<ResultType>;
|
||||
export async function GET<
|
||||
ResultType extends ValidResponse,
|
||||
ErrorType extends InvalidResponse,
|
||||
ArgType extends undefined = undefined
|
||||
>(args: ArgType | undefined, returnError: true): Promise<ResultType | ErrorType>;
|
||||
export async function GET<
|
||||
ResultType extends ValidResponse,
|
||||
ErrorType extends InvalidResponse,
|
||||
ArgType extends undefined = undefined
|
||||
>(args?: ArgType, returnError = false): Promise<ResultType | ErrorType> {
|
||||
console.error(`Can't directly call GET. ${args}, ${returnError}. Use auto-generated path API.`);
|
||||
return callClientAPI<ResultType, ErrorType>("get", [], args, undefined, true);
|
||||
}
|
||||
|
||||
export async function POST<ResultType extends ValidResponse, ArgType extends RawArgType = RawArgType>(
|
||||
args?: ArgType
|
||||
): Promise<ResultType>;
|
||||
export async function POST<ResultType extends ValidResponse, ArgType extends RawArgType = RawArgType>(
|
||||
args: ArgType | undefined,
|
||||
returnError: false
|
||||
): Promise<ResultType>;
|
||||
export async function POST<
|
||||
ResultType extends ValidResponse,
|
||||
ErrorType extends InvalidResponse,
|
||||
ArgType extends RawArgType = RawArgType
|
||||
>(args: ArgType | undefined, returnError: true): Promise<ResultType | ErrorType>;
|
||||
export async function POST<
|
||||
ResultType extends ValidResponse,
|
||||
ErrorType extends InvalidResponse,
|
||||
ArgType extends RawArgType = RawArgType
|
||||
>(args?: ArgType, returnError = false): Promise<ResultType | ErrorType> {
|
||||
console.error(`Can't directly call POST. ${args}, ${returnError}. Use auto-generated path API.`);
|
||||
return callClientAPI<ResultType, ErrorType>("post", [], args, undefined, true);
|
||||
}
|
||||
|
||||
export async function PUT<ResultType extends ValidResponse, ArgType extends RawArgType = RawArgType>(
|
||||
args?: ArgType
|
||||
): Promise<ResultType>;
|
||||
export async function PUT<ResultType extends ValidResponse, ArgType extends RawArgType = RawArgType>(
|
||||
args: ArgType | undefined,
|
||||
returnError: false
|
||||
): Promise<ResultType>;
|
||||
export async function PUT<
|
||||
ResultType extends ValidResponse,
|
||||
ErrorType extends InvalidResponse,
|
||||
ArgType extends RawArgType = RawArgType
|
||||
>(args: ArgType | undefined, returnError: true): Promise<ResultType | ErrorType>;
|
||||
export async function PUT<
|
||||
ResultType extends ValidResponse,
|
||||
ErrorType extends InvalidResponse,
|
||||
ArgType extends RawArgType = RawArgType
|
||||
>(args?: ArgType, returnError = false): Promise<ResultType | ErrorType> {
|
||||
console.error(`Can't directly call PUT. ${args}, ${returnError}. Use auto-generated path API.`);
|
||||
return callClientAPI<ResultType, ErrorType>("put", [], args, undefined, true);
|
||||
}
|
||||
|
||||
export async function PATCH<ResultType extends ValidResponse, ArgType extends RawArgType = RawArgType>(
|
||||
args?: ArgType
|
||||
): Promise<ResultType>;
|
||||
export async function PATCH<ResultType extends ValidResponse, ArgType extends RawArgType = RawArgType>(
|
||||
args: ArgType | undefined,
|
||||
returnError: false
|
||||
): Promise<ResultType>;
|
||||
export async function PATCH<
|
||||
ResultType extends ValidResponse,
|
||||
ErrorType extends InvalidResponse,
|
||||
ArgType extends RawArgType = RawArgType
|
||||
>(args: ArgType | undefined, returnError: true): Promise<ResultType | ErrorType>;
|
||||
export async function PATCH<
|
||||
ResultType extends ValidResponse,
|
||||
ErrorType extends InvalidResponse,
|
||||
ArgType extends RawArgType = RawArgType
|
||||
>(args?: ArgType, returnError = false): Promise<ResultType | ErrorType> {
|
||||
console.error(`Can't directly call PATCH. ${args}, ${returnError}. Use auto-generated path API.`);
|
||||
return callClientAPI<ResultType, ErrorType>("patch", [], args, undefined, true);
|
||||
}
|
||||
|
||||
export async function HEAD<ResultType extends ValidResponse, ArgType extends undefined = undefined>(
|
||||
args?: ArgType
|
||||
): Promise<ResultType>;
|
||||
export async function HEAD<ResultType extends ValidResponse, ArgType extends undefined = undefined>(
|
||||
args: ArgType | undefined,
|
||||
returnError: false
|
||||
): Promise<ResultType>;
|
||||
export async function HEAD<
|
||||
ResultType extends ValidResponse,
|
||||
ErrorType extends InvalidResponse,
|
||||
ArgType extends undefined = undefined
|
||||
>(args: ArgType | undefined, returnError: true): Promise<ResultType | ErrorType>;
|
||||
export async function HEAD<
|
||||
ResultType extends ValidResponse,
|
||||
ErrorType extends InvalidResponse,
|
||||
ArgType extends undefined = undefined
|
||||
>(args?: ArgType, returnError = false): Promise<ResultType | ErrorType> {
|
||||
console.error(`Can't directly call HEAD. ${args}, ${returnError}. Use auto-generated path API.`);
|
||||
return callClientAPI<ResultType, ErrorType>("head", [], args, undefined, true);
|
||||
}
|
||||
|
||||
export async function DELETE<ResultType extends ValidResponse, ArgType extends RawArgType = RawArgType>(
|
||||
args?: ArgType
|
||||
): Promise<ResultType>;
|
||||
export async function DELETE<ResultType extends ValidResponse, ArgType extends RawArgType = RawArgType>(
|
||||
args: ArgType | undefined,
|
||||
returnError: false
|
||||
): Promise<ResultType>;
|
||||
export async function DELETE<
|
||||
ResultType extends ValidResponse,
|
||||
ErrorType extends InvalidResponse,
|
||||
ArgType extends RawArgType = RawArgType
|
||||
>(args: ArgType | undefined, returnError: true): Promise<ResultType | ErrorType>;
|
||||
export async function DELETE<
|
||||
ResultType extends ValidResponse,
|
||||
ErrorType extends InvalidResponse,
|
||||
ArgType extends RawArgType = RawArgType
|
||||
>(args?: ArgType, returnError = false): Promise<ResultType | ErrorType> {
|
||||
console.error(`Can't directly call DELETE. ${args}, ${returnError}. Use auto-generated path API.`);
|
||||
return callClientAPI<ResultType, ErrorType>("patch", [], args, undefined, true);
|
||||
}
|
||||
|
||||
|
||||
export async function callClientAPI<ResultType extends ValidResponse>(
|
||||
method: HttpMethod,
|
||||
path: string | string[],
|
||||
args: RawArgType,
|
||||
paramArgs: Record<string, string | number> | undefined
|
||||
): Promise<ResultType>;
|
||||
export async function callClientAPI<ResultType extends ValidResponse>(
|
||||
method: HttpMethod,
|
||||
path: string | string[],
|
||||
args: RawArgType,
|
||||
paramArgs: Record<string, string | number> | undefined,
|
||||
returnError: false
|
||||
): Promise<ResultType>;
|
||||
export async function callClientAPI<ResultType extends ValidResponse, ErrorType extends InvalidResponse>(
|
||||
method: HttpMethod,
|
||||
path: string | string[],
|
||||
args: RawArgType,
|
||||
paramArgs: Record<string, string | number> | undefined,
|
||||
returnError: true
|
||||
): Promise<ResultType | ErrorType>;
|
||||
export async function callClientAPI<ResultType extends ValidResponse, ErrorType extends InvalidResponse>(
|
||||
method: HttpMethod,
|
||||
path: string | string[],
|
||||
args: RawArgType,
|
||||
paramArgs: Record<string, string | number> | undefined,
|
||||
returnError = false
|
||||
): Promise<ResultType | ErrorType> {
|
||||
if (isArray(path)) {
|
||||
path = path.join("/");
|
||||
}
|
||||
|
||||
if (args && isEmpty(args)) {
|
||||
args = undefined;
|
||||
}
|
||||
|
||||
const result = (await (() => {
|
||||
if (method == "get") {
|
||||
return ky(apiPath, {
|
||||
searchParams: {
|
||||
...paramArgs,
|
||||
...(args as typeof paramArgs),
|
||||
path,
|
||||
},
|
||||
method,
|
||||
headers: {
|
||||
"content-type": "application/json",
|
||||
},
|
||||
timeout: 30000,
|
||||
retry: 0,
|
||||
});
|
||||
}
|
||||
return ky(apiPath, {
|
||||
searchParams: {
|
||||
...paramArgs,
|
||||
path,
|
||||
},
|
||||
method,
|
||||
json: args,
|
||||
headers: {
|
||||
"content-type": "application/json",
|
||||
},
|
||||
timeout: 30000,
|
||||
retry: 0,
|
||||
});
|
||||
})().json()) as ErrorType | ResultType;
|
||||
|
||||
if (!result.result) {
|
||||
if (returnError) {
|
||||
return result;
|
||||
}
|
||||
throw result.reason;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
//TODO: type이 하나 만들어져야 함. 이것은 typescript의 type system으로.
|
||||
//TODO: const로 실제 구조가 하나 더 만들어 져야 함. 이것은 위의 type을 satisfy하여야 함.
|
||||
@@ -0,0 +1,56 @@
|
||||
import { APIPathGen } from "../api/APIPathGen";
|
||||
import { RawArgType } from "../api/base";
|
||||
import { APICallT, APITail, GET, POST, callClientAPI, extractHttpMethod } from "./clientAPI";
|
||||
|
||||
export type LoginResponse = {
|
||||
result: true,
|
||||
nextToken: [number, string] | undefined,
|
||||
}
|
||||
|
||||
export type LoginFailed = {
|
||||
result: false,
|
||||
reqOTP: boolean,
|
||||
reason: string,
|
||||
}
|
||||
|
||||
|
||||
export type AutoLoginNonceResponse = {
|
||||
result: true,
|
||||
loginNonce: string,
|
||||
};
|
||||
|
||||
export type AutoLoginResponse = {
|
||||
result: true,
|
||||
nextToken: [number, string] | undefined,
|
||||
}
|
||||
|
||||
|
||||
export type AutoLoginFailed = {
|
||||
result: false,
|
||||
silent: boolean,
|
||||
reason: string,
|
||||
}
|
||||
|
||||
const apiRealPath = {
|
||||
Login: {
|
||||
LoginByID: POST as APICallT<{
|
||||
username: string,
|
||||
password: string,
|
||||
}, LoginResponse, LoginFailed>,
|
||||
LoginByToken: POST as APICallT<{
|
||||
hashedToken: string,
|
||||
token_id: number,
|
||||
}, AutoLoginResponse, AutoLoginFailed>,
|
||||
ReqNonce: GET as APICallT<undefined, AutoLoginNonceResponse, AutoLoginFailed>
|
||||
},
|
||||
} as const;
|
||||
|
||||
export const SammoRootAPI = APIPathGen(apiRealPath, (path: string[], tail: APITail, pathParam) => {
|
||||
const method = extractHttpMethod(tail);
|
||||
return (args?: RawArgType, returnError?: boolean) => {
|
||||
if (returnError) {
|
||||
return callClientAPI(method, path.join('/'), args, pathParam, true);
|
||||
}
|
||||
return callClientAPI(method, path.join('/'), args, pathParam);
|
||||
};
|
||||
});
|
||||
Reference in New Issue
Block a user