refac: SammoAPI에서 인자, 반환 타입을 사전 지정 가능

This commit is contained in:
2022-03-25 22:48:34 +09:00
parent da91d66179
commit 11e28b7a87
5 changed files with 31 additions and 18 deletions
+24 -10
View File
@@ -1,13 +1,23 @@
import type { InvalidResponse } from "./defs";
import { APIPathGen } from "./util/APIPathGen";
import type { InvalidResponse, ReserveBulkCommandResponse } from "./defs";
import type { Args } from "./processing/args";
import { callSammoAPI, type ValidResponse } from "./util/callSammoAPI";
export type { ValidResponse, InvalidResponse };
async function done<ResultType extends ValidResponse>(args?: Record<string, unknown> | Record<string, unknown>[]): Promise<ResultType>;
async function done<ResultType extends ValidResponse>(args: Record<string, unknown> | Record<string, unknown>[] | undefined, returnError: false): Promise<ResultType>;
async function done<ResultType extends ValidResponse, ErrorType extends InvalidResponse>(args: Record<string, unknown> | Record<string, unknown>[] | undefined, returnError: true): Promise<ResultType | ErrorType>;
import { APIPathGen } from "./util/APIPathGen.js";
async function done<ResultType extends ValidResponse, ErrorType extends InvalidResponse>(args?: Record<string, unknown> | Record<string, unknown>[], returnError = false): Promise<ResultType | ErrorType> {
type RawArgType = Record<string, unknown>|Record<string, unknown>[];
interface CallbackT<ResultType extends ValidResponse, ErrorType extends InvalidResponse, ArgType extends RawArgType>{
(args?: ArgType): Promise<ResultType>;
(args: ArgType | undefined, returnError: false): Promise<ResultType>;
(args: ArgType | undefined, returnError: true): Promise<ResultType | ErrorType>;
}
async function done<ResultType extends ValidResponse>(args?: RawArgType): Promise<ResultType>;
async function done<ResultType extends ValidResponse>(args: RawArgType | undefined, returnError: false): Promise<ResultType>;
async function done<ResultType extends ValidResponse, ErrorType extends InvalidResponse>(args: RawArgType | undefined, returnError: true): Promise<ResultType | ErrorType>;
async function done<ResultType extends ValidResponse, ErrorType extends InvalidResponse>(args?: RawArgType, returnError = false): Promise<ResultType | ErrorType> {
console.error(`Can't directly call. ${args}, ${returnError}. Use auto-generated path API.`);
return callSammoAPI<ResultType, ErrorType>([], args, true);
}
@@ -23,7 +33,11 @@ const apiRealPath = {
PushCommand: done,
RepeatCommand: done,
ReserveCommand: done,
ReserveBulkCommand: done,
ReserveBulkCommand: done as CallbackT<ReserveBulkCommandResponse, InvalidResponse, {
turnList: number[],
action: string,
arg: Args
}[]>,
},
General: {
Join: done,
@@ -54,11 +68,11 @@ const apiRealPath = {
},
} as const;
export const SammoAPI = APIPathGen<typeof done, typeof apiRealPath>(apiRealPath, (path: string[]) => {
return (args?: Record<string, unknown> | Record<string, unknown>[], returnError?: boolean) => {
export const SammoAPI = APIPathGen(apiRealPath, (path: string[]) => {
return (args?: RawArgType, returnError?: boolean) => {
if (returnError) {
return callSammoAPI(path.join('/'), args, true);
}
return callSammoAPI(path.join('/'), args);
};
});
}) as typeof apiRealPath;