From 00ceb3f8422170ea950fe6bc06ced0a7d5f8a2ba Mon Sep 17 00:00:00 2001 From: Hide_D Date: Fri, 25 Mar 2022 23:29:10 +0900 Subject: [PATCH] =?UTF-8?q?refac:=20SammoAPI=20Path=20=EC=A0=95=EC=9D=98?= =?UTF-8?q?=EC=97=90=20NumVar,=20StrVar=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hwe/ts/SammoAPI.ts | 26 ++++---------------------- hwe/ts/SammoRootAPI.ts | 11 +---------- hwe/ts/util/APIPathGen.d.ts | 20 +++++++++++++++++++- hwe/ts/util/APIPathGen.js | 10 ++++++++++ hwe/ts/util/callSammoAPI.ts | 17 +++++++++++++++++ 5 files changed, 51 insertions(+), 33 deletions(-) diff --git a/hwe/ts/SammoAPI.ts b/hwe/ts/SammoAPI.ts index c9c798d9..c7161f49 100644 --- a/hwe/ts/SammoAPI.ts +++ b/hwe/ts/SammoAPI.ts @@ -1,27 +1,9 @@ import type { InvalidResponse, ReserveBulkCommandResponse } from "./defs"; import type { Args } from "./processing/args"; -import { callSammoAPI, type ValidResponse } from "./util/callSammoAPI"; +import { callSammoAPI, done, type CallbackT, type RawArgType, type ValidResponse } from "./util/callSammoAPI"; export type { ValidResponse, InvalidResponse }; - import { APIPathGen } from "./util/APIPathGen.js"; -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); -} - const apiRealPath = { Betting: { Bet: done, @@ -33,11 +15,11 @@ const apiRealPath = { PushCommand: done, RepeatCommand: done, ReserveCommand: done, - ReserveBulkCommand: done as CallbackT, + }[], ReserveBulkCommandResponse>, }, General: { Join: done, @@ -75,4 +57,4 @@ export const SammoAPI = APIPathGen(apiRealPath, (path: string[]) => { } return callSammoAPI(path.join('/'), args); }; -}) as typeof apiRealPath; \ No newline at end of file +}); \ No newline at end of file diff --git a/hwe/ts/SammoRootAPI.ts b/hwe/ts/SammoRootAPI.ts index ffdfb8d5..4d5db970 100644 --- a/hwe/ts/SammoRootAPI.ts +++ b/hwe/ts/SammoRootAPI.ts @@ -1,17 +1,8 @@ import type { InvalidResponse } from "./defs"; import { APIPathGen } from "./util/APIPathGen"; -import { callSammoAPI, type ValidResponse } from "./util/callSammoAPI"; +import { callSammoAPI, done, type ValidResponse } from "./util/callSammoAPI"; export type { ValidResponse, InvalidResponse }; -async function done(args?: Record): Promise; -async function done(args: Record | undefined, returnError: false): Promise; -async function done(args: Record | undefined, returnError: true): Promise; - -async function done(args?: Record, returnError = false): Promise{ - console.error(`Can't directly call. ${args}, ${returnError}. Use auto-generated path API.`); - return callSammoAPI([], args, true); -} - const apiRealPath = { Login: { LoginByID: done, diff --git a/hwe/ts/util/APIPathGen.d.ts b/hwe/ts/util/APIPathGen.d.ts index 7da633e0..4e9748f6 100644 --- a/hwe/ts/util/APIPathGen.d.ts +++ b/hwe/ts/util/APIPathGen.d.ts @@ -1 +1,19 @@ -export function APIPathGen(obj: T, callback: (path: string[])=>unknown): T; \ No newline at end of file +export function APIPathGen(obj: T, callback: (path: string[])=>unknown): T; + +export function StrVar(): (next: NextCall)=>{ + [v in PathType]: NextCall +}; + +export function NumVar(next: NextCall):{ + [v: number]: NextCall +}; + +/* +const apiPath = { + SomePath: someFunc, + User: StrVar<'a'|'b'>()({ + Update: someFunc, + Delete: someFunc, + }) +} +*/ \ No newline at end of file diff --git a/hwe/ts/util/APIPathGen.js b/hwe/ts/util/APIPathGen.js index 36f2c5a6..0b877b40 100644 --- a/hwe/ts/util/APIPathGen.js +++ b/hwe/ts/util/APIPathGen.js @@ -1,6 +1,12 @@ export function APIPathGen(obj, callback, path) { return new Proxy(obj, { get(target, key) { + if(typeof key === 'number'){ + key = key.toString(); + } + else if(typeof key !== 'string'){ + throw `${key} is not string`; + } let nextPath; if (path === undefined) { nextPath = [key]; @@ -21,4 +27,8 @@ export function APIPathGen(obj, callback, path) { return APIPathGen(next, callback, nextPath); } }) +} + +export function StrVar(){ + return (next)=>next; } \ No newline at end of file diff --git a/hwe/ts/util/callSammoAPI.ts b/hwe/ts/util/callSammoAPI.ts index bfb0652d..cb809ec3 100644 --- a/hwe/ts/util/callSammoAPI.ts +++ b/hwe/ts/util/callSammoAPI.ts @@ -6,6 +6,14 @@ export type ValidResponse = { result: true } +export type RawArgType = Record|Record[]; + +export interface CallbackT{ + (args?: ArgType): Promise; + (args: ArgType | undefined, returnError: false): Promise; + (args: ArgType | undefined, returnError: true): Promise; +} + export async function callSammoAPI(path: string | string[], args?: Record | Record[]): Promise; export async function callSammoAPI(path: string | string[], args: Record | Record[] | undefined, returnError: false): Promise; export async function callSammoAPI(path: string | string[], args: Record | Record[] | undefined, returnError: true): Promise; @@ -29,4 +37,13 @@ export async function callSammoAPI(args?: RawArgType): Promise; +export async function done(args: RawArgType | undefined, returnError: false): Promise; +export async function done(args: RawArgType | undefined, returnError: true): Promise; + +export 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); } \ No newline at end of file