From 3c211fe01dc2c08a41ba77888265180340992c16 Mon Sep 17 00:00:00 2001 From: kouros51 Date: Sun, 3 Mar 2019 16:45:17 -0500 Subject: [PATCH 1/5] Add enter and exist for a context so we set and get values and keys for certain context --- types/cls-hooked/index.d.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/types/cls-hooked/index.d.ts b/types/cls-hooked/index.d.ts index 57e6b79c60..c81966970c 100644 --- a/types/cls-hooked/index.d.ts +++ b/types/cls-hooked/index.d.ts @@ -12,6 +12,8 @@ export interface Namespace { set(key: string, value: T): T; get(key: string): any; + enter(context: any): void; + exit(context: any): void; run(fn: (...args: any[]) => void): void; runAndReturn(fn: (...args: any[]) => T): T; runPromise(fn: (...args: any[]) => Promise): Promise; From 1c43f86f73ca178bdc96fbd98e8e8c3b6b8219d9 Mon Sep 17 00:00:00 2001 From: kouros51 Date: Sun, 3 Mar 2019 17:32:17 -0500 Subject: [PATCH 2/5] Add in the test a use case explaining the need to expose those too new functions --- types/cls-hooked/cls-hooked-tests.ts | 21 +++++++++++++++++++++ types/cls-hooked/index.d.ts | 6 +++--- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/types/cls-hooked/cls-hooked-tests.ts b/types/cls-hooked/cls-hooked-tests.ts index affaa6c54a..04c0789e71 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 application, we want to set value 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 c81966970c..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 @@ -12,14 +12,14 @@ export interface Namespace { set(key: string, value: T): T; get(key: string): any; - enter(context: any): void; - exit(context: any): void; run(fn: (...args: any[]) => void): void; runAndReturn(fn: (...args: any[]) => T): T; runPromise(fn: (...args: any[]) => Promise): Promise; 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; From 405f9855ed3194061579b15253f5a616f52310b2 Mon Sep 17 00:00:00 2001 From: kouros51 Date: Sun, 3 Mar 2019 17:39:15 -0500 Subject: [PATCH 3/5] Adding missing whitespaces --- types/cls-hooked/cls-hooked-tests.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/cls-hooked/cls-hooked-tests.ts b/types/cls-hooked/cls-hooked-tests.ts index 04c0789e71..e1552c16e2 100644 --- a/types/cls-hooked/cls-hooked-tests.ts +++ b/types/cls-hooked/cls-hooked-tests.ts @@ -29,7 +29,7 @@ function bindWithMiddleware(middlewareFn: () => void) { return session.bind(middlewareFn, context); } -bindWithMiddleware(()=>{ +bindWithMiddleware(() => { // Some middleware that doing something in the application }); From 047237d00c412b2b5b64b92938645f8ad8dfc4a0 Mon Sep 17 00:00:00 2001 From: kouros51 Date: Sun, 3 Mar 2019 17:41:08 -0500 Subject: [PATCH 4/5] Adding missing whitespaces --- types/cls-hooked/cls-hooked-tests.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/cls-hooked/cls-hooked-tests.ts b/types/cls-hooked/cls-hooked-tests.ts index e1552c16e2..9d9e14af1b 100644 --- a/types/cls-hooked/cls-hooked-tests.ts +++ b/types/cls-hooked/cls-hooked-tests.ts @@ -35,7 +35,7 @@ bindWithMiddleware(() => { // In some place in application, we want to set value to be used elsewhere appNamespace.enter(context); -appNamespace.set('requestId','someId'); +appNamespace.set('requestId', 'someId'); appNamespace.exit(context); // Retrieve that value set before without losing the context when chaining several middleware From 1c05fef87ebbaa0fba845af52e0ac5771a130a71 Mon Sep 17 00:00:00 2001 From: kouros51 Date: Sun, 3 Mar 2019 17:43:23 -0500 Subject: [PATCH 5/5] Fixing typo in comment --- types/cls-hooked/cls-hooked-tests.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/cls-hooked/cls-hooked-tests.ts b/types/cls-hooked/cls-hooked-tests.ts index 9d9e14af1b..60931e1253 100644 --- a/types/cls-hooked/cls-hooked-tests.ts +++ b/types/cls-hooked/cls-hooked-tests.ts @@ -33,7 +33,7 @@ bindWithMiddleware(() => { // Some middleware that doing something in the application }); -// In some place in application, we want to set value to be used elsewhere +// 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);