꼬이네..
This commit is contained in:
@@ -11,10 +11,14 @@ class Q implements QType {
|
||||
username!: string;
|
||||
password!: string;
|
||||
}
|
||||
export class LoginByID extends POST<RType, EType, QType>{
|
||||
|
||||
const procDecorator = [] as const;
|
||||
|
||||
export class LoginByID extends POST<RType, EType, QType, typeof procDecorator>{
|
||||
readonly argValidator = Q;
|
||||
readonly procDecorator = procDecorator;
|
||||
protected LoginByID = Symbol("LoginByID");//TODO: remove this
|
||||
protected override async api(query: QType): Promise<RType | EType | true> {
|
||||
protected override async api(query: QType, ctx,): Promise<RType | EType | true> {
|
||||
throw new Error("Method not implemented.");
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,7 @@ type QType = ExtractQuery<BaseAPI>;
|
||||
|
||||
export class ReqNonce extends GET<RType, EType, QType>{
|
||||
readonly argValidator = undefined;
|
||||
readonly procDecorator = [] as const;
|
||||
protected ReqNonce = Symbol("ReqNonce");//TODO: remove this
|
||||
protected override async api(query: QType): Promise<RType | EType | true> {
|
||||
throw new Error("Method not implemented.");
|
||||
|
||||
@@ -1,67 +1,28 @@
|
||||
import type { Request, Response } from "express";
|
||||
import type { InvalidResponse } from "../../apiStructure/defs";
|
||||
|
||||
type MayBePromise<T> = T | Promise<T>;
|
||||
|
||||
export type InvalidProc = {
|
||||
export type Empty = Record<string, never>;
|
||||
export type InvalidProc = InvalidResponse & {
|
||||
invalidProcSymbol: unknown;
|
||||
reason?: string;
|
||||
alreadyProcessed?: boolean;
|
||||
}
|
||||
};
|
||||
|
||||
export interface ProcDecorator<Out, In extends object = Record<string, never>> {
|
||||
(inCtx: In, req: Request, res: Response): MayBePromise<Out | InvalidProc>;
|
||||
}
|
||||
|
||||
export type Empty = Record<string, never>;
|
||||
|
||||
type Login = {
|
||||
userID: number;
|
||||
}
|
||||
type GameLogin = {
|
||||
userID: number;
|
||||
generalID: number;
|
||||
}
|
||||
type UserLevel = {
|
||||
userID: number;
|
||||
userLevel: number;
|
||||
}
|
||||
|
||||
export function ParseUserLevel<Q extends Login = Login>(): ProcDecorator<Q & UserLevel, Q> {
|
||||
return (inCtx) => {
|
||||
return {
|
||||
userLevel: 123,
|
||||
...inCtx,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export function ReqLogin<Q extends object = Empty>(): [ProcDecorator<Q & Login, Q>, ProcDecorator<Login & object, Login & object>] {
|
||||
return [
|
||||
(inCtx) => {
|
||||
return {
|
||||
userID: 999,
|
||||
...inCtx,
|
||||
};
|
||||
},
|
||||
(inCtx) => {
|
||||
//무언가
|
||||
return inCtx;
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
export function ReqGameLogin<Q extends Login = Login>(): ProcDecorator<Q & GameLogin, Q> {
|
||||
return (inCtx) => {
|
||||
return {
|
||||
generalID: inCtx.userID * 4,
|
||||
...inCtx,
|
||||
};
|
||||
}
|
||||
(inCtx: In, req: Request, res: Response, isValidRoute: boolean): MayBePromise<Out | InvalidProc>;
|
||||
}
|
||||
|
||||
export type ProcDecoratorGenerator<B extends object, A extends object> = ProcDecorator<B & A, A>;
|
||||
export type ProcDecoratorPrePostGenerator<B extends object, A extends object> = [ProcDecorator<B & A, A>, ProcDecorator<B & object, B & object>];
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
export type ProcDecoratorChain = undefined | readonly ((() => ProcDecorator<any, any>) | (() => [ProcDecorator<any, any>, ProcDecorator<any, any>]))[];
|
||||
|
||||
export type ResolveChain<T> = T extends undefined ? Empty : T extends ProcDecoratorChain ? Resolve<PackChain<T>> : never;
|
||||
|
||||
|
||||
|
||||
type Compose2<B extends object, A extends object, D, C> = D & B extends A & C ? B extends C ? ProcDecorator<D & B, A> : never : never;
|
||||
|
||||
type PD1<B extends object, A extends object> = () => ProcDecorator<B, A>;
|
||||
@@ -79,74 +40,3 @@ type PackChain<T> =
|
||||
never;
|
||||
|
||||
type Resolve<T> = T extends ProcDecorator<infer B, infer A> ? Empty extends A ? B : never : never;
|
||||
|
||||
|
||||
export type ResolveChain<T extends ProcDecoratorChain> = Resolve<PackChain<T>>;
|
||||
|
||||
const q1 = [
|
||||
ReqLogin,
|
||||
ParseUserLevel,
|
||||
ReqGameLogin,
|
||||
] as const;
|
||||
|
||||
const q2 = [
|
||||
ReqLogin,
|
||||
ParseUserLevel,
|
||||
] as const;
|
||||
|
||||
|
||||
type R1 = Resolve<PackChain<typeof q1>>;
|
||||
type R2 = Resolve<PackChain<typeof q2>>;
|
||||
const r1: R1 = undefined as unknown as R1;
|
||||
const r2: R2 = undefined as unknown as R2;
|
||||
r1.userLevel;
|
||||
r2.userID;
|
||||
|
||||
|
||||
async function test(req: Request, res: Response) {
|
||||
const a0 = await ReqLogin()[0]({}, req, res);
|
||||
if ('invalidProcSymbol' in a0) {
|
||||
return false;
|
||||
}
|
||||
const a1 = await ParseUserLevel()(a0, req, res);
|
||||
if ('invalidProcSymbol' in a1) {
|
||||
return false;
|
||||
}
|
||||
const a2 = await ReqGameLogin()(a1, req, res);
|
||||
if ('invalidProcSymbol' in a2) {
|
||||
return false;
|
||||
}
|
||||
const b0 = await ReqLogin()[1](a2, req, res);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
export type ProcDecoratorChain = readonly ((() => ProcDecorator<any, any>) | (() => [ProcDecorator<any, any>, ProcDecorator<any, any>]))[];
|
||||
|
||||
function unpack2<A extends ProcDecorator<any, any>, B>(v: A | [A, B]): A {
|
||||
if (Array.isArray(v)) {
|
||||
return v[0];
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
function test2<T extends ProcDecoratorChain>(req: Request, res: Response, chain: T): Resolve<PackChain<T>> | false {
|
||||
const tmp = chain[0]();
|
||||
|
||||
const actors = chain.map((v) => unpack2(v()));
|
||||
|
||||
let result = {};
|
||||
for (const actor of actors) {
|
||||
result = actor(result, req, res);
|
||||
if ('isInvalid' in result) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return result as Resolve<PackChain<T>>;
|
||||
}
|
||||
|
||||
const finalResult1 = test2({} as Request, {} as Response, q1);
|
||||
if (finalResult1 !== false) {
|
||||
finalResult1.userLevel;
|
||||
}
|
||||
+153
-29
@@ -6,46 +6,164 @@ import {
|
||||
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>>
|
||||
| ClassType<GET<any, any, any, any>>
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
| ClassType<POST<any, any, any>>
|
||||
| ClassType<POST<any, any, any, any>>
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
| ClassType<PUT<any, any, any>>
|
||||
| ClassType<PUT<any, any, any, any>>
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
| ClassType<DELETE<any, any, any>>
|
||||
| ClassType<DELETE<any, any, any, any>>
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
| ClassType<PATCH<any, any, any>>
|
||||
| ClassType<PATCH<any, any, any, any>>
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
| ClassType<HEAD<any, any, 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>;
|
||||
export type AnyAPIExecuter = APIExecuter<any, any, any, any>;
|
||||
|
||||
export abstract class APIExecuter<R extends ValidResponse, E extends InvalidResponse, Q extends RawArgType>{
|
||||
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>;
|
||||
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> {
|
||||
const query = await this.parseQuery(expressReq);
|
||||
//TODO: middleware (인증, 요구사항, 아마도 decorator)
|
||||
const result = await this.api(query, expressReq, expressRes);
|
||||
if (result === true) {
|
||||
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;
|
||||
}
|
||||
expressRes.json(result);
|
||||
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> extends APIExecuter<R, E, Q>{
|
||||
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) {
|
||||
@@ -63,7 +181,7 @@ export abstract class APIBodyParseExecuter<R extends ValidResponse, E extends In
|
||||
}
|
||||
}
|
||||
|
||||
export abstract class GET<R extends ValidResponse, E extends InvalidResponse, Q extends RawArgType> extends APIExecuter<R, E, 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) {
|
||||
@@ -81,23 +199,23 @@ export abstract class GET<R extends ValidResponse, E extends InvalidResponse, Q
|
||||
}
|
||||
}
|
||||
|
||||
export abstract class POST<R extends ValidResponse, E extends InvalidResponse, Q extends RawArgType> extends APIBodyParseExecuter<R, E, 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> extends APIBodyParseExecuter<R, E, Q>{
|
||||
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> extends APIBodyParseExecuter<R, E, Q>{
|
||||
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> extends APIBodyParseExecuter<R, E, Q>{
|
||||
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> extends APIBodyParseExecuter<R, E, Q>{
|
||||
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';
|
||||
}
|
||||
|
||||
@@ -120,12 +238,18 @@ export function raiseError(reason: string, recovery?: recoveryMethod): InvalidRe
|
||||
export type ClassType<T> = new (...args: any[]) => T;
|
||||
|
||||
export type APIServerType<T extends Callable> =
|
||||
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>> :
|
||||
// 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]:
|
||||
|
||||
Reference in New Issue
Block a user