|
1 | 1 | import { Callback, Context } from 'aws-lambda'; |
2 | 2 | import Router from './Router'; |
3 | 3 | import { RequestEvent, HandlerContext } from './request-response-types'; |
4 | | -import { StringUnknownMap, Writable } from '@silvermine/toolbox'; |
| 4 | +import { isUndefined, StringUnknownMap, Writable } from '@silvermine/toolbox'; |
5 | 5 | import { Request, Response } from '.'; |
6 | | -import _ from 'underscore'; |
7 | 6 | import { isErrorWithStatusCode } from './interfaces'; |
8 | 7 |
|
9 | 8 | export default class Application extends Router { |
@@ -102,28 +101,26 @@ export default class Application extends Router { |
102 | 101 | } |
103 | 102 |
|
104 | 103 | private _createHandlerContext(context: Context): HandlerContext { |
105 | | - // keys should exist on both `HandlerContext` and `Context` |
106 | | - const keys: (keyof HandlerContext & keyof Context)[] = [ |
107 | | - 'functionName', 'functionVersion', 'invokedFunctionArn', 'memoryLimitInMB', |
108 | | - 'awsRequestId', 'logGroupName', 'logStreamName', 'identity', 'clientContext', |
109 | | - 'getRemainingTimeInMillis', |
110 | | - ]; |
111 | | - |
112 | | - let handlerContext: Writable<HandlerContext>; |
113 | | - |
114 | | - handlerContext = _.reduce(keys, (memo, key) => { |
115 | | - let contextValue = context[key]; |
| 104 | + const newContext: Writable<HandlerContext> = { |
| 105 | + functionName: context.functionName, |
| 106 | + functionVersion: context.functionVersion, |
| 107 | + invokedFunctionArn: context.invokedFunctionArn, |
| 108 | + memoryLimitInMB: context.memoryLimitInMB, |
| 109 | + awsRequestId: context.awsRequestId, |
| 110 | + logGroupName: context.logGroupName, |
| 111 | + logStreamName: context.logStreamName, |
| 112 | + getRemainingTimeInMillis: context.getRemainingTimeInMillis, |
| 113 | + }; |
| 114 | + |
| 115 | + if (!isUndefined(context.identity)) { |
| 116 | + newContext.identity = Object.freeze({ ...context.identity }); |
| 117 | + } |
116 | 118 |
|
117 | | - if (typeof contextValue === 'object' && contextValue) { |
118 | | - // Freeze sub-objects |
119 | | - memo[key] = Object.freeze(_.extend({}, contextValue)); |
120 | | - } else if (typeof contextValue !== 'undefined') { |
121 | | - memo[key] = contextValue; |
122 | | - } |
123 | | - return memo; |
124 | | - }, {} as Writable<HandlerContext>); |
| 119 | + if (!isUndefined(context.clientContext)) { |
| 120 | + newContext.clientContext = Object.freeze({ ...context.clientContext }); |
| 121 | + } |
125 | 122 |
|
126 | | - return Object.freeze(handlerContext); |
| 123 | + return Object.freeze(newContext); |
127 | 124 | } |
128 | 125 |
|
129 | 126 | } |
0 commit comments