-
Notifications
You must be signed in to change notification settings - Fork 4
/
add-context-config-defaults.ts
66 lines (62 loc) · 1.91 KB
/
add-context-config-defaults.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
import { hostname, platform } from 'os';
import { basename, extname } from 'path';
import { Request } from 'express';
import { merge } from 'lodash';
import {
ContextConfigType,
ContextName,
HttpContextRequestProperty as HttpProp,
} from '../interfaces';
import {
CONTEXT_BIN,
CONTEXT_CONTENT_TYPE,
CONTEXT_CORRELATION_ID,
CONTEXT_HOSTNAME,
CONTEXT_PLATFORM,
CONTEXT_PATH,
CONTEXT_PROTOCOL,
HEADER_CONTENT_TYPE,
HEADER_CORRELATION_ID,
CONTEXT_REQUEST_ID,
HEADER_REQUEST_ID,
} from '../constants';
// TODO JDM add stackdrive and information needed for cloud events
const createHttpContextDefaults = (config: Partial<ContextConfigType>) => {
const { header = HEADER_CORRELATION_ID } = config?.correlation_id ?? {};
const build = {
// TODO fallback must disappear for this context when we create other contexts !
[CONTEXT_REQUEST_ID]: [
`req.${HEADER_REQUEST_ID}`,
`req.${HttpProp.HEADERS}.${HEADER_REQUEST_ID}`,
],
[CONTEXT_CORRELATION_ID]: [`req.${HttpProp.HEADERS}.${header}`],
[CONTEXT_PLATFORM]: [platform()],
[CONTEXT_HOSTNAME]: [hostname(), 'req.hostname'],
[CONTEXT_BIN]: [
process.argv?.[1]
? basename(process.argv[1], extname(process.argv[1]))
: `${hostname()}_${process.argv?.[0] || null}`,
],
[CONTEXT_PATH]: [
(req: Request) =>
req?.baseUrl ? `${req.baseUrl}${req.path || '/'}` : '/',
],
[CONTEXT_PROTOCOL]: ['req.protocol'],
[CONTEXT_CONTENT_TYPE]: [`req.${HttpProp.HEADERS}.${HEADER_CONTENT_TYPE}`],
};
return {
build,
cached: false,
} as Partial<ContextConfigType>;
};
export const addContextConfigDefaults: (
config: Partial<ContextConfigType> & { type: ContextConfigType['type'] },
) => ContextConfigType = (config) => {
const { type } = config;
switch (type) {
case ContextName.HTTP:
return merge(createHttpContextDefaults(config), config);
default:
return config;
}
};