fix: APIPathGen에서 Var 모드 사용시 에러

This commit is contained in:
2022-03-26 00:04:18 +09:00
parent da3c171c53
commit 75c7f5246a
2 changed files with 32 additions and 12 deletions
+3
View File
@@ -14,6 +14,9 @@ const apiPath = {
User: StrVar<'a'|'b'>()({ User: StrVar<'a'|'b'>()({
Update: someFunc, Update: someFunc,
Delete: someFunc, Delete: someFunc,
}),
NationInfo: NumVar({
show: someFunc
}) })
} }
*/ */
+29 -12
View File
@@ -1,25 +1,29 @@
export function APIPathGen(obj, callback, path) { export function APIPathGen(obj, callback, path) {
return new Proxy(obj, { return new Proxy(obj, {
get(target, key) { get(target, key) {
if(typeof key === 'number'){
key = key.toString();
}
else if(typeof key !== 'string'){
throw `${key} is not string`;
}
let nextPath; let nextPath;
if (path === undefined) { if (path === undefined) {
nextPath = [key]; nextPath = [key.toString()];
} }
else { else {
nextPath = [...path, key]; nextPath = [...path, key.toString()];
} }
if (!(key in target)) { const varType = target.__nextVarType;
let next;
if (varType !== undefined) {
if (typeof key !== varType) {
throw `${key} is not ${varType}`;
}
next = target.next;
}
else if (key in target) {
next = target[key];
}
else {
throw `${nextPath} is not exists`; throw `${nextPath} is not exists`;
} }
const next = target[key];
if (typeof (next) === 'function') { if (typeof (next) === 'function') {
return callback(nextPath); return callback(nextPath);
} }
@@ -28,6 +32,19 @@ export function APIPathGen(obj, callback, path) {
}) })
} }
export function StrVar(){ //generic 인자로 '자동'을 주려면 생략해야하므로 2단 호출
return (next)=>next; export function StrVar() {
return (next) => {
return {
__nextVarType: 'string',
next
}
}
}
export function NumVar(next) {
return {
__nextVarType: 'number',
next
}
} }