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)
This commit is contained in:
Vendored
+9
-5
@@ -1,21 +1,25 @@
|
||||
export function APIPathGen<T>(obj: T, callback: (path: string[])=>unknown): T;
|
||||
export function APIPathGen<T, V>(
|
||||
obj: T,
|
||||
callback: (path: string[], tail: V, pathParam?: Record<string, string | number>) => unknown,
|
||||
pathParam?: Record<string, string | number>
|
||||
): T;
|
||||
|
||||
export function StrVar<PathType extends string>(): <NextCall>(next: NextCall)=>{
|
||||
export function StrVar<PathType extends string>(paramKey: string): <NextCall>(next: NextCall) => {
|
||||
[v in PathType]: NextCall
|
||||
};
|
||||
|
||||
export function NumVar<NextCall>(next: NextCall):{
|
||||
export function NumVar<NextCall>(paramKey: string, next: NextCall): {
|
||||
[v: number]: NextCall
|
||||
};
|
||||
|
||||
/*
|
||||
const apiPath = {
|
||||
SomePath: someFunc,
|
||||
User: StrVar<'a'|'b'>()({
|
||||
User: StrVar<'a'|'b'>('name')({
|
||||
Update: someFunc,
|
||||
Delete: someFunc,
|
||||
}),
|
||||
NationInfo: NumVar({
|
||||
NationInfo: NumVar('id', {
|
||||
show: someFunc
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
export function APIPathGen(obj, callback, path) {
|
||||
export function APIPathGen(obj, callback, path, pathParams) {
|
||||
return new Proxy(obj, {
|
||||
get(target, key) {
|
||||
let nextPath;
|
||||
@@ -9,12 +9,21 @@ export function APIPathGen(obj, callback, path) {
|
||||
nextPath = [...path, key.toString()];
|
||||
}
|
||||
|
||||
if (pathParams !== undefined) {
|
||||
pathParams = { ...pathParams };
|
||||
}
|
||||
|
||||
const varType = target.__nextVarType;
|
||||
const varKey = target.__nextVarKey;
|
||||
let next;
|
||||
if (varType !== undefined) {
|
||||
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) {
|
||||
@@ -25,26 +34,28 @@ export function APIPathGen(obj, callback, path) {
|
||||
}
|
||||
|
||||
if (typeof (next) === 'function') {
|
||||
return callback(nextPath);
|
||||
return callback(nextPath, next, pathParams);
|
||||
}
|
||||
return APIPathGen(next, callback, nextPath);
|
||||
return APIPathGen(next, callback, nextPath, pathParams);
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
//generic 인자로 '자동'을 주려면 생략해야하므로 2단 호출
|
||||
export function StrVar() {
|
||||
export function StrVar(key) {
|
||||
return (next) => {
|
||||
return {
|
||||
__nextVarType: 'string',
|
||||
__nextVarKey: key,
|
||||
next
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function NumVar(next) {
|
||||
export function NumVar(key, next) {
|
||||
return {
|
||||
__nextVarType: 'number',
|
||||
__nextVarKey: key,
|
||||
next
|
||||
}
|
||||
}
|
||||
+90
-22
@@ -1,35 +1,64 @@
|
||||
import axios from "axios";
|
||||
import { isArray } from "lodash";
|
||||
import type { InvalidResponse } from '@/defs';
|
||||
import ky from 'ky';
|
||||
import { isArray, isEmpty } from "lodash";
|
||||
|
||||
export type ValidResponse = {
|
||||
result: true
|
||||
}
|
||||
|
||||
export type RawArgType = Record<string, unknown>|Record<string, unknown>[];
|
||||
export type InvalidResponse = {
|
||||
result: false;
|
||||
reason: string;
|
||||
}
|
||||
|
||||
export interface CallbackT<ArgType extends RawArgType, ResultType extends ValidResponse = ValidResponse, ErrorType extends InvalidResponse = InvalidResponse>{
|
||||
|
||||
export type RawArgType = Record<string, unknown> | Record<string, unknown>[] | undefined;
|
||||
|
||||
export interface APICallT<ArgType extends RawArgType, ResultType extends ValidResponse = ValidResponse, ErrorType extends InvalidResponse = InvalidResponse> {
|
||||
(args?: ArgType): Promise<ResultType>;
|
||||
(args: ArgType | undefined, returnError: false): Promise<ResultType>;
|
||||
(args: ArgType | undefined, returnError: true): Promise<ResultType | ErrorType>;
|
||||
}
|
||||
|
||||
export async function callSammoAPI<ResultType extends ValidResponse>(path: string | string[], args?: Record<string, unknown> | Record<string, unknown>[]): Promise<ResultType>;
|
||||
export async function callSammoAPI<ResultType extends ValidResponse>(path: string | string[], args: Record<string, unknown> | Record<string, unknown>[] | undefined, returnError: false): Promise<ResultType>;
|
||||
export async function callSammoAPI<ResultType extends ValidResponse, ErrorType extends InvalidResponse>(path: string | string[], args: Record<string, unknown> | Record<string, unknown>[] | undefined, returnError: true): Promise<ResultType | ErrorType>;
|
||||
type HttpMethod = 'get' | 'post' | 'put' | 'patch' | 'head' | 'delete';
|
||||
export type APITail = typeof GET | typeof POST | typeof PUT | typeof PATCH | typeof HEAD | typeof DELETE;
|
||||
|
||||
export async function callSammoAPI<ResultType extends ValidResponse, ErrorType extends InvalidResponse>(path: string | string[], args?: Record<string, unknown> | Record<string, unknown>[], returnError = false): Promise<ResultType | ErrorType> {
|
||||
const httpMethodMap = new Map<APITail, HttpMethod>([
|
||||
[GET, 'get'],
|
||||
[POST, 'post'],
|
||||
[PUT, 'put'],
|
||||
[PATCH, 'patch'],
|
||||
[HEAD, 'head'],
|
||||
[DELETE, 'delete'],
|
||||
]);
|
||||
|
||||
export function extractHttpMethod(tail: APITail): HttpMethod {
|
||||
return httpMethodMap.get(tail) ?? 'post';
|
||||
}
|
||||
|
||||
export async function callSammoAPI<ResultType extends ValidResponse>(method: HttpMethod, path: string | string[], args: RawArgType, paramArgs: Record<string, string | number> | undefined): Promise<ResultType>;
|
||||
export async function callSammoAPI<ResultType extends ValidResponse>(method: HttpMethod, path: string | string[], args: RawArgType, paramArgs: Record<string, string | number> | undefined, returnError: false): Promise<ResultType>;
|
||||
export async function callSammoAPI<ResultType extends ValidResponse, ErrorType extends InvalidResponse>(method: HttpMethod, path: string | string[], args: RawArgType, paramArgs: Record<string, string | number> | undefined, returnError: true): Promise<ResultType | ErrorType>;
|
||||
export async function callSammoAPI<ResultType extends ValidResponse, ErrorType extends InvalidResponse>(method: HttpMethod, path: string | string[], args: RawArgType, paramArgs: Record<string, string | number> | undefined, returnError = false): Promise<ResultType | ErrorType> {
|
||||
if (isArray(path)) {
|
||||
path = path.join('/');
|
||||
}
|
||||
|
||||
const response = await axios({
|
||||
url: `api.php?path=${path}`,
|
||||
method: "post",
|
||||
responseType: "json",
|
||||
data: args
|
||||
});
|
||||
const result: ErrorType | ResultType = response.data;
|
||||
if (args && isEmpty(args)) {
|
||||
args = undefined;
|
||||
}
|
||||
|
||||
const result = await ky('api.php', {
|
||||
searchParams: {
|
||||
...paramArgs,
|
||||
path,
|
||||
},
|
||||
method,
|
||||
json: args,
|
||||
headers: {
|
||||
'content-type': 'application/json'
|
||||
}
|
||||
}).json() as ErrorType | ResultType;
|
||||
|
||||
if (!result.result) {
|
||||
if (returnError) {
|
||||
return result;
|
||||
@@ -39,11 +68,50 @@ export async function callSammoAPI<ResultType extends ValidResponse, ErrorType e
|
||||
return result;
|
||||
}
|
||||
|
||||
export async function done<ResultType extends ValidResponse>(args?: RawArgType): Promise<ResultType>;
|
||||
export async function done<ResultType extends ValidResponse>(args: RawArgType | undefined, returnError: false): Promise<ResultType>;
|
||||
export async function done<ResultType extends ValidResponse, ErrorType extends InvalidResponse>(args: RawArgType | undefined, returnError: true): Promise<ResultType | ErrorType>;
|
||||
export async function GET<ResultType extends ValidResponse, ArgType extends undefined = undefined>(args?: ArgType): Promise<ResultType>;
|
||||
export async function GET<ResultType extends ValidResponse, ArgType extends undefined = undefined>(args: ArgType | undefined, returnError: false): Promise<ResultType>;
|
||||
export async function GET<ResultType extends ValidResponse, ErrorType extends InvalidResponse, ArgType extends undefined = undefined>(args: ArgType | undefined, returnError: true): Promise<ResultType | ErrorType>;
|
||||
export async function GET<ResultType extends ValidResponse, ErrorType extends InvalidResponse, ArgType extends undefined = undefined>(args?: ArgType, returnError = false): Promise<ResultType | ErrorType> {
|
||||
console.error(`Can't directly call GET. ${args}, ${returnError}. Use auto-generated path API.`);
|
||||
return callSammoAPI<ResultType, ErrorType>('get', [], args, undefined, true);
|
||||
}
|
||||
|
||||
export async function done<ResultType extends ValidResponse, ErrorType extends InvalidResponse>(args?: RawArgType, returnError = false): Promise<ResultType | ErrorType> {
|
||||
console.error(`Can't directly call. ${args}, ${returnError}. Use auto-generated path API.`);
|
||||
return callSammoAPI<ResultType, ErrorType>([], args, true);
|
||||
export async function POST<ResultType extends ValidResponse, ArgType extends RawArgType = RawArgType>(args?: ArgType): Promise<ResultType>;
|
||||
export async function POST<ResultType extends ValidResponse, ArgType extends RawArgType = RawArgType>(args: ArgType | undefined, returnError: false): Promise<ResultType>;
|
||||
export async function POST<ResultType extends ValidResponse, ErrorType extends InvalidResponse, ArgType extends RawArgType = RawArgType>(args: ArgType | undefined, returnError: true): Promise<ResultType | ErrorType>;
|
||||
export async function POST<ResultType extends ValidResponse, ErrorType extends InvalidResponse, ArgType extends RawArgType = RawArgType>(args?: ArgType, returnError = false): Promise<ResultType | ErrorType> {
|
||||
console.error(`Can't directly call POST. ${args}, ${returnError}. Use auto-generated path API.`);
|
||||
return callSammoAPI<ResultType, ErrorType>('post', [], args, undefined, true);
|
||||
}
|
||||
|
||||
export async function PUT<ResultType extends ValidResponse, ArgType extends RawArgType = RawArgType>(args?: ArgType): Promise<ResultType>;
|
||||
export async function PUT<ResultType extends ValidResponse, ArgType extends RawArgType = RawArgType>(args: ArgType | undefined, returnError: false): Promise<ResultType>;
|
||||
export async function PUT<ResultType extends ValidResponse, ErrorType extends InvalidResponse, ArgType extends RawArgType = RawArgType>(args: ArgType | undefined, returnError: true): Promise<ResultType | ErrorType>;
|
||||
export async function PUT<ResultType extends ValidResponse, ErrorType extends InvalidResponse, ArgType extends RawArgType = RawArgType>(args?: ArgType, returnError = false): Promise<ResultType | ErrorType> {
|
||||
console.error(`Can't directly call PUT. ${args}, ${returnError}. Use auto-generated path API.`);
|
||||
return callSammoAPI<ResultType, ErrorType>('put', [], args, undefined, true);
|
||||
}
|
||||
|
||||
export async function PATCH<ResultType extends ValidResponse, ArgType extends RawArgType = RawArgType>(args?: ArgType): Promise<ResultType>;
|
||||
export async function PATCH<ResultType extends ValidResponse, ArgType extends RawArgType = RawArgType>(args: ArgType | undefined, returnError: false): Promise<ResultType>;
|
||||
export async function PATCH<ResultType extends ValidResponse, ErrorType extends InvalidResponse, ArgType extends RawArgType = RawArgType>(args: ArgType | undefined, returnError: true): Promise<ResultType | ErrorType>;
|
||||
export async function PATCH<ResultType extends ValidResponse, ErrorType extends InvalidResponse, ArgType extends RawArgType = RawArgType>(args?: ArgType, returnError = false): Promise<ResultType | ErrorType> {
|
||||
console.error(`Can't directly call PATCH. ${args}, ${returnError}. Use auto-generated path API.`);
|
||||
return callSammoAPI<ResultType, ErrorType>('patch', [], args, undefined, true);
|
||||
}
|
||||
|
||||
export async function HEAD<ResultType extends ValidResponse, ArgType extends undefined = undefined>(args?: ArgType): Promise<ResultType>;
|
||||
export async function HEAD<ResultType extends ValidResponse, ArgType extends undefined = undefined>(args: ArgType | undefined, returnError: false): Promise<ResultType>;
|
||||
export async function HEAD<ResultType extends ValidResponse, ErrorType extends InvalidResponse, ArgType extends undefined = undefined>(args: ArgType | undefined, returnError: true): Promise<ResultType | ErrorType>;
|
||||
export async function HEAD<ResultType extends ValidResponse, ErrorType extends InvalidResponse, ArgType extends undefined = undefined>(args?: ArgType, returnError = false): Promise<ResultType | ErrorType> {
|
||||
console.error(`Can't directly call HEAD. ${args}, ${returnError}. Use auto-generated path API.`);
|
||||
return callSammoAPI<ResultType, ErrorType>('head', [], args, undefined, true);
|
||||
}
|
||||
|
||||
export async function DELETE<ResultType extends ValidResponse, ArgType extends RawArgType = RawArgType>(args?: ArgType): Promise<ResultType>;
|
||||
export async function DELETE<ResultType extends ValidResponse, ArgType extends RawArgType = RawArgType>(args: ArgType | undefined, returnError: false): Promise<ResultType>;
|
||||
export async function DELETE<ResultType extends ValidResponse, ErrorType extends InvalidResponse, ArgType extends RawArgType = RawArgType>(args: ArgType | undefined, returnError: true): Promise<ResultType | ErrorType>;
|
||||
export async function DELETE<ResultType extends ValidResponse, ErrorType extends InvalidResponse, ArgType extends RawArgType = RawArgType>(args?: ArgType, returnError = false): Promise<ResultType | ErrorType> {
|
||||
console.error(`Can't directly call DELETE. ${args}, ${returnError}. Use auto-generated path API.`);
|
||||
return callSammoAPI<ResultType, ErrorType>('patch', [], args, undefined, true);
|
||||
}
|
||||
Reference in New Issue
Block a user