Files
core/hwe/ts/util/APIPathGen.js
T
Hide_D 5879661267 refac, feat: API 호출 구조 재작성
- api.php에서 param path 강제
- api.php에서 GET param 허용
- SammoAPI 호출자를 axios에서 fetch 기반(ky)로 변경
- SammoAPI에서 단순 POST대신 REST에 따라 지정 가능하도록 재구성
- SammoAPI에서 NumVar, StrVar를 PathParam으로 변경하도록 변경
- API CallType들을 def/API로 분리 시작
  - 일부 API를 시험삼아 변경(login)
2022-04-02 23:31:05 +09:00

61 lines
1.7 KiB
JavaScript

export function APIPathGen(obj, callback, path, pathParams) {
return new Proxy(obj, {
get(target, key) {
let nextPath;
if (path === undefined) {
nextPath = [key.toString()];
}
else {
nextPath = [...path, key.toString()];
}
if (pathParams !== undefined) {
pathParams = { ...pathParams };
}
const varType = target.__nextVarType;
const varKey = target.__nextVarKey;
let next;
if (varType !== undefined && varKey !== undefined) {
if (typeof key !== varType) {
throw `${key} is not ${varType}`;
}
if(pathParams === undefined){
pathParams = {}
}
pathParams[varKey] = key;
next = target.next;
}
else if (key in target) {
next = target[key];
}
else {
throw `${nextPath} is not exists`;
}
if (typeof (next) === 'function') {
return callback(nextPath, next, pathParams);
}
return APIPathGen(next, callback, nextPath, pathParams);
}
})
}
//generic 인자로 '자동'을 주려면 생략해야하므로 2단 호출
export function StrVar(key) {
return (next) => {
return {
__nextVarType: 'string',
__nextVarKey: key,
next
}
}
}
export function NumVar(key, next) {
return {
__nextVarType: 'number',
__nextVarKey: key,
next
}
}