api 설계 변경

This commit is contained in:
2023-08-18 13:02:40 +00:00
parent 7716c6936b
commit bfe0b99d44
7 changed files with 168 additions and 179 deletions
@@ -1,22 +0,0 @@
import type { LoginCtx } from "./ReqLogin.js";
import type { SessionCtx } from "./StartSession.js";
import type { ProcDecoratorGenerator } from "./base.js";
import type { Request, Response } from "express";
export interface UserLevelCtx {
userLevel: number;
}
export function ParseUserLevel<Q extends LoginCtx & SessionCtx>(): ProcDecoratorGenerator<UserLevelCtx, Q> {
return (ctx) => {
const userLevel = 123;
ctx.setValue('userLevel', userLevel);
return [{
result: true,
}, {
userLevel,
...ctx,
}];
}
}
+21 -25
View File
@@ -1,39 +1,35 @@
import type { LoginCtx } from "./ReqLogin.js";
import type { SessionCtx } from "./StartSession.js";
import type { ProcDecoratorGenerator } from "./base.js";
import type { Request, Response } from "express";
import { delay } from "../../util/delay.js";
import type { DecoratorResult, ProcDecoratorGenerator } from "./base.js";
export type GameLoginCtx = {
generalID: number;
generalName: string;
gameLoginDate: Date;
}
export function ReqGameLogin<Q extends LoginCtx & SessionCtx>(): ProcDecoratorGenerator<GameLoginCtx, Q> {
return async (inCtx) => {
const ctx: Q & Partial<GameLoginCtx> = inCtx;
if (ctx.generalID !== undefined) {
return [{
result: true,
type: 'GameLogin',
info: 'UseSession',
}, {
generalID: ctx.generalID,
...inCtx,
}, ];
return async (ctx) => {
//NOTE: 게임 서버별로 gameLoginCtx를 따로 가져야 하는가?
let gameLoginCtx = ctx.session.getItem<GameLoginCtx>(`gameLoginCtx`);
if(gameLoginCtx){
if(gameLoginCtx.gameLoginDate >= ctx.loginDate){
return [{
result: true,
},{
...gameLoginCtx,
...ctx,
}];
}
gameLoginCtx = undefined;
}
console.log('Something GameLogin');
await delay(0);
inCtx.setValue('generalID', ctx.userID * 4);
//TODO: DB에서 generalID를 가져오는 로직
return [{
result: true,
type: 'GameLogin',
info: 'UseDB',
}, {
generalID: ctx.userID * 4,
...inCtx,
}, ]
result: false,
type: 'Required GameLogin',
info: 'NotYetImplemented'
}, ctx];
}
}
+13 -6
View File
@@ -1,15 +1,18 @@
import type { SessionCtx } from "./StartSession.js";
import type { DecoratorResultFalse, DecoratorResultTrue, ProcDecorator, ProcDecoratorGenerator } from "./base.js";
import type { ProcDecorator } from "./base.js";
export type LoginCtx = {
userID: number;
userName: string;
userLevel: number;
allowServerAction: Record<string, number>;
loginDate: Date;
}
export function ReqLogin<Q extends SessionCtx>(): ProcDecorator<LoginCtx & Q, Q> {
return (ctx) => {
const userID = ctx.userID;
if (userID === undefined) {
const loginCtx = ctx.session.getItem<LoginCtx>('loginCtx');
if(!loginCtx){
return [{
result: false,
type: 'Required Login',
@@ -17,7 +20,11 @@ export function ReqLogin<Q extends SessionCtx>(): ProcDecorator<LoginCtx & Q, Q>
}, ctx];
}
return [{ result: true }, ctx as Q & LoginCtx];
return [{
result: true,
},{
...loginCtx,
...ctx,
}];
}
}
+49 -30
View File
@@ -1,39 +1,58 @@
import { type Session, type SessionData } from "express-session";
import type { Empty, ProcDecoratorGenerator } from "./base.js";
export type SessionCtx = {
clearSession: () => Promise<void>;
deleteValue: (key: string) => Promise<void>;
getValue: <T>(key: string) => Promise<T | undefined>;
setValue: (key: string, value: unknown) => Promise<void>;
sessionRaw: {
raw: object;
changed: boolean;
};
session: {
clear: () => Promise<void>;
removeItem: (key: string) => boolean;
getItem: <T>(key: string) => T | undefined;
setItem: (key: string, value: unknown) => void;
raw: Session & Partial<SessionData> & Record<string, unknown>;
}
}
export function StartSession<Q extends object = Empty>(): ProcDecoratorGenerator<SessionCtx, Q> {
return (inCtx) => {
const raw: Record<string, unknown> = {};
return [{ result: true }, {
clearSession: () => Promise.resolve(),
deleteValue: async (key: string) => {
console.log('deleteValue', key);
if (raw[key] !== undefined) {
delete raw[key];
}
return (inCtx, req) => {
if (!req.session) {
throw 'Express-session required';
}
const sessionObj = {
raw: req.session
} as SessionCtx['session'];
sessionObj.clear = () => {
return new Promise((resolve) => {
sessionObj.raw = req.session.regenerate(resolve) as SessionCtx['session']['raw'];
})
}
sessionObj.removeItem = (key: string): boolean => {
if (key in sessionObj.raw) {
delete sessionObj.raw[key];
return true;
}
return false;
}
sessionObj.getItem = <T>(key: string): T | undefined => {
if (!(key in sessionObj.raw)) {
return undefined;
}
return sessionObj.raw[key] as T;
}
sessionObj.setItem = <T>(key: string, value: T | undefined): void => {
if (value === undefined) {
sessionObj.removeItem(key);
return;
}
sessionObj.raw[key] = value;
}
return [
{
result: true,
},
getValue: async <T>(key: string) => {
return raw[key] as T | undefined;
},
setValue: async (key: string, value: unknown) => {
console.log('setValue', key, value);
raw[key] = value;
},
sessionRaw: {
raw,
changed: false,
},
...inCtx,
}];
{
session: sessionObj,
...inCtx,
}
]
}
}
+35 -21
View File
@@ -16,10 +16,9 @@ export type DecoratorResultFalse = {
export type DecoratorResult = DecoratorResultTrue | DecoratorResultFalse;
export type DecoratorStack = DecoratorResult[];
export interface ProcDecorator<Out extends object, In extends object = Record<string, never>> {
export interface ProcDecorator<Out extends object, In extends object = Empty> {
(inCtx: In & Partial<Out>, req: Request, res: Response)
: MayBePromise<[DecoratorResultTrue, Out]>
| MayBePromise<[DecoratorResultFalse, In & Partial<Out>]>;
: MayBePromise<[DecoratorResultTrue, Out] | [DecoratorResultFalse, In & Partial<Out>]>;
}
export interface PostProcDecorator<T extends object> {
@@ -50,12 +49,15 @@ export type ResolveChain<T> = T extends undefined ? Empty : T extends ProcDecora
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type ParseInType<T> = T extends ProcDecorator<any, infer A> ? A : never;
export type ParseInType<T> = T extends ProcDecorator<any, infer A> ? object extends A ? A : never : never;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type ParseOutType<T> = T extends ProcDecorator<infer B, any> ? B : never;
export type ParseOutType<T> = T extends ProcDecorator<infer B, object> ? B : never;
export function declProcDecorators<T extends ProcDecoratorChain>(decorators: T) {
type InType = ParseInType<PackChain<T>>;
export const EmptyProcDecorator: readonly [ProcDecoratorRunner<Empty, Empty>, PostProcDecoratorRunner<Empty>] = [
async (ctx) => [[], ctx], async (ctx, stack) => [stack, ctx]
];
export function declProcDecorators<T extends ProcDecoratorChain>(...decorators: T) {
type OutType = ParseOutType<PackChain<T>>;
const preDecorator: PlainDecorator[] = [];
@@ -74,7 +76,7 @@ export function declProcDecorators<T extends ProcDecoratorChain>(decorators: T)
}
}
const packedDecorators: readonly [ProcDecoratorRunner<OutType, InType>, PostProcDecoratorRunner<OutType>] = [
const packedDecorators: readonly [ProcDecoratorRunner<OutType, Empty>, PostProcDecoratorRunner<OutType>] = [
async (ctx, req, res) => {
let rctx = ctx as unknown as OutType;
const decoratorStack: DecoratorStack = [];
@@ -111,35 +113,47 @@ export function declProcDecorators<T extends ProcDecoratorChain>(decorators: T)
return [decoratorStack, rctx];
},
async (ctx, stack, req, res) => {
if (!postDecorator) {
if (!postDecorator.length) {
return [stack, ctx];
}
const isValidRoute = stack.length === postDecorator.length && stack.every(v => v.result);
while (stack.length > 0) {
const preStackResult = stack.pop() as DecoratorResult;
const idx = stack.length;
let isValidRoute = stack.length === postDecorator.length && stack.every(v => v.result);
for (let idx = stack.length - 1; idx >= 0; idx--) {
const preStackResult = stack[idx] as DecoratorResult;
if (!preStackResult.result) {
continue;
}
const proc = postDecorator[idx];
if (!proc) {
if (isValidRoute) {
stack.pop();
}
continue;
}
try {
const [postStackResult, nextCtx] = await proc(ctx, preStackResult, req, res, isValidRoute);
if (!postStackResult.result) {
stack.push(postStackResult);
return [stack, nextCtx];
if (postStackResult.result) {
if (isValidRoute) {
stack.pop();
}
ctx = nextCtx;
continue;
}
isValidRoute = false;
stack[idx] = postStackResult;
ctx = nextCtx;
}
catch (e) {
stack.push({
isValidRoute = false;
stack[idx] = {
result: false,
type: 'PostThrow',
info: `internal error: ${e}`,
});
return [stack, ctx];
};
}
}
@@ -150,7 +164,7 @@ export function declProcDecorators<T extends ProcDecoratorChain>(decorators: T)
return packedDecorators;
}
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 Compose2<B extends object, A extends object, D extends object, C> = B extends C ? ProcDecorator<D & B, A> : never;
type PD1<B extends object, A extends object> = () => ProcDecorator<B, A>;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -166,4 +180,4 @@ export type PackChain<T> =
T extends readonly [PD2<infer B, infer A>, PD2<infer D, infer C>, ... infer R] ? PackChain<[() => Compose2<B, A, D, C>, ...R]> :
never;
type Resolve<T> = T extends ProcDecorator<infer B, infer A> ? Empty extends A ? B : never : never;
type Resolve<T> = T extends ProcDecorator<infer B, infer A> ? Empty extends A ? B : never : never;