feat: APIPathGen 추상화

This commit is contained in:
2021-12-29 05:46:35 +09:00
parent 470672e367
commit 5471672ee5
3 changed files with 23 additions and 23 deletions
+9 -2
View File
@@ -7,7 +7,7 @@ async function done<ResultType extends ValidResponse>(args?: Record<string, unkn
async function done<ResultType extends ValidResponse>(args: Record<string, unknown> | undefined, returnError: false): Promise<ResultType>; async function done<ResultType extends ValidResponse>(args: Record<string, unknown> | undefined, returnError: false): Promise<ResultType>;
async function done<ResultType extends ValidResponse, ErrorType extends InvalidResponse>(args: Record<string, unknown> | undefined, returnError: true): Promise<ResultType | ErrorType>; async function done<ResultType extends ValidResponse, ErrorType extends InvalidResponse>(args: Record<string, unknown> | undefined, returnError: true): Promise<ResultType | ErrorType>;
async function done<ResultType extends ValidResponse, ErrorType extends InvalidResponse>(args?: Record<string, unknown>, returnError = false): Promise<ResultType | ErrorType>{ async function done<ResultType extends ValidResponse, ErrorType extends InvalidResponse>(args?: Record<string, unknown>, 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);
} }
@@ -43,4 +43,11 @@ const apiRealPath = {
}, },
} as const; } as const;
export const SammoAPI = APIPathGen(apiRealPath); export const SammoAPI = APIPathGen<typeof done, typeof apiRealPath>(apiRealPath, (path: string[]) => {
return (args?: Record<string, unknown>, returnError?: boolean) => {
if (returnError) {
return callSammoAPI(path.join('/'), args, true);
}
return callSammoAPI(path.join('/'), args);
};
});
+8 -1
View File
@@ -20,4 +20,11 @@ const apiRealPath = {
}, },
} as const; } as const;
export const SammoRootAPI = APIPathGen(apiRealPath); export const SammoRootAPI = APIPathGen<typeof done, typeof apiRealPath>(apiRealPath, (path: string[]) => {
return (args?: Record<string, unknown>, returnError?: boolean) => {
if (returnError) {
return callSammoAPI(path.join('/'), args, true);
}
return callSammoAPI(path.join('/'), args);
};
});
+6 -20
View File
@@ -1,40 +1,26 @@
import { InvalidResponse } from "@/defs"; type SubValue<V extends (...args: unknown[]) => unknown> = V | { [property: string]: SubValue<V> };
import { callSammoAPI, ValidResponse } from "./callSammoAPI";
export type CurryCall<ResultType extends ValidResponse, ErrorType extends InvalidResponse> = export function APIPathGen<V extends () => unknown, T extends { [property: string]: SubValue<V> }>(obj: T, callback: (path: string[]) => V, path?: string[]): T {
((args?: Record<string, unknown>) => Promise<ResultType>)
| ((args: Record<string, unknown> | undefined, returnError: false) => Promise<ResultType>)
| ((args: Record<string, unknown> | undefined, returnError: true) => Promise<ResultType | ErrorType>);
type SubValue<ResultType extends ValidResponse, ErrorType extends InvalidResponse> = CurryCall<ResultType, ErrorType> | { [property: string]: SubValue<ResultType, ErrorType> };
export function APIPathGen<ResultType extends ValidResponse, ErrorType extends InvalidResponse, T extends { [property: string]: SubValue<ResultType, ErrorType> },>(obj: T, path?: string[]): T {
return new Proxy(obj, { return new Proxy(obj, {
get(target, key: string) { get(target, key: string) {
let nextPath: string[]; let nextPath: string[];
if (path === undefined) { if (path === undefined) {
nextPath = [key]; nextPath = [key];
} }
else{ else {
nextPath = path; nextPath = path;
nextPath.push(key); nextPath.push(key);
} }
if (!(key in target)) { if (!(key in target)) {
throw `${nextPath.join('/')} is not exists`; throw `${nextPath} is not exists`;
} }
const next = target[key]; const next = target[key];
if (typeof (next) === 'function') { if (typeof (next) === 'function') {
const callAPI: CurryCall<ResultType, ErrorType> = (args: Record<string, unknown> | undefined, returnError?: boolean) => { return callback(nextPath);
if (returnError) {
return callSammoAPI<ResultType, ErrorType>(nextPath.join('/'), args, returnError);
}
return callSammoAPI<ResultType>(nextPath.join('/'), args, false);
}
return callAPI;
} }
return APIPathGen(next, nextPath); return APIPathGen(next, callback, nextPath);
} }
}) })
} }