@nest-boot/request-context 7.7.6 → 8.0.0-beta.1
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/dist/create-request-context.decorator.d.ts +1 -57
- package/dist/create-request-context.decorator.js +3 -59
- package/dist/create-request-context.decorator.js.map +1 -1
- package/dist/index.d.ts +7 -7
- package/dist/index.js +7 -23
- package/dist/index.js.map +1 -1
- package/dist/repl.d.ts +0 -41
- package/dist/repl.js +23 -100
- package/dist/repl.js.map +1 -1
- package/dist/request-context.constants.d.ts +0 -22
- package/dist/request-context.constants.js +2 -27
- package/dist/request-context.constants.js.map +1 -1
- package/dist/request-context.d.ts +0 -296
- package/dist/request-context.interceptor.d.ts +0 -33
- package/dist/request-context.interceptor.js +11 -49
- package/dist/request-context.interceptor.js.map +1 -1
- package/dist/request-context.js +12 -265
- package/dist/request-context.js.map +1 -1
- package/dist/request-context.middleware.d.ts +0 -27
- package/dist/request-context.middleware.js +11 -41
- package/dist/request-context.middleware.js.map +1 -1
- package/dist/request-context.module.d.ts +1 -42
- package/dist/request-context.module.js +18 -60
- package/dist/request-context.module.js.map +1 -1
- package/package.json +33 -21
- package/dist/request-context.interceptor.spec.d.ts +0 -1
- package/dist/request-context.interceptor.spec.js +0 -247
- package/dist/request-context.interceptor.spec.js.map +0 -1
- package/dist/tsconfig.build.tsbuildinfo +0 -1
package/dist/request-context.js
CHANGED
|
@@ -1,111 +1,25 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
* needs to be accessed across different parts of the application.
|
|
13
|
-
*
|
|
14
|
-
* @example Basic usage
|
|
15
|
-
* ```typescript
|
|
16
|
-
* import { RequestContext } from '@nest-boot/request-context';
|
|
17
|
-
*
|
|
18
|
-
* // Get the current request ID
|
|
19
|
-
* const requestId = RequestContext.id;
|
|
20
|
-
*
|
|
21
|
-
* // Store a value in the context
|
|
22
|
-
* RequestContext.set('userId', 123);
|
|
23
|
-
*
|
|
24
|
-
* // Retrieve a value from the context
|
|
25
|
-
* const userId = RequestContext.get<number>('userId');
|
|
26
|
-
* ```
|
|
27
|
-
*
|
|
28
|
-
* @example Running code in a new context
|
|
29
|
-
* ```typescript
|
|
30
|
-
* await RequestContext.run(
|
|
31
|
-
* new RequestContext({ type: 'job' }),
|
|
32
|
-
* async (ctx) => {
|
|
33
|
-
* ctx.set('jobId', 'abc123');
|
|
34
|
-
* await processJob();
|
|
35
|
-
* }
|
|
36
|
-
* );
|
|
37
|
-
* ```
|
|
38
|
-
*
|
|
39
|
-
* @example Creating a child context
|
|
40
|
-
* ```typescript
|
|
41
|
-
* await RequestContext.child(async (childCtx) => {
|
|
42
|
-
* // Child context inherits values from parent
|
|
43
|
-
* // but can have its own values that don't affect parent
|
|
44
|
-
* childCtx.set('tempValue', 'only in child');
|
|
45
|
-
* });
|
|
46
|
-
* ```
|
|
47
|
-
*/
|
|
48
|
-
class RequestContext {
|
|
49
|
-
/** Creates a new RequestContext instance.
|
|
50
|
-
* @param options - Options for creating the request context (id, type, parent)
|
|
51
|
-
*/
|
|
1
|
+
import { AsyncLocalStorage } from "async_hooks";
|
|
2
|
+
import { randomUUID } from "crypto";
|
|
3
|
+
export class RequestContext {
|
|
4
|
+
id;
|
|
5
|
+
type;
|
|
6
|
+
parent;
|
|
7
|
+
container = new Map();
|
|
8
|
+
static storage = new AsyncLocalStorage();
|
|
9
|
+
static middlewares = new Map();
|
|
10
|
+
static middlewareDependencies = new Map();
|
|
11
|
+
static middlewaresStack = [];
|
|
52
12
|
constructor(options) {
|
|
53
|
-
|
|
54
|
-
this.container = new Map();
|
|
55
|
-
this.id = options.id ?? (0, crypto_1.randomUUID)();
|
|
13
|
+
this.id = options.id ?? randomUUID();
|
|
56
14
|
this.type = options.type;
|
|
57
15
|
this.parent = options.parent;
|
|
58
16
|
}
|
|
59
|
-
/**
|
|
60
|
-
* Gets a value from the context by its token.
|
|
61
|
-
* If not found in this context, looks up the parent context.
|
|
62
|
-
*
|
|
63
|
-
* @typeParam T - The expected type of the value
|
|
64
|
-
* @param token - The key to look up (string, symbol, function, or class)
|
|
65
|
-
* @returns The value if found, otherwise undefined
|
|
66
|
-
*
|
|
67
|
-
* @example
|
|
68
|
-
* ```typescript
|
|
69
|
-
* const ctx = RequestContext.current();
|
|
70
|
-
* const user = ctx.get<User>('currentUser');
|
|
71
|
-
* const service = ctx.get(MyService);
|
|
72
|
-
* ```
|
|
73
|
-
*/
|
|
74
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
|
|
75
17
|
get(token) {
|
|
76
18
|
return this.container.get(token) ?? this.parent?.get(token);
|
|
77
19
|
}
|
|
78
|
-
/**
|
|
79
|
-
* Sets a value in the context.
|
|
80
|
-
*
|
|
81
|
-
* @typeParam T - The type of the value
|
|
82
|
-
* @param typeOrToken - The key to store the value under
|
|
83
|
-
* @param value - The value to store
|
|
84
|
-
*
|
|
85
|
-
* @example
|
|
86
|
-
* ```typescript
|
|
87
|
-
* const ctx = RequestContext.current();
|
|
88
|
-
* ctx.set('userId', 123);
|
|
89
|
-
* ctx.set(UserService, userServiceInstance);
|
|
90
|
-
* ```
|
|
91
|
-
*/
|
|
92
20
|
set(typeOrToken, value) {
|
|
93
21
|
this.container.set(typeOrToken, value);
|
|
94
22
|
}
|
|
95
|
-
/**
|
|
96
|
-
* Gets a value from the context, or sets it if not present.
|
|
97
|
-
*
|
|
98
|
-
* @typeParam T - The type of the value
|
|
99
|
-
* @param typeOrToken - The key to look up or store under
|
|
100
|
-
* @param value - The value to set if not already present
|
|
101
|
-
* @returns The existing value or the newly set value
|
|
102
|
-
*
|
|
103
|
-
* @example
|
|
104
|
-
* ```typescript
|
|
105
|
-
* const ctx = RequestContext.current();
|
|
106
|
-
* const cache = ctx.getOrSet('cache', new Map());
|
|
107
|
-
* ```
|
|
108
|
-
*/
|
|
109
23
|
getOrSet(typeOrToken, value) {
|
|
110
24
|
const existing = this.get(typeOrToken);
|
|
111
25
|
if (typeof existing !== "undefined") {
|
|
@@ -114,90 +28,23 @@ class RequestContext {
|
|
|
114
28
|
this.set(typeOrToken, value);
|
|
115
29
|
return value;
|
|
116
30
|
}
|
|
117
|
-
/**
|
|
118
|
-
* Gets a value from the current context by its key.
|
|
119
|
-
* Static method that accesses the current context automatically.
|
|
120
|
-
*
|
|
121
|
-
* @typeParam T - The expected type of the value
|
|
122
|
-
* @param key - The key to look up
|
|
123
|
-
* @returns The value if found, otherwise undefined
|
|
124
|
-
* @throws Error if no request context is active
|
|
125
|
-
*
|
|
126
|
-
* @example
|
|
127
|
-
* ```typescript
|
|
128
|
-
* const userId = RequestContext.get<number>('userId');
|
|
129
|
-
* ```
|
|
130
|
-
*/
|
|
131
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
|
|
132
31
|
static get(key) {
|
|
133
32
|
const ctx = this.current();
|
|
134
33
|
return ctx.get(key);
|
|
135
34
|
}
|
|
136
|
-
/**
|
|
137
|
-
* Sets a value in the current context.
|
|
138
|
-
* Static method that accesses the current context automatically.
|
|
139
|
-
*
|
|
140
|
-
* @typeParam T - The type of the value
|
|
141
|
-
* @param key - The key to store the value under
|
|
142
|
-
* @param value - The value to store
|
|
143
|
-
* @throws Error if no request context is active
|
|
144
|
-
*
|
|
145
|
-
* @example
|
|
146
|
-
* ```typescript
|
|
147
|
-
* RequestContext.set('userId', 123);
|
|
148
|
-
* ```
|
|
149
|
-
*/
|
|
150
35
|
static set(key, value) {
|
|
151
36
|
const ctx = this.current();
|
|
152
37
|
if (typeof key !== "undefined") {
|
|
153
38
|
ctx.set(key, value);
|
|
154
39
|
}
|
|
155
40
|
}
|
|
156
|
-
/**
|
|
157
|
-
* Gets a value from the current context, or sets it if not present.
|
|
158
|
-
* Static method that accesses the current context automatically.
|
|
159
|
-
*
|
|
160
|
-
* @typeParam T - The type of the value
|
|
161
|
-
* @param key - The key to look up or store under
|
|
162
|
-
* @param value - The value to set if not already present
|
|
163
|
-
* @returns The existing value or the newly set value
|
|
164
|
-
* @throws Error if no request context is active
|
|
165
|
-
*
|
|
166
|
-
* @example
|
|
167
|
-
* ```typescript
|
|
168
|
-
* const cache = RequestContext.getOrSet('cache', new Map());
|
|
169
|
-
* ```
|
|
170
|
-
*/
|
|
171
41
|
static getOrSet(key, value) {
|
|
172
42
|
const ctx = this.current();
|
|
173
43
|
return ctx.getOrSet(key, value);
|
|
174
44
|
}
|
|
175
|
-
/**
|
|
176
|
-
* Gets the ID of the current request context.
|
|
177
|
-
*
|
|
178
|
-
* @returns The unique identifier of the current context
|
|
179
|
-
* @throws Error if no request context is active
|
|
180
|
-
*
|
|
181
|
-
* @example
|
|
182
|
-
* ```typescript
|
|
183
|
-
* console.log(`Processing request ${RequestContext.id}`);
|
|
184
|
-
* ```
|
|
185
|
-
*/
|
|
186
45
|
static get id() {
|
|
187
46
|
return this.current().id;
|
|
188
47
|
}
|
|
189
|
-
/**
|
|
190
|
-
* Gets the current request context.
|
|
191
|
-
*
|
|
192
|
-
* @returns The current RequestContext instance
|
|
193
|
-
* @throws Error if no request context is active
|
|
194
|
-
*
|
|
195
|
-
* @example
|
|
196
|
-
* ```typescript
|
|
197
|
-
* const ctx = RequestContext.current();
|
|
198
|
-
* console.log(ctx.type); // 'http'
|
|
199
|
-
* ```
|
|
200
|
-
*/
|
|
201
48
|
static current() {
|
|
202
49
|
const ctx = this.storage.getStore();
|
|
203
50
|
if (typeof ctx === "undefined") {
|
|
@@ -205,41 +52,9 @@ class RequestContext {
|
|
|
205
52
|
}
|
|
206
53
|
return ctx;
|
|
207
54
|
}
|
|
208
|
-
/**
|
|
209
|
-
* Checks if a request context is currently active.
|
|
210
|
-
*
|
|
211
|
-
* @returns true if a context is active, false otherwise
|
|
212
|
-
*
|
|
213
|
-
* @example
|
|
214
|
-
* ```typescript
|
|
215
|
-
* if (RequestContext.isActive()) {
|
|
216
|
-
* const userId = RequestContext.get('userId');
|
|
217
|
-
* }
|
|
218
|
-
* ```
|
|
219
|
-
*/
|
|
220
55
|
static isActive() {
|
|
221
56
|
return !!this.storage.getStore();
|
|
222
57
|
}
|
|
223
|
-
/**
|
|
224
|
-
* Runs a callback within a request context.
|
|
225
|
-
* All registered middlewares are executed before the callback.
|
|
226
|
-
*
|
|
227
|
-
* @typeParam T - The return type of the callback
|
|
228
|
-
* @param ctx - The request context to run within
|
|
229
|
-
* @param callback - The function to execute within the context
|
|
230
|
-
* @returns A promise resolving to the callback's return value
|
|
231
|
-
*
|
|
232
|
-
* @example
|
|
233
|
-
* ```typescript
|
|
234
|
-
* const result = await RequestContext.run(
|
|
235
|
-
* new RequestContext({ type: 'job' }),
|
|
236
|
-
* async (ctx) => {
|
|
237
|
-
* ctx.set('jobId', 'abc123');
|
|
238
|
-
* return await processJob();
|
|
239
|
-
* }
|
|
240
|
-
* );
|
|
241
|
-
* ```
|
|
242
|
-
*/
|
|
243
58
|
static async run(ctx, callback) {
|
|
244
59
|
let i = 0;
|
|
245
60
|
const next = async () => {
|
|
@@ -250,33 +65,6 @@ class RequestContext {
|
|
|
250
65
|
};
|
|
251
66
|
return await this.storage.run(ctx, next);
|
|
252
67
|
}
|
|
253
|
-
/**
|
|
254
|
-
* Creates and runs a child context that inherits from the current context.
|
|
255
|
-
* Child contexts can read values from parent contexts but modifications
|
|
256
|
-
* are isolated to the child.
|
|
257
|
-
*
|
|
258
|
-
* @typeParam T - The return type of the callback
|
|
259
|
-
* @param callback - The function to execute within the child context
|
|
260
|
-
* @returns A promise resolving to the callback's return value
|
|
261
|
-
* @throws Error if no request context is active
|
|
262
|
-
*
|
|
263
|
-
* @example
|
|
264
|
-
* ```typescript
|
|
265
|
-
* // In parent context
|
|
266
|
-
* RequestContext.set('userId', 123);
|
|
267
|
-
*
|
|
268
|
-
* await RequestContext.child(async (childCtx) => {
|
|
269
|
-
* // Can read parent values
|
|
270
|
-
* const userId = childCtx.get('userId'); // 123
|
|
271
|
-
*
|
|
272
|
-
* // Child-only values don't affect parent
|
|
273
|
-
* childCtx.set('tempData', 'child only');
|
|
274
|
-
* });
|
|
275
|
-
*
|
|
276
|
-
* // Parent context unchanged
|
|
277
|
-
* RequestContext.get('tempData'); // undefined
|
|
278
|
-
* ```
|
|
279
|
-
*/
|
|
280
68
|
static async child(callback) {
|
|
281
69
|
const parent = this.storage.getStore();
|
|
282
70
|
if (typeof parent === "undefined") {
|
|
@@ -289,42 +77,11 @@ class RequestContext {
|
|
|
289
77
|
});
|
|
290
78
|
return await this.storage.run(ctx, () => callback(ctx));
|
|
291
79
|
}
|
|
292
|
-
/**
|
|
293
|
-
* Registers a middleware to be executed when running a request context.
|
|
294
|
-
* Middlewares are executed in dependency order.
|
|
295
|
-
*
|
|
296
|
-
* @param name - Unique name for the middleware
|
|
297
|
-
* @param middleware - The middleware function to register
|
|
298
|
-
* @param dependencies - Names of middlewares that must run before this one
|
|
299
|
-
*
|
|
300
|
-
* @example
|
|
301
|
-
* ```typescript
|
|
302
|
-
* RequestContext.registerMiddleware(
|
|
303
|
-
* 'auth',
|
|
304
|
-
* async (ctx, next) => {
|
|
305
|
-
* ctx.set('user', await loadUser());
|
|
306
|
-
* return next();
|
|
307
|
-
* }
|
|
308
|
-
* );
|
|
309
|
-
*
|
|
310
|
-
* // Middleware with dependencies
|
|
311
|
-
* RequestContext.registerMiddleware(
|
|
312
|
-
* 'permissions',
|
|
313
|
-
* async (ctx, next) => {
|
|
314
|
-
* const user = ctx.get('user');
|
|
315
|
-
* ctx.set('permissions', await loadPermissions(user));
|
|
316
|
-
* return next();
|
|
317
|
-
* },
|
|
318
|
-
* ['auth'] // Runs after 'auth' middleware
|
|
319
|
-
* );
|
|
320
|
-
* ```
|
|
321
|
-
*/
|
|
322
80
|
static registerMiddleware(name, middleware, dependencies) {
|
|
323
81
|
this.middlewares.set(name, middleware);
|
|
324
82
|
this.middlewareDependencies.set(name, dependencies ?? []);
|
|
325
83
|
this.generateMiddlewaresStack();
|
|
326
84
|
}
|
|
327
|
-
/** Resolves middleware dependencies via topological sort. @internal */
|
|
328
85
|
static resolveDependencies(name, resolved, seen) {
|
|
329
86
|
if (seen.has(name)) {
|
|
330
87
|
throw new Error(`Circular dependency detected: ${name}`);
|
|
@@ -338,7 +95,6 @@ class RequestContext {
|
|
|
338
95
|
}
|
|
339
96
|
resolved.add(name);
|
|
340
97
|
}
|
|
341
|
-
/** Rebuilds the middleware execution stack after registration changes. @internal */
|
|
342
98
|
static generateMiddlewaresStack() {
|
|
343
99
|
const resolved = new Set();
|
|
344
100
|
const seen = new Set();
|
|
@@ -352,13 +108,4 @@ class RequestContext {
|
|
|
352
108
|
.filter((middleware) => typeof middleware !== "undefined");
|
|
353
109
|
}
|
|
354
110
|
}
|
|
355
|
-
exports.RequestContext = RequestContext;
|
|
356
|
-
/** Async local storage backing the request context. @internal */
|
|
357
|
-
RequestContext.storage = new async_hooks_1.AsyncLocalStorage();
|
|
358
|
-
/** Registered middleware map keyed by name. @internal */
|
|
359
|
-
RequestContext.middlewares = new Map();
|
|
360
|
-
/** Dependency graph for middleware ordering. @internal */
|
|
361
|
-
RequestContext.middlewareDependencies = new Map();
|
|
362
|
-
/** Topologically-sorted middleware execution stack. @internal */
|
|
363
|
-
RequestContext.middlewaresStack = [];
|
|
364
111
|
//# sourceMappingURL=request-context.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"request-context.js","sourceRoot":"","sources":["../src/request-context.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"request-context.js","sourceRoot":"","sources":["../src/request-context.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAChD,OAAO,EAAE,UAAU,EAAE,MAAM,QAAQ,CAAC;AAuFpC,MAAM,OAAO,cAAc;IAKhB,EAAE,CAAS;IAUX,IAAI,CAAS;IAMb,MAAM,CAAkB;IAGhB,SAAS,GAAG,IAAI,GAAG,EAAE,C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AC;YACvB,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC;gBACvB,IAAI,CAAC,mBAAmB,CAAC,GAAG,EAAE,QAAQ,EAAE,IAAI,CAAC,CAAC;YAChD,CAAC;QACH,CAAC;QAED,QAAQ,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;IACrB,CAAC;IAGO,MAAM,CAAC,wBAAwB;QACrC,MAAM,QAAQ,GAAG,IAAI,GAAG,EAAU,CAAC;QACnC,MAAM,IAAI,GAAG,IAAI,GAAG,EAAU,CAAC;QAE/B,KAAK,MAAM,IAAI,IAAI,IAAI,CAAC,WAAW,CAAC,IAAI,EAAE,EAAE,CAAC;YAC3C,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC;gBACxB,IAAI,CAAC,mBAAmB,CAAC,IAAI,EAAE,QAAQ,EAAE,IAAI,CAAC,CAAC;YACjD,CAAC;QACH,CAAC;QAED,IAAI,CAAC,gBAAgB,GAAG,KAAK,CAAC,IAAI,CAAC,QAAQ,CAAC;aACzC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;aACzC,MAAM,CAAC,CAAC,UAAU,EAAE,EAAE,CAAC,OAAO,UAAU,KAAK,WAAW,CAAC,CAAC;IAC/D,CAAC"}
|
|
@@ -1,32 +1,5 @@
|
|
|
1
1
|
import { type NestMiddleware } from "@nestjs/common";
|
|
2
2
|
import { type NextFunction, type Request, type Response } from "express";
|
|
3
|
-
/**
|
|
4
|
-
* Express middleware that creates and manages request context for HTTP requests.
|
|
5
|
-
*
|
|
6
|
-
* This middleware:
|
|
7
|
-
* - Creates a new RequestContext for each incoming HTTP request
|
|
8
|
-
* - Uses the `x-request-id` header as the context ID if provided
|
|
9
|
-
* - Stores the Express request and response objects in the context
|
|
10
|
-
* - Maintains the context throughout the request lifecycle
|
|
11
|
-
*
|
|
12
|
-
* The middleware is automatically applied by RequestContextModule to all routes.
|
|
13
|
-
*
|
|
14
|
-
* @example Accessing request/response from context
|
|
15
|
-
* ```typescript
|
|
16
|
-
* import { RequestContext, REQUEST, RESPONSE } from '@nest-boot/request-context';
|
|
17
|
-
* import { Request, Response } from 'express';
|
|
18
|
-
*
|
|
19
|
-
* const req = RequestContext.get<Request>(REQUEST);
|
|
20
|
-
* const res = RequestContext.get<Response>(RESPONSE);
|
|
21
|
-
* ```
|
|
22
|
-
*/
|
|
23
3
|
export declare class RequestContextMiddleware implements NestMiddleware {
|
|
24
|
-
/**
|
|
25
|
-
* Processes an incoming HTTP request and establishes request context.
|
|
26
|
-
*
|
|
27
|
-
* @param req - The Express request object
|
|
28
|
-
* @param res - The Express response object
|
|
29
|
-
* @param next - The next middleware function
|
|
30
|
-
*/
|
|
31
4
|
use(req: Request, res: Response, next: NextFunction): Promise<void>;
|
|
32
5
|
}
|
|
@@ -1,55 +1,25 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
2
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
3
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
4
|
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
5
|
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
6
|
};
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
const request_context_1 = require("./request-context");
|
|
12
|
-
const request_context_constants_1 = require("./request-context.constants");
|
|
13
|
-
/**
|
|
14
|
-
* Express middleware that creates and manages request context for HTTP requests.
|
|
15
|
-
*
|
|
16
|
-
* This middleware:
|
|
17
|
-
* - Creates a new RequestContext for each incoming HTTP request
|
|
18
|
-
* - Uses the `x-request-id` header as the context ID if provided
|
|
19
|
-
* - Stores the Express request and response objects in the context
|
|
20
|
-
* - Maintains the context throughout the request lifecycle
|
|
21
|
-
*
|
|
22
|
-
* The middleware is automatically applied by RequestContextModule to all routes.
|
|
23
|
-
*
|
|
24
|
-
* @example Accessing request/response from context
|
|
25
|
-
* ```typescript
|
|
26
|
-
* import { RequestContext, REQUEST, RESPONSE } from '@nest-boot/request-context';
|
|
27
|
-
* import { Request, Response } from 'express';
|
|
28
|
-
*
|
|
29
|
-
* const req = RequestContext.get<Request>(REQUEST);
|
|
30
|
-
* const res = RequestContext.get<Response>(RESPONSE);
|
|
31
|
-
* ```
|
|
32
|
-
*/
|
|
7
|
+
import { Injectable } from "@nestjs/common";
|
|
8
|
+
import { REQUEST as CTX_REQUEST_TOKEN, RESPONSE as CTX_RESPONSE_TOKEN, } from "./request-context.constants.js";
|
|
9
|
+
import { RequestContext } from "./request-context.js";
|
|
33
10
|
let RequestContextMiddleware = class RequestContextMiddleware {
|
|
34
|
-
/**
|
|
35
|
-
* Processes an incoming HTTP request and establishes request context.
|
|
36
|
-
*
|
|
37
|
-
* @param req - The Express request object
|
|
38
|
-
* @param res - The Express response object
|
|
39
|
-
* @param next - The next middleware function
|
|
40
|
-
*/
|
|
41
11
|
async use(req, res, next) {
|
|
42
|
-
if (
|
|
12
|
+
if (RequestContext.isActive()) {
|
|
43
13
|
next();
|
|
44
14
|
return;
|
|
45
15
|
}
|
|
46
|
-
const ctx = new
|
|
16
|
+
const ctx = new RequestContext({
|
|
47
17
|
id: req.get("x-request-id"),
|
|
48
18
|
type: "http",
|
|
49
19
|
});
|
|
50
|
-
ctx.set(
|
|
51
|
-
ctx.set(
|
|
52
|
-
await
|
|
20
|
+
ctx.set(CTX_REQUEST_TOKEN, req);
|
|
21
|
+
ctx.set(CTX_RESPONSE_TOKEN, res);
|
|
22
|
+
await RequestContext.run(ctx, () => {
|
|
53
23
|
return new Promise((resolve) => {
|
|
54
24
|
const onResponseComplete = () => {
|
|
55
25
|
res.removeListener("close", onResponseComplete);
|
|
@@ -65,8 +35,8 @@ let RequestContextMiddleware = class RequestContextMiddleware {
|
|
|
65
35
|
});
|
|
66
36
|
}
|
|
67
37
|
};
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
(0, common_1.Injectable)()
|
|
38
|
+
RequestContextMiddleware = __decorate([
|
|
39
|
+
Injectable()
|
|
71
40
|
], RequestContextMiddleware);
|
|
41
|
+
export { RequestContextMiddleware };
|
|
72
42
|
//# sourceMappingURL=request-context.middleware.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"request-context.middleware.js","sourceRoot":"","sources":["../src/request-context.middleware.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"request-context.middleware.js","sourceRoot":"","sources":["../src/request-context.middleware.ts"],"names":[],"mappings":";;;;;;AAAA,OAAO,EAAE,UAAU,EAAuB,MAAM,gBAAgB,CAAC;AAGjE,OAAO,EACL,OAAO,IAAI,iBAAiB,EAC5B,QAAQ,IAAI,kBAAkB,GAC/B,MAAM,gCAAgC,CAAC;AACxC,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAuB/C,IAAM,wBAAwB,GAA9B,MAAM,wBAAwB;IAQnC,KAAK,CAAC,GAAG,CAAC,GAAY,EAAE,GAAa,EAAE,IAAkB;QACvD,IAAI,cAAc,CAAC,QAAQ,EAAE,EAAE,CAAC;YAC9B,IAAI,EAAE,CAAC;YACP,OAAO;QACT,CAAC;QAED,MAAM,GAAG,GAAG,IAAI,cAAc,CAAC;YAC7B,EAAE,EAAE,GAAG,CAAC,GAAG,CAAC,cAAc,CAAC;YAC3B,IAAI,EAAE,MAAM;SACb,CAAC,CAAC;QAEH,GAAG,CAAC,GAAG,CAAU,iBAAiB,EAAE,GAAG,CAAC,CAAC;QACzC,GAAG,CAAC,GAAG,CAAW,kBAAkB,EAAE,GAAG,CAAC,CAAC;QAE3C,MAAM,cAAc,CAAC,GAAG,CAAC,GAAG,EAAE,GAAG,EAAE;YACjC,OAAO,IAAI,OAAO,CAAO,CAAC,OAAO,EAAE,EAAE;gBACnC,MAAM,kBAAkB,GAAG,GAAG,EAAE;oBAC9B,GAAG,CAAC,cAAc,CAAC,OAAO,EAAE,kBAAkB,CAAC,CAAC;oBAChD,GAAG,CAAC,cAAc,CAAC,QAAQ,EAAE,kBAAkB,CAAC,CAAC;oBACjD,GAAG,CAAC,cAAc,CAAC,OAAO,EAAE,kBAAkB,CAAC,CAAC;oBAChD,OAAO,EAAE,CAAC;gBACZ,CAAC,CAAC;gBAEF,GAAG,CAAC,EAAE,CAAC,QAAQ,EAAE,kBAAkB,CAAC,CAAC;gBACrC,GAAG,CAAC,EAAE,CAAC,OAAO,EAAE,kBAAkB,CAAC,CAAC;gBACpC,GAAG,CAAC,EAAE,CAAC,OAAO,EAAE,kBAAkB,CAAC,CAAC;gBAEpC,IAAI,EAAE,CAAC;YACT,CAAC,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;IACL,CAAC;CACF,CAAA;AAvCY,wBAAwB;IADpC,UAAU,EAAE;GACA,wBAAwB,CAuCpC"}
|
|
@@ -1,48 +1,7 @@
|
|
|
1
1
|
import { MiddlewareManager } from "@nest-boot/middleware";
|
|
2
|
-
import { RequestContextMiddleware } from "./request-context.middleware";
|
|
3
|
-
/**
|
|
4
|
-
* NestJS module that provides request context functionality.
|
|
5
|
-
*
|
|
6
|
-
* This module automatically sets up request context for HTTP requests
|
|
7
|
-
* using both middleware (for Express) and interceptor (for GraphQL).
|
|
8
|
-
* It stores the request and response objects in the context and makes
|
|
9
|
-
* them available throughout the request lifecycle.
|
|
10
|
-
*
|
|
11
|
-
* The module is global, so it only needs to be imported once in the root module.
|
|
12
|
-
*
|
|
13
|
-
* @example
|
|
14
|
-
* ```typescript
|
|
15
|
-
* import { Module } from '@nestjs/common';
|
|
16
|
-
* import { RequestContextModule } from '@nest-boot/request-context';
|
|
17
|
-
*
|
|
18
|
-
* @Module({
|
|
19
|
-
* imports: [RequestContextModule],
|
|
20
|
-
* })
|
|
21
|
-
* export class AppModule {}
|
|
22
|
-
* ```
|
|
23
|
-
*
|
|
24
|
-
* @example Using in a service
|
|
25
|
-
* ```typescript
|
|
26
|
-
* import { Injectable } from '@nestjs/common';
|
|
27
|
-
* import { RequestContext, REQUEST } from '@nest-boot/request-context';
|
|
28
|
-
* import { Request } from 'express';
|
|
29
|
-
*
|
|
30
|
-
* @Injectable()
|
|
31
|
-
* export class MyService {
|
|
32
|
-
* getCurrentUser() {
|
|
33
|
-
* const req = RequestContext.get<Request>(REQUEST);
|
|
34
|
-
* return req?.user;
|
|
35
|
-
* }
|
|
36
|
-
* }
|
|
37
|
-
* ```
|
|
38
|
-
*/
|
|
2
|
+
import { RequestContextMiddleware } from "./request-context.middleware.js";
|
|
39
3
|
export declare class RequestContextModule {
|
|
40
4
|
private readonly middlewareManager;
|
|
41
5
|
private readonly requestContextMiddleware;
|
|
42
|
-
/**
|
|
43
|
-
* Creates a new RequestContextModule instance.
|
|
44
|
-
* @param middlewareManager - Middleware manager for registering the context middleware
|
|
45
|
-
* @param requestContextMiddleware - The request context middleware instance
|
|
46
|
-
*/
|
|
47
6
|
constructor(middlewareManager: MiddlewareManager, requestContextMiddleware: RequestContextMiddleware);
|
|
48
7
|
}
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
2
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
3
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
@@ -8,55 +7,14 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
|
|
|
8
7
|
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
8
|
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
9
|
};
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
const request_context_interceptor_1 = require("./request-context.interceptor");
|
|
17
|
-
const request_context_middleware_1 = require("./request-context.middleware");
|
|
18
|
-
/**
|
|
19
|
-
* NestJS module that provides request context functionality.
|
|
20
|
-
*
|
|
21
|
-
* This module automatically sets up request context for HTTP requests
|
|
22
|
-
* using both middleware (for Express) and interceptor (for GraphQL).
|
|
23
|
-
* It stores the request and response objects in the context and makes
|
|
24
|
-
* them available throughout the request lifecycle.
|
|
25
|
-
*
|
|
26
|
-
* The module is global, so it only needs to be imported once in the root module.
|
|
27
|
-
*
|
|
28
|
-
* @example
|
|
29
|
-
* ```typescript
|
|
30
|
-
* import { Module } from '@nestjs/common';
|
|
31
|
-
* import { RequestContextModule } from '@nest-boot/request-context';
|
|
32
|
-
*
|
|
33
|
-
* @Module({
|
|
34
|
-
* imports: [RequestContextModule],
|
|
35
|
-
* })
|
|
36
|
-
* export class AppModule {}
|
|
37
|
-
* ```
|
|
38
|
-
*
|
|
39
|
-
* @example Using in a service
|
|
40
|
-
* ```typescript
|
|
41
|
-
* import { Injectable } from '@nestjs/common';
|
|
42
|
-
* import { RequestContext, REQUEST } from '@nest-boot/request-context';
|
|
43
|
-
* import { Request } from 'express';
|
|
44
|
-
*
|
|
45
|
-
* @Injectable()
|
|
46
|
-
* export class MyService {
|
|
47
|
-
* getCurrentUser() {
|
|
48
|
-
* const req = RequestContext.get<Request>(REQUEST);
|
|
49
|
-
* return req?.user;
|
|
50
|
-
* }
|
|
51
|
-
* }
|
|
52
|
-
* ```
|
|
53
|
-
*/
|
|
10
|
+
import { MiddlewareManager, MiddlewareModule } from "@nest-boot/middleware";
|
|
11
|
+
import { Global, Module } from "@nestjs/common";
|
|
12
|
+
import { APP_INTERCEPTOR } from "@nestjs/core";
|
|
13
|
+
import { RequestContextInterceptor } from "./request-context.interceptor.js";
|
|
14
|
+
import { RequestContextMiddleware } from "./request-context.middleware.js";
|
|
54
15
|
let RequestContextModule = class RequestContextModule {
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
* @param middlewareManager - Middleware manager for registering the context middleware
|
|
58
|
-
* @param requestContextMiddleware - The request context middleware instance
|
|
59
|
-
*/
|
|
16
|
+
middlewareManager;
|
|
17
|
+
requestContextMiddleware;
|
|
60
18
|
constructor(middlewareManager, requestContextMiddleware) {
|
|
61
19
|
this.middlewareManager = middlewareManager;
|
|
62
20
|
this.requestContextMiddleware = requestContextMiddleware;
|
|
@@ -66,21 +24,21 @@ let RequestContextModule = class RequestContextModule {
|
|
|
66
24
|
.forRoutes("*");
|
|
67
25
|
}
|
|
68
26
|
};
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
(
|
|
72
|
-
|
|
73
|
-
imports: [middleware_1.MiddlewareModule],
|
|
27
|
+
RequestContextModule = __decorate([
|
|
28
|
+
Global(),
|
|
29
|
+
Module({
|
|
30
|
+
imports: [MiddlewareModule],
|
|
74
31
|
providers: [
|
|
75
|
-
|
|
32
|
+
RequestContextMiddleware,
|
|
76
33
|
{
|
|
77
|
-
provide:
|
|
78
|
-
useClass:
|
|
34
|
+
provide: APP_INTERCEPTOR,
|
|
35
|
+
useClass: RequestContextInterceptor,
|
|
79
36
|
},
|
|
80
37
|
],
|
|
81
|
-
exports: [
|
|
38
|
+
exports: [RequestContextMiddleware],
|
|
82
39
|
}),
|
|
83
|
-
__metadata("design:paramtypes", [
|
|
84
|
-
|
|
40
|
+
__metadata("design:paramtypes", [MiddlewareManager,
|
|
41
|
+
RequestContextMiddleware])
|
|
85
42
|
], RequestContextModule);
|
|
43
|
+
export { RequestContextModule };
|
|
86
44
|
//# sourceMappingURL=request-context.module.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"request-context.module.js","sourceRoot":"","sources":["../src/request-context.module.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"request-context.module.js","sourceRoot":"","sources":["../src/request-context.module.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,EAAE,iBAAiB,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AAC5E,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAC;AAChD,OAAO,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAE/C,OAAO,EAAE,yBAAyB,EAAE,MAAM,kCAAkC,CAAC;AAC7E,OAAO,EAAE,wBAAwB,EAAE,MAAM,iCAAiC,CAAC;AAkDpE,IAAM,oBAAoB,GAA1B,MAAM,oBAAoB;IAOZ;IACA;IAFnB,YACmB,iBAAoC,EACpC,wBAAkD;QADlD,sBAAiB,GAAjB,iBAAiB,CAAmB;QACpC,6BAAwB,GAAxB,wBAAwB,CAA0B;QAEnE,IAAI,CAAC,iBAAiB;aACnB,KAAK,CAAC,IAAI,CAAC,wBAAwB,CAAC;aACpC,0BAA0B,EAAE;aAC5B,SAAS,CAAC,GAAG,CAAC,CAAC;IACpB,CAAC;CACF,CAAA;AAfY,oBAAoB;IAZhC,MAAM,EAAE;IACR,MAAM,CAAC;QACN,OAAO,EAAE,CAAC,gBAAgB,CAAC;QAC3B,SAAS,EAAE;YACT,wBAAwB;YACxB;gBACE,OAAO,EAAE,eAAe;gBACxB,QAAQ,EAAE,yBAAyB;aACpC;SACF;QACD,OAAO,EAAE,CAAC,wBAAwB,CAAC;KACpC,CAAC;qCAQsC,iBAAiB;QACV,wBAAwB;GAR1D,oBAAoB,CAehC"}
|