-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext.d.ts
More file actions
63 lines (61 loc) · 1.78 KB
/
context.d.ts
File metadata and controls
63 lines (61 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/**
* Represents the context for a single HTTP request.
* It holds all relevant information for the request lifecycle.
*/
export type RequestContext = {
/** The original, immutable Request object. */
readonly req: Readonly<Request>
/** The parsed URL of the request. */
readonly url: Readonly<URL>
/** A key-value store of cookies sent with the request. */
readonly cookies: Readonly<Record<string, string>>
/** A unique identifier for tracing the request through the system. */
readonly trace: number
/** An optional identifier for a specific span within a trace, for more granular performance monitoring. */
readonly span: number | undefined
}
/**
* A function that returns the current request's context.
*
* @example
* ```ts
* import { getContext } from '@01edu/context';
*
* const context = getContext();
* console.log(context.url.pathname);
* ```
*/
export type GetContext = () => RequestContext
/**
* A function that creates a new request context. Useful for testing or running background jobs.
*
* @example
* ```ts
* import { newContext } from '@01edu/context';
*
* const context = newContext('/users/123');
* console.log(context.url.pathname);
* ```
*/
export type NewContext = (
urlInit: string | URL,
extra?: Partial<RequestContext>,
) => RequestContext
/**
* A function that runs a callback within a specific request context.
* This is the foundation of how the context is managed across asynchronous operations.
*
* @example
* ```ts
* import { runContext, newContext } from '@01edu/context';
*
* const context = newContext('/');
* runContext(context, () => {
* // all code here will have access to `context` via `getContext()`
* });
* ```
*/
export type RunContext = <X = unknown>(
store: RequestContext,
cb: (store: RequestContext) => X,
) => X