From 0685d1caac70fd1cda20cf5e26c66ed1d1915672 Mon Sep 17 00:00:00 2001 From: Hide_D Date: Sat, 26 Mar 2022 00:04:18 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20APIPathGen=EC=97=90=EC=84=9C=20Var=20?= =?UTF-8?q?=EB=AA=A8=EB=93=9C=20=EC=82=AC=EC=9A=A9=EC=8B=9C=20=EC=97=90?= =?UTF-8?q?=EB=9F=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hwe/ts/util/APIPathGen.d.ts | 3 +++ hwe/ts/util/APIPathGen.js | 41 ++++++++++++++++++++++++++----------- 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/hwe/ts/util/APIPathGen.d.ts b/hwe/ts/util/APIPathGen.d.ts index 4e9748f6..015058ba 100644 --- a/hwe/ts/util/APIPathGen.d.ts +++ b/hwe/ts/util/APIPathGen.d.ts @@ -14,6 +14,9 @@ const apiPath = { User: StrVar<'a'|'b'>()({ Update: someFunc, Delete: someFunc, + }), + NationInfo: NumVar({ + show: someFunc }) } */ \ No newline at end of file diff --git a/hwe/ts/util/APIPathGen.js b/hwe/ts/util/APIPathGen.js index fa4d05ed..0b44fdcd 100644 --- a/hwe/ts/util/APIPathGen.js +++ b/hwe/ts/util/APIPathGen.js @@ -1,25 +1,29 @@ export function APIPathGen(obj, callback, path) { return new Proxy(obj, { get(target, key) { - if(typeof key === 'number'){ - key = key.toString(); - } - else if(typeof key !== 'string'){ - throw `${key} is not string`; - } let nextPath; if (path === undefined) { - nextPath = [key]; + nextPath = [key.toString()]; } 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`; } - const next = target[key]; if (typeof (next) === 'function') { return callback(nextPath); } @@ -28,6 +32,19 @@ export function APIPathGen(obj, callback, path) { }) } -export function StrVar(){ - return (next)=>next; +//generic 인자로 '자동'을 주려면 생략해야하므로 2단 호출 +export function StrVar() { + return (next) => { + return { + __nextVarType: 'string', + next + } + } +} + +export function NumVar(next) { + return { + __nextVarType: 'number', + next + } } \ No newline at end of file