Skip to content

Commit c05f48d

Browse files
authored
fix: relax ContextMap value constraint (#61)
1 parent 6dc8a96 commit c05f48d

File tree

5 files changed

+30
-11
lines changed

5 files changed

+30
-11
lines changed

src/context.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@ const CONTEXT_SYMBOL = Symbol.for('aws.lambda.runtime.context');
33

44
export interface ContextMap {
55
awsRequestId: string;
6-
[key: string]: string;
6+
[key: string]: unknown;
77
}
88

99
export interface ContextStorageProvider {
1010
getContext: () => ContextMap;
1111
setContext: (map: ContextMap) => void;
12-
updateContext: (values: Record<string, string>) => void;
12+
updateContext: (values: Record<string, unknown>) => void;
1313
}
1414

1515
export const GlobalContextStorageProvider: ContextStorageProvider = {
1616
getContext: () => (global as any)[CONTEXT_SYMBOL] as ContextMap,
1717
setContext: (map: ContextMap) => ((global as any)[CONTEXT_SYMBOL] = map),
18-
updateContext: (values: Record<string, string>) => {
18+
updateContext: (values: Record<string, unknown>) => {
1919
const ctx = GlobalContextStorageProvider.getContext();
2020
(global as any)[CONTEXT_SYMBOL] = {
2121
...ctx,

src/request.ts

+5-7
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,16 @@ export const lambdaRequestTracker = (
2626

2727
// capture any correlation headers sent from upstream callers
2828
if (event.headers) {
29-
Object.keys(event.headers).forEach((header) => {
29+
for (const [header, value] of Object.entries(event.headers)) {
3030
if (header.toLowerCase().startsWith(CORRELATION_HEADER)) {
31-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
32-
ctx[header] = event.headers![header] as string;
31+
ctx[header] = value;
3332
}
34-
});
33+
}
3534
}
3635

3736
// capture the xray trace id if its enabled
3837
if (process.env[AMAZON_TRACE_ID]) {
39-
ctx[CORRELATION_TRACE_ID] = process.env[AMAZON_TRACE_ID] as string;
38+
ctx[CORRELATION_TRACE_ID] = process.env[AMAZON_TRACE_ID];
4039
}
4140

4241
// set the correlation id if not already set by upstream callers
@@ -49,9 +48,8 @@ export const lambdaRequestTracker = (
4948
if (options.requestMixin) {
5049
const result = options.requestMixin(event, context);
5150
for (const key in result) {
52-
// Cast this to string for typescript
5351
// when the JSON serializer runs, by default it omits undefined properties
54-
ctx[key] = result[key] as string;
52+
ctx[key] = result[key];
5553
}
5654
}
5755

src/tests/index.spec.ts

+17
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,23 @@ tap.test('should allow removing default request data', (t) => {
180180
t.end();
181181
});
182182

183+
tap.test('should allow different types of context values', (t) => {
184+
const { log, output, withRequest } = createLogger(undefined, {
185+
requestMixin: () => ({
186+
'number': 12,
187+
'boolean': true,
188+
'object': {
189+
foo: "bar"
190+
}
191+
}),
192+
});
193+
194+
withRequest({}, { awsRequestId: '431234' });
195+
log.info('Message with trace ID');
196+
t.matchSnapshot(output.buffer);
197+
t.end();
198+
});
199+
183200
tap.test('should allow default pino formatter', (t) => {
184201
const { log, output, withRequest } = createLogger(undefined, {
185202
formatter: new PinoLogFormatter(),

src/types.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export interface LambdaRequestTrackerOptions {
7070
requestMixin?: (
7171
event: LambdaEvent,
7272
context: LambdaContext,
73-
) => { [key: string]: string | undefined };
73+
) => { [key: string]: unknown };
7474
/**
7575
* Custom storage provider.
7676
* If not supplied, defaults to global context storage.

tap-snapshots/src/tests/index.spec.ts.test.cjs

+4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ exports[`src/tests/index.spec.ts TAP should allow default pino formatter > must
1717
{"awsRequestId":"431234","x-correlation-id":"431234","level":30,"time":1480572000000,"msg":"Message with pino formatter"}
1818
`
1919

20+
exports[`src/tests/index.spec.ts TAP should allow different types of context values > must match snapshot 1`] = `
21+
2016-12-01T06:00:00.000Z 431234 INFO Message with trace ID {"awsRequestId":"431234","x-correlation-id":"431234","number":12,"boolean":true,"object":{"foo":"bar"},"level":30,"time":1480572000000,"msg":"Message with trace ID"}
22+
`
23+
2024
exports[`src/tests/index.spec.ts TAP should allow modifying the global context > must match snapshot 1`] = `
2125
2016-12-01T06:00:00.000Z 98875 ERROR Context updates {"awsRequestId":"98875","x-correlation-data":"abbb","x-correlation-id":"98875","userId":"12","level":50,"time":1480572000000,"msg":"Context updates"}
2226
`

0 commit comments

Comments
 (0)