@dalet-oss/express-http-context 1.2.1 → 1.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/cjs/index.js CHANGED
@@ -6,7 +6,11 @@ if (!global.expressHttpContextStorage) {
6
6
  global.expressHttpContextStorage = new async_hooks_1.AsyncLocalStorage();
7
7
  }
8
8
  /**
9
- * Express.js middleware that is responsible for initializing the context for each request.
9
+ * Express.js middleware that is responsible for initializing the context for
10
+ * each request.
11
+ *
12
+ * Declared as type 'any' because otherwise Typescript consumers can get tie
13
+ * themselves in knots.
10
14
  */
11
15
  const middleware = (_req, _res, next) => {
12
16
  global.expressHttpContextStorage.run(new Map(), () => next());
@@ -14,8 +18,9 @@ const middleware = (_req, _res, next) => {
14
18
  exports.middleware = middleware;
15
19
  /**
16
20
  * Gets a value from the context by key.
17
- * Will return undefined if the context has not yet been initialized for this request
18
- * or if a value is not found for the specified key.
21
+ *
22
+ * Will return undefined if the context has not yet been initialized for this
23
+ * request, or if a value is not found for the specified key.
19
24
  */
20
25
  function get(key) {
21
26
  const store = global.expressHttpContextStorage.getStore();
@@ -24,12 +29,20 @@ function get(key) {
24
29
  exports.get = get;
25
30
  /**
26
31
  * Adds a value to the context by key.
32
+ *
27
33
  * If the key already exists, its value will be overwritten.
28
- * No value will persist if the context has not yet been initialized.
34
+ * No value will be persisted if the context has not yet been initialized.
35
+ *
36
+ * Returns the value that was set, or undefined if the context has not yet been
37
+ * initialized for this request.
29
38
  */
30
39
  function set(key, value) {
31
40
  const store = global.expressHttpContextStorage.getStore();
32
- store?.set(key, value);
41
+ if (store) {
42
+ store.set(key, value);
43
+ return value;
44
+ }
45
+ return undefined;
33
46
  }
34
47
  exports.set = set;
35
48
  exports.default = { get, middleware: exports.middleware, set };
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;AAAA,6CAA+C;AAS/C,IAAI,CAAC,MAAM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AU,cAMtB;AAGD;;;;;GAKG;AACH,SAAgB,GAAG,CAAU,GAAW;IAEvC,MAAM,KAAK,GAAG,MAAM,CAAC,yBAAyB,CAAC,QAAQ,EAAE,CAAC;IAE1D,OAAO,KAAK,EAAE,GAAG,CAAC,GAAG,CAAC,CAAC;AACxB,CAAC;AALD,kBAKC;AAGD;;;;;;;;GAQG;AACH,SAAgB,GAAG,CAAU,GAAW,EAAE,KAAQ;IAEjD,MAAM,KAAK,GAAG,MAAM,CAAC,yBAAyB,CAAC,QAAQ,EAAE,CAAC;IAEvD,IAAI,KAAK,EAAE;QACP,KAAK,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;QACtB,OAAO,KAAK,CAAC;KAChB;IAED,OAAO,SAAS,CAAC;AACrB,CAAC;AAVD,kBAUC;AAGD,kBAAe,EAAE,GAAG,EAAE,UAAU,EAAV,kBAAU,EAAE,GAAG,EAAE,CAAA"}
package/lib/esm/index.js CHANGED
@@ -3,15 +3,20 @@ if (!global.expressHttpContextStorage) {
3
3
  global.expressHttpContextStorage = new AsyncLocalStorage();
4
4
  }
5
5
  /**
6
- * Express.js middleware that is responsible for initializing the context for each request.
6
+ * Express.js middleware that is responsible for initializing the context for
7
+ * each request.
8
+ *
9
+ * Declared as type 'any' because otherwise Typescript consumers can get tie
10
+ * themselves in knots.
7
11
  */
8
12
  export const middleware = (_req, _res, next) => {
9
13
  global.expressHttpContextStorage.run(new Map(), () => next());
10
14
  };
11
15
  /**
12
16
  * Gets a value from the context by key.
13
- * Will return undefined if the context has not yet been initialized for this request
14
- * or if a value is not found for the specified key.
17
+ *
18
+ * Will return undefined if the context has not yet been initialized for this
19
+ * request, or if a value is not found for the specified key.
15
20
  */
16
21
  export function get(key) {
17
22
  const store = global.expressHttpContextStorage.getStore();
@@ -19,12 +24,20 @@ export function get(key) {
19
24
  }
20
25
  /**
21
26
  * Adds a value to the context by key.
27
+ *
22
28
  * If the key already exists, its value will be overwritten.
23
- * No value will persist if the context has not yet been initialized.
29
+ * No value will be persisted if the context has not yet been initialized.
30
+ *
31
+ * Returns the value that was set, or undefined if the context has not yet been
32
+ * initialized for this request.
24
33
  */
25
34
  export function set(key, value) {
26
35
  const store = global.expressHttpContextStorage.getStore();
27
- store?.set(key, value);
36
+ if (store) {
37
+ store.set(key, value);
38
+ return value;
39
+ }
40
+ return undefined;
28
41
  }
29
42
  export default { get, middleware, set };
30
43
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAA;AAS/C,IAAI,CAAC,MAAM,CAAC,yBAAyB,EAAE;IACtC,MAAM,CAAC,yBAAyB,GAAG,IAAI,iBAAiB,EAAoB,CAAA;CAC5E;AAID;;GAEG;AACH,MAAM,CAAC,MAAM,UAAU,GAAmB,CACzC,IAAa,EACb,IAAc,EACd,IAAkB,EACX,EAAE;IACT,MAAM,CAAC,yBAAyB,CAAC,GAAG,CAAC,IAAI,GAAG,EAAE,EAAE,GAAG,EAAE,CAAC,IAAI,EAAE,CAAC,CAAA;AAC9D,CAAC,CAAA;AAED;;;;GAIG;AACH,MAAM,UAAU,GAAG,CAAU,GAAW;IAEvC,MAAM,KAAK,GAAG,MAAM,CAAC,yBAAyB,CAAC,QAAQ,EAAE,CAAC;IAE1D,OAAO,KAAK,EAAE,GAAG,CAAC,GAAG,CAAC,CAAA;AACvB,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,GAAG,CAAU,GAAW,EAAE,KAAQ;IAEjD,MAAM,KAAK,GAAG,MAAM,CAAC,yBAAyB,CAAC,QAAQ,EAAE,CAAC;IAE1D,KAAK,EAAE,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,CAAA;AACvB,CAAC;AAED,eAAe,EAAE,GAAG,EAAE,UAAU,EAAE,GAAG,EAAE,CAAA"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAUhD,IAAI,CAAC,MAAM,CAAC,yBAAyB,EAAE;IACtC,MAAM,CAAC,yBAAyB,GAAG,IAAI,iBAAiB,EAAoB,CAAC;CAC7E;AAGD;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,UAAU,GAAQ,CAC9B,IAAa,EACb,IAAc,EACd,IAAkB,EACX,EAAE;IACT,MAAM,CAAC,yBAAyB,CAAC,GAAG,CAAC,IAAI,GAAG,EAAE,EAAE,GAAG,EAAE,CAAC,IAAI,EAAE,CAAC,CAAC;AAC/D,CAAC,CAAA;AAGD;;;;;GAKG;AACH,MAAM,UAAU,GAAG,CAAU,GAAW;IAEvC,MAAM,KAAK,GAAG,MAAM,CAAC,yBAAyB,CAAC,QAAQ,EAAE,CAAC;IAE1D,OAAO,KAAK,EAAE,GAAG,CAAC,GAAG,CAAC,CAAC;AACxB,CAAC;AAGD;;;;;;;;GAQG;AACH,MAAM,UAAU,GAAG,CAAU,GAAW,EAAE,KAAQ;IAEjD,MAAM,KAAK,GAAG,MAAM,CAAC,yBAAyB,CAAC,QAAQ,EAAE,CAAC;IAEvD,IAAI,KAAK,EAAE;QACP,KAAK,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;QACtB,OAAO,KAAK,CAAC;KAChB;IAED,OAAO,SAAS,CAAC;AACrB,CAAC;AAGD,eAAe,EAAE,GAAG,EAAE,UAAU,EAAE,GAAG,EAAE,CAAA"}
@@ -1,29 +1,36 @@
1
1
  /// <reference types="node" />
2
- /// <reference types="qs" />
3
2
  import { AsyncLocalStorage } from 'async_hooks';
4
- import type { RequestHandler } from 'express';
5
3
  declare global {
6
4
  var expressHttpContextStorage: AsyncLocalStorage<Map<string, any>>;
7
5
  }
8
6
  /**
9
- * Express.js middleware that is responsible for initializing the context for each request.
7
+ * Express.js middleware that is responsible for initializing the context for
8
+ * each request.
9
+ *
10
+ * Declared as type 'any' because otherwise Typescript consumers can get tie
11
+ * themselves in knots.
10
12
  */
11
- export declare const middleware: RequestHandler;
13
+ export declare const middleware: any;
12
14
  /**
13
15
  * Gets a value from the context by key.
14
- * Will return undefined if the context has not yet been initialized for this request
15
- * or if a value is not found for the specified key.
16
+ *
17
+ * Will return undefined if the context has not yet been initialized for this
18
+ * request, or if a value is not found for the specified key.
16
19
  */
17
20
  export declare function get<T = any>(key: string): T | undefined;
18
21
  /**
19
22
  * Adds a value to the context by key.
23
+ *
20
24
  * If the key already exists, its value will be overwritten.
21
- * No value will persist if the context has not yet been initialized.
25
+ * No value will be persisted if the context has not yet been initialized.
26
+ *
27
+ * Returns the value that was set, or undefined if the context has not yet been
28
+ * initialized for this request.
22
29
  */
23
- export declare function set<T = any>(key: string, value: T): void;
30
+ export declare function set<T = any>(key: string, value: T): T | undefined;
24
31
  declare const _default: {
25
32
  get: typeof get;
26
- middleware: RequestHandler<import("express-serve-static-core").ParamsDictionary, any, any, import("qs").ParsedQs, Record<string, any>>;
33
+ middleware: any;
27
34
  set: typeof set;
28
35
  };
29
36
  export default _default;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dalet-oss/express-http-context",
3
- "version": "1.2.1",
3
+ "version": "1.2.2",
4
4
  "description": "Modern request-scoped storage support for Express.js based on Asynchronous Local Storage.",
5
5
  "author": {
6
6
  "name": "Dalet OSS",
package/src/index.ts CHANGED
@@ -1,5 +1,6 @@
1
- import { AsyncLocalStorage } from 'async_hooks'
2
- import type { NextFunction, RequestHandler, Request, Response } from 'express'
1
+ import { AsyncLocalStorage } from 'async_hooks';
2
+ import type { NextFunction, Request, Response } from 'express';
3
+
3
4
 
4
5
  declare global {
5
6
  // eslint-disable-next-line no-var
@@ -8,44 +9,60 @@ declare global {
8
9
 
9
10
 
10
11
  if (!global.expressHttpContextStorage) {
11
- global.expressHttpContextStorage = new AsyncLocalStorage<Map<string, any>>()
12
+ global.expressHttpContextStorage = new AsyncLocalStorage<Map<string, any>>();
12
13
  }
13
14
 
14
15
 
15
-
16
16
  /**
17
- * Express.js middleware that is responsible for initializing the context for each request.
17
+ * Express.js middleware that is responsible for initializing the context for
18
+ * each request.
19
+ *
20
+ * Declared as type 'any' because otherwise Typescript consumers can get tie
21
+ * themselves in knots.
18
22
  */
19
- export const middleware: RequestHandler = (
23
+ export const middleware: any = (
20
24
  _req: Request,
21
25
  _res: Response,
22
26
  next: NextFunction
23
27
  ): void => {
24
- global.expressHttpContextStorage.run(new Map(), () => next())
28
+ global.expressHttpContextStorage.run(new Map(), () => next());
25
29
  }
26
30
 
31
+
27
32
  /**
28
33
  * Gets a value from the context by key.
29
- * Will return undefined if the context has not yet been initialized for this request
30
- * or if a value is not found for the specified key.
34
+ *
35
+ * Will return undefined if the context has not yet been initialized for this
36
+ * request, or if a value is not found for the specified key.
31
37
  */
32
38
  export function get<T = any>(key: string): T | undefined {
33
39
 
34
40
  const store = global.expressHttpContextStorage.getStore();
35
41
 
36
- return store?.get(key)
42
+ return store?.get(key);
37
43
  }
38
44
 
45
+
39
46
  /**
40
47
  * Adds a value to the context by key.
48
+ *
41
49
  * If the key already exists, its value will be overwritten.
42
- * No value will persist if the context has not yet been initialized.
50
+ * No value will be persisted if the context has not yet been initialized.
51
+ *
52
+ * Returns the value that was set, or undefined if the context has not yet been
53
+ * initialized for this request.
43
54
  */
44
- export function set<T = any>(key: string, value: T): void {
55
+ export function set<T = any>(key: string, value: T): T | undefined {
45
56
 
46
57
  const store = global.expressHttpContextStorage.getStore();
47
58
 
48
- store?.set(key, value)
59
+ if (store) {
60
+ store.set(key, value);
61
+ return value;
62
+ }
63
+
64
+ return undefined;
49
65
  }
50
66
 
67
+
51
68
  export default { get, middleware, set }