경로 이동
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
export function APIPathGen<T extends object, V>(
|
||||
obj: T,
|
||||
callback: (path: string[], tail: V, pathParam?: Record<string, string | number>) => unknown,
|
||||
path: string[] = [],
|
||||
): T {
|
||||
const map = new Map<string, unknown>();
|
||||
return new Proxy(obj, {
|
||||
get(target, key) {
|
||||
if(typeof key === 'symbol'){
|
||||
throw new Error('Symbol is not supported');
|
||||
}
|
||||
|
||||
if(map.has(key)){
|
||||
return map.get(key)!;
|
||||
}
|
||||
|
||||
const nextPath = [...path, key];
|
||||
|
||||
let next: T[keyof T];
|
||||
if (key in target) {
|
||||
next = target[key as keyof typeof target];
|
||||
}
|
||||
else {
|
||||
throw `${nextPath} is not exists`;
|
||||
}
|
||||
|
||||
if (typeof (next) === 'function') {
|
||||
const result = callback(nextPath, next);
|
||||
map.set(key, result);
|
||||
return result;
|
||||
}
|
||||
const result = APIPathGen<T[keyof T], V>(next, callback, nextPath);
|
||||
map.set(key, result);
|
||||
return result;
|
||||
}
|
||||
}) as T;
|
||||
};
|
||||
@@ -1,4 +1,4 @@
|
||||
import { APIPathGen } from "../api/APIPathGen";
|
||||
import { APIPathGen } from "./APIPathGen";
|
||||
import { extractHttpMethod } from "../apiStructure/defs";
|
||||
import type { APITail, RawArgType } from "../apiStructure/defs";
|
||||
import { structure } from "../apiStructure/sammoRootAPI";
|
||||
|
||||
Reference in New Issue
Block a user