{"version":3,"sources":["../src/types/Context.ts"],"sourcesContent":["import { Uuid } from './Uuid';\nimport { Identity } from './Identity';\nimport { Optional } from './Types';\nimport { text } from './Template';\n\nexport interface EnvContext {\n  readonly domain: string;\n  readonly name: string;\n  readonly host: string;\n  readonly port: number;\n  readonly app: string;\n\n  get(key: string, alt?: string): Optional<string>;\n}\n\nexport class DotEnvContext implements EnvContext {\n  readonly domain = process.env.DOMAIN ?? 'easy';\n  readonly name = process.env.ENVIRONMENT_NAME ?? '';\n  readonly host = process.env.HOST ?? '';\n  readonly port = Number.parseInt(process.env.PORT ?? '8080');\n  readonly app = process.env.APP ?? '';\n\n  readonly get = (key: string, alt?: string): Optional<string> => {\n    const k = text(key)\n      .map(k => k.replace(/([a-z])([A-Z])/g, '$1 $2'))\n      .snake.toString();\n    return process.env[k] ?? alt;\n  };\n}\n\nexport interface RequestContext {\n  token?: any;\n  identity?: Identity;\n  jwt: string;\n  correlationId?: Uuid;\n  lastError?: string;\n  lastErrorStack?: string;\n  get<T>(key: string): T;\n  set<T>(key: string, value: T): T;\n  create: (f: () => void) => void;\n  wrap<T>(f: () => Promise<T>): Promise<T>;\n}\n\nexport class BaseRequestContext implements RequestContext {\n  private state: any = {};\n\n  get token(): any {\n    return this.get('token');\n  }\n\n  set token(token: any) {\n    this.set('token', token);\n  }\n\n  get identity(): Identity {\n    return this.token as Identity;\n  }\n\n  get jwt(): string {\n    return this.get('jwt');\n  }\n\n  set jwt(jwt: string) {\n    this.set('jwt', jwt);\n  }\n\n  get correlationId(): Uuid {\n    return this.get('correlationId');\n  }\n\n  set correlationId(id: Uuid) {\n    this.set('correlationId', id);\n  }\n\n  get lastError(): Optional<string> {\n    return this.get('lastError');\n  }\n\n  set lastError(error: Optional<string>) {\n    this.set('lastError', error);\n  }\n\n  public get<T>(key: string): T {\n    return this.state[key] as T;\n  }\n\n  public set<T>(key: string, value: T): T {\n    return (this.state[key] = value);\n  }\n\n  public readonly create = (f: () => void): void => f();\n\n  public readonly wrap = <T>(f: () => Promise<T>): Promise<T> => f();\n}\n\n/**\n * @deprecated Renamed to BaseRequestContext\n */\nexport class BaseContext extends BaseRequestContext {}\n\nexport interface Contexts {\n  env?: EnvContext;\n  request?: RequestContext;\n  other?: any;\n}\n\nexport class Context {\n  constructor(protected state: Contexts = {}) {\n    this.state = {\n      ...{\n        env: new DotEnvContext(),\n        request: new BaseRequestContext(),\n        other: {},\n      },\n      ...this.state,\n    };\n  }\n\n  get env(): EnvContext {\n    return this.state.env as EnvContext;\n  }\n\n  set env(env: EnvContext) {\n    this.state.env = env;\n  }\n\n  get request(): RequestContext {\n    return this.state.request as RequestContext;\n  }\n\n  set request(request: RequestContext) {\n    this.state.request = request;\n  }\n\n  get other(): any {\n    return this.state.other;\n  }\n}\n\nexport const ctx = new Context();\n"],"mappings":";;;;;AAeO,IAAM,gBAAN,MAA0C;AAAA,EACtC,SAAS,QAAQ,IAAI,UAAU;AAAA,EAC/B,OAAO,QAAQ,IAAI,oBAAoB;AAAA,EACvC,OAAO,QAAQ,IAAI,QAAQ;AAAA,EAC3B,OAAO,OAAO,SAAS,QAAQ,IAAI,QAAQ,MAAM;AAAA,EACjD,MAAM,QAAQ,IAAI,OAAO;AAAA,EAEzB,MAAM,CAAC,KAAa,QAAmC;AAC9D,UAAM,IAAI,KAAK,GAAG,EACf,IAAI,CAAAA,OAAKA,GAAE,QAAQ,mBAAmB,OAAO,CAAC,EAC9C,MAAM,SAAS;AAClB,WAAO,QAAQ,IAAI,CAAC,KAAK;AAAA,EAC3B;AACF;AAeO,IAAM,qBAAN,MAAmD;AAAA,EAChD,QAAa,CAAC;AAAA,EAEtB,IAAI,QAAa;AACf,WAAO,KAAK,IAAI,OAAO;AAAA,EACzB;AAAA,EAEA,IAAI,MAAM,OAAY;AACpB,SAAK,IAAI,SAAS,KAAK;AAAA,EACzB;AAAA,EAEA,IAAI,WAAqB;AACvB,WAAO,KAAK;AAAA,EACd;AAAA,EAEA,IAAI,MAAc;AAChB,WAAO,KAAK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