ProcDecorator 정리
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
import { LoginCtx } from "./ReqLogin";
|
||||
import type { ProcDecoratorGenerator } from "./base";
|
||||
import type { Request, Response } from "express";
|
||||
|
||||
|
||||
export interface UserLevelCtx {
|
||||
userLevel: number;
|
||||
}
|
||||
|
||||
export function ParseUserLevel<Q extends LoginCtx>(): ProcDecoratorGenerator<UserLevelCtx, Q> {
|
||||
return (inCtx) => {
|
||||
return {
|
||||
userLevel: 123,
|
||||
...inCtx,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
import { LoginCtx } from "./ReqLogin";
|
||||
import type { ProcDecoratorGenerator } from "./base";
|
||||
import type { Request, Response } from "express";
|
||||
|
||||
export type GameLoginCtx = {
|
||||
generalID: number;
|
||||
}
|
||||
|
||||
export function ReqGameLogin<Q extends LoginCtx>(): ProcDecoratorGenerator<GameLoginCtx, Q> {
|
||||
return (inCtx) => {
|
||||
return {
|
||||
generalID: inCtx.userID * 4,
|
||||
...inCtx,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
import type { Empty, ProcDecoratorPrePostGenerator } from "./base";
|
||||
|
||||
export type LoginCtx = {
|
||||
userID: number;
|
||||
}
|
||||
|
||||
export function ReqLogin<Q extends object = Empty>(): ProcDecoratorPrePostGenerator<LoginCtx, Q> {
|
||||
return [
|
||||
(inCtx) => {
|
||||
return {
|
||||
userID: 999,
|
||||
...inCtx,
|
||||
};
|
||||
},
|
||||
(inCtx) => {
|
||||
console.log('ReqLogin post');
|
||||
return inCtx;
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -1,3 +1,5 @@
|
||||
import type { Request, Response } from "express";
|
||||
|
||||
type MayBePromise<T> = T | Promise<T>;
|
||||
|
||||
export type InvalidProc = {
|
||||
@@ -10,7 +12,7 @@ export interface ProcDecorator<Out, In extends object = Record<string, never>> {
|
||||
(inCtx: In, req: Request, res: Response): MayBePromise<Out | InvalidProc>;
|
||||
}
|
||||
|
||||
type Empty = Record<string, never>;
|
||||
export type Empty = Record<string, never>;
|
||||
|
||||
type Login = {
|
||||
userID: number;
|
||||
@@ -57,6 +59,9 @@ export function ReqGameLogin<Q extends Login = Login>(): ProcDecorator<Q & GameL
|
||||
}
|
||||
}
|
||||
|
||||
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>];
|
||||
|
||||
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>;
|
||||
@@ -1,32 +0,0 @@
|
||||
import { type BaseConverter, InvalidConversion } from "./base";
|
||||
import { Request, Response, NextFunction } from "express";
|
||||
|
||||
|
||||
interface GameLoginIn {
|
||||
userID: number;
|
||||
[key: string]: unknown;
|
||||
}
|
||||
|
||||
interface UserLevelOut {
|
||||
userID: number;
|
||||
userLevel: number;
|
||||
[key: string]: unknown;
|
||||
}
|
||||
|
||||
/*export class ReqGameLogin0 extends BaseConverter<SessionIn, SessionOut> {
|
||||
override transform(inCtx: SessionIn, request: Request, response: Response): SessionOut | InvalidConversion {
|
||||
return {
|
||||
generalID: inCtx.userID * 4,
|
||||
...inCtx,
|
||||
}
|
||||
}
|
||||
}*/
|
||||
|
||||
export function ParseUserLevel(): BaseConverter<GameLoginIn, UserLevelOut> {
|
||||
return (inCtx: GameLoginIn, request: Request, response: Response): UserLevelOut | InvalidConversion => {
|
||||
return {
|
||||
userLevel: 123,
|
||||
...inCtx,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
import { type BaseConverter, InvalidConversion } from "./base";
|
||||
import { Request, Response, NextFunction } from "express";
|
||||
|
||||
|
||||
interface GameLoginIn {
|
||||
userID: number;
|
||||
[key: string]: unknown;
|
||||
}
|
||||
|
||||
interface GameLoginOut {
|
||||
userID: number;
|
||||
generalID: number;
|
||||
[key: string]: unknown;
|
||||
}
|
||||
|
||||
/*export class ReqGameLogin0 extends BaseConverter<SessionIn, SessionOut> {
|
||||
override transform(inCtx: SessionIn, request: Request, response: Response): SessionOut | InvalidConversion {
|
||||
return {
|
||||
generalID: inCtx.userID * 4,
|
||||
...inCtx,
|
||||
}
|
||||
}
|
||||
}*/
|
||||
|
||||
export function ReqGameLogin(): BaseConverter<GameLoginIn, GameLoginOut> {
|
||||
return (inCtx: GameLoginIn, request: Request, response: Response): GameLoginOut | InvalidConversion => {
|
||||
return {
|
||||
generalID: inCtx.userID * 4,
|
||||
...inCtx,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,42 +0,0 @@
|
||||
import { BaseConverter, InvalidConversion } from "./base";
|
||||
import { Request, Response, NextFunction } from "express";
|
||||
|
||||
|
||||
interface SessionIn {
|
||||
[key: string]: unknown;
|
||||
}
|
||||
|
||||
interface SessionOut {
|
||||
userID: number;
|
||||
[key: string]: unknown;
|
||||
}
|
||||
|
||||
/*export class ReqLogin extends BaseConverter<SessionIn, SessionOut> {
|
||||
override transform(inCtx: SessionIn, request: Request, response: Response): SessionOut | InvalidConversion {
|
||||
if(request.params['something'] === 'haha'){
|
||||
return {
|
||||
isInvalid: true,
|
||||
reason: 'haha',
|
||||
}
|
||||
}
|
||||
return {
|
||||
generalID: inCtx.userID * 4,
|
||||
...inCtx,
|
||||
}
|
||||
}
|
||||
}*/
|
||||
|
||||
export function ReqLogin(): BaseConverter<SessionIn, SessionOut> {
|
||||
return (inCtx: SessionIn, request: Request, response: Response): SessionOut | InvalidConversion => {
|
||||
if(request.params['something'] === 'haha'){
|
||||
return {
|
||||
isInvalid: true,
|
||||
reason: 'haha',
|
||||
}
|
||||
}
|
||||
return {
|
||||
userID: 123,
|
||||
...inCtx,
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user