작업중
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
export function APIPathGen<T extends object, V>(
|
||||
obj: T,
|
||||
callback: (path: string[], tail: V, pathParam?: Record<string, string | number>) => 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');
|
||||
}
|
||||
|
||||
if(map.has(key)){
|
||||
return map.get(key)!;
|
||||
}
|
||||
|
||||
const nextPath = [...path, key];
|
||||
|
||||
let next: T[keyof T];
|
||||
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);
|
||||
map.set(key, result);
|
||||
return result;
|
||||
}
|
||||
const result = APIPathGen<T[keyof T], V>(next, callback, nextPath);
|
||||
map.set(key, result);
|
||||
return result;
|
||||
}
|
||||
}) as T;
|
||||
};
|
||||
+10
-10
@@ -28,8 +28,8 @@ export type APINamespace = {
|
||||
;
|
||||
}
|
||||
|
||||
export abstract class APIExecuter<Q extends object, R extends ValidResponse, E extends InvalidResponse>{
|
||||
readonly reqType: HttpMethod | HttpMethod[];
|
||||
export abstract class APIExecuter<R extends ValidResponse, E extends InvalidResponse, Q extends RawArgType>{
|
||||
readonly abstract reqType: HttpMethod | HttpMethod[];
|
||||
protected abstract parseQuery(expressReq: Request): Promise<Q>;
|
||||
protected abstract api(query: Q, expressReq: Request, expressRes: Response): Promise<R | E | typeof treatedSpecial>;
|
||||
public async run(expressReq: Request, expressRes: Response): Promise<void> {
|
||||
@@ -43,15 +43,15 @@ export abstract class APIExecuter<Q extends object, R extends ValidResponse, E e
|
||||
}
|
||||
}
|
||||
|
||||
export abstract class APIBodyParseExecuter<Q extends object, R extends ValidResponse, E extends InvalidResponse> extends APIExecuter<Q, R, E>{
|
||||
export abstract class APIBodyParseExecuter<R extends ValidResponse, E extends InvalidResponse, Q extends RawArgType> extends APIExecuter<R, E, Q>{
|
||||
protected async parseQuery(expressReq: Request): Promise<Q> {
|
||||
throw new Error('Not implemented');
|
||||
return expressReq.body;
|
||||
}
|
||||
}
|
||||
|
||||
export abstract class GET<Q extends object, R extends ValidResponse, E extends InvalidResponse> extends APIExecuter<Q, R, E>{
|
||||
readonly reqType = 'get';
|
||||
export abstract class GET<R extends ValidResponse, E extends InvalidResponse, Q extends RawArgType> extends APIExecuter<R, E, Q>{
|
||||
override readonly reqType = 'get';
|
||||
protected async parseQuery(expressReq: Request): Promise<Q> {
|
||||
expressReq.query;
|
||||
throw new Error('Not implemented');
|
||||
@@ -59,22 +59,22 @@ export abstract class GET<Q extends object, R extends ValidResponse, E extends I
|
||||
}
|
||||
}
|
||||
|
||||
export abstract class POST<Q extends object, R extends ValidResponse, E extends InvalidResponse> extends APIBodyParseExecuter<Q, R, E>{
|
||||
export abstract class POST<R extends ValidResponse, E extends InvalidResponse, Q extends RawArgType> extends APIBodyParseExecuter<R, E, Q>{
|
||||
readonly reqType = 'post';
|
||||
}
|
||||
|
||||
export abstract class PUT<Q extends object, R extends ValidResponse, E extends InvalidResponse> extends APIBodyParseExecuter<Q, R, E>{
|
||||
export abstract class PUT<R extends ValidResponse, E extends InvalidResponse, Q extends RawArgType> extends APIBodyParseExecuter<R, E, Q>{
|
||||
readonly reqType = 'put';
|
||||
}
|
||||
|
||||
export abstract class DELETE<Q extends object, R extends ValidResponse, E extends InvalidResponse> extends APIBodyParseExecuter<Q, R, E>{
|
||||
export abstract class DELETE<R extends ValidResponse, E extends InvalidResponse, Q extends RawArgType> extends APIBodyParseExecuter<R, E, Q>{
|
||||
readonly reqType = 'delete';
|
||||
}
|
||||
|
||||
export abstract class PATCH<Q extends object, R extends ValidResponse, E extends InvalidResponse> extends APIBodyParseExecuter<Q, R, E>{
|
||||
export abstract class PATCH<R extends ValidResponse, E extends InvalidResponse, Q extends RawArgType> extends APIBodyParseExecuter<R, E, Q>{
|
||||
readonly reqType = 'patch';
|
||||
}
|
||||
|
||||
export abstract class HEAD<Q extends object, R extends ValidResponse, E extends InvalidResponse> extends APIBodyParseExecuter<Q, R, E>{
|
||||
export abstract class HEAD<R extends ValidResponse, E extends InvalidResponse, Q extends RawArgType> extends APIBodyParseExecuter<R, E, Q>{
|
||||
readonly reqType = 'head';
|
||||
}
|
||||
@@ -1,211 +0,0 @@
|
||||
import ky from 'ky';
|
||||
import type { APIExecuter, APINamespace, HttpMethod, InvalidResponse, RawArgType, ValidResponse } from './base.js';
|
||||
import isArray from 'lodash-es/isArray.js';
|
||||
import isEmpty from 'lodash-es/isEmpty.js';
|
||||
|
||||
const apiPath = process.env.API_ROOT_PATH ?? process.env.VITE_API_ROOT_PATH ?? '/api';
|
||||
|
||||
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 callClientAPI<ResultType, ErrorType>("get", [], args, undefined, 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 callClientAPI<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 callClientAPI<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 callClientAPI<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 callClientAPI<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 callClientAPI<ResultType, ErrorType>("patch", [], args, undefined, true);
|
||||
}
|
||||
|
||||
|
||||
export async function callClientAPI<ResultType extends ValidResponse>(
|
||||
method: HttpMethod,
|
||||
path: string | string[],
|
||||
args: RawArgType,
|
||||
paramArgs: Record<string, string | number> | undefined
|
||||
): Promise<ResultType>;
|
||||
export async function callClientAPI<ResultType extends ValidResponse>(
|
||||
method: HttpMethod,
|
||||
path: string | string[],
|
||||
args: RawArgType,
|
||||
paramArgs: Record<string, string | number> | undefined,
|
||||
returnError: false
|
||||
): Promise<ResultType>;
|
||||
export async function callClientAPI<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 callClientAPI<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("/");
|
||||
}
|
||||
|
||||
if (args && isEmpty(args)) {
|
||||
args = undefined;
|
||||
}
|
||||
|
||||
const result = (await (() => {
|
||||
if (method == "get") {
|
||||
return ky(apiPath, {
|
||||
searchParams: {
|
||||
...paramArgs,
|
||||
...(args as typeof paramArgs),
|
||||
path,
|
||||
},
|
||||
method,
|
||||
headers: {
|
||||
"content-type": "application/json",
|
||||
},
|
||||
timeout: 30000,
|
||||
retry: 0,
|
||||
});
|
||||
}
|
||||
return ky(apiPath, {
|
||||
searchParams: {
|
||||
...paramArgs,
|
||||
path,
|
||||
},
|
||||
method,
|
||||
json: args,
|
||||
headers: {
|
||||
"content-type": "application/json",
|
||||
},
|
||||
timeout: 30000,
|
||||
retry: 0,
|
||||
});
|
||||
})().json()) as ErrorType | ResultType;
|
||||
|
||||
if (!result.result) {
|
||||
if (returnError) {
|
||||
return result;
|
||||
}
|
||||
throw result.reason;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
//TODO: type이 하나 만들어져야 함. 이것은 typescript의 type system으로.
|
||||
//TODO: const로 실제 구조가 하나 더 만들어 져야 함. 이것은 위의 type을 satisfy하여야 함.
|
||||
Reference in New Issue
Block a user