UNPKG

@villedemontreal/correlation-id

Version:

Express middleware to set a correlation in Express. The correlation id will be consistent across async calls within the handling of a request.

69 lines 2.2 kB
import * as express from 'express'; /** * CorrelationId type */ export type CorrelationId = string; /** * Informations about the correlation ID. */ export interface ICidInfo { /** * Current cid */ current: string; /** * Cid received in the request (may be undefined) */ receivedInRequest: string; /** * Cid generated (may be undefined) */ generated: string; } /** * CorrelationId service */ export interface ICorrelationIdService { /** * Creates a new correlation ID that can then be passed to the * "withId()" function. */ createNewId(): CorrelationId; /** * Executes a function inside a context where the correlation ID is defined. * * @param work the function to run within the cid context. * @param cid the correlation ID to use. * */ withId<T>(work: () => T, cid?: CorrelationId): T; /** * Executes a function inside a context where the correlation ID is defined. * This is the promisified version of the `withId` method. * @param work a callback to invoke with the submitted correlation ID * @param cid the correlation ID to install be before invoking the submitted callback * * @deprecated `#withId` is preferable instead: if the wrapped operation * is asynchronous it will still be properly scoped (correlation context) and * can safely be awaited for outside of `#withId`. */ withIdAsync<T>(work: () => Promise<T>, cid?: string): Promise<T>; /** * binds the current correlation context to the target * @param target the target to bind to * @returns either the submitted target (if it is an emitter) or a wrapped target (for a function) * @remarks you might have to bind to an emitter in order to maitain * the correlation context. */ bind<T>(target: T): T; /** * Returns the correlation ID from the current context */ getId(): CorrelationId; /** * Returns all correlation ID info */ getCidInfo(req: express.Request): ICidInfo; } export declare const correlationIdService: ICorrelationIdService; //# sourceMappingURL=correlationIdService.d.ts.map