diff --git a/types/cls-hooked/cls-hooked-tests.ts b/types/cls-hooked/cls-hooked-tests.ts index affaa6c54a..60931e1253 100644 --- a/types/cls-hooked/cls-hooked-tests.ts +++ b/types/cls-hooked/cls-hooked-tests.ts @@ -21,3 +21,24 @@ bindLater((x: number) => { const session2 = cls.getNamespace('my session'); session2.get('user'); + +const appNamespace = cls.createNamespace('applicationNameSpace'); +const context = appNamespace.createContext(); + +function bindWithMiddleware(middlewareFn: () => void) { + return session.bind(middlewareFn, context); +} + +bindWithMiddleware(() => { + // Some middleware that doing something in the application +}); + +// In some place in the application, we want to set a value to a given key to be used elsewhere +appNamespace.enter(context); +appNamespace.set('requestId', 'someId'); +appNamespace.exit(context); + +// Retrieve that value set before without losing the context when chaining several middleware +appNamespace.enter(context); +appNamespace.get('requestId'); +appNamespace.exit(context); diff --git a/types/cls-hooked/index.d.ts b/types/cls-hooked/index.d.ts index 57e6b79c60..4fa385c52f 100644 --- a/types/cls-hooked/index.d.ts +++ b/types/cls-hooked/index.d.ts @@ -1,4 +1,4 @@ -// Type definitions for cls-hooked 4.2 +// Type definitions for cls-hooked 4.3 // Project: https://github.com/jeff-lewis/cls-hooked // Definitions by: Leo Liang // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped @@ -18,6 +18,8 @@ export interface Namespace { bind(fn: F, context?: any): F; // tslint:disable-line: ban-types bindEmitter(emitter: EventEmitter): void; createContext(): any; + enter(context: any): void; + exit(context: any): void; } export function createNamespace(name: string): Namespace;