gateway 코드 이식 중

This commit is contained in:
2023-09-22 21:34:01 +09:00
parent 438ee85ed2
commit 93686c4912
18 changed files with 431 additions and 2 deletions
+2 -1
View File
@@ -12,7 +12,8 @@
"license": "MIT",
"dependencies": {
"@sammo/api_def": "workspace:^",
"@sammo/util": "workspace:^"
"@sammo/util": "workspace:^",
"@strpc/express": "workspace:^"
},
"devDependencies": {
"lodash-es": "^4.17.21"
+58
View File
@@ -0,0 +1,58 @@
import type { Empty, ProcDecoratorGenerator } from "@strpc/express/proc_decorator";
import { type Session, type SessionData } from "express-session";
export type SessionCtx = {
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, 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,
},
{
session: sessionObj,
...inCtx,
}
]
}
}
+2 -1
View File
@@ -1 +1,2 @@
export { UniqueNumberAllocator, type StateIncrementer } from './UniqueNumberAllocator.js';
export { UniqueNumberAllocator, type StateIncrementer } from './UniqueNumberAllocator.js';
export * from './StartSession.js';
+3
View File
@@ -23,6 +23,9 @@
"composite": true
},
"references": [
{
"path": "../../@strpc/express"
},
{
"path": "../util"
},