refac: SammoAPI에서 인자, 반환 타입을 사전 지정 가능
This commit is contained in:
@@ -636,7 +636,7 @@ async function reserveCommandDirect(args: [number[], TurnObj][], reload = true):
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await SammoAPI.Command.ReserveBulkCommand<ReserveBulkCommandResponse>(query);
|
await SammoAPI.Command.ReserveBulkCommand(query);
|
||||||
releaseSelectedTurnList();
|
releaseSelectedTurnList();
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(e);
|
console.error(e);
|
||||||
|
|||||||
+24
-10
@@ -1,13 +1,23 @@
|
|||||||
import type { InvalidResponse } from "./defs";
|
import type { InvalidResponse, ReserveBulkCommandResponse } from "./defs";
|
||||||
import { APIPathGen } from "./util/APIPathGen";
|
import type { Args } from "./processing/args";
|
||||||
import { callSammoAPI, type ValidResponse } from "./util/callSammoAPI";
|
import { callSammoAPI, type ValidResponse } from "./util/callSammoAPI";
|
||||||
export type { ValidResponse, InvalidResponse };
|
export type { ValidResponse, InvalidResponse };
|
||||||
|
|
||||||
async function done<ResultType extends ValidResponse>(args?: Record<string, unknown> | Record<string, unknown>[]): Promise<ResultType>;
|
import { APIPathGen } from "./util/APIPathGen.js";
|
||||||
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>;
|
|
||||||
|
|
||||||
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.`);
|
console.error(`Can't directly call. ${args}, ${returnError}. Use auto-generated path API.`);
|
||||||
return callSammoAPI<ResultType, ErrorType>([], args, true);
|
return callSammoAPI<ResultType, ErrorType>([], args, true);
|
||||||
}
|
}
|
||||||
@@ -23,7 +33,11 @@ const apiRealPath = {
|
|||||||
PushCommand: done,
|
PushCommand: done,
|
||||||
RepeatCommand: done,
|
RepeatCommand: done,
|
||||||
ReserveCommand: done,
|
ReserveCommand: done,
|
||||||
ReserveBulkCommand: done,
|
ReserveBulkCommand: done as CallbackT<ReserveBulkCommandResponse, InvalidResponse, {
|
||||||
|
turnList: number[],
|
||||||
|
action: string,
|
||||||
|
arg: Args
|
||||||
|
}[]>,
|
||||||
},
|
},
|
||||||
General: {
|
General: {
|
||||||
Join: done,
|
Join: done,
|
||||||
@@ -54,11 +68,11 @@ const apiRealPath = {
|
|||||||
},
|
},
|
||||||
} as const;
|
} as const;
|
||||||
|
|
||||||
export const SammoAPI = APIPathGen<typeof done, typeof apiRealPath>(apiRealPath, (path: string[]) => {
|
export const SammoAPI = APIPathGen(apiRealPath, (path: string[]) => {
|
||||||
return (args?: Record<string, unknown> | Record<string, unknown>[], returnError?: boolean) => {
|
return (args?: RawArgType, returnError?: boolean) => {
|
||||||
if (returnError) {
|
if (returnError) {
|
||||||
return callSammoAPI(path.join('/'), args, true);
|
return callSammoAPI(path.join('/'), args, true);
|
||||||
}
|
}
|
||||||
return callSammoAPI(path.join('/'), args);
|
return callSammoAPI(path.join('/'), args);
|
||||||
};
|
};
|
||||||
});
|
}) as typeof apiRealPath;
|
||||||
@@ -20,11 +20,11 @@ const apiRealPath = {
|
|||||||
},
|
},
|
||||||
} as const;
|
} as const;
|
||||||
|
|
||||||
export const SammoRootAPI = APIPathGen<typeof done, typeof apiRealPath>(apiRealPath, (path: string[]) => {
|
export const SammoRootAPI = APIPathGen(apiRealPath, (path: string[]) => {
|
||||||
return (args?: Record<string, unknown>, returnError?: boolean) => {
|
return (args?: Record<string, unknown>, returnError?: boolean) => {
|
||||||
if (returnError) {
|
if (returnError) {
|
||||||
return callSammoAPI(path.join('/'), args, true);
|
return callSammoAPI(path.join('/'), args, true);
|
||||||
}
|
}
|
||||||
return callSammoAPI(path.join('/'), args);
|
return callSammoAPI(path.join('/'), args);
|
||||||
};
|
};
|
||||||
});
|
}) as typeof apiRealPath;
|
||||||
Vendored
+1
@@ -0,0 +1 @@
|
|||||||
|
export function APIPathGen<T>(obj: T, callback: (path: string[])=>unknown): T;
|
||||||
@@ -1,9 +1,7 @@
|
|||||||
type SubValue<V extends (...args: unknown[]) => unknown> = V | { [property: string]: SubValue<V> };
|
export function APIPathGen(obj, callback, path) {
|
||||||
|
|
||||||
export function APIPathGen<V extends () => unknown, T extends { [property: string]: SubValue<V> }>(obj: T, callback: (path: string[]) => V, path?: string[]): T {
|
|
||||||
return new Proxy(obj, {
|
return new Proxy(obj, {
|
||||||
get(target, key: string) {
|
get(target, key) {
|
||||||
let nextPath: string[];
|
let nextPath;
|
||||||
if (path === undefined) {
|
if (path === undefined) {
|
||||||
nextPath = [key];
|
nextPath = [key];
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user