{"version":3,"file":"storage-CxDBSIoI.mjs","names":[],"sources":["../src/shared/storage.ts"],"sourcesContent":["import { AsyncLocalStorage } from 'node:async_hooks'\nimport type { RequestLogger } from '../types'\n\n/**\n * Create a request-scoped `AsyncLocalStorage` and a matching `useLogger` accessor.\n *\n * Every framework that needs `useLogger()` (Express, Fastify, NestJS, SvelteKit)\n * calls this once at module level to get its own isolated storage + accessor pair.\n *\n * @param contextHint - Human-readable hint appended to the error message when\n *   `useLogger()` is called outside of a request (e.g.\n *   `\"middleware context. Make sure app.use(mxllog()) is registered before your routes.\"`).\n *\n * @beta Part of `@safaricom-mxl/log/toolkit` — the public API for building custom integrations.\n */\nexport function createLoggerStorage(contextHint: string) {\n  const storage = new AsyncLocalStorage<RequestLogger>()\n\n  /**\n   * Access the request-scoped logger created by the mxllog middleware.\n   *\n   * Must be called inside a request that is handled by the mxllog middleware.\n   * Throws if called outside of a request context.\n   *\n   * @example\n   * ```ts\n   * import { useLogger } from '@safaricom-mxl/log/express' // or /fastify, /nestjs, /sveltekit, /elysia\n   *\n   * function myService() {\n   *   const log = useLogger()\n   *   log.set({ users: { count: 42 } })\n   * }\n   * ```\n   */\n  function useLogger<T extends object = Record<string, unknown>>(): RequestLogger<T> {\n    const logger = storage.getStore()\n    if (!logger) {\n      throw new Error(\n        `[mxllog] useLogger() was called outside of an mxllog ${contextHint}`,\n      )\n    }\n    return logger as RequestLogger<T>\n  }\n\n  return { storage, useLogger }\n}\n"],"mappings":";;;;;;;;;;;;;;AAeA,SAAgB,oBAAoB,aAAqB;CACvD,MAAM,UAAU,IAAI,mBAAkC;;;;;;;;;;;;;;;;;CAkBtD,SAAS,YAA0E;EACjF,MAAM,SAAS,QAAQ,UAAU;AACjC,MAAI,CAAC,OACH,OAAM,IAAI,MACR,wDAAwD,cACzD;AAEH,SAAO;;AAGT,QAAO;EAAE;EAAS;EAAW"}