@nest-boot/request-context 7.4.1 → 7.4.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/dist/create-request-context.decorator.d.ts +57 -0
- package/dist/create-request-context.decorator.js +53 -0
- package/dist/create-request-context.decorator.js.map +1 -1
- package/dist/repl.d.ts +43 -1
- package/dist/repl.js +41 -0
- package/dist/repl.js.map +1 -1
- package/dist/request-context.constants.d.ts +22 -0
- package/dist/request-context.constants.js +22 -0
- package/dist/request-context.constants.js.map +1 -1
- package/dist/request-context.d.ts +294 -0
- package/dist/request-context.interceptor.d.ts +30 -0
- package/dist/request-context.interceptor.js +30 -0
- package/dist/request-context.interceptor.js.map +1 -1
- package/dist/request-context.js +260 -0
- package/dist/request-context.js.map +1 -1
- package/dist/request-context.middleware.d.ts +27 -0
- package/dist/request-context.middleware.js +27 -0
- package/dist/request-context.middleware.js.map +1 -1
- package/dist/request-context.module.d.ts +37 -0
- package/dist/request-context.module.js +37 -0
- package/dist/request-context.module.js.map +1 -1
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/package.json +6 -6
|
@@ -10,7 +10,37 @@ exports.RequestContextInterceptor = void 0;
|
|
|
10
10
|
const common_1 = require("@nestjs/common");
|
|
11
11
|
const rxjs_1 = require("rxjs");
|
|
12
12
|
const request_context_1 = require("./request-context");
|
|
13
|
+
/**
|
|
14
|
+
* NestJS interceptor that creates request context for HTTP and GraphQL requests.
|
|
15
|
+
*
|
|
16
|
+
* This interceptor serves as a fallback for cases where the middleware doesn't
|
|
17
|
+
* run (e.g., GraphQL resolvers). It:
|
|
18
|
+
* - Creates a new RequestContext if one doesn't already exist
|
|
19
|
+
* - Uses the `x-request-id` header as the context ID if provided
|
|
20
|
+
* - Supports both HTTP and GraphQL execution contexts
|
|
21
|
+
*
|
|
22
|
+
* The interceptor is automatically registered by RequestContextModule.
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* The interceptor is typically used automatically, but can be applied manually:
|
|
26
|
+
* ```typescript
|
|
27
|
+
* import { Controller, UseInterceptors } from '@nestjs/common';
|
|
28
|
+
* import { RequestContextInterceptor } from '@nest-boot/request-context';
|
|
29
|
+
*
|
|
30
|
+
* @Controller()
|
|
31
|
+
* @UseInterceptors(RequestContextInterceptor)
|
|
32
|
+
* export class MyController {}
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
13
35
|
let RequestContextInterceptor = class RequestContextInterceptor {
|
|
36
|
+
/**
|
|
37
|
+
* Intercepts the request and wraps execution in a request context.
|
|
38
|
+
*
|
|
39
|
+
* @typeParam T - The type of the response
|
|
40
|
+
* @param executionContext - The NestJS execution context
|
|
41
|
+
* @param next - The call handler for the next interceptor or handler
|
|
42
|
+
* @returns An observable of the response
|
|
43
|
+
*/
|
|
14
44
|
intercept(executionContext, next) {
|
|
15
45
|
if (request_context_1.RequestContext.isActive() ||
|
|
16
46
|
!["http", "graphql"].includes(executionContext.getType())) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"request-context.interceptor.js","sourceRoot":"","sources":["../src/request-context.interceptor.ts"],"names":[],"mappings":";;;;;;;;;AAAA,2CAKwB;AAExB,+BAAkC;AAElC,uDAAmD;
|
|
1
|
+
{"version":3,"file":"request-context.interceptor.js","sourceRoot":"","sources":["../src/request-context.interceptor.ts"],"names":[],"mappings":";;;;;;;;;AAAA,2CAKwB;AAExB,+BAAkC;AAElC,uDAAmD;AAEnD;;;;;;;;;;;;;;;;;;;;;GAqBG;AAEI,IAAM,yBAAyB,GAA/B,MAAM,yBAAyB;IACpC;;;;;;;OAOG;IACH,SAAS,CACP,gBAAkC,EAClC,IAAoB;QAEpB,IACE,gCAAc,CAAC,QAAQ,EAAE;YACzB,CAAC,CAAC,MAAM,EAAE,SAAS,CAAC,CAAC,QAAQ,CAAC,gBAAgB,CAAC,OAAO,EAAE,CAAC,EACzD,CAAC;YACD,OAAO,IAAI,CAAC,MAAM,EAAE,CAAC;QACvB,CAAC;QAED,MAAM,EAAE,GAAG,CACT,gBAAgB,CAAC,YAAY,EAAE,CAAC,UAAU,EAAW;YACrD,gBAAgB,CAAC,aAAa,CAAmB,CAAC,CAAC,CAAC,GAAG,CACxD,EAAE,GAAG,EAAE,CAAC,cAAc,CAAC,CAAC;QAEzB,MAAM,GAAG,GAAG,IAAI,gCAAc,CAAC;YAC7B,EAAE;YACF,IAAI,EAAE,MAAM;SACb,CAAC,CAAC;QAEH,OAAO,IAAI,iBAAU,CAAC,CAAC,UAAU,EAAE,EAAE;YACnC,KAAK,gCAAc,CAAC,GAAG,CAAC,GAAG,EAAE,GAAG,EAAE;gBAChC,IAAI,CAAC;oBACH,IAAI;yBACD,MAAM,EAAE;yBACR,IAAI,EAAE;yBACN,SAAS,CAAC;wBACT,IAAI,EAAE,CAAC,GAAG,EAAE,EAAE;4BACZ,UAAU,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;wBACvB,CAAC;wBACD,KAAK,EAAE,CAAC,GAAG,EAAE,EAAE;4BACb,UAAU,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;wBACxB,CAAC;wBACD,QAAQ,EAAE,GAAG,EAAE;4BACb,UAAU,CAAC,QAAQ,EAAE,CAAC;wBACxB,CAAC;qBACF,CAAC,CAAC;gBACP,CAAC;gBAAC,OAAO,GAAG,EAAE,CAAC;oBACb,UAAU,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;gBACxB,CAAC;YACH,CAAC,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;IACL,CAAC;CACF,CAAA;AArDY,8DAAyB;oCAAzB,yBAAyB;IADrC,IAAA,mBAAU,GAAE;GACA,yBAAyB,CAqDrC"}
|
package/dist/request-context.js
CHANGED
|
@@ -3,20 +3,119 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.RequestContext = void 0;
|
|
4
4
|
const async_hooks_1 = require("async_hooks");
|
|
5
5
|
const crypto_1 = require("crypto");
|
|
6
|
+
/**
|
|
7
|
+
* RequestContext provides a way to store and access request-scoped data
|
|
8
|
+
* throughout the lifecycle of a request using AsyncLocalStorage.
|
|
9
|
+
*
|
|
10
|
+
* This is useful for storing data like the current user, request ID,
|
|
11
|
+
* database transactions, and other request-specific information that
|
|
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
|
+
*/
|
|
6
48
|
class RequestContext {
|
|
49
|
+
/**
|
|
50
|
+
* Creates a new RequestContext instance.
|
|
51
|
+
*
|
|
52
|
+
* @param options - Configuration options for the context
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* ```typescript
|
|
56
|
+
* const ctx = new RequestContext({
|
|
57
|
+
* id: 'custom-id',
|
|
58
|
+
* type: 'http',
|
|
59
|
+
* });
|
|
60
|
+
* ```
|
|
61
|
+
*/
|
|
7
62
|
constructor(options) {
|
|
63
|
+
/** @internal */
|
|
8
64
|
this.container = new Map();
|
|
9
65
|
this.id = options.id ?? (0, crypto_1.randomUUID)();
|
|
10
66
|
this.type = options.type;
|
|
11
67
|
this.parent = options.parent;
|
|
12
68
|
}
|
|
69
|
+
/**
|
|
70
|
+
* Gets a value from the context by its token.
|
|
71
|
+
* If not found in this context, looks up the parent context.
|
|
72
|
+
*
|
|
73
|
+
* @typeParam T - The expected type of the value
|
|
74
|
+
* @param token - The key to look up (string, symbol, function, or class)
|
|
75
|
+
* @returns The value if found, otherwise undefined
|
|
76
|
+
*
|
|
77
|
+
* @example
|
|
78
|
+
* ```typescript
|
|
79
|
+
* const ctx = RequestContext.current();
|
|
80
|
+
* const user = ctx.get<User>('currentUser');
|
|
81
|
+
* const service = ctx.get(MyService);
|
|
82
|
+
* ```
|
|
83
|
+
*/
|
|
13
84
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
|
|
14
85
|
get(token) {
|
|
15
86
|
return this.container.get(token) ?? this.parent?.get(token);
|
|
16
87
|
}
|
|
88
|
+
/**
|
|
89
|
+
* Sets a value in the context.
|
|
90
|
+
*
|
|
91
|
+
* @typeParam T - The type of the value
|
|
92
|
+
* @param typeOrToken - The key to store the value under
|
|
93
|
+
* @param value - The value to store
|
|
94
|
+
*
|
|
95
|
+
* @example
|
|
96
|
+
* ```typescript
|
|
97
|
+
* const ctx = RequestContext.current();
|
|
98
|
+
* ctx.set('userId', 123);
|
|
99
|
+
* ctx.set(UserService, userServiceInstance);
|
|
100
|
+
* ```
|
|
101
|
+
*/
|
|
17
102
|
set(typeOrToken, value) {
|
|
18
103
|
this.container.set(typeOrToken, value);
|
|
19
104
|
}
|
|
105
|
+
/**
|
|
106
|
+
* Gets a value from the context, or sets it if not present.
|
|
107
|
+
*
|
|
108
|
+
* @typeParam T - The type of the value
|
|
109
|
+
* @param typeOrToken - The key to look up or store under
|
|
110
|
+
* @param value - The value to set if not already present
|
|
111
|
+
* @returns The existing value or the newly set value
|
|
112
|
+
*
|
|
113
|
+
* @example
|
|
114
|
+
* ```typescript
|
|
115
|
+
* const ctx = RequestContext.current();
|
|
116
|
+
* const cache = ctx.getOrSet('cache', new Map());
|
|
117
|
+
* ```
|
|
118
|
+
*/
|
|
20
119
|
getOrSet(typeOrToken, value) {
|
|
21
120
|
const existing = this.get(typeOrToken);
|
|
22
121
|
if (typeof existing !== "undefined") {
|
|
@@ -25,24 +124,90 @@ class RequestContext {
|
|
|
25
124
|
this.set(typeOrToken, value);
|
|
26
125
|
return value;
|
|
27
126
|
}
|
|
127
|
+
/**
|
|
128
|
+
* Gets a value from the current context by its key.
|
|
129
|
+
* Static method that accesses the current context automatically.
|
|
130
|
+
*
|
|
131
|
+
* @typeParam T - The expected type of the value
|
|
132
|
+
* @param key - The key to look up
|
|
133
|
+
* @returns The value if found, otherwise undefined
|
|
134
|
+
* @throws Error if no request context is active
|
|
135
|
+
*
|
|
136
|
+
* @example
|
|
137
|
+
* ```typescript
|
|
138
|
+
* const userId = RequestContext.get<number>('userId');
|
|
139
|
+
* ```
|
|
140
|
+
*/
|
|
28
141
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
|
|
29
142
|
static get(key) {
|
|
30
143
|
const ctx = this.current();
|
|
31
144
|
return ctx.get(key);
|
|
32
145
|
}
|
|
146
|
+
/**
|
|
147
|
+
* Sets a value in the current context.
|
|
148
|
+
* Static method that accesses the current context automatically.
|
|
149
|
+
*
|
|
150
|
+
* @typeParam T - The type of the value
|
|
151
|
+
* @param key - The key to store the value under
|
|
152
|
+
* @param value - The value to store
|
|
153
|
+
* @throws Error if no request context is active
|
|
154
|
+
*
|
|
155
|
+
* @example
|
|
156
|
+
* ```typescript
|
|
157
|
+
* RequestContext.set('userId', 123);
|
|
158
|
+
* ```
|
|
159
|
+
*/
|
|
33
160
|
static set(key, value) {
|
|
34
161
|
const ctx = this.current();
|
|
35
162
|
if (typeof key !== "undefined") {
|
|
36
163
|
ctx.set(key, value);
|
|
37
164
|
}
|
|
38
165
|
}
|
|
166
|
+
/**
|
|
167
|
+
* Gets a value from the current context, or sets it if not present.
|
|
168
|
+
* Static method that accesses the current context automatically.
|
|
169
|
+
*
|
|
170
|
+
* @typeParam T - The type of the value
|
|
171
|
+
* @param key - The key to look up or store under
|
|
172
|
+
* @param value - The value to set if not already present
|
|
173
|
+
* @returns The existing value or the newly set value
|
|
174
|
+
* @throws Error if no request context is active
|
|
175
|
+
*
|
|
176
|
+
* @example
|
|
177
|
+
* ```typescript
|
|
178
|
+
* const cache = RequestContext.getOrSet('cache', new Map());
|
|
179
|
+
* ```
|
|
180
|
+
*/
|
|
39
181
|
static getOrSet(key, value) {
|
|
40
182
|
const ctx = this.current();
|
|
41
183
|
return ctx.getOrSet(key, value);
|
|
42
184
|
}
|
|
185
|
+
/**
|
|
186
|
+
* Gets the ID of the current request context.
|
|
187
|
+
*
|
|
188
|
+
* @returns The unique identifier of the current context
|
|
189
|
+
* @throws Error if no request context is active
|
|
190
|
+
*
|
|
191
|
+
* @example
|
|
192
|
+
* ```typescript
|
|
193
|
+
* console.log(`Processing request ${RequestContext.id}`);
|
|
194
|
+
* ```
|
|
195
|
+
*/
|
|
43
196
|
static get id() {
|
|
44
197
|
return this.current().id;
|
|
45
198
|
}
|
|
199
|
+
/**
|
|
200
|
+
* Gets the current request context.
|
|
201
|
+
*
|
|
202
|
+
* @returns The current RequestContext instance
|
|
203
|
+
* @throws Error if no request context is active
|
|
204
|
+
*
|
|
205
|
+
* @example
|
|
206
|
+
* ```typescript
|
|
207
|
+
* const ctx = RequestContext.current();
|
|
208
|
+
* console.log(ctx.type); // 'http'
|
|
209
|
+
* ```
|
|
210
|
+
*/
|
|
46
211
|
static current() {
|
|
47
212
|
const ctx = this.storage.getStore();
|
|
48
213
|
if (typeof ctx === "undefined") {
|
|
@@ -50,9 +215,41 @@ class RequestContext {
|
|
|
50
215
|
}
|
|
51
216
|
return ctx;
|
|
52
217
|
}
|
|
218
|
+
/**
|
|
219
|
+
* Checks if a request context is currently active.
|
|
220
|
+
*
|
|
221
|
+
* @returns true if a context is active, false otherwise
|
|
222
|
+
*
|
|
223
|
+
* @example
|
|
224
|
+
* ```typescript
|
|
225
|
+
* if (RequestContext.isActive()) {
|
|
226
|
+
* const userId = RequestContext.get('userId');
|
|
227
|
+
* }
|
|
228
|
+
* ```
|
|
229
|
+
*/
|
|
53
230
|
static isActive() {
|
|
54
231
|
return !!this.storage.getStore();
|
|
55
232
|
}
|
|
233
|
+
/**
|
|
234
|
+
* Runs a callback within a request context.
|
|
235
|
+
* All registered middlewares are executed before the callback.
|
|
236
|
+
*
|
|
237
|
+
* @typeParam T - The return type of the callback
|
|
238
|
+
* @param ctx - The request context to run within
|
|
239
|
+
* @param callback - The function to execute within the context
|
|
240
|
+
* @returns A promise resolving to the callback's return value
|
|
241
|
+
*
|
|
242
|
+
* @example
|
|
243
|
+
* ```typescript
|
|
244
|
+
* const result = await RequestContext.run(
|
|
245
|
+
* new RequestContext({ type: 'job' }),
|
|
246
|
+
* async (ctx) => {
|
|
247
|
+
* ctx.set('jobId', 'abc123');
|
|
248
|
+
* return await processJob();
|
|
249
|
+
* }
|
|
250
|
+
* );
|
|
251
|
+
* ```
|
|
252
|
+
*/
|
|
56
253
|
static async run(ctx, callback) {
|
|
57
254
|
let i = 0;
|
|
58
255
|
const next = async () => {
|
|
@@ -63,6 +260,33 @@ class RequestContext {
|
|
|
63
260
|
};
|
|
64
261
|
return await this.storage.run(ctx, next);
|
|
65
262
|
}
|
|
263
|
+
/**
|
|
264
|
+
* Creates and runs a child context that inherits from the current context.
|
|
265
|
+
* Child contexts can read values from parent contexts but modifications
|
|
266
|
+
* are isolated to the child.
|
|
267
|
+
*
|
|
268
|
+
* @typeParam T - The return type of the callback
|
|
269
|
+
* @param callback - The function to execute within the child context
|
|
270
|
+
* @returns A promise resolving to the callback's return value
|
|
271
|
+
* @throws Error if no request context is active
|
|
272
|
+
*
|
|
273
|
+
* @example
|
|
274
|
+
* ```typescript
|
|
275
|
+
* // In parent context
|
|
276
|
+
* RequestContext.set('userId', 123);
|
|
277
|
+
*
|
|
278
|
+
* await RequestContext.child(async (childCtx) => {
|
|
279
|
+
* // Can read parent values
|
|
280
|
+
* const userId = childCtx.get('userId'); // 123
|
|
281
|
+
*
|
|
282
|
+
* // Child-only values don't affect parent
|
|
283
|
+
* childCtx.set('tempData', 'child only');
|
|
284
|
+
* });
|
|
285
|
+
*
|
|
286
|
+
* // Parent context unchanged
|
|
287
|
+
* RequestContext.get('tempData'); // undefined
|
|
288
|
+
* ```
|
|
289
|
+
*/
|
|
66
290
|
static async child(callback) {
|
|
67
291
|
const parent = this.storage.getStore();
|
|
68
292
|
if (typeof parent === "undefined") {
|
|
@@ -75,11 +299,42 @@ class RequestContext {
|
|
|
75
299
|
});
|
|
76
300
|
return await this.storage.run(ctx, () => callback(ctx));
|
|
77
301
|
}
|
|
302
|
+
/**
|
|
303
|
+
* Registers a middleware to be executed when running a request context.
|
|
304
|
+
* Middlewares are executed in dependency order.
|
|
305
|
+
*
|
|
306
|
+
* @param name - Unique name for the middleware
|
|
307
|
+
* @param middleware - The middleware function to register
|
|
308
|
+
* @param dependencies - Names of middlewares that must run before this one
|
|
309
|
+
*
|
|
310
|
+
* @example
|
|
311
|
+
* ```typescript
|
|
312
|
+
* RequestContext.registerMiddleware(
|
|
313
|
+
* 'auth',
|
|
314
|
+
* async (ctx, next) => {
|
|
315
|
+
* ctx.set('user', await loadUser());
|
|
316
|
+
* return next();
|
|
317
|
+
* }
|
|
318
|
+
* );
|
|
319
|
+
*
|
|
320
|
+
* // Middleware with dependencies
|
|
321
|
+
* RequestContext.registerMiddleware(
|
|
322
|
+
* 'permissions',
|
|
323
|
+
* async (ctx, next) => {
|
|
324
|
+
* const user = ctx.get('user');
|
|
325
|
+
* ctx.set('permissions', await loadPermissions(user));
|
|
326
|
+
* return next();
|
|
327
|
+
* },
|
|
328
|
+
* ['auth'] // Runs after 'auth' middleware
|
|
329
|
+
* );
|
|
330
|
+
* ```
|
|
331
|
+
*/
|
|
78
332
|
static registerMiddleware(name, middleware, dependencies) {
|
|
79
333
|
this.middlewares.set(name, middleware);
|
|
80
334
|
this.middlewareDependencies.set(name, dependencies ?? []);
|
|
81
335
|
this.generateMiddlewaresStack();
|
|
82
336
|
}
|
|
337
|
+
/** @internal */
|
|
83
338
|
static resolveDependencies(name, resolved, seen) {
|
|
84
339
|
if (seen.has(name)) {
|
|
85
340
|
throw new Error(`Circular dependency detected: ${name}`);
|
|
@@ -93,6 +348,7 @@ class RequestContext {
|
|
|
93
348
|
}
|
|
94
349
|
resolved.add(name);
|
|
95
350
|
}
|
|
351
|
+
/** @internal */
|
|
96
352
|
static generateMiddlewaresStack() {
|
|
97
353
|
const resolved = new Set();
|
|
98
354
|
const seen = new Set();
|
|
@@ -107,8 +363,12 @@ class RequestContext {
|
|
|
107
363
|
}
|
|
108
364
|
}
|
|
109
365
|
exports.RequestContext = RequestContext;
|
|
366
|
+
/** @internal */
|
|
110
367
|
RequestContext.storage = new async_hooks_1.AsyncLocalStorage();
|
|
368
|
+
/** @internal */
|
|
111
369
|
RequestContext.middlewares = new Map();
|
|
370
|
+
/** @internal */
|
|
112
371
|
RequestContext.middlewareDependencies = new Map();
|
|
372
|
+
/** @internal */
|
|
113
373
|
RequestContext.middlewaresStack = [];
|
|
114
374
|
//# sourceMappingURL=request-context.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"request-context.js","sourceRoot":"","sources":["../src/request-context.ts"],"names":[],"mappings":";;;AACA,6CAAgD;AAChD,mCAAoC;
|
|
1
|
+
{"version":3,"file":"request-context.js","sourceRoot":"","sources":["../src/request-context.ts"],"names":[],"mappings":";;;AACA,6CAAgD;AAChD,mCAAoC;AAsCpC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAyCG;AACH,MAAa,cAAc;IA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,GAAG,EAG1C,AAHgC,CAG/B;AAEJ,gBAAgB;AACQ,qCAAsB,GAAG,IAAI,GAAG,EAAoB,AAA9B,CAA+B;AAE7E,gBAAgB;AACD,+BAAgB,GAA+B,EAAE,AAAjC,CAAkC"}
|
|
@@ -1,5 +1,32 @@
|
|
|
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
|
+
*/
|
|
3
23
|
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
|
+
*/
|
|
4
31
|
use(req: Request, res: Response, next: NextFunction): Promise<void>;
|
|
5
32
|
}
|
|
@@ -10,7 +10,34 @@ exports.RequestContextMiddleware = void 0;
|
|
|
10
10
|
const common_1 = require("@nestjs/common");
|
|
11
11
|
const request_context_1 = require("./request-context");
|
|
12
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
|
+
*/
|
|
13
33
|
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
|
+
*/
|
|
14
41
|
async use(req, res, next) {
|
|
15
42
|
if (request_context_1.RequestContext.isActive()) {
|
|
16
43
|
next();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"request-context.middleware.js","sourceRoot":"","sources":["../src/request-context.middleware.ts"],"names":[],"mappings":";;;;;;;;;AAAA,2CAAiE;AAGjE,uDAAmD;AACnD,2EAGqC;
|
|
1
|
+
{"version":3,"file":"request-context.middleware.js","sourceRoot":"","sources":["../src/request-context.middleware.ts"],"names":[],"mappings":";;;;;;;;;AAAA,2CAAiE;AAGjE,uDAAmD;AACnD,2EAGqC;AAErC;;;;;;;;;;;;;;;;;;;GAmBG;AAEI,IAAM,wBAAwB,GAA9B,MAAM,wBAA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
|
|
1
1
|
import { MiddlewareManager } from "@nest-boot/middleware";
|
|
2
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
|
+
*/
|
|
3
39
|
export declare class RequestContextModule {
|
|
4
40
|
private readonly middlewareManager;
|
|
5
41
|
private readonly requestContextMiddleware;
|
|
42
|
+
/** @internal */
|
|
6
43
|
constructor(middlewareManager: MiddlewareManager, requestContextMiddleware: RequestContextMiddleware);
|
|
7
44
|
}
|
|
@@ -15,7 +15,44 @@ const common_1 = require("@nestjs/common");
|
|
|
15
15
|
const core_1 = require("@nestjs/core");
|
|
16
16
|
const request_context_interceptor_1 = require("./request-context.interceptor");
|
|
17
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
|
+
*/
|
|
18
54
|
let RequestContextModule = class RequestContextModule {
|
|
55
|
+
/** @internal */
|
|
19
56
|
constructor(middlewareManager, requestContextMiddleware) {
|
|
20
57
|
this.middlewareManager = middlewareManager;
|
|
21
58
|
this.requestContextMiddleware = requestContextMiddleware;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"request-context.module.js","sourceRoot":"","sources":["../src/request-context.module.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,sDAA4E;AAC5E,2CAAgD;AAChD,uCAA+C;AAE/C,+EAA0E;AAC1E,6EAAwE;
|
|
1
|
+
{"version":3,"file":"request-context.module.js","sourceRoot":"","sources":["../src/request-context.module.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,sDAA4E;AAC5E,2CAAgD;AAChD,uCAA+C;AAE/C,+EAA0E;AAC1E,6EAAwE;AAExE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AAaI,IAAM,oBAAoB,GAA1B,MAAM,oBAAoB;IAC/B,gBAAgB;IAChB,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;AAXY,oDAAoB;+BAApB,oBAAoB;IAZhC,IAAA,eAAM,GAAE;IACR,IAAA,eAAM,EAAC;QACN,OAAO,EAAE,CAAC,6BAAgB,CAAC;QAC3B,SAAS,EAAE;YACT,qDAAwB;YACxB;gBACE,OAAO,EAAE,sBAAe;gBACxB,QAAQ,EAAE,uDAAyB;aACpC;SACF;QACD,OAAO,EAAE,CAAC,qDAAwB,CAAC;KACpC,CAAC;qCAIsC,8BAAiB;QACV,qDAAwB;GAJ1D,oBAAoB,CAWhC"}
|