From 5471672ee5636dbb0a1d82d2809d94100fc8675a Mon Sep 17 00:00:00 2001 From: hide_d Date: Wed, 29 Dec 2021 05:46:35 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20APIPathGen=20=EC=B6=94=EC=83=81?= =?UTF-8?q?=ED=99=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hwe/ts/SammoAPI.ts | 11 +++++++++-- hwe/ts/SammoRootAPI.ts | 9 ++++++++- hwe/ts/util/APIPathGen.ts | 26 ++++++-------------------- 3 files changed, 23 insertions(+), 23 deletions(-) diff --git a/hwe/ts/SammoAPI.ts b/hwe/ts/SammoAPI.ts index 31f9dada..db2338c9 100644 --- a/hwe/ts/SammoAPI.ts +++ b/hwe/ts/SammoAPI.ts @@ -7,7 +7,7 @@ async function done(args?: Record(args: Record | undefined, returnError: false): Promise; async function done(args: Record | undefined, returnError: true): Promise; -async function done(args?: Record, returnError = false): 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); } @@ -43,4 +43,11 @@ const apiRealPath = { }, } as const; -export const SammoAPI = APIPathGen(apiRealPath); \ No newline at end of file +export const SammoAPI = 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 diff --git a/hwe/ts/SammoRootAPI.ts b/hwe/ts/SammoRootAPI.ts index 4fb8a075..fe3f5413 100644 --- a/hwe/ts/SammoRootAPI.ts +++ b/hwe/ts/SammoRootAPI.ts @@ -20,4 +20,11 @@ const apiRealPath = { }, } as const; -export const SammoRootAPI = APIPathGen(apiRealPath); \ No newline at end of file +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 diff --git a/hwe/ts/util/APIPathGen.ts b/hwe/ts/util/APIPathGen.ts index 7ba5bed7..eee9c623 100644 --- a/hwe/ts/util/APIPathGen.ts +++ b/hwe/ts/util/APIPathGen.ts @@ -1,40 +1,26 @@ -import { InvalidResponse } from "@/defs"; -import { callSammoAPI, ValidResponse } from "./callSammoAPI"; +type SubValue unknown> = V | { [property: string]: SubValue }; -export type CurryCall = - ((args?: Record) => Promise) - | ((args: Record | undefined, returnError: false) => Promise) - | ((args: Record | undefined, returnError: true) => Promise); - -type SubValue = CurryCall | { [property: string]: SubValue }; - -export function APIPathGen },>(obj: T, path?: string[]): T { +export function APIPathGen unknown, T extends { [property: string]: SubValue }>(obj: T, callback: (path: string[]) => V, path?: string[]): T { return new Proxy(obj, { get(target, key: string) { let nextPath: string[]; if (path === undefined) { nextPath = [key]; } - else{ + else { nextPath = path; nextPath.push(key); } if (!(key in target)) { - throw `${nextPath.join('/')} is not exists`; + throw `${nextPath} is not exists`; } const next = target[key]; if (typeof (next) === 'function') { - const callAPI: CurryCall = (args: Record | undefined, returnError?: boolean) => { - if (returnError) { - return callSammoAPI(nextPath.join('/'), args, returnError); - } - return callSammoAPI(nextPath.join('/'), args, false); - } - return callAPI; + return callback(nextPath); } - return APIPathGen(next, nextPath); + return APIPathGen(next, callback, nextPath); } }) } \ No newline at end of file