feat: APIPathGen 추상화
This commit is contained in:
+9
-2
@@ -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, 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.`);
|
||||
return callSammoAPI<ResultType, ErrorType>([], args, true);
|
||||
}
|
||||
@@ -43,4 +43,11 @@ const apiRealPath = {
|
||||
},
|
||||
} 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);
|
||||
};
|
||||
});
|
||||
@@ -20,4 +20,11 @@ const apiRealPath = {
|
||||
},
|
||||
} 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);
|
||||
};
|
||||
});
|
||||
@@ -1,40 +1,26 @@
|
||||
import { InvalidResponse } from "@/defs";
|
||||
import { callSammoAPI, ValidResponse } from "./callSammoAPI";
|
||||
type SubValue<V extends (...args: unknown[]) => unknown> = V | { [property: string]: SubValue<V> };
|
||||
|
||||
export type CurryCall<ResultType extends ValidResponse, ErrorType extends InvalidResponse> =
|
||||
((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 {
|
||||
export function APIPathGen<V extends () => unknown, T extends { [property: string]: SubValue<V> }>(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<ResultType, ErrorType> = (args: Record<string, unknown> | undefined, returnError?: boolean) => {
|
||||
if (returnError) {
|
||||
return callSammoAPI<ResultType, ErrorType>(nextPath.join('/'), args, returnError);
|
||||
}
|
||||
return callSammoAPI<ResultType>(nextPath.join('/'), args, false);
|
||||
}
|
||||
return callAPI;
|
||||
return callback(nextPath);
|
||||
}
|
||||
return APIPathGen(next, nextPath);
|
||||
return APIPathGen(next, callback, nextPath);
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user