@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
|
@@ -1,59 +1,3 @@
|
|
|
1
1
|
import { RequestContext } from "./request-context.js";
|
|
2
|
-
/**
|
|
3
|
-
* Helper type that extracts the argument types of a method.
|
|
4
|
-
*/
|
|
5
2
|
export type MethodArgs<T, M extends keyof T> = T[M] extends (...args: infer A) => any ? A : never;
|
|
6
|
-
/**
|
|
7
|
-
* Method decorator that wraps the method execution in a new request context.
|
|
8
|
-
*
|
|
9
|
-
* This is useful for creating request contexts for background jobs, event handlers,
|
|
10
|
-
* or any other code that runs outside of HTTP request handling.
|
|
11
|
-
*
|
|
12
|
-
* @typeParam T - The class type containing the method
|
|
13
|
-
* @typeParam P - The property key of the method
|
|
14
|
-
* @param fn - A function that creates the RequestContext, receiving the class instance and method arguments
|
|
15
|
-
* @returns A method decorator
|
|
16
|
-
*
|
|
17
|
-
* @example Basic usage with a job processor
|
|
18
|
-
* ```typescript
|
|
19
|
-
* import { CreateRequestContext, RequestContext } from '@nest-boot/request-context';
|
|
20
|
-
*
|
|
21
|
-
* class JobProcessor {
|
|
22
|
-
* @CreateRequestContext((instance, jobData) =>
|
|
23
|
-
* new RequestContext({ type: 'job', id: jobData.id })
|
|
24
|
-
* )
|
|
25
|
-
* async processJob(jobData: { id: string; payload: any }) {
|
|
26
|
-
* // This code runs within a request context
|
|
27
|
-
* console.log(`Processing job ${RequestContext.id}`);
|
|
28
|
-
* // ...
|
|
29
|
-
* }
|
|
30
|
-
* }
|
|
31
|
-
* ```
|
|
32
|
-
*
|
|
33
|
-
* @example With service injection
|
|
34
|
-
* ```typescript
|
|
35
|
-
* import { Injectable } from '@nestjs/common';
|
|
36
|
-
* import { CreateRequestContext, RequestContext } from '@nest-boot/request-context';
|
|
37
|
-
*
|
|
38
|
-
* @Injectable()
|
|
39
|
-
* class EventHandler {
|
|
40
|
-
* @CreateRequestContext((instance, event) =>
|
|
41
|
-
* new RequestContext({
|
|
42
|
-
* type: 'event',
|
|
43
|
-
* id: event.correlationId,
|
|
44
|
-
* })
|
|
45
|
-
* )
|
|
46
|
-
* async handleEvent(event: { correlationId: string; data: any }) {
|
|
47
|
-
* // Access context within the handler
|
|
48
|
-
* RequestContext.set('eventType', event.data.type);
|
|
49
|
-
* await this.processEvent(event);
|
|
50
|
-
* }
|
|
51
|
-
*
|
|
52
|
-
* private async processEvent(event: any) {
|
|
53
|
-
* // Context is still available here
|
|
54
|
-
* const eventType = RequestContext.get('eventType');
|
|
55
|
-
* }
|
|
56
|
-
* }
|
|
57
|
-
* ```
|
|
58
|
-
*/
|
|
59
3
|
export declare function CreateRequestContext<T extends object, P extends keyof T>(fn: (instance: T, ...args: MethodArgs<T, P>) => RequestContext): (_target: T, _propertyKey: P, descriptor: PropertyDescriptor) => PropertyDescriptor;
|
|
@@ -1,57 +1,4 @@
|
|
|
1
1
|
import { RequestContext } from "./request-context.js";
|
|
2
|
-
/**
|
|
3
|
-
* Method decorator that wraps the method execution in a new request context.
|
|
4
|
-
*
|
|
5
|
-
* This is useful for creating request contexts for background jobs, event handlers,
|
|
6
|
-
* or any other code that runs outside of HTTP request handling.
|
|
7
|
-
*
|
|
8
|
-
* @typeParam T - The class type containing the method
|
|
9
|
-
* @typeParam P - The property key of the method
|
|
10
|
-
* @param fn - A function that creates the RequestContext, receiving the class instance and method arguments
|
|
11
|
-
* @returns A method decorator
|
|
12
|
-
*
|
|
13
|
-
* @example Basic usage with a job processor
|
|
14
|
-
* ```typescript
|
|
15
|
-
* import { CreateRequestContext, RequestContext } from '@nest-boot/request-context';
|
|
16
|
-
*
|
|
17
|
-
* class JobProcessor {
|
|
18
|
-
* @CreateRequestContext((instance, jobData) =>
|
|
19
|
-
* new RequestContext({ type: 'job', id: jobData.id })
|
|
20
|
-
* )
|
|
21
|
-
* async processJob(jobData: { id: string; payload: any }) {
|
|
22
|
-
* // This code runs within a request context
|
|
23
|
-
* console.log(`Processing job ${RequestContext.id}`);
|
|
24
|
-
* // ...
|
|
25
|
-
* }
|
|
26
|
-
* }
|
|
27
|
-
* ```
|
|
28
|
-
*
|
|
29
|
-
* @example With service injection
|
|
30
|
-
* ```typescript
|
|
31
|
-
* import { Injectable } from '@nestjs/common';
|
|
32
|
-
* import { CreateRequestContext, RequestContext } from '@nest-boot/request-context';
|
|
33
|
-
*
|
|
34
|
-
* @Injectable()
|
|
35
|
-
* class EventHandler {
|
|
36
|
-
* @CreateRequestContext((instance, event) =>
|
|
37
|
-
* new RequestContext({
|
|
38
|
-
* type: 'event',
|
|
39
|
-
* id: event.correlationId,
|
|
40
|
-
* })
|
|
41
|
-
* )
|
|
42
|
-
* async handleEvent(event: { correlationId: string; data: any }) {
|
|
43
|
-
* // Access context within the handler
|
|
44
|
-
* RequestContext.set('eventType', event.data.type);
|
|
45
|
-
* await this.processEvent(event);
|
|
46
|
-
* }
|
|
47
|
-
*
|
|
48
|
-
* private async processEvent(event: any) {
|
|
49
|
-
* // Context is still available here
|
|
50
|
-
* const eventType = RequestContext.get('eventType');
|
|
51
|
-
* }
|
|
52
|
-
* }
|
|
53
|
-
* ```
|
|
54
|
-
*/
|
|
55
2
|
export function CreateRequestContext(fn) {
|
|
56
3
|
return (_target, _propertyKey, descriptor) => {
|
|
57
4
|
if (descriptor.value) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"create-request-context.decorator.js","sourceRoot":"","sources":["../src/create-request-context.decorator.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;
|
|
1
|
+
{"version":3,"file":"create-request-context.decorator.js","sourceRoot":"","sources":["../src/create-request-context.decorator.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAgEtD,MAAM,UAAU,oBAAoB,CAClC,EAA8D;IAE9D,OAAO,CAAC,OAAU,EAAE,YAAe,EAAE,UAA8B,EAAE,EAAE;QACrE,IAAI,UAAU,CAAC,KAAK,EAAE,CAAC;YACrB,MAAM,QAAQ,GAAG,UAAU,CAAC,KAAK,CAAC;YAElC,UAAU,CAAC,KAAK,GAAG,UAAmB,GAAG,IAAsB;gBAC7D,MAAM,GAAG,GAAG,EAAE,CAAC,IAAI,EAAE,GAAG,IAAI,CAAC,CAAC;gBAC9B,OAAO,cAAc,CAAC,GAAG,CAAC,GAAG,EAAE,GAAG,EAAE,CAAC,QAAQ,CAAC,KAAK,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC,CAAC;YACnE,CAAC,CAAC;QACJ,CAAC;QAED,OAAO,UAAU,CAAC;IACpB,CAAC,CAAC;AACJ,CAAC"}
|
package/dist/repl.d.ts
CHANGED
|
@@ -1,44 +1,3 @@
|
|
|
1
1
|
import { DynamicModule, Type } from "@nestjs/common";
|
|
2
2
|
import type { REPLServer } from "repl";
|
|
3
|
-
/**
|
|
4
|
-
* Starts a REPL (Read-Eval-Print Loop) session with request context support.
|
|
5
|
-
*
|
|
6
|
-
* This function creates a NestJS application context and starts an interactive
|
|
7
|
-
* REPL session where all commands run within a request context. This is useful
|
|
8
|
-
* for debugging and testing services that depend on request context.
|
|
9
|
-
*
|
|
10
|
-
* The REPL session:
|
|
11
|
-
* - Runs within a request context of type 'repl'
|
|
12
|
-
* - Has access to all NestJS providers
|
|
13
|
-
* - Maintains context across async operations
|
|
14
|
-
*
|
|
15
|
-
* @param module - The NestJS module (class or DynamicModule) to create the context from
|
|
16
|
-
* @returns A promise that resolves to the REPL server instance
|
|
17
|
-
*
|
|
18
|
-
* @example
|
|
19
|
-
* ```typescript
|
|
20
|
-
* // repl.ts
|
|
21
|
-
* import { repl } from '@nest-boot/request-context';
|
|
22
|
-
* import { AppModule } from './app.module';
|
|
23
|
-
*
|
|
24
|
-
* async function bootstrap() {
|
|
25
|
-
* await repl(AppModule);
|
|
26
|
-
* }
|
|
27
|
-
*
|
|
28
|
-
* bootstrap();
|
|
29
|
-
* ```
|
|
30
|
-
*
|
|
31
|
-
* @example Running the REPL
|
|
32
|
-
* ```bash
|
|
33
|
-
* npx ts-node -r tsconfig-paths/register repl.ts
|
|
34
|
-
* ```
|
|
35
|
-
*
|
|
36
|
-
* @example Using services in REPL
|
|
37
|
-
* ```typescript
|
|
38
|
-
* // In the REPL session:
|
|
39
|
-
* > const userService = get(UserService)
|
|
40
|
-
* > await userService.findAll()
|
|
41
|
-
* > RequestContext.id // Access current context ID
|
|
42
|
-
* ```
|
|
43
|
-
*/
|
|
44
3
|
export declare function repl(module: Type | DynamicModule): Promise<REPLServer>;
|
package/dist/repl.js
CHANGED
|
@@ -9,47 +9,6 @@ import { defineDefaultCommandsOnRepl } from "@nestjs/core/repl/repl-native-comma
|
|
|
9
9
|
import { AsyncResource } from "async_hooks";
|
|
10
10
|
import { Transform } from "stream";
|
|
11
11
|
import { RequestContext } from "./request-context.js";
|
|
12
|
-
/**
|
|
13
|
-
* Starts a REPL (Read-Eval-Print Loop) session with request context support.
|
|
14
|
-
*
|
|
15
|
-
* This function creates a NestJS application context and starts an interactive
|
|
16
|
-
* REPL session where all commands run within a request context. This is useful
|
|
17
|
-
* for debugging and testing services that depend on request context.
|
|
18
|
-
*
|
|
19
|
-
* The REPL session:
|
|
20
|
-
* - Runs within a request context of type 'repl'
|
|
21
|
-
* - Has access to all NestJS providers
|
|
22
|
-
* - Maintains context across async operations
|
|
23
|
-
*
|
|
24
|
-
* @param module - The NestJS module (class or DynamicModule) to create the context from
|
|
25
|
-
* @returns A promise that resolves to the REPL server instance
|
|
26
|
-
*
|
|
27
|
-
* @example
|
|
28
|
-
* ```typescript
|
|
29
|
-
* // repl.ts
|
|
30
|
-
* import { repl } from '@nest-boot/request-context';
|
|
31
|
-
* import { AppModule } from './app.module';
|
|
32
|
-
*
|
|
33
|
-
* async function bootstrap() {
|
|
34
|
-
* await repl(AppModule);
|
|
35
|
-
* }
|
|
36
|
-
*
|
|
37
|
-
* bootstrap();
|
|
38
|
-
* ```
|
|
39
|
-
*
|
|
40
|
-
* @example Running the REPL
|
|
41
|
-
* ```bash
|
|
42
|
-
* npx ts-node -r tsconfig-paths/register repl.ts
|
|
43
|
-
* ```
|
|
44
|
-
*
|
|
45
|
-
* @example Using services in REPL
|
|
46
|
-
* ```typescript
|
|
47
|
-
* // In the REPL session:
|
|
48
|
-
* > const userService = get(UserService)
|
|
49
|
-
* > await userService.findAll()
|
|
50
|
-
* > RequestContext.id // Access current context ID
|
|
51
|
-
* ```
|
|
52
|
-
*/
|
|
53
12
|
export async function repl(module) {
|
|
54
13
|
const app = await NestFactory.createApplicationContext(module, {
|
|
55
14
|
abortOnError: false,
|
package/dist/repl.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"repl.js","sourceRoot":"","sources":["../src/repl.ts"],"names":[],"mappings":"AAAA,OAAO,EAAiB,MAAM,EAAQ,MAAM,gBAAgB,CAAC;AAC7D,OAAO,EAAE,GAAG,EAAE,MAAM,sCAAsC,CAAC;AAC3D,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,C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AAC,OAAO,EAAE,WAAW,CAAC,WAAW,CAAC,CAAC;QAE5D,2BAA2B,CAAC,UAAU,CAAC,CAAC;QAExC,OAAO,UAAU,CAAC;IACpB,CAAC,CACF,CAAC;AACJ,CAAC"}
|
|
@@ -1,24 +1,2 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Token for storing the HTTP request object in the request context.
|
|
3
|
-
*
|
|
4
|
-
* @example
|
|
5
|
-
* ```typescript
|
|
6
|
-
* import { REQUEST } from '@nest-boot/request-context';
|
|
7
|
-
* import { Request } from 'express';
|
|
8
|
-
*
|
|
9
|
-
* const req = RequestContext.get<Request>(REQUEST);
|
|
10
|
-
* ```
|
|
11
|
-
*/
|
|
12
1
|
export declare const REQUEST = "REQUEST";
|
|
13
|
-
/**
|
|
14
|
-
* Token for storing the HTTP response object in the request context.
|
|
15
|
-
*
|
|
16
|
-
* @example
|
|
17
|
-
* ```typescript
|
|
18
|
-
* import { RESPONSE } from '@nest-boot/request-context';
|
|
19
|
-
* import { Response } from 'express';
|
|
20
|
-
*
|
|
21
|
-
* const res = RequestContext.get<Response>(RESPONSE);
|
|
22
|
-
* ```
|
|
23
|
-
*/
|
|
24
2
|
export declare const RESPONSE = "RESPONSE";
|
|
@@ -1,25 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Token for storing the HTTP request object in the request context.
|
|
3
|
-
*
|
|
4
|
-
* @example
|
|
5
|
-
* ```typescript
|
|
6
|
-
* import { REQUEST } from '@nest-boot/request-context';
|
|
7
|
-
* import { Request } from 'express';
|
|
8
|
-
*
|
|
9
|
-
* const req = RequestContext.get<Request>(REQUEST);
|
|
10
|
-
* ```
|
|
11
|
-
*/
|
|
12
1
|
export const REQUEST = "REQUEST";
|
|
13
|
-
/**
|
|
14
|
-
* Token for storing the HTTP response object in the request context.
|
|
15
|
-
*
|
|
16
|
-
* @example
|
|
17
|
-
* ```typescript
|
|
18
|
-
* import { RESPONSE } from '@nest-boot/request-context';
|
|
19
|
-
* import { Response } from 'express';
|
|
20
|
-
*
|
|
21
|
-
* const res = RequestContext.get<Response>(RESPONSE);
|
|
22
|
-
* ```
|
|
23
|
-
*/
|
|
24
2
|
export const RESPONSE = "RESPONSE";
|
|
25
3
|
//# sourceMappingURL=request-context.constants.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"request-context.constants.js","sourceRoot":"","sources":["../src/request-context.constants.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"request-context.constants.js","sourceRoot":"","sources":["../src/request-context.constants.ts"],"names":[],"mappings":"AAWA,MAAM,CAAC,MAAM,OAAO,GAAG,SAAS,CAAC;AAajC,MAAM,CAAC,MAAM,QAAQ,GAAG,UAAU,CAAC"}
|
|
@@ -1,316 +1,32 @@
|
|
|
1
1
|
import { type Type } from "@nestjs/common";
|
|
2
|
-
/**
|
|
3
|
-
* Middleware function type for request context.
|
|
4
|
-
* Middlewares are executed in order when running a request context.
|
|
5
|
-
*
|
|
6
|
-
* @typeParam T - The return type of the middleware chain
|
|
7
|
-
* @param ctx - The current request context
|
|
8
|
-
* @param next - Function to call the next middleware in the chain
|
|
9
|
-
* @returns A promise resolving to the result of the middleware chain
|
|
10
|
-
*/
|
|
11
2
|
export type RequestContextMiddlewareType = <T>(ctx: RequestContext, next: () => Promise<T>) => Promise<T>;
|
|
12
|
-
/**
|
|
13
|
-
* Options for creating a new RequestContext instance.
|
|
14
|
-
*/
|
|
15
3
|
export interface RequestContextCreateOptions {
|
|
16
|
-
/**
|
|
17
|
-
* Unique identifier for the request context.
|
|
18
|
-
* If not provided, a random UUID will be generated.
|
|
19
|
-
*/
|
|
20
4
|
id?: string;
|
|
21
|
-
/**
|
|
22
|
-
* The type of context (e.g., 'http', 'graphql', 'repl', 'job').
|
|
23
|
-
*/
|
|
24
5
|
type: string;
|
|
25
|
-
/**
|
|
26
|
-
* Parent context for creating nested/child contexts.
|
|
27
|
-
* Child contexts can access values from parent contexts.
|
|
28
|
-
*/
|
|
29
6
|
parent?: RequestContext;
|
|
30
7
|
}
|
|
31
|
-
/**
|
|
32
|
-
* RequestContext provides a way to store and access request-scoped data
|
|
33
|
-
* throughout the lifecycle of a request using AsyncLocalStorage.
|
|
34
|
-
*
|
|
35
|
-
* This is useful for storing data like the current user, request ID,
|
|
36
|
-
* database transactions, and other request-specific information that
|
|
37
|
-
* needs to be accessed across different parts of the application.
|
|
38
|
-
*
|
|
39
|
-
* @example Basic usage
|
|
40
|
-
* ```typescript
|
|
41
|
-
* import { RequestContext } from '@nest-boot/request-context';
|
|
42
|
-
*
|
|
43
|
-
* // Get the current request ID
|
|
44
|
-
* const requestId = RequestContext.id;
|
|
45
|
-
*
|
|
46
|
-
* // Store a value in the context
|
|
47
|
-
* RequestContext.set('userId', 123);
|
|
48
|
-
*
|
|
49
|
-
* // Retrieve a value from the context
|
|
50
|
-
* const userId = RequestContext.get<number>('userId');
|
|
51
|
-
* ```
|
|
52
|
-
*
|
|
53
|
-
* @example Running code in a new context
|
|
54
|
-
* ```typescript
|
|
55
|
-
* await RequestContext.run(
|
|
56
|
-
* new RequestContext({ type: 'job' }),
|
|
57
|
-
* async (ctx) => {
|
|
58
|
-
* ctx.set('jobId', 'abc123');
|
|
59
|
-
* await processJob();
|
|
60
|
-
* }
|
|
61
|
-
* );
|
|
62
|
-
* ```
|
|
63
|
-
*
|
|
64
|
-
* @example Creating a child context
|
|
65
|
-
* ```typescript
|
|
66
|
-
* await RequestContext.child(async (childCtx) => {
|
|
67
|
-
* // Child context inherits values from parent
|
|
68
|
-
* // but can have its own values that don't affect parent
|
|
69
|
-
* childCtx.set('tempValue', 'only in child');
|
|
70
|
-
* });
|
|
71
|
-
* ```
|
|
72
|
-
*/
|
|
73
8
|
export declare class RequestContext {
|
|
74
|
-
/**
|
|
75
|
-
* Unique identifier for this request context.
|
|
76
|
-
* Automatically generated as a UUID if not provided.
|
|
77
|
-
*/
|
|
78
9
|
readonly id: string;
|
|
79
|
-
/**
|
|
80
|
-
* The type of this context (e.g., 'http', 'graphql', 'repl', 'job').
|
|
81
|
-
*/
|
|
82
10
|
readonly type: string;
|
|
83
|
-
/**
|
|
84
|
-
* Parent context, if this is a child context.
|
|
85
|
-
* Values not found in this context will be looked up in the parent.
|
|
86
|
-
*/
|
|
87
11
|
readonly parent?: RequestContext;
|
|
88
|
-
/** Internal storage map for context values. @internal */
|
|
89
12
|
private readonly container;
|
|
90
|
-
/** Async local storage backing the request context. @internal */
|
|
91
13
|
private static readonly storage;
|
|
92
|
-
/** Registered middleware map keyed by name. @internal */
|
|
93
14
|
private static readonly middlewares;
|
|
94
|
-
/** Dependency graph for middleware ordering. @internal */
|
|
95
15
|
private static readonly middlewareDependencies;
|
|
96
|
-
/** Topologically-sorted middleware execution stack. @internal */
|
|
97
16
|
private static middlewaresStack;
|
|
98
|
-
/** Creates a new RequestContext instance.
|
|
99
|
-
* @param options - Options for creating the request context (id, type, parent)
|
|
100
|
-
*/
|
|
101
17
|
constructor(options: RequestContextCreateOptions);
|
|
102
|
-
/**
|
|
103
|
-
* Gets a value from the context by its token.
|
|
104
|
-
* If not found in this context, looks up the parent context.
|
|
105
|
-
*
|
|
106
|
-
* @typeParam T - The expected type of the value
|
|
107
|
-
* @param token - The key to look up (string, symbol, function, or class)
|
|
108
|
-
* @returns The value if found, otherwise undefined
|
|
109
|
-
*
|
|
110
|
-
* @example
|
|
111
|
-
* ```typescript
|
|
112
|
-
* const ctx = RequestContext.current();
|
|
113
|
-
* const user = ctx.get<User>('currentUser');
|
|
114
|
-
* const service = ctx.get(MyService);
|
|
115
|
-
* ```
|
|
116
|
-
*/
|
|
117
18
|
get<T>(token: string | symbol | Function | Type<T>): T | undefined;
|
|
118
|
-
/**
|
|
119
|
-
* Sets a value in the context.
|
|
120
|
-
*
|
|
121
|
-
* @typeParam T - The type of the value
|
|
122
|
-
* @param typeOrToken - The key to store the value under
|
|
123
|
-
* @param value - The value to store
|
|
124
|
-
*
|
|
125
|
-
* @example
|
|
126
|
-
* ```typescript
|
|
127
|
-
* const ctx = RequestContext.current();
|
|
128
|
-
* ctx.set('userId', 123);
|
|
129
|
-
* ctx.set(UserService, userServiceInstance);
|
|
130
|
-
* ```
|
|
131
|
-
*/
|
|
132
19
|
set<T>(typeOrToken: string | symbol | Type<T>, value: T): void;
|
|
133
|
-
/**
|
|
134
|
-
* Gets a value from the context, or sets it if not present.
|
|
135
|
-
*
|
|
136
|
-
* @typeParam T - The type of the value
|
|
137
|
-
* @param typeOrToken - The key to look up or store under
|
|
138
|
-
* @param value - The value to set if not already present
|
|
139
|
-
* @returns The existing value or the newly set value
|
|
140
|
-
*
|
|
141
|
-
* @example
|
|
142
|
-
* ```typescript
|
|
143
|
-
* const ctx = RequestContext.current();
|
|
144
|
-
* const cache = ctx.getOrSet('cache', new Map());
|
|
145
|
-
* ```
|
|
146
|
-
*/
|
|
147
20
|
getOrSet<T>(typeOrToken: string | symbol | Type<T>, value: T): T;
|
|
148
|
-
/**
|
|
149
|
-
* Gets a value from the current context by its key.
|
|
150
|
-
* Static method that accesses the current context automatically.
|
|
151
|
-
*
|
|
152
|
-
* @typeParam T - The expected type of the value
|
|
153
|
-
* @param key - The key to look up
|
|
154
|
-
* @returns The value if found, otherwise undefined
|
|
155
|
-
* @throws Error if no request context is active
|
|
156
|
-
*
|
|
157
|
-
* @example
|
|
158
|
-
* ```typescript
|
|
159
|
-
* const userId = RequestContext.get<number>('userId');
|
|
160
|
-
* ```
|
|
161
|
-
*/
|
|
162
21
|
static get<T>(key: string | symbol | Function | Type<T>): T | undefined;
|
|
163
|
-
/**
|
|
164
|
-
* Sets a value in the current context.
|
|
165
|
-
* Static method that accesses the current context automatically.
|
|
166
|
-
*
|
|
167
|
-
* @typeParam T - The type of the value
|
|
168
|
-
* @param key - The key to store the value under
|
|
169
|
-
* @param value - The value to store
|
|
170
|
-
* @throws Error if no request context is active
|
|
171
|
-
*
|
|
172
|
-
* @example
|
|
173
|
-
* ```typescript
|
|
174
|
-
* RequestContext.set('userId', 123);
|
|
175
|
-
* ```
|
|
176
|
-
*/
|
|
177
22
|
static set<T>(key: string | symbol | Type<T>, value: T): void;
|
|
178
|
-
/**
|
|
179
|
-
* Gets a value from the current context, or sets it if not present.
|
|
180
|
-
* Static method that accesses the current context automatically.
|
|
181
|
-
*
|
|
182
|
-
* @typeParam T - The type of the value
|
|
183
|
-
* @param key - The key to look up or store under
|
|
184
|
-
* @param value - The value to set if not already present
|
|
185
|
-
* @returns The existing value or the newly set value
|
|
186
|
-
* @throws Error if no request context is active
|
|
187
|
-
*
|
|
188
|
-
* @example
|
|
189
|
-
* ```typescript
|
|
190
|
-
* const cache = RequestContext.getOrSet('cache', new Map());
|
|
191
|
-
* ```
|
|
192
|
-
*/
|
|
193
23
|
static getOrSet<T>(key: string | symbol | Type<T>, value: T): T;
|
|
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
24
|
static get id(): string;
|
|
206
|
-
/**
|
|
207
|
-
* Gets the current request context.
|
|
208
|
-
*
|
|
209
|
-
* @returns The current RequestContext instance
|
|
210
|
-
* @throws Error if no request context is active
|
|
211
|
-
*
|
|
212
|
-
* @example
|
|
213
|
-
* ```typescript
|
|
214
|
-
* const ctx = RequestContext.current();
|
|
215
|
-
* console.log(ctx.type); // 'http'
|
|
216
|
-
* ```
|
|
217
|
-
*/
|
|
218
25
|
static current(): RequestContext;
|
|
219
|
-
/**
|
|
220
|
-
* Checks if a request context is currently active.
|
|
221
|
-
*
|
|
222
|
-
* @returns true if a context is active, false otherwise
|
|
223
|
-
*
|
|
224
|
-
* @example
|
|
225
|
-
* ```typescript
|
|
226
|
-
* if (RequestContext.isActive()) {
|
|
227
|
-
* const userId = RequestContext.get('userId');
|
|
228
|
-
* }
|
|
229
|
-
* ```
|
|
230
|
-
*/
|
|
231
26
|
static isActive(): boolean;
|
|
232
|
-
/**
|
|
233
|
-
* Runs a callback within a request context.
|
|
234
|
-
* All registered middlewares are executed before the callback.
|
|
235
|
-
*
|
|
236
|
-
* @typeParam T - The return type of the callback
|
|
237
|
-
* @param ctx - The request context to run within
|
|
238
|
-
* @param callback - The function to execute within the context
|
|
239
|
-
* @returns A promise resolving to the callback's return value
|
|
240
|
-
*
|
|
241
|
-
* @example
|
|
242
|
-
* ```typescript
|
|
243
|
-
* const result = await RequestContext.run(
|
|
244
|
-
* new RequestContext({ type: 'job' }),
|
|
245
|
-
* async (ctx) => {
|
|
246
|
-
* ctx.set('jobId', 'abc123');
|
|
247
|
-
* return await processJob();
|
|
248
|
-
* }
|
|
249
|
-
* );
|
|
250
|
-
* ```
|
|
251
|
-
*/
|
|
252
27
|
static run<T>(ctx: RequestContext, callback: (ctx: RequestContext) => T | Promise<T>): Promise<T>;
|
|
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
28
|
static child<T>(callback: (ctx: RequestContext) => T | Promise<T>): Promise<T>;
|
|
281
|
-
/**
|
|
282
|
-
* Registers a middleware to be executed when running a request context.
|
|
283
|
-
* Middlewares are executed in dependency order.
|
|
284
|
-
*
|
|
285
|
-
* @param name - Unique name for the middleware
|
|
286
|
-
* @param middleware - The middleware function to register
|
|
287
|
-
* @param dependencies - Names of middlewares that must run before this one
|
|
288
|
-
*
|
|
289
|
-
* @example
|
|
290
|
-
* ```typescript
|
|
291
|
-
* RequestContext.registerMiddleware(
|
|
292
|
-
* 'auth',
|
|
293
|
-
* async (ctx, next) => {
|
|
294
|
-
* ctx.set('user', await loadUser());
|
|
295
|
-
* return next();
|
|
296
|
-
* }
|
|
297
|
-
* );
|
|
298
|
-
*
|
|
299
|
-
* // Middleware with dependencies
|
|
300
|
-
* RequestContext.registerMiddleware(
|
|
301
|
-
* 'permissions',
|
|
302
|
-
* async (ctx, next) => {
|
|
303
|
-
* const user = ctx.get('user');
|
|
304
|
-
* ctx.set('permissions', await loadPermissions(user));
|
|
305
|
-
* return next();
|
|
306
|
-
* },
|
|
307
|
-
* ['auth'] // Runs after 'auth' middleware
|
|
308
|
-
* );
|
|
309
|
-
* ```
|
|
310
|
-
*/
|
|
311
29
|
static registerMiddleware(name: string, middleware: RequestContextMiddlewareType, dependencies?: string[]): void;
|
|
312
|
-
/** Resolves middleware dependencies via topological sort. @internal */
|
|
313
30
|
private static resolveDependencies;
|
|
314
|
-
/** Rebuilds the middleware execution stack after registration changes. @internal */
|
|
315
31
|
private static generateMiddlewaresStack;
|
|
316
32
|
}
|
|
@@ -1,35 +1,5 @@
|
|
|
1
1
|
import { type CallHandler, type ExecutionContext, type NestInterceptor } from "@nestjs/common";
|
|
2
2
|
import { Observable } from "rxjs";
|
|
3
|
-
/**
|
|
4
|
-
* NestJS interceptor that creates request context for HTTP and GraphQL requests.
|
|
5
|
-
*
|
|
6
|
-
* This interceptor serves as a fallback for cases where the middleware doesn't
|
|
7
|
-
* run (e.g., GraphQL resolvers). It:
|
|
8
|
-
* - Creates a new RequestContext if one doesn't already exist
|
|
9
|
-
* - Uses the `x-request-id` header as the context ID if provided
|
|
10
|
-
* - Supports both HTTP and GraphQL execution contexts
|
|
11
|
-
*
|
|
12
|
-
* The interceptor is automatically registered by RequestContextModule.
|
|
13
|
-
*
|
|
14
|
-
* @example
|
|
15
|
-
* The interceptor is typically used automatically, but can be applied manually:
|
|
16
|
-
* ```typescript
|
|
17
|
-
* import { Controller, UseInterceptors } from '@nestjs/common';
|
|
18
|
-
* import { RequestContextInterceptor } from '@nest-boot/request-context';
|
|
19
|
-
*
|
|
20
|
-
* @Controller()
|
|
21
|
-
* @UseInterceptors(RequestContextInterceptor)
|
|
22
|
-
* export class MyController {}
|
|
23
|
-
* ```
|
|
24
|
-
*/
|
|
25
3
|
export declare class RequestContextInterceptor implements NestInterceptor {
|
|
26
|
-
/**
|
|
27
|
-
* Intercepts the request and wraps execution in a request context.
|
|
28
|
-
*
|
|
29
|
-
* @typeParam T - The type of the response
|
|
30
|
-
* @param executionContext - The NestJS execution context
|
|
31
|
-
* @param next - The call handler for the next interceptor or handler
|
|
32
|
-
* @returns An observable of the response
|
|
33
|
-
*/
|
|
34
4
|
intercept<T>(executionContext: ExecutionContext, next: CallHandler<T>): Observable<T>;
|
|
35
5
|
}
|