ArgType 변경
This commit is contained in:
+43
-15
@@ -1,8 +1,14 @@
|
||||
|
||||
import type { Request, Response } from 'express';
|
||||
import type { Callable, DefAPINamespace, DeleteAPICallT, GetAPICallT, HeadAPICallT, HttpMethod, InvalidResponse, PatchAPICallT, PostAPICallT, PutAPICallT, RawArgType, ValidResponse, recoveryMethod } from '../apiStructure/defs';
|
||||
import {
|
||||
validate,
|
||||
validateSync,
|
||||
validateOrReject,
|
||||
ValidatorOptions,
|
||||
} from "class-validator";
|
||||
import { ClassTransformOptions, plainToInstance } from "class-transformer";
|
||||
|
||||
export const treatedSpecial = Symbol('treatedSpecial');
|
||||
export type APINamespace = {
|
||||
[key: string]: APINamespace
|
||||
| GET<any, any, any>
|
||||
@@ -14,8 +20,11 @@ export type APINamespace = {
|
||||
;
|
||||
}
|
||||
|
||||
type ValidatorType<T> = T extends undefined ? undefined : ClassType<T>;
|
||||
|
||||
export abstract class APIExecuter<R extends ValidResponse, E extends InvalidResponse, Q extends RawArgType>{
|
||||
readonly abstract reqType: HttpMethod;
|
||||
readonly abstract argValidator?: ValidatorType<Q>;
|
||||
protected abstract parseQuery(expressReq: Request): Promise<Q>;
|
||||
protected abstract api(query: Q, expressReq: Request, expressRes: Response): Promise<R | E | true>;
|
||||
public async run(expressReq: Request, expressRes: Response): Promise<void> {
|
||||
@@ -31,17 +40,37 @@ export abstract class APIExecuter<R extends ValidResponse, E extends InvalidResp
|
||||
|
||||
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;
|
||||
|
||||
if(!this.argValidator){
|
||||
return expressReq.body as Q;
|
||||
}
|
||||
|
||||
const query = expressReq.body;
|
||||
const classObject = plainToInstance(this.argValidator, query);
|
||||
const errors = await validate(classObject);
|
||||
if (errors.length) {
|
||||
throw errors;
|
||||
}
|
||||
|
||||
return classObject as Q;
|
||||
}
|
||||
}
|
||||
|
||||
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');
|
||||
return expressReq.query as unknown as Q;
|
||||
if(!this.argValidator){
|
||||
return expressReq.query as Q;
|
||||
}
|
||||
|
||||
const query = expressReq.query;
|
||||
const classObject = plainToInstance(this.argValidator, query);
|
||||
const errors = await validate(classObject);
|
||||
if (errors.length) {
|
||||
throw errors;
|
||||
}
|
||||
|
||||
return classObject as Q;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -79,16 +108,15 @@ export function raiseError(reason: string, recovery?: recoveryMethod): InvalidRe
|
||||
};
|
||||
}
|
||||
|
||||
export type GetClassGenerator<T> = {
|
||||
prototype: T
|
||||
};
|
||||
export type ClassType<T> = new (...args: any[]) => T;
|
||||
|
||||
export type APIServerType<T extends Callable> =
|
||||
T extends GetAPICallT<infer Q, infer R, infer E> ? GetClassGenerator<GET<R, E, Q>> :
|
||||
T extends PostAPICallT<infer Q, infer R, infer E> ? GetClassGenerator<POST<R, E, Q>> :
|
||||
T extends PutAPICallT<infer Q, infer R, infer E> ? GetClassGenerator<PUT<R, E, Q>> :
|
||||
T extends DeleteAPICallT<infer Q, infer R, infer E> ? GetClassGenerator<DELETE<R, E, Q>> :
|
||||
T extends PatchAPICallT<infer Q, infer R, infer E> ? GetClassGenerator<PATCH<R, E, Q>> :
|
||||
T extends HeadAPICallT<infer Q, infer R, infer E> ? GetClassGenerator<HEAD<R, E, Q>> :
|
||||
T extends GetAPICallT<infer Q, infer R, infer E> ? ClassType<GET<R, E, Q>> :
|
||||
T extends PostAPICallT<infer Q, infer R, infer E> ? ClassType<POST<R, E, Q>> :
|
||||
T extends PutAPICallT<infer Q, infer R, infer E> ? ClassType<PUT<R, E, Q>> :
|
||||
T extends DeleteAPICallT<infer Q, infer R, infer E> ? ClassType<DELETE<R, E, Q>> :
|
||||
T extends PatchAPICallT<infer Q, infer R, infer E> ? ClassType<PATCH<R, E, Q>> :
|
||||
T extends HeadAPICallT<infer Q, infer R, infer E> ? ClassType<HEAD<R, E, Q>> :
|
||||
never;
|
||||
export type APINamespaceType<T extends DefAPINamespace> = {
|
||||
[K in keyof T]:
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
export type HttpMethod = 'get' | 'post' | 'put' | 'delete' | 'patch' | 'head';
|
||||
export type RawArgType = Record<string, unknown> | Record<string, unknown>[] | undefined;
|
||||
export type RawArgType = {
|
||||
[key: string]: unknown;
|
||||
} | undefined;
|
||||
|
||||
|
||||
export interface BasicAPICallT<
|
||||
|
||||
@@ -34,6 +34,7 @@ const ownConfig = {
|
||||
AppDataSource.initialize().then(async () => {
|
||||
// create express app
|
||||
const app = express()
|
||||
app.use(express.json());
|
||||
app.use(session({
|
||||
secret: ownConfig.sessionSecret,
|
||||
resave: false,
|
||||
|
||||
Reference in New Issue
Block a user