Files
lcbp3/backend/src/common/services/request-context.service.ts
T
admin 1d3479770b
Build and Deploy / deploy (push) Has been cancelled
260320:1131 Refactor Overrall #01
2026-03-20 11:31:27 +07:00

36 lines
960 B
TypeScript

// File: src/common/services/request-context.service.ts
// บันทึกการแก้ไข: เก็บ Context ระหว่าง Request (User, TraceID) (T1.1)
import { Injectable, Scope } from '@nestjs/common';
import { AsyncLocalStorage } from 'async_hooks';
@Injectable({ scope: Scope.DEFAULT })
export class RequestContextService {
private static readonly cls = new AsyncLocalStorage<Map<string, any>>();
static run(fn: () => void) {
this.cls.run(new Map(), fn);
}
static set(key: string, value: unknown) {
const store = this.cls.getStore();
if (store) {
store.set(key, value);
}
}
static get<T>(key: string): T | undefined {
const store = this.cls.getStore();
return store?.get(key);
}
// Helper methods
static get currentUserId(): number | null {
return this.get('user_id') || null;
}
static get requestId(): string | null {
return this.get('request_id') || null;
}
}