diff --git a/collectInvocation.js b/collectInvocation.js index 4a11b0a..2cbb480 100644 --- a/collectInvocation.js +++ b/collectInvocation.js @@ -14,7 +14,7 @@ export default function collectInvocation(event, context, handlerType) { const ctx = {}; if (context) { - for (const [key, value] of Object.entries(context)) { + const handle = (key, value) => { if (typeof value === 'function') { try { ctx[key] = value.call(context); @@ -24,6 +24,20 @@ export default function collectInvocation(event, context, handlerType) { } else { ctx[key] = value; } + }; + + for (const [key, value] of Object.entries(context)) { + handle(key, value); + } + + const proto = Object.getPrototypeOf(context); + if (proto && proto !== Object.prototype) { + for (const key of Reflect.ownKeys(proto)) { + if (key === 'constructor' || Object.prototype.hasOwnProperty.call(ctx, key)) { + continue; + } + handle(key, context[key]); + } } } return { event, context: ctx, handlerType }; diff --git a/tests/collectInvocation.test.js b/tests/collectInvocation.test.js index 26fc4bb..2fbe580 100644 --- a/tests/collectInvocation.test.js +++ b/tests/collectInvocation.test.js @@ -41,4 +41,28 @@ describe('collectInvocation', () => { handlerType: 'test' }); }); + + test('captures prototype methods', () => { + const event = {}; + class ProtoCtx { + protoMethod() { return 'proto'; } + protoFail() { throw new Error('oops'); } + } + const context = new ProtoCtx(); + context.awsRequestId = 'req-2'; + context.own = () => 'own'; + + const result = collectInvocation(event, context, 'test'); + + expect(result).toEqual({ + event, + context: { + awsRequestId: 'req-2', + own: 'own', + protoMethod: 'proto', + protoFail: 'oops' + }, + handlerType: 'test' + }); + }); });