259 lines
9.9 KiB
TypeScript
259 lines
9.9 KiB
TypeScript
|
|
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,
|
|
type ValidatorOptions,
|
|
} from "class-validator";
|
|
import { type ClassTransformOptions, plainToInstance } from "class-transformer";
|
|
import { InvalidProc, ProcDecorator, ProcDecoratorChain, ResolveChain } from './ProcDecorator/base';
|
|
import { clamp } from 'lodash-es';
|
|
|
|
export type APINamespace = {
|
|
[key: string]: APINamespace
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
| ClassType<GET<any, any, any, any>>
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
| ClassType<POST<any, any, any, any>>
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
| ClassType<PUT<any, any, any, any>>
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
| ClassType<DELETE<any, any, any, any>>
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
| ClassType<PATCH<any, any, any, any>>
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
| ClassType<HEAD<any, any, any, any>>
|
|
;
|
|
}
|
|
|
|
type ValidatorType<T> = T extends undefined ? undefined : ClassType<T>;
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
export type AnyAPIExecuter = APIExecuter<any, any, any, any>;
|
|
|
|
export abstract class APIExecuter<R extends ValidResponse, E extends InvalidResponse, Q extends RawArgType, PD extends ProcDecoratorChain = ProcDecoratorChain>{
|
|
readonly abstract reqType: HttpMethod;
|
|
readonly abstract argValidator?: ValidatorType<Q>;
|
|
readonly abstract procDecorator: PD;
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
private preDecorator: ProcDecorator<any, any>[] = [];
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
private postDecorator: (ProcDecorator<any, any> | undefined)[] = [];
|
|
private decoratorParsed = false;
|
|
|
|
public parseDecorator(): void {
|
|
if (this.decoratorParsed) {
|
|
return;
|
|
}
|
|
this.decoratorParsed = true;
|
|
if (!this.procDecorator) {
|
|
return;
|
|
}
|
|
for (const procGen of this.procDecorator) {
|
|
const proc = procGen();
|
|
if (Array.isArray(proc)) {
|
|
this.preDecorator.push(proc[0]);
|
|
this.postDecorator.push(undefined);
|
|
}
|
|
else {
|
|
this.preDecorator.push(proc);
|
|
this.postDecorator.push(proc);
|
|
}
|
|
}
|
|
}
|
|
|
|
private async preCtx(expressReq: Request, expressRes: Response): Promise<ResolveChain<PD> | [InvalidProc, ResolveChain<PD>, number]> {
|
|
if (!this.procDecorator) {
|
|
return {} as ResolveChain<PD>;
|
|
}
|
|
|
|
let ctx = {} as ResolveChain<PD>;
|
|
for (const [idx, proc] of this.preDecorator.entries()) {
|
|
try {
|
|
const result = await proc(ctx, expressReq, expressRes, true);
|
|
if ('invalidProcSymbol' in result) {
|
|
return [result, ctx, idx];
|
|
}
|
|
ctx = result;
|
|
}
|
|
catch (e) {
|
|
return [{
|
|
result: false,
|
|
reason: `internal error[${idx}]: ${e}`,
|
|
invalidProcSymbol: ['preCtx', idx],
|
|
}, ctx, idx];
|
|
}
|
|
}
|
|
return ctx;
|
|
}
|
|
|
|
private async postCtx(ctx: ResolveChain<PD>, expressReq: Request, expressRes: Response, index = -1): Promise<[InvalidProc, number] | undefined> {
|
|
if (!this.procDecorator) {
|
|
return;
|
|
}
|
|
|
|
let isValidRoute = true;
|
|
if(index !== -1 && index !== this.postDecorator.length){
|
|
isValidRoute = false;
|
|
index = clamp(index, 0, this.postDecorator.length);
|
|
}
|
|
|
|
for (let i = index - 1; i >= 0; i--) {
|
|
const proc = this.postDecorator[i];
|
|
if(!proc){
|
|
continue;
|
|
}
|
|
|
|
try {
|
|
const result = await proc(ctx, expressReq, expressRes, isValidRoute);
|
|
if ('invalidProcSymbol' in result) {
|
|
result.invalidProcSymbol = ['postCtx', i, result.invalidProcSymbol];
|
|
return [result, i];
|
|
}
|
|
ctx = result;
|
|
}
|
|
catch (e) {
|
|
return [{
|
|
result: false,
|
|
reason: `internal error[${i}]: ${e}`,
|
|
invalidProcSymbol: ['postCtx', i],
|
|
}, i];
|
|
}
|
|
}
|
|
return;
|
|
}
|
|
|
|
protected abstract parseQuery(expressReq: Request): Promise<Q>;
|
|
protected abstract api(query: Q, ctx: ResolveChain<PD>, expressReq: Request, expressRes: Response): Promise<R | E | true>;
|
|
public async run(expressReq: Request, expressRes: Response): Promise<void> {
|
|
const query = await this.parseQuery(expressReq);
|
|
const ctx = await this.preCtx(expressReq, expressRes);
|
|
if (Array.isArray(ctx)) {
|
|
const [invalidProc, errCtx, idx] = ctx;
|
|
if (idx == 0){
|
|
expressRes.json(invalidProc);
|
|
return;
|
|
}
|
|
const result = await this.postCtx(errCtx, expressReq, expressRes, idx);
|
|
if (result === undefined) {
|
|
expressRes.json(invalidProc);
|
|
return;
|
|
}
|
|
const [postInvalidProc,] = result;
|
|
expressRes.json(postInvalidProc);
|
|
return;
|
|
}
|
|
const result = await this.api(query, ctx, expressReq, expressRes);
|
|
const postResult = await this.postCtx(ctx, expressReq, expressRes);
|
|
if (postResult !== undefined) {
|
|
if(result === true){
|
|
//NOTE: 이미 api에서 response를 보낸 특이 케이스.
|
|
return;
|
|
}
|
|
const [invalidProc,] = postResult;
|
|
invalidProc.alreadyProcessed = true;
|
|
expressRes.json(invalidProc);
|
|
return;
|
|
}
|
|
|
|
if (result !== true) {
|
|
expressRes.json(result);
|
|
}
|
|
}
|
|
}
|
|
|
|
export abstract class APIBodyParseExecuter<R extends ValidResponse, E extends InvalidResponse, Q extends RawArgType, PD extends ProcDecoratorChain> extends APIExecuter<R, E, Q, PD>{
|
|
protected async parseQuery(expressReq: Request): Promise<Q> {
|
|
|
|
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, PD extends ProcDecoratorChain = []> extends APIExecuter<R, E, Q, PD>{
|
|
override readonly reqType = 'get';
|
|
protected async parseQuery(expressReq: Request): Promise<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;
|
|
}
|
|
}
|
|
|
|
export abstract class POST<R extends ValidResponse, E extends InvalidResponse, Q extends RawArgType, PD extends ProcDecoratorChain> extends APIBodyParseExecuter<R, E, Q, PD>{
|
|
readonly reqType = 'post';
|
|
}
|
|
|
|
export abstract class PUT<R extends ValidResponse, E extends InvalidResponse, Q extends RawArgType, PD extends ProcDecoratorChain> extends APIBodyParseExecuter<R, E, Q, PD>{
|
|
readonly reqType = 'put';
|
|
}
|
|
|
|
export abstract class DELETE<R extends ValidResponse, E extends InvalidResponse, Q extends RawArgType, PD extends ProcDecoratorChain> extends APIBodyParseExecuter<R, E, Q, PD>{
|
|
readonly reqType = 'delete';
|
|
}
|
|
|
|
export abstract class PATCH<R extends ValidResponse, E extends InvalidResponse, Q extends RawArgType, PD extends ProcDecoratorChain> extends APIBodyParseExecuter<R, E, Q, PD>{
|
|
readonly reqType = 'patch';
|
|
}
|
|
|
|
export abstract class HEAD<R extends ValidResponse, E extends InvalidResponse, Q extends RawArgType, PD extends ProcDecoratorChain> extends APIBodyParseExecuter<R, E, Q, PD>{
|
|
readonly reqType = 'head';
|
|
}
|
|
|
|
export function raiseError(reason: string, recovery?: recoveryMethod): InvalidResponse {
|
|
if (recovery) {
|
|
return {
|
|
result: false,
|
|
reason,
|
|
recovery,
|
|
};
|
|
}
|
|
return {
|
|
result: false,
|
|
reason,
|
|
};
|
|
}
|
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
export type ClassType<T> = new (...args: any[]) => T;
|
|
|
|
export type APIServerType<T extends Callable> =
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
T extends GetAPICallT<infer Q, infer R, infer E> ? ClassType<GET<R, E, Q, any>> :
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
T extends PostAPICallT<infer Q, infer R, infer E> ? ClassType<POST<R, E, Q, any>> :
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
T extends PutAPICallT<infer Q, infer R, infer E> ? ClassType<PUT<R, E, Q, any>> :
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
T extends DeleteAPICallT<infer Q, infer R, infer E> ? ClassType<DELETE<R, E, Q, any>> :
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
T extends PatchAPICallT<infer Q, infer R, infer E> ? ClassType<PATCH<R, E, Q, any>> :
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
T extends HeadAPICallT<infer Q, infer R, infer E> ? ClassType<HEAD<R, E, Q, any>> :
|
|
never;
|
|
export type APINamespaceType<T extends DefAPINamespace> = {
|
|
[K in keyof T]:
|
|
T[K] extends Callable ? APIServerType<T[K]> :
|
|
T[K] extends DefAPINamespace ? APINamespaceType<T[K]> :
|
|
never;
|
|
}; |