@bootloader/context 0.0.4 → 1.0.17

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.
Files changed (3) hide show
  1. package/index.js +2 -2
  2. package/package.json +1 -2
  3. package/request.js +37 -0
package/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  const { AsyncLocalStorage } = require('async_hooks');
2
2
 
3
- //const request = require('./request');
4
- const request = require('@bootloader/utils/request');
3
+ const request = require('./request');
4
+ //const request = require('@bootloader/utils/request');
5
5
 
6
6
  const crypto = require('crypto');
7
7
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bootloader/context",
3
- "version": "0.0.4",
3
+ "version": "1.0.17",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -13,7 +13,6 @@
13
13
  "author": "",
14
14
  "license": "ISC",
15
15
  "dependencies": {
16
- "@bootloader/utils": "^1.0.15"
17
16
  },
18
17
  "bugs": {
19
18
  "url": "https://github.com/bootloader/bootjs-context/issues"
package/request.js ADDED
@@ -0,0 +1,37 @@
1
+ class Request {
2
+ constructor(req) {
3
+ this.req = req;
4
+ }
5
+ header(key) {
6
+ return this.req.header(key);
7
+ }
8
+ param(key) {
9
+ // Set default values
10
+ let traceId;
11
+
12
+ //Check in query params
13
+ if (traceId === undefined) {
14
+ traceId = this.req.query?.[key];
15
+ }
16
+
17
+ // Check in cookies
18
+ if (traceId === undefined) {
19
+ traceId = this.req.cookies?.[key];
20
+ }
21
+ // Check in headers
22
+ if (traceId === undefined) {
23
+ traceId = this.req.header(key);
24
+ }
25
+
26
+ return traceId;
27
+ }
28
+ headerOrParam(key) {
29
+ return this.header(key) || this.param(key);
30
+ }
31
+
32
+ static context(req) {
33
+ return new Request(req);
34
+ }
35
+ }
36
+ // Export a singleton instance
37
+ module.exports = Request;