@nest-boot/request-context 8.0.0-beta.0 → 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 +0 -56
- package/dist/create-request-context.decorator.js +0 -53
- package/dist/create-request-context.decorator.js.map +1 -1
- package/dist/repl.d.ts +0 -41
- package/dist/repl.js +0 -41
- package/dist/repl.js.map +1 -1
- package/dist/request-context.constants.d.ts +0 -22
- package/dist/request-context.constants.js +0 -22
- package/dist/request-context.constants.js.map +1 -1
- package/dist/request-context.d.ts +0 -284
- package/dist/request-context.interceptor.d.ts +0 -30
- package/dist/request-context.interceptor.js +52 -45
- package/dist/request-context.interceptor.js.map +1 -1
- package/dist/request-context.js +0 -263
- package/dist/request-context.js.map +1 -1
- package/dist/request-context.middleware.d.ts +0 -27
- package/dist/request-context.middleware.js +0 -27
- package/dist/request-context.middleware.js.map +1 -1
- package/dist/request-context.module.d.ts +0 -41
- package/dist/request-context.module.js +0 -41
- package/dist/request-context.module.js.map +1 -1
- package/package.json +30 -13
|
@@ -7,37 +7,7 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
|
|
|
7
7
|
import { Injectable, } from "@nestjs/common";
|
|
8
8
|
import { Observable } from "rxjs";
|
|
9
9
|
import { RequestContext } from "./request-context.js";
|
|
10
|
-
/**
|
|
11
|
-
* NestJS interceptor that creates request context for HTTP and GraphQL requests.
|
|
12
|
-
*
|
|
13
|
-
* This interceptor serves as a fallback for cases where the middleware doesn't
|
|
14
|
-
* run (e.g., GraphQL resolvers). It:
|
|
15
|
-
* - Creates a new RequestContext if one doesn't already exist
|
|
16
|
-
* - Uses the `x-request-id` header as the context ID if provided
|
|
17
|
-
* - Supports both HTTP and GraphQL execution contexts
|
|
18
|
-
*
|
|
19
|
-
* The interceptor is automatically registered by RequestContextModule.
|
|
20
|
-
*
|
|
21
|
-
* @example
|
|
22
|
-
* The interceptor is typically used automatically, but can be applied manually:
|
|
23
|
-
* ```typescript
|
|
24
|
-
* import { Controller, UseInterceptors } from '@nestjs/common';
|
|
25
|
-
* import { RequestContextInterceptor } from '@nest-boot/request-context';
|
|
26
|
-
*
|
|
27
|
-
* @Controller()
|
|
28
|
-
* @UseInterceptors(RequestContextInterceptor)
|
|
29
|
-
* export class MyController {}
|
|
30
|
-
* ```
|
|
31
|
-
*/
|
|
32
10
|
let RequestContextInterceptor = class RequestContextInterceptor {
|
|
33
|
-
/**
|
|
34
|
-
* Intercepts the request and wraps execution in a request context.
|
|
35
|
-
*
|
|
36
|
-
* @typeParam T - The type of the response
|
|
37
|
-
* @param executionContext - The NestJS execution context
|
|
38
|
-
* @param next - The call handler for the next interceptor or handler
|
|
39
|
-
* @returns An observable of the response
|
|
40
|
-
*/
|
|
41
11
|
intercept(executionContext, next) {
|
|
42
12
|
if (RequestContext.isActive() ||
|
|
43
13
|
!["http", "graphql"].includes(executionContext.getType())) {
|
|
@@ -50,24 +20,61 @@ let RequestContextInterceptor = class RequestContextInterceptor {
|
|
|
50
20
|
type: "http",
|
|
51
21
|
});
|
|
52
22
|
return new Observable((subscriber) => {
|
|
53
|
-
|
|
23
|
+
let resolveTermination;
|
|
24
|
+
let rejectTermination;
|
|
25
|
+
let terminated = false;
|
|
26
|
+
const termination = new Promise((resolve, reject) => {
|
|
27
|
+
resolveTermination = () => {
|
|
28
|
+
if (!terminated) {
|
|
29
|
+
terminated = true;
|
|
30
|
+
resolve();
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
rejectTermination = (reason) => {
|
|
34
|
+
if (!terminated) {
|
|
35
|
+
terminated = true;
|
|
36
|
+
reject(reason);
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
});
|
|
40
|
+
let subscribeToSource;
|
|
41
|
+
const sourceSubscription = new Observable((sourceSubscriber) => {
|
|
42
|
+
subscribeToSource = (source) => {
|
|
43
|
+
source.subscribe(sourceSubscriber);
|
|
44
|
+
};
|
|
45
|
+
}).subscribe({
|
|
46
|
+
next: (res) => {
|
|
47
|
+
subscriber.next(res);
|
|
48
|
+
},
|
|
49
|
+
error: (err) => {
|
|
50
|
+
rejectTermination(err);
|
|
51
|
+
},
|
|
52
|
+
complete: () => {
|
|
53
|
+
resolveTermination();
|
|
54
|
+
},
|
|
55
|
+
});
|
|
56
|
+
subscriber.add(sourceSubscription);
|
|
57
|
+
subscriber.add(resolveTermination);
|
|
58
|
+
void RequestContext.run(ctx, async () => {
|
|
59
|
+
if (subscriber.closed) {
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
54
62
|
try {
|
|
55
|
-
next
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
next: (res) => {
|
|
60
|
-
subscriber.next(res);
|
|
61
|
-
},
|
|
62
|
-
error: (err) => {
|
|
63
|
-
subscriber.error(err);
|
|
64
|
-
},
|
|
65
|
-
complete: () => {
|
|
66
|
-
subscriber.complete();
|
|
67
|
-
},
|
|
68
|
-
});
|
|
63
|
+
const source = next.handle();
|
|
64
|
+
if (!subscriber.closed) {
|
|
65
|
+
subscribeToSource(source);
|
|
66
|
+
}
|
|
69
67
|
}
|
|
70
68
|
catch (err) {
|
|
69
|
+
rejectTermination(err);
|
|
70
|
+
}
|
|
71
|
+
await termination;
|
|
72
|
+
}).then(() => {
|
|
73
|
+
if (!subscriber.closed) {
|
|
74
|
+
subscriber.complete();
|
|
75
|
+
}
|
|
76
|
+
}, (err) => {
|
|
77
|
+
if (!subscriber.closed) {
|
|
71
78
|
subscriber.error(err);
|
|
72
79
|
}
|
|
73
80
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"request-context.interceptor.js","sourceRoot":"","sources":["../src/request-context.interceptor.ts"],"names":[],"mappings":";;;;;;AAAA,OAAO,EAGL,UAAU,GAEX,MAAM,gBAAgB,CAAC;AAExB,OAAO,EAAE,UAAU,EAAE,MAAM,MAAM,CAAC;AAElC,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;
|
|
1
|
+
{"version":3,"file":"request-context.interceptor.js","sourceRoot":"","sources":["../src/request-context.interceptor.ts"],"names":[],"mappings":";;;;;;AAAA,OAAO,EAGL,UAAU,GAEX,MAAM,gBAAgB,CAAC;AAExB,OAAO,EAAE,UAAU,EAAE,MAAM,MAAM,CAAC;AAElC,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AA4B/C,IAAM,yBAAyB,GAA/B,MAAM,yBAAyB;IASpC,SAAS,CACP,gBAAkC,EAClC,IAAoB;QAEpB,IACE,cAAc,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,cAAc,CAAC;YAC7B,EAAE;YACF,IAAI,EAAE,MAAM;SACb,CAAC,CAAC;QAEH,OAAO,IAAI,UAAU,CAAC,CAAC,UAAU,EAAE,EAAE;YACnC,IAAI,kBAA+B,CAAC;YACpC,IAAI,iBAA6C,CAAC;YAClD,IAAI,UAAU,GAAG,KAAK,CAAC;YACvB,MAAM,WAAW,GAAG,IAAI,OAAO,CAAO,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;gBACxD,kBAAkB,GAAG,GAAG,EAAE;oBACxB,IAAI,CAAC,UAAU,EAAE,CAAC;wBAChB,UAAU,GAAG,IAAI,CAAC;wBAClB,OAAO,EAAE,CAAC;oBACZ,CAAC;gBACH,CAAC,CAAC;gBACF,iBAAiB,GAAG,CAAC,MAAM,EAAE,EAAE;oBAC7B,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CAAC,CAAC,CAAC,IAAI,CACL,GAAG,EAAE;gBACH,IAAI,CAAC,UAAU,CAAC,MAAM,EAAE,CAAC;oBACvB,UAAU,CAAC,QAAQ,EAAE,CAAC;gBACxB,CAAC;YACH,CAAC,EACD,CAAC,GAAY,EAAE,EAAE;gBACf,IAAI,CAAC,UAAU,CAAC,MAAM,EAAE,CAAC;oBACvB,UAAU,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;gBACxB,CAAC;YACH,CAAC,CACF,CAAC;QACJ,CAAC,CAAC,CAAC;IACL,CAAC;CACF,CAAA;AAnGY,yBAAyB;IADrC,UAAU,EAAE;GACA,yBAAyB,CAmGrC"}
|
package/dist/request-context.js
CHANGED
|
@@ -1,130 +1,25 @@
|
|
|
1
1
|
import { AsyncLocalStorage } from "async_hooks";
|
|
2
2
|
import { randomUUID } from "crypto";
|
|
3
|
-
/**
|
|
4
|
-
* RequestContext provides a way to store and access request-scoped data
|
|
5
|
-
* throughout the lifecycle of a request using AsyncLocalStorage.
|
|
6
|
-
*
|
|
7
|
-
* This is useful for storing data like the current user, request ID,
|
|
8
|
-
* database transactions, and other request-specific information that
|
|
9
|
-
* needs to be accessed across different parts of the application.
|
|
10
|
-
*
|
|
11
|
-
* @example Basic usage
|
|
12
|
-
* ```typescript
|
|
13
|
-
* import { RequestContext } from '@nest-boot/request-context';
|
|
14
|
-
*
|
|
15
|
-
* // Get the current request ID
|
|
16
|
-
* const requestId = RequestContext.id;
|
|
17
|
-
*
|
|
18
|
-
* // Store a value in the context
|
|
19
|
-
* RequestContext.set('userId', 123);
|
|
20
|
-
*
|
|
21
|
-
* // Retrieve a value from the context
|
|
22
|
-
* const userId = RequestContext.get<number>('userId');
|
|
23
|
-
* ```
|
|
24
|
-
*
|
|
25
|
-
* @example Running code in a new context
|
|
26
|
-
* ```typescript
|
|
27
|
-
* await RequestContext.run(
|
|
28
|
-
* new RequestContext({ type: 'job' }),
|
|
29
|
-
* async (ctx) => {
|
|
30
|
-
* ctx.set('jobId', 'abc123');
|
|
31
|
-
* await processJob();
|
|
32
|
-
* }
|
|
33
|
-
* );
|
|
34
|
-
* ```
|
|
35
|
-
*
|
|
36
|
-
* @example Creating a child context
|
|
37
|
-
* ```typescript
|
|
38
|
-
* await RequestContext.child(async (childCtx) => {
|
|
39
|
-
* // Child context inherits values from parent
|
|
40
|
-
* // but can have its own values that don't affect parent
|
|
41
|
-
* childCtx.set('tempValue', 'only in child');
|
|
42
|
-
* });
|
|
43
|
-
* ```
|
|
44
|
-
*/
|
|
45
3
|
export class RequestContext {
|
|
46
|
-
/**
|
|
47
|
-
* Unique identifier for this request context.
|
|
48
|
-
* Automatically generated as a UUID if not provided.
|
|
49
|
-
*/
|
|
50
4
|
id;
|
|
51
|
-
/**
|
|
52
|
-
* The type of this context (e.g., 'http', 'graphql', 'repl', 'job').
|
|
53
|
-
*/
|
|
54
5
|
type;
|
|
55
|
-
/**
|
|
56
|
-
* Parent context, if this is a child context.
|
|
57
|
-
* Values not found in this context will be looked up in the parent.
|
|
58
|
-
*/
|
|
59
6
|
parent;
|
|
60
|
-
/** Internal storage map for context values. @internal */
|
|
61
7
|
container = new Map();
|
|
62
|
-
/** Async local storage backing the request context. @internal */
|
|
63
8
|
static storage = new AsyncLocalStorage();
|
|
64
|
-
/** Registered middleware map keyed by name. @internal */
|
|
65
9
|
static middlewares = new Map();
|
|
66
|
-
/** Dependency graph for middleware ordering. @internal */
|
|
67
10
|
static middlewareDependencies = new Map();
|
|
68
|
-
/** Topologically-sorted middleware execution stack. @internal */
|
|
69
11
|
static middlewaresStack = [];
|
|
70
|
-
/** Creates a new RequestContext instance.
|
|
71
|
-
* @param options - Options for creating the request context (id, type, parent)
|
|
72
|
-
*/
|
|
73
12
|
constructor(options) {
|
|
74
13
|
this.id = options.id ?? randomUUID();
|
|
75
14
|
this.type = options.type;
|
|
76
15
|
this.parent = options.parent;
|
|
77
16
|
}
|
|
78
|
-
/**
|
|
79
|
-
* Gets a value from the context by its token.
|
|
80
|
-
* If not found in this context, looks up the parent context.
|
|
81
|
-
*
|
|
82
|
-
* @typeParam T - The expected type of the value
|
|
83
|
-
* @param token - The key to look up (string, symbol, function, or class)
|
|
84
|
-
* @returns The value if found, otherwise undefined
|
|
85
|
-
*
|
|
86
|
-
* @example
|
|
87
|
-
* ```typescript
|
|
88
|
-
* const ctx = RequestContext.current();
|
|
89
|
-
* const user = ctx.get<User>('currentUser');
|
|
90
|
-
* const service = ctx.get(MyService);
|
|
91
|
-
* ```
|
|
92
|
-
*/
|
|
93
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
|
|
94
17
|
get(token) {
|
|
95
18
|
return this.container.get(token) ?? this.parent?.get(token);
|
|
96
19
|
}
|
|
97
|
-
/**
|
|
98
|
-
* Sets a value in the context.
|
|
99
|
-
*
|
|
100
|
-
* @typeParam T - The type of the value
|
|
101
|
-
* @param typeOrToken - The key to store the value under
|
|
102
|
-
* @param value - The value to store
|
|
103
|
-
*
|
|
104
|
-
* @example
|
|
105
|
-
* ```typescript
|
|
106
|
-
* const ctx = RequestContext.current();
|
|
107
|
-
* ctx.set('userId', 123);
|
|
108
|
-
* ctx.set(UserService, userServiceInstance);
|
|
109
|
-
* ```
|
|
110
|
-
*/
|
|
111
20
|
set(typeOrToken, value) {
|
|
112
21
|
this.container.set(typeOrToken, value);
|
|
113
22
|
}
|
|
114
|
-
/**
|
|
115
|
-
* Gets a value from the context, or sets it if not present.
|
|
116
|
-
*
|
|
117
|
-
* @typeParam T - The type of the value
|
|
118
|
-
* @param typeOrToken - The key to look up or store under
|
|
119
|
-
* @param value - The value to set if not already present
|
|
120
|
-
* @returns The existing value or the newly set value
|
|
121
|
-
*
|
|
122
|
-
* @example
|
|
123
|
-
* ```typescript
|
|
124
|
-
* const ctx = RequestContext.current();
|
|
125
|
-
* const cache = ctx.getOrSet('cache', new Map());
|
|
126
|
-
* ```
|
|
127
|
-
*/
|
|
128
23
|
getOrSet(typeOrToken, value) {
|
|
129
24
|
const existing = this.get(typeOrToken);
|
|
130
25
|
if (typeof existing !== "undefined") {
|
|
@@ -133,90 +28,23 @@ export class RequestContext {
|
|
|
133
28
|
this.set(typeOrToken, value);
|
|
134
29
|
return value;
|
|
135
30
|
}
|
|
136
|
-
/**
|
|
137
|
-
* Gets a value from the current context by its key.
|
|
138
|
-
* Static method that accesses the current context automatically.
|
|
139
|
-
*
|
|
140
|
-
* @typeParam T - The expected type of the value
|
|
141
|
-
* @param key - The key to look up
|
|
142
|
-
* @returns The value if found, otherwise undefined
|
|
143
|
-
* @throws Error if no request context is active
|
|
144
|
-
*
|
|
145
|
-
* @example
|
|
146
|
-
* ```typescript
|
|
147
|
-
* const userId = RequestContext.get<number>('userId');
|
|
148
|
-
* ```
|
|
149
|
-
*/
|
|
150
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
|
|
151
31
|
static get(key) {
|
|
152
32
|
const ctx = this.current();
|
|
153
33
|
return ctx.get(key);
|
|
154
34
|
}
|
|
155
|
-
/**
|
|
156
|
-
* Sets a value in the current context.
|
|
157
|
-
* Static method that accesses the current context automatically.
|
|
158
|
-
*
|
|
159
|
-
* @typeParam T - The type of the value
|
|
160
|
-
* @param key - The key to store the value under
|
|
161
|
-
* @param value - The value to store
|
|
162
|
-
* @throws Error if no request context is active
|
|
163
|
-
*
|
|
164
|
-
* @example
|
|
165
|
-
* ```typescript
|
|
166
|
-
* RequestContext.set('userId', 123);
|
|
167
|
-
* ```
|
|
168
|
-
*/
|
|
169
35
|
static set(key, value) {
|
|
170
36
|
const ctx = this.current();
|
|
171
37
|
if (typeof key !== "undefined") {
|
|
172
38
|
ctx.set(key, value);
|
|
173
39
|
}
|
|
174
40
|
}
|
|
175
|
-
/**
|
|
176
|
-
* Gets a value from the current context, or sets it if not present.
|
|
177
|
-
* Static method that accesses the current context automatically.
|
|
178
|
-
*
|
|
179
|
-
* @typeParam T - The type of the value
|
|
180
|
-
* @param key - The key to look up or store under
|
|
181
|
-
* @param value - The value to set if not already present
|
|
182
|
-
* @returns The existing value or the newly set value
|
|
183
|
-
* @throws Error if no request context is active
|
|
184
|
-
*
|
|
185
|
-
* @example
|
|
186
|
-
* ```typescript
|
|
187
|
-
* const cache = RequestContext.getOrSet('cache', new Map());
|
|
188
|
-
* ```
|
|
189
|
-
*/
|
|
190
41
|
static getOrSet(key, value) {
|
|
191
42
|
const ctx = this.current();
|
|
192
43
|
return ctx.getOrSet(key, value);
|
|
193
44
|
}
|
|
194
|
-
/**
|
|
195
|
-
* Gets the ID of the current request context.
|
|
196
|
-
*
|
|
197
|
-
* @returns The unique identifier of the current context
|
|
198
|
-
* @throws Error if no request context is active
|
|
199
|
-
*
|
|
200
|
-
* @example
|
|
201
|
-
* ```typescript
|
|
202
|
-
* console.log(`Processing request ${RequestContext.id}`);
|
|
203
|
-
* ```
|
|
204
|
-
*/
|
|
205
45
|
static get id() {
|
|
206
46
|
return this.current().id;
|
|
207
47
|
}
|
|
208
|
-
/**
|
|
209
|
-
* Gets the current request context.
|
|
210
|
-
*
|
|
211
|
-
* @returns The current RequestContext instance
|
|
212
|
-
* @throws Error if no request context is active
|
|
213
|
-
*
|
|
214
|
-
* @example
|
|
215
|
-
* ```typescript
|
|
216
|
-
* const ctx = RequestContext.current();
|
|
217
|
-
* console.log(ctx.type); // 'http'
|
|
218
|
-
* ```
|
|
219
|
-
*/
|
|
220
48
|
static current() {
|
|
221
49
|
const ctx = this.storage.getStore();
|
|
222
50
|
if (typeof ctx === "undefined") {
|
|
@@ -224,41 +52,9 @@ export class RequestContext {
|
|
|
224
52
|
}
|
|
225
53
|
return ctx;
|
|
226
54
|
}
|
|
227
|
-
/**
|
|
228
|
-
* Checks if a request context is currently active.
|
|
229
|
-
*
|
|
230
|
-
* @returns true if a context is active, false otherwise
|
|
231
|
-
*
|
|
232
|
-
* @example
|
|
233
|
-
* ```typescript
|
|
234
|
-
* if (RequestContext.isActive()) {
|
|
235
|
-
* const userId = RequestContext.get('userId');
|
|
236
|
-
* }
|
|
237
|
-
* ```
|
|
238
|
-
*/
|
|
239
55
|
static isActive() {
|
|
240
56
|
return !!this.storage.getStore();
|
|
241
57
|
}
|
|
242
|
-
/**
|
|
243
|
-
* Runs a callback within a request context.
|
|
244
|
-
* All registered middlewares are executed before the callback.
|
|
245
|
-
*
|
|
246
|
-
* @typeParam T - The return type of the callback
|
|
247
|
-
* @param ctx - The request context to run within
|
|
248
|
-
* @param callback - The function to execute within the context
|
|
249
|
-
* @returns A promise resolving to the callback's return value
|
|
250
|
-
*
|
|
251
|
-
* @example
|
|
252
|
-
* ```typescript
|
|
253
|
-
* const result = await RequestContext.run(
|
|
254
|
-
* new RequestContext({ type: 'job' }),
|
|
255
|
-
* async (ctx) => {
|
|
256
|
-
* ctx.set('jobId', 'abc123');
|
|
257
|
-
* return await processJob();
|
|
258
|
-
* }
|
|
259
|
-
* );
|
|
260
|
-
* ```
|
|
261
|
-
*/
|
|
262
58
|
static async run(ctx, callback) {
|
|
263
59
|
let i = 0;
|
|
264
60
|
const next = async () => {
|
|
@@ -269,33 +65,6 @@ export class RequestContext {
|
|
|
269
65
|
};
|
|
270
66
|
return await this.storage.run(ctx, next);
|
|
271
67
|
}
|
|
272
|
-
/**
|
|
273
|
-
* Creates and runs a child context that inherits from the current context.
|
|
274
|
-
* Child contexts can read values from parent contexts but modifications
|
|
275
|
-
* are isolated to the child.
|
|
276
|
-
*
|
|
277
|
-
* @typeParam T - The return type of the callback
|
|
278
|
-
* @param callback - The function to execute within the child context
|
|
279
|
-
* @returns A promise resolving to the callback's return value
|
|
280
|
-
* @throws Error if no request context is active
|
|
281
|
-
*
|
|
282
|
-
* @example
|
|
283
|
-
* ```typescript
|
|
284
|
-
* // In parent context
|
|
285
|
-
* RequestContext.set('userId', 123);
|
|
286
|
-
*
|
|
287
|
-
* await RequestContext.child(async (childCtx) => {
|
|
288
|
-
* // Can read parent values
|
|
289
|
-
* const userId = childCtx.get('userId'); // 123
|
|
290
|
-
*
|
|
291
|
-
* // Child-only values don't affect parent
|
|
292
|
-
* childCtx.set('tempData', 'child only');
|
|
293
|
-
* });
|
|
294
|
-
*
|
|
295
|
-
* // Parent context unchanged
|
|
296
|
-
* RequestContext.get('tempData'); // undefined
|
|
297
|
-
* ```
|
|
298
|
-
*/
|
|
299
68
|
static async child(callback) {
|
|
300
69
|
const parent = this.storage.getStore();
|
|
301
70
|
if (typeof parent === "undefined") {
|
|
@@ -308,42 +77,11 @@ export class RequestContext {
|
|
|
308
77
|
});
|
|
309
78
|
return await this.storage.run(ctx, () => callback(ctx));
|
|
310
79
|
}
|
|
311
|
-
/**
|
|
312
|
-
* Registers a middleware to be executed when running a request context.
|
|
313
|
-
* Middlewares are executed in dependency order.
|
|
314
|
-
*
|
|
315
|
-
* @param name - Unique name for the middleware
|
|
316
|
-
* @param middleware - The middleware function to register
|
|
317
|
-
* @param dependencies - Names of middlewares that must run before this one
|
|
318
|
-
*
|
|
319
|
-
* @example
|
|
320
|
-
* ```typescript
|
|
321
|
-
* RequestContext.registerMiddleware(
|
|
322
|
-
* 'auth',
|
|
323
|
-
* async (ctx, next) => {
|
|
324
|
-
* ctx.set('user', await loadUser());
|
|
325
|
-
* return next();
|
|
326
|
-
* }
|
|
327
|
-
* );
|
|
328
|
-
*
|
|
329
|
-
* // Middleware with dependencies
|
|
330
|
-
* RequestContext.registerMiddleware(
|
|
331
|
-
* 'permissions',
|
|
332
|
-
* async (ctx, next) => {
|
|
333
|
-
* const user = ctx.get('user');
|
|
334
|
-
* ctx.set('permissions', await loadPermissions(user));
|
|
335
|
-
* return next();
|
|
336
|
-
* },
|
|
337
|
-
* ['auth'] // Runs after 'auth' middleware
|
|
338
|
-
* );
|
|
339
|
-
* ```
|
|
340
|
-
*/
|
|
341
80
|
static registerMiddleware(name, middleware, dependencies) {
|
|
342
81
|
this.middlewares.set(name, middleware);
|
|
343
82
|
this.middlewareDependencies.set(name, dependencies ?? []);
|
|
344
83
|
this.generateMiddlewaresStack();
|
|
345
84
|
}
|
|
346
|
-
/** Resolves middleware dependencies via topological sort. @internal */
|
|
347
85
|
static resolveDependencies(name, resolved, seen) {
|
|
348
86
|
if (seen.has(name)) {
|
|
349
87
|
throw new Error(`Circular dependency detected: ${name}`);
|
|
@@ -357,7 +95,6 @@ export class RequestContext {
|
|
|
357
95
|
}
|
|
358
96
|
resolved.add(name);
|
|
359
97
|
}
|
|
360
|
-
/** Rebuilds the middleware execution stack after registration changes. @internal */
|
|
361
98
|
static generateMiddlewaresStack() {
|
|
362
99
|
const resolved = new Set();
|
|
363
100
|
const seen = new Set();
|
|
@@ -1 +1 @@
|
|
|
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;
|
|
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,CAAC;IAG/B,MAAM,CAAU,OAAO,GAAG,IAAI,iBAAiB,EAAkB,CAAC;IAGlE,MAAM,CAAU,WAAW,GAAG,IAAI,GAAG,EAG1C,CAAC;IAGI,MAAM,CAAU,sBAAsB,GAAG,IAAI,GAAG,EAAoB,CAAC;IAGrE,MAAM,CAAC,gBAAgB,GAAmC,EAAE,CAAC;IAKrE,YAAY,OAAoC;QAC9C,IAAI,CAAC,EAAE,GAAG,OAAO,CAAC,EAAE,IAAI,UAAU,EAAE,CAAC;QACrC,IAAI,CAAC,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;QACzB,IAAI,CAAC,MAAM,GAAG,OAAO,CAAC,M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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
|
}
|
|
@@ -7,34 +7,7 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
|
|
|
7
7
|
import { Injectable } from "@nestjs/common";
|
|
8
8
|
import { REQUEST as CTX_REQUEST_TOKEN, RESPONSE as CTX_RESPONSE_TOKEN, } from "./request-context.constants.js";
|
|
9
9
|
import { RequestContext } from "./request-context.js";
|
|
10
|
-
/**
|
|
11
|
-
* Express middleware that creates and manages request context for HTTP requests.
|
|
12
|
-
*
|
|
13
|
-
* This middleware:
|
|
14
|
-
* - Creates a new RequestContext for each incoming HTTP request
|
|
15
|
-
* - Uses the `x-request-id` header as the context ID if provided
|
|
16
|
-
* - Stores the Express request and response objects in the context
|
|
17
|
-
* - Maintains the context throughout the request lifecycle
|
|
18
|
-
*
|
|
19
|
-
* The middleware is automatically applied by RequestContextModule to all routes.
|
|
20
|
-
*
|
|
21
|
-
* @example Accessing request/response from context
|
|
22
|
-
* ```typescript
|
|
23
|
-
* import { RequestContext, REQUEST, RESPONSE } from '@nest-boot/request-context';
|
|
24
|
-
* import { Request, Response } from 'express';
|
|
25
|
-
*
|
|
26
|
-
* const req = RequestContext.get<Request>(REQUEST);
|
|
27
|
-
* const res = RequestContext.get<Response>(RESPONSE);
|
|
28
|
-
* ```
|
|
29
|
-
*/
|
|
30
10
|
let RequestContextMiddleware = class RequestContextMiddleware {
|
|
31
|
-
/**
|
|
32
|
-
* Processes an incoming HTTP request and establishes request context.
|
|
33
|
-
*
|
|
34
|
-
* @param req - The Express request object
|
|
35
|
-
* @param res - The Express response object
|
|
36
|
-
* @param next - The next middleware function
|
|
37
|
-
*/
|
|
38
11
|
async use(req, res, next) {
|
|
39
12
|
if (RequestContext.isActive()) {
|
|
40
13
|
next();
|
|
@@ -1 +1 @@
|
|
|
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;
|
|
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
2
|
import { RequestContextMiddleware } from "./request-context.middleware.js";
|
|
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
|
-
*/
|
|
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
|
}
|
|
@@ -12,50 +12,9 @@ import { Global, Module } from "@nestjs/common";
|
|
|
12
12
|
import { APP_INTERCEPTOR } from "@nestjs/core";
|
|
13
13
|
import { RequestContextInterceptor } from "./request-context.interceptor.js";
|
|
14
14
|
import { RequestContextMiddleware } from "./request-context.middleware.js";
|
|
15
|
-
/**
|
|
16
|
-
* NestJS module that provides request context functionality.
|
|
17
|
-
*
|
|
18
|
-
* This module automatically sets up request context for HTTP requests
|
|
19
|
-
* using both middleware (for Express) and interceptor (for GraphQL).
|
|
20
|
-
* It stores the request and response objects in the context and makes
|
|
21
|
-
* them available throughout the request lifecycle.
|
|
22
|
-
*
|
|
23
|
-
* The module is global, so it only needs to be imported once in the root module.
|
|
24
|
-
*
|
|
25
|
-
* @example
|
|
26
|
-
* ```typescript
|
|
27
|
-
* import { Module } from '@nestjs/common';
|
|
28
|
-
* import { RequestContextModule } from '@nest-boot/request-context';
|
|
29
|
-
*
|
|
30
|
-
* @Module({
|
|
31
|
-
* imports: [RequestContextModule],
|
|
32
|
-
* })
|
|
33
|
-
* export class AppModule {}
|
|
34
|
-
* ```
|
|
35
|
-
*
|
|
36
|
-
* @example Using in a service
|
|
37
|
-
* ```typescript
|
|
38
|
-
* import { Injectable } from '@nestjs/common';
|
|
39
|
-
* import { RequestContext, REQUEST } from '@nest-boot/request-context';
|
|
40
|
-
* import { Request } from 'express';
|
|
41
|
-
*
|
|
42
|
-
* @Injectable()
|
|
43
|
-
* export class MyService {
|
|
44
|
-
* getCurrentUser() {
|
|
45
|
-
* const req = RequestContext.get<Request>(REQUEST);
|
|
46
|
-
* return req?.user;
|
|
47
|
-
* }
|
|
48
|
-
* }
|
|
49
|
-
* ```
|
|
50
|
-
*/
|
|
51
15
|
let RequestContextModule = class RequestContextModule {
|
|
52
16
|
middlewareManager;
|
|
53
17
|
requestContextMiddleware;
|
|
54
|
-
/**
|
|
55
|
-
* Creates a new RequestContextModule instance.
|
|
56
|
-
* @param middlewareManager - Middleware manager for registering the context middleware
|
|
57
|
-
* @param requestContextMiddleware - The request context middleware instance
|
|
58
|
-
*/
|
|
59
18
|
constructor(middlewareManager, requestContextMiddleware) {
|
|
60
19
|
this.middlewareManager = middlewareManager;
|
|
61
20
|
this.requestContextMiddleware = requestContextMiddleware;
|