import { Injectable } from '@nestjs/common'; import { AsyncLocalStorage } from 'node:async_hooks'; export type RequestLogContext = { brokerageAccountId?: string; clientRequestId?: string; requestId: string; userId?: string; }; @Injectable() export class RequestContextService { private readonly storage = new AsyncLocalStorage(); run(context: RequestLogContext, callback: () => T): T { return this.storage.run(context, callback); } get(): RequestLogContext | undefined { return this.storage.getStore(); } set(context: Partial): void { const current = this.storage.getStore(); if (current) Object.assign(current, context); } }