import { InvalidResponse } from "@/defs"; import { callSammoAPI, ValidResponse } from "./callSammoAPI"; 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 { return new Proxy(obj, { get(target, key: string) { let nextPath: string[]; if (path === undefined) { nextPath = [key]; } else{ nextPath = path; nextPath.push(key); } if (!(key in target)) { throw `${nextPath.join('/')} 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 APIPathGen(next, nextPath); } }) }