@nest-boot/request-context 7.4.1 → 7.4.3
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 -2
- 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 +286 -3
- 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 +250 -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 +41 -0
- package/dist/request-context.module.js +41 -0
- package/dist/request-context.module.js.map +1 -1
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/package.json +19 -10
|
@@ -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,CA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equestContext = 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
|
+
/** Creates a new RequestContext instance.
|
|
50
|
+
* @param options - Options for creating the request context (id, type, parent)
|
|
51
|
+
*/
|
|
7
52
|
constructor(options) {
|
|
53
|
+
/** Internal storage map for context values. @internal */
|
|
8
54
|
this.container = new Map();
|
|
9
55
|
this.id = options.id ?? (0, crypto_1.randomUUID)();
|
|
10
56
|
this.type = options.type;
|
|
11
57
|
this.parent = options.parent;
|
|
12
58
|
}
|
|
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
|
+
*/
|
|
13
74
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
|
|
14
75
|
get(token) {
|
|
15
76
|
return this.container.get(token) ?? this.parent?.get(token);
|
|
16
77
|
}
|
|
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
|
+
*/
|
|
17
92
|
set(typeOrToken, value) {
|
|
18
93
|
this.container.set(typeOrToken, value);
|
|
19
94
|
}
|
|
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
|
+
*/
|
|
20
109
|
getOrSet(typeOrToken, value) {
|
|
21
110
|
const existing = this.get(typeOrToken);
|
|
22
111
|
if (typeof existing !== "undefined") {
|
|
@@ -25,24 +114,90 @@ class RequestContext {
|
|
|
25
114
|
this.set(typeOrToken, value);
|
|
26
115
|
return value;
|
|
27
116
|
}
|
|
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
|
+
*/
|
|
28
131
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
|
|
29
132
|
static get(key) {
|
|
30
133
|
const ctx = this.current();
|
|
31
134
|
return ctx.get(key);
|
|
32
135
|
}
|
|
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
|
+
*/
|
|
33
150
|
static set(key, value) {
|
|
34
151
|
const ctx = this.current();
|
|
35
152
|
if (typeof key !== "undefined") {
|
|
36
153
|
ctx.set(key, value);
|
|
37
154
|
}
|
|
38
155
|
}
|
|
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
|
+
*/
|
|
39
171
|
static getOrSet(key, value) {
|
|
40
172
|
const ctx = this.current();
|
|
41
173
|
return ctx.getOrSet(key, value);
|
|
42
174
|
}
|
|
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
|
+
*/
|
|
43
186
|
static get id() {
|
|
44
187
|
return this.current().id;
|
|
45
188
|
}
|
|
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
|
+
*/
|
|
46
201
|
static current() {
|
|
47
202
|
const ctx = this.storage.getStore();
|
|
48
203
|
if (typeof ctx === "undefined") {
|
|
@@ -50,9 +205,41 @@ class RequestContext {
|
|
|
50
205
|
}
|
|
51
206
|
return ctx;
|
|
52
207
|
}
|
|
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
|
+
*/
|
|
53
220
|
static isActive() {
|
|
54
221
|
return !!this.storage.getStore();
|
|
55
222
|
}
|
|
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
|
+
*/
|
|
56
243
|
static async run(ctx, callback) {
|
|
57
244
|
let i = 0;
|
|
58
245
|
const next = async () => {
|
|
@@ -63,6 +250,33 @@ class RequestContext {
|
|
|
63
250
|
};
|
|
64
251
|
return await this.storage.run(ctx, next);
|
|
65
252
|
}
|
|
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
|
+
*/
|
|
66
280
|
static async child(callback) {
|
|
67
281
|
const parent = this.storage.getStore();
|
|
68
282
|
if (typeof parent === "undefined") {
|
|
@@ -75,11 +289,42 @@ class RequestContext {
|
|
|
75
289
|
});
|
|
76
290
|
return await this.storage.run(ctx, () => callback(ctx));
|
|
77
291
|
}
|
|
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
|
+
*/
|
|
78
322
|
static registerMiddleware(name, middleware, dependencies) {
|
|
79
323
|
this.middlewares.set(name, middleware);
|
|
80
324
|
this.middlewareDependencies.set(name, dependencies ?? []);
|
|
81
325
|
this.generateMiddlewaresStack();
|
|
82
326
|
}
|
|
327
|
+
/** Resolves middleware dependencies via topological sort. @internal */
|
|
83
328
|
static resolveDependencies(name, resolved, seen) {
|
|
84
329
|
if (seen.has(name)) {
|
|
85
330
|
throw new Error(`Circular dependency detected: ${name}`);
|
|
@@ -93,6 +338,7 @@ class RequestContext {
|
|
|
93
338
|
}
|
|
94
339
|
resolved.add(name);
|
|
95
340
|
}
|
|
341
|
+
/** Rebuilds the middleware execution stack after registration changes. @internal */
|
|
96
342
|
static generateMiddlewaresStack() {
|
|
97
343
|
const resolved = new Set();
|
|
98
344
|
const seen = new Set();
|
|
@@ -107,8 +353,12 @@ class RequestContext {
|
|
|
107
353
|
}
|
|
108
354
|
}
|
|
109
355
|
exports.RequestContext = RequestContext;
|
|
356
|
+
/** Async local storage backing the request context. @internal */
|
|
110
357
|
RequestContext.storage = new async_hooks_1.AsyncLocalStorage();
|
|
358
|
+
/** Registered middleware map keyed by name. @internal */
|
|
111
359
|
RequestContext.middlewares = new Map();
|
|
360
|
+
/** Dependency graph for middleware ordering. @internal */
|
|
112
361
|
RequestContext.middlewareDependencies = new Map();
|
|
362
|
+
/** Topologically-sorted middleware execution stack. @internal */
|
|
113
363
|
RequestContext.middlewaresStack = [];
|
|
114
364
|
//# 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;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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E,yDAAyD;AACjC,0BAAW,GAAG,IAAI,GAAG,EAG1C,AAHgC,CAG/B;AAEJ,0DAA0D;AAClC,qCAAsB,GAAG,IAAI,GAAG,EAAoB,AAA9B,CAA+B;AAE7E,iEAAiE;AAClD,+BAAgB,GAAmC,EAAE,AAArC,CAAsC"}
|
|
@@ -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;AAE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AuCpC"}
|
|
@@ -1,7 +1,48 @@
|
|
|
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
|
+
/**
|
|
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
|
+
*/
|
|
6
47
|
constructor(middlewareManager: MiddlewareManager, requestContextMiddleware: RequestContextMiddleware);
|
|
7
48
|
}
|
|
@@ -15,7 +15,48 @@ 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
|
+
/**
|
|
56
|
+
* Creates a new RequestContextModule instance.
|
|
57
|
+
* @param middlewareManager - Middleware manager for registering the context middleware
|
|
58
|
+
* @param requestContextMiddleware - The request context middleware instance
|
|
59
|
+
*/
|
|
19
60
|
constructor(middlewareManager, requestContextMiddleware) {
|
|
20
61
|
this.middlewareManager = middlewareManager;
|
|
21
62
|
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;;;;OAIG;IACH,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,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;qCAQsC,8BAAiB;QACV,qDAAwB;GAR1D,oBAAoB,CAehC"}
|