-
Notifications
You must be signed in to change notification settings - Fork 14
/
dvelop-context-middleware.ts
102 lines (93 loc) · 3.23 KB
/
dvelop-context-middleware.ts
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import {
DVELOP_REQUEST_ID_HEADER,
DVELOP_REQUEST_SIGNATURE_HEADER,
DVELOP_SYSTEM_BASE_URI_HEADER,
DVELOP_TENANT_ID_HEADER,
TRACEPARENT_HEADER,
TraceContext,
parseTraceparentHeader,
generateTraceContext
} from "@dvelop-sdk/core";
import { Request, Response, NextFunction } from "express";
/**
* Factory for the {@link contextMiddleware}-function. See [Advanced Topics](https://github.com/d-velop/dvelop-sdk-node#advanced-topics) for more information.
* @internal
* @category Core
*/
export function contextMiddlewareFactory(
parseTraceparentHeader: (traceparentHeader: string) => TraceContext,
generateTraceContext: () => TraceContext,
systemBaseUri?: string,
tenantId?: string
): (req: Request, _: Response, next: NextFunction) => void {
return (req: Request, _: Response, next: NextFunction) => {
if (systemBaseUri) {
req.dvelopContext = {
systemBaseUri: systemBaseUri,
tenantId: tenantId || req.header(DVELOP_TENANT_ID_HEADER),
requestId: req.header(DVELOP_REQUEST_ID_HEADER)
}
} else {
req.dvelopContext = {
systemBaseUri: req.header(DVELOP_SYSTEM_BASE_URI_HEADER),
tenantId: req.header(DVELOP_TENANT_ID_HEADER),
requestId: req.header(DVELOP_REQUEST_ID_HEADER),
requestSignature: req.header(DVELOP_REQUEST_SIGNATURE_HEADER),
};
}
const traceparentHeader = req.header(TRACEPARENT_HEADER);
if (traceparentHeader) {
try {
req.dvelopContext.traceContext = parseTraceparentHeader(traceparentHeader);
} catch (e: any) {
req.dvelopContext.traceContext = generateTraceContext();
}
} else {
req.dvelopContext.traceContext = generateTraceContext();
}
next();
};
}
/**
* Sets a {@link DvelopContext} to the express-request-object. Accessable via the ```req.dvelopContext```-property.
*
* ```typescript
* import { contextMiddleware } from "@dvelop-sdk/express-utils";
*
* app.use(contextMiddleware);
*
* app.use((req: Request, _: Response, next: NextFunction) => {
* console.log(req.dvelopContext);
* next();
* });
* ```
*
* @category Middleware
*/
/* istanbul ignore next */
export function contextMiddleware(req: Request, _: Response, next: NextFunction): void {
return contextMiddlewareFactory(parseTraceparentHeader, generateTraceContext)(req, _, next);
}
/**
* Sets a {@link DvelopContext} to the express-request-object. Accessable via the ```req.dvelopContext```-property.
* This is a version with a fixed systemBaseUri, primarily used on premise.
*
* ```typescript
* import { contextMiddlewareFactoryWithFixedSystemBaseUri } from "@dvelop-sdk/express-utils";
*
* app.use(contextMiddlewareFactoryWithFixedSystemBaseUri("https://my.local.baseuri")); //could optionally supply a tenantId (default: 0)
*
* app.use((req: Request, _: Response, next: NextFunction) => {
* console.log(req.dvelopContext);
* next();
* });
* ```
*
* @category Middleware
*/
/* istanbul ignore next */
export function contextMiddlewareFactoryWithFixedSystemBaseUri(systemBaseUri: string, tenantId: string = "0") {
return (req: Request, _: Response, next: NextFunction) => {
return contextMiddlewareFactory(parseTraceparentHeader, generateTraceContext, systemBaseUri, tenantId)(req, _, next);
}
}