From 9d868cb2f41f0eee60ab2752797f2565a9bb07f3 Mon Sep 17 00:00:00 2001 From: Hide_D Date: Fri, 25 Mar 2022 22:48:34 +0900 Subject: [PATCH] =?UTF-8?q?refac:=20SammoAPI=EC=97=90=EC=84=9C=20=EC=9D=B8?= =?UTF-8?q?=EC=9E=90,=20=EB=B0=98=ED=99=98=20=ED=83=80=EC=9E=85=EC=9D=84?= =?UTF-8?q?=20=EC=82=AC=EC=A0=84=20=EC=A7=80=EC=A0=95=20=EA=B0=80=EB=8A=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hwe/ts/PartialReservedCommand.vue | 2 +- hwe/ts/SammoAPI.ts | 34 ++++++++++++++------ hwe/ts/SammoRootAPI.ts | 4 +-- hwe/ts/util/APIPathGen.d.ts | 1 + hwe/ts/util/{APIPathGen.ts => APIPathGen.js} | 8 ++--- 5 files changed, 31 insertions(+), 18 deletions(-) create mode 100644 hwe/ts/util/APIPathGen.d.ts rename hwe/ts/util/{APIPathGen.ts => APIPathGen.js} (61%) diff --git a/hwe/ts/PartialReservedCommand.vue b/hwe/ts/PartialReservedCommand.vue index e2403bde..b49c894c 100644 --- a/hwe/ts/PartialReservedCommand.vue +++ b/hwe/ts/PartialReservedCommand.vue @@ -636,7 +636,7 @@ async function reserveCommandDirect(args: [number[], TurnObj][], reload = true): } try { - await SammoAPI.Command.ReserveBulkCommand(query); + await SammoAPI.Command.ReserveBulkCommand(query); releaseSelectedTurnList(); } catch (e) { console.error(e); diff --git a/hwe/ts/SammoAPI.ts b/hwe/ts/SammoAPI.ts index 74308537..c9c798d9 100644 --- a/hwe/ts/SammoAPI.ts +++ b/hwe/ts/SammoAPI.ts @@ -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(args?: Record | Record[]): Promise; -async function done(args: Record | Record[] | undefined, returnError: false): Promise; -async function done(args: Record | Record[] | undefined, returnError: true): Promise; +import { APIPathGen } from "./util/APIPathGen.js"; -async function done(args?: Record | Record[], returnError = false): Promise { +type RawArgType = Record|Record[]; + +interface CallbackT{ + (args?: ArgType): Promise; + (args: ArgType | undefined, returnError: false): Promise; + (args: ArgType | undefined, returnError: true): Promise; +} + +async function done(args?: RawArgType): Promise; +async function done(args: RawArgType | undefined, returnError: false): Promise; +async function done(args: RawArgType | undefined, returnError: true): Promise; + +async function done(args?: RawArgType, returnError = false): Promise { console.error(`Can't directly call. ${args}, ${returnError}. Use auto-generated path API.`); return callSammoAPI([], args, true); } @@ -23,7 +33,11 @@ const apiRealPath = { PushCommand: done, RepeatCommand: done, ReserveCommand: done, - ReserveBulkCommand: done, + ReserveBulkCommand: done as CallbackT, }, General: { Join: done, @@ -54,11 +68,11 @@ const apiRealPath = { }, } as const; -export const SammoAPI = APIPathGen(apiRealPath, (path: string[]) => { - return (args?: Record | Record[], 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); }; -}); \ No newline at end of file +}) as typeof apiRealPath; \ No newline at end of file diff --git a/hwe/ts/SammoRootAPI.ts b/hwe/ts/SammoRootAPI.ts index 1d1a8356..ffdfb8d5 100644 --- a/hwe/ts/SammoRootAPI.ts +++ b/hwe/ts/SammoRootAPI.ts @@ -20,11 +20,11 @@ const apiRealPath = { }, } as const; -export const SammoRootAPI = APIPathGen(apiRealPath, (path: string[]) => { +export const SammoRootAPI = APIPathGen(apiRealPath, (path: string[]) => { return (args?: Record, returnError?: boolean) => { if (returnError) { return callSammoAPI(path.join('/'), args, true); } return callSammoAPI(path.join('/'), args); }; -}); \ No newline at end of file +}) as typeof apiRealPath; \ No newline at end of file diff --git a/hwe/ts/util/APIPathGen.d.ts b/hwe/ts/util/APIPathGen.d.ts new file mode 100644 index 00000000..7da633e0 --- /dev/null +++ b/hwe/ts/util/APIPathGen.d.ts @@ -0,0 +1 @@ +export function APIPathGen(obj: T, callback: (path: string[])=>unknown): T; \ No newline at end of file diff --git a/hwe/ts/util/APIPathGen.ts b/hwe/ts/util/APIPathGen.js similarity index 61% rename from hwe/ts/util/APIPathGen.ts rename to hwe/ts/util/APIPathGen.js index eee9c623..36f2c5a6 100644 --- a/hwe/ts/util/APIPathGen.ts +++ b/hwe/ts/util/APIPathGen.js @@ -1,9 +1,7 @@ -type SubValue unknown> = V | { [property: string]: SubValue }; - -export function APIPathGen unknown, T extends { [property: string]: SubValue }>(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]; }