강력한 API 타입 체크
This commit is contained in:
@@ -0,0 +1,202 @@
|
||||
import { APIExecuter } from "../api/base";
|
||||
|
||||
export type HttpMethod = 'get' | 'post' | 'put' | 'delete' | 'patch' | 'head';
|
||||
export type RawArgType = Record<string, unknown> | Record<string, unknown>[] | undefined;
|
||||
|
||||
|
||||
export 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>;
|
||||
}
|
||||
|
||||
export 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> {
|
||||
throw `Can't directly call GET. ${args}, ${returnError}. Use auto-generated path API.`
|
||||
}
|
||||
|
||||
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> {
|
||||
throw `Can't directly call POST. ${args}, ${returnError}. Use auto-generated path API.`
|
||||
}
|
||||
|
||||
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> {
|
||||
throw `Can't directly call PUT. ${args}, ${returnError}. Use auto-generated path API.`;
|
||||
}
|
||||
|
||||
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> {
|
||||
throw `Can't directly call PATCH. ${args}, ${returnError}. Use auto-generated path API.`
|
||||
}
|
||||
|
||||
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> {
|
||||
throw `Can't directly call HEAD. ${args}, ${returnError}. Use auto-generated path API.`;
|
||||
}
|
||||
|
||||
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> {
|
||||
throw `Can't directly call DELETE. ${args}, ${returnError}. Use auto-generated path API.`;
|
||||
}
|
||||
|
||||
|
||||
export interface ValidResponse {
|
||||
result: true;
|
||||
}
|
||||
|
||||
export type recoveryMethod = 'refreshEntirePage' | 'retryAPI' | 'gameLogin' | 'gatewayLogin' | 'gateway2FA' | 'gameQuota';
|
||||
export interface InvalidResponse {
|
||||
result: false;
|
||||
reason: string;
|
||||
recovery?: recoveryMethod;
|
||||
}
|
||||
|
||||
type ExtractValid<T> = T extends ValidResponse ? T : never;
|
||||
type ExtractInvalid<T> = T extends InvalidResponse ? T : never;
|
||||
|
||||
export type Callable = (...args: any)=>any;
|
||||
|
||||
export type ExtractResponse<T extends Callable> = ExtractValid<Awaited<ReturnType<T>>>;
|
||||
export type ExtractError<T extends Callable> = ExtractInvalid<Awaited<ReturnType<T>>>;
|
||||
export type ExtractQuery<T extends Callable> = Parameters<T>[0];
|
||||
|
||||
export type DefAPINamespace = {
|
||||
[key: string]: DefAPINamespace
|
||||
| typeof GET<any, any, any>
|
||||
| typeof POST<any, any, any>
|
||||
| typeof PUT<any, any, any>
|
||||
| typeof DELETE<any, any, any>
|
||||
| typeof PATCH<any, any, any>
|
||||
| typeof HEAD<any, any, any>
|
||||
;
|
||||
}
|
||||
|
||||
export type APICompatType<T extends Callable> = T extends APICallT<infer A, infer R, infer E> ? APICallT<A, R, E> : never;
|
||||
@@ -0,0 +1,45 @@
|
||||
import { type APICallT, GET, POST } from "./defs";
|
||||
|
||||
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,
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
export const structure = {
|
||||
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;
|
||||
Reference in New Issue
Block a user