빌드
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { POST, ngPOST } from "../defs";
|
||||
import { ngPOST } from "../defs";
|
||||
import type { structure } from "../../apiStructure/sammoRootAPI";
|
||||
import type { ExtractError, ExtractQuery, ExtractResponse } from "../../apiStructure/defs";
|
||||
import { StartSession } from "../ProcDecorator/StartSession";
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { APINamespace, APINamespaceType, ngGET } from "../defs";
|
||||
import { type APINamespaceType, ngGET } from "../defs";
|
||||
import type { structure } from "../../apiStructure/sammoRootAPI";
|
||||
import type { DefAPINamespace, ExtractError, ExtractQuery, ExtractResponse } from "../../apiStructure/defs";
|
||||
import { StartSession } from "../ProcDecorator/StartSession";
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { GET, ngGET } from "../defs";
|
||||
import { ngGET } from "../defs";
|
||||
import type { structure } from "../../apiStructure/sammoRootAPI";
|
||||
import type { ExtractError, ExtractQuery, ExtractResponse } from "../../apiStructure/defs";
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { LoginCtx } from "./ReqLogin";
|
||||
import { SessionCtx } from "./StartSession";
|
||||
import type { LoginCtx } from "./ReqLogin";
|
||||
import type { SessionCtx } from "./StartSession";
|
||||
import type { ProcDecoratorGenerator } from "./base";
|
||||
import type { Request, Response } from "express";
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { LoginCtx } from "./ReqLogin";
|
||||
import { SessionCtx } from "./StartSession";
|
||||
import type { LoginCtx } from "./ReqLogin";
|
||||
import type { SessionCtx } from "./StartSession";
|
||||
import type { ProcDecoratorGenerator } from "./base";
|
||||
import type { Request, Response } from "express";
|
||||
import { delay } from "../../util/delay.js";
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { SessionCtx } from "./StartSession";
|
||||
import type { SessionCtx } from "./StartSession";
|
||||
import type { InvalidProc, ProcDecoratorGenerator } from "./base";
|
||||
|
||||
export type LoginCtx = {
|
||||
|
||||
@@ -13,7 +13,7 @@ export type SessionCtx = {
|
||||
|
||||
export function StartSession<Q extends object = Empty>(): ProcDecoratorGenerator<SessionCtx, Q> {
|
||||
return (inCtx) => {
|
||||
const raw: object = {};
|
||||
const raw: Record<string, unknown> = {};
|
||||
return {
|
||||
clearSession: () => Promise.resolve(),
|
||||
deleteValue: async (key: string) => {
|
||||
|
||||
@@ -9,7 +9,7 @@ export type InvalidProc = InvalidResponse & {
|
||||
alreadyProcessed?: boolean;
|
||||
};
|
||||
|
||||
export interface ProcDecorator<Out, In extends object = Record<string, never>> {
|
||||
export interface ProcDecorator<Out extends object, In extends object = Record<string, never>> {
|
||||
(inCtx: In, req: Request, res: Response, isValidRoute: boolean): MayBePromise<Out | InvalidProc>;
|
||||
}
|
||||
|
||||
|
||||
+8
-198
@@ -6,162 +6,30 @@ import {
|
||||
type ValidatorOptions,
|
||||
} from "class-validator";
|
||||
import { type ClassTransformOptions, plainToInstance } from "class-transformer";
|
||||
import { InvalidProc, ProcDecorator, ProcDecoratorChain, ResolveChain } from './ProcDecorator/base';
|
||||
import type { InvalidProc, ProcDecorator, ProcDecoratorChain, ResolveChain } from './ProcDecorator/base.js';
|
||||
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>>
|
||||
| iAPI_GET<any, any, any, any>
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
| ClassType<POST<any, any, any, any>>
|
||||
| iAPI_POST<any, any, any, any>
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
| ClassType<PUT<any, any, any, any>>
|
||||
| iAPI_PUT<any, any, any, any>
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
| ClassType<DELETE<any, any, any, any>>
|
||||
| iAPI_DELETE<any, any, any, any>
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
| ClassType<PATCH<any, any, any, any>>
|
||||
| iAPI_PATCH<any, any, any, any>
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
| ClassType<HEAD<any, any, any, any>>
|
||||
| iAPI_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 type AnyAPIExecuter = ngAPIExecuter<any, any, any, any>;
|
||||
|
||||
interface ngAPIExecuter<R extends ValidResponse, E extends InvalidResponse, Q extends RawArgType, PD extends ProcDecoratorChain> {
|
||||
(query: Q, ctx: ResolveChain<PD>, expressReq: Request, expressRes: Response): Promise<R | E | true>;
|
||||
@@ -305,64 +173,6 @@ export function ngHEAD<R extends ValidResponse, E extends InvalidResponse, Q ext
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
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 {
|
||||
|
||||
+175
-6
@@ -1,17 +1,186 @@
|
||||
import { Router } from 'express';
|
||||
import type { AnyAPIExecuter, APINamespace } from './defs.js';
|
||||
import { Router, type Request, type Response } from 'express';
|
||||
import type { AnyAPIExecuter, APINamespace, ClassType } from './defs.js';
|
||||
import { plainToInstance } from 'class-transformer';
|
||||
import { validate } from 'class-validator';
|
||||
import type { ExtractQuery, RawArgType } from '../apiStructure/defs.js';
|
||||
import type { Empty, InvalidProc, ProcDecorator, ProcDecoratorChain, ResolveChain } from './ProcDecorator/base.js';
|
||||
import clamp from 'lodash-es/clamp.js';
|
||||
|
||||
async function parseParam<Q extends Exclude<RawArgType, undefined>>(req: Request, argValidator?: ClassType<Q>): Promise<Q> {
|
||||
if (!argValidator) {
|
||||
return req.query as Q;
|
||||
}
|
||||
|
||||
const query = req.query;
|
||||
const classObject = plainToInstance(argValidator, query);
|
||||
const errors = await validate(classObject);
|
||||
if (errors.length) {
|
||||
throw errors;
|
||||
}
|
||||
|
||||
return classObject as Q;
|
||||
}
|
||||
|
||||
async function parseBody<Q extends Exclude<RawArgType, undefined>>(req: Request, argValidator?: ClassType<Q>): Promise<Q> {
|
||||
if (!argValidator) {
|
||||
return req.body as Q;
|
||||
}
|
||||
|
||||
const query = req.body;
|
||||
const classObject = plainToInstance(argValidator, query);
|
||||
const errors = await validate(classObject);
|
||||
if (errors.length) {
|
||||
throw errors;
|
||||
}
|
||||
|
||||
return classObject as Q;
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
type PlainDecorator = ProcDecorator<any, any>;
|
||||
|
||||
async function preCtx<PD extends ProcDecorator<any, any>>(preDecorator: PlainDecorator[], req: Request, res: Response): Promise<ResolveChain<PD> | [InvalidProc, ResolveChain<PD>, number]> {
|
||||
if (!preDecorator) {
|
||||
return {} as ResolveChain<PD>;
|
||||
}
|
||||
|
||||
let ctx = {} as ResolveChain<PD>;
|
||||
for (const [idx, proc] of preDecorator.entries()) {
|
||||
try {
|
||||
const result = await proc(ctx, req, res, 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;
|
||||
}
|
||||
|
||||
async function postCtx<PD extends ProcDecoratorChain>(ctx: ResolveChain<PD>, postDecorator: (undefined|PlainDecorator)[], req: Request, res: Response, index = -1): Promise<[InvalidProc, number] | undefined> {
|
||||
if (!postDecorator) {
|
||||
return;
|
||||
}
|
||||
|
||||
let isValidRoute = true;
|
||||
if (index !== -1 && index !== postDecorator.length) {
|
||||
isValidRoute = false;
|
||||
index = clamp(index, 0, postDecorator.length);
|
||||
}
|
||||
|
||||
for (let i = index - 1; i >= 0; i--) {
|
||||
const proc = postDecorator[i];
|
||||
if (!proc) {
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
const result = await proc(ctx, req, res, 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;
|
||||
}
|
||||
|
||||
async function apiRun(query: object, req: Request, res: Response, api: AnyAPIExecuter): Promise<void> {
|
||||
const ctx = await preCtx(api.preDecorator, req, res);
|
||||
if (Array.isArray(ctx)) {
|
||||
const [invalidProc, errCtx, idx] = ctx;
|
||||
if (idx == 0) {
|
||||
res.json(invalidProc);
|
||||
return;
|
||||
}
|
||||
const result = await postCtx(errCtx, api.postDecorator, req, res, idx);
|
||||
if (result === undefined) {
|
||||
res.json(invalidProc);
|
||||
return;
|
||||
}
|
||||
const [postInvalidProc,] = result;
|
||||
res.json(postInvalidProc);
|
||||
return;
|
||||
}
|
||||
const result = await api(query, ctx, req, res);
|
||||
const postResult = await postCtx(ctx, api.postDecorator, req, res);
|
||||
if (postResult !== undefined) {
|
||||
if (result === true) {
|
||||
//NOTE: 이미 api에서 response를 보낸 특이 케이스.
|
||||
return;
|
||||
}
|
||||
const [invalidProc,] = postResult;
|
||||
invalidProc.alreadyProcessed = true;
|
||||
res.json(invalidProc);
|
||||
return;
|
||||
}
|
||||
|
||||
if (result !== true) {
|
||||
res.json(result);
|
||||
}
|
||||
}
|
||||
|
||||
export function buildAPISystem<N extends APINamespace, Q extends AnyAPIExecuter>(api: N | Q): Router {
|
||||
const router = Router();
|
||||
for (const [key, value] of Object.entries(api)) {
|
||||
if (typeof value === 'function') {
|
||||
const executer = new value() as Q;
|
||||
if (!executer.reqType) {
|
||||
|
||||
if (!executer.httpMethod) {
|
||||
throw new Error('APIExecuter.reqType is not defined');
|
||||
}
|
||||
router[executer.reqType](key, async (req, res) => {
|
||||
await executer.run(req, res);
|
||||
});
|
||||
|
||||
if (executer.httpMethod === 'head') {
|
||||
router[executer.httpMethod](key, async (req, res) => {
|
||||
let query: ExtractQuery<Q>;
|
||||
try {
|
||||
query = await parseParam(req, executer.argValidator);
|
||||
}
|
||||
catch (e) {
|
||||
res.json({
|
||||
result: false,
|
||||
reason: `invalid parameter: ${e}`,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
await apiRun(query, req, res, executer);
|
||||
});
|
||||
}
|
||||
else {
|
||||
router[executer.httpMethod](key, async (req, res) => {
|
||||
let query: ExtractQuery<Q>;
|
||||
try {
|
||||
query = await parseBody(req, executer.argValidator);
|
||||
}
|
||||
catch (e) {
|
||||
res.json({
|
||||
result: false,
|
||||
reason: `invalid parameter: ${e}`,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
await apiRun(query, req, res, executer);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
continue;
|
||||
} else {
|
||||
router.use(key, buildAPISystem(value));
|
||||
|
||||
Reference in New Issue
Block a user