refac: SammoAPI에서 인자, 반환 타입을 사전 지정 가능
This commit is contained in:
@@ -636,7 +636,7 @@ async function reserveCommandDirect(args: [number[], TurnObj][], reload = true):
|
||||
}
|
||||
|
||||
try {
|
||||
await SammoAPI.Command.ReserveBulkCommand<ReserveBulkCommandResponse>(query);
|
||||
await SammoAPI.Command.ReserveBulkCommand(query);
|
||||
releaseSelectedTurnList();
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
|
||||
+24
-10
@@ -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;
|
||||
@@ -20,11 +20,11 @@ const apiRealPath = {
|
||||
},
|
||||
} 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) => {
|
||||
if (returnError) {
|
||||
return callSammoAPI(path.join('/'), args, true);
|
||||
}
|
||||
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<V extends () => unknown, T extends { [property: string]: SubValue<V> }>(obj: T, callback: (path: string[]) => V, path?: string[]): T {
|
||||
export function APIPathGen(obj, callback, path) {
|
||||
return new Proxy(obj, {
|
||||
get(target, key: string) {
|
||||
let nextPath: string[];
|
||||
get(target, key) {
|
||||
let nextPath;
|
||||
if (path === undefined) {
|
||||
nextPath = [key];
|
||||
}
|
||||
Reference in New Issue
Block a user