Files
core_ng/@strpc/def/src/clientAPIPathGen.ts
T
Hide_D e59f9a9659 monorepo 버전 준비
## @strpc
기존 RPC를 package화

### @strpc/express
express의 middleware + router 결함

## @sammo
게임 전체
- server, client
- gateway_server, gateway_client
2023-09-23 16:29:18 +00:00

38 lines
1.1 KiB
TypeScript

export function clientAPIPathGen<T extends object, V>(
obj: T,
callback: (path: string[], tail: V) => 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');
}
const cachedResult = map.get(key);
if(cachedResult !== undefined){
return cachedResult;
}
const nextPath = [...path, key];
let next;
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 as V);
map.set(key, result);
return result;
}
const result = clientAPIPathGen<object, V>(next as object, callback, nextPath);
map.set(key, result);
return result;
}
}) as T;
}