feat: APIPathGen 추상화

This commit is contained in:
2021-12-29 05:46:35 +09:00
parent def230bf30
commit ee6bec2e0d
3 changed files with 23 additions and 23 deletions
+8 -1
View File
@@ -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);
};
});
+5 -19
View File
@@ -1,14 +1,6 @@
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[];
@@ -21,20 +13,14 @@ export function APIPathGen<ResultType extends ValidResponse, ErrorType extends I
} }
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 APIPathGen(next, callback, nextPath);
}
return callAPI;
}
return APIPathGen(next, nextPath);
} }
}) })
} }