/
dimamir
/
undb
Обзор
Документация
Войти
/
dimamir
/
undb
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
develop
packages/context/src/server.ts
54 строки
1 KB
nichenqin
chore: context injection
09 окт 2024, 12:45
09 окт 2024, 12:45
16f233b
Код
Авторство
О чём код?
import { singleton } from "@undb/di" import { AsyncLocalStorage } from "node:async_hooks" import type { ExecuteContext, IContext, IExecutionContext, SetContextValue } from "." export const executionContext = new AsyncLocalStorage() satisfies IExecutionContext export const setContextValue: SetContextValue = (key: keyof ExecuteContext, value: any): void => { const store = executionContext.getStore() if (store) { ;(store as any)[key] = value } } export const getCurrentUser = () => { return executionContext.getStore()?.user! } export const getCurrentUserId = () => { return executionContext.getStore()?.user?.userId! } export const getCurrentMember = () => { return executionContext.getStore()?.member! } export const getCurrentRole = () => { return getCurrentMember().role! } export const getCurrentSpaceId = () => { return executionContext.getStore()?.spaceId } export const mustGetCurrentSpaceId = () => { const spaceId = getCurrentSpaceId() if (!spaceId) { throw new Error("SpaceId is required") } return spaceId } @singleton() export class ServerContext implements IContext { mustGetCurrentSpaceId(): string { return mustGetCurrentSpaceId() } mustGetCurrentUserId(): string { return getCurrentUserId() } getCurrentUserId(): string | undefined { return getCurrentUserId() } }