Skip to content

Commit e3972c5

Browse files
author
Jun-Zhe Lai
authored
Merge pull request spring-media#12 from spring-media/passthrough-context
Context Object is available now
2 parents f3fd3f8 + 72b4e5f commit e3972c5

File tree

9 files changed

+108
-48
lines changed

9 files changed

+108
-48
lines changed

README.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ exports.handler = router.handler(
2626
// activate CORS on all http-methods (OPTIONS requests are handled automagically);
2727
// if set to true, these default headers will be sent on every response:
2828
// "Access-Control-Allow-Origin" = "'*'"
29-
// "Access-Control-Allow-Methods" = "'GET,POST,PUT,DELETE,HEAD'"
29+
// "Access-Control-Allow-Methods" = "'GET,POST,PUT,DELETE,HEAD,PATCH'"
3030
// "Access-Control-Allow-Headers" = "'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token'"
3131
cors: true,
3232
routes: [
@@ -36,19 +36,19 @@ exports.handler = router.handler(
3636
// http method to match
3737
method: 'POST',
3838
// provide a function to be called with the propriate data
39-
action: request=>doAnything(request.body)
39+
action: (request, context) => doAnything(request.body)
4040
},
4141
{
4242
// request-path-pattern with a path variable:
4343
path: '/article/:id',
4444
method: 'GET',
4545
// we can use the path param 'id' in the action call:
46-
action: request=>getSomething(request.paths.id)
46+
action: (request, context) => getSomething(request.paths.id)
4747
},
4848
{
4949
path: '/:id',
5050
method: 'DELETE',
51-
action: request=>deleteSomething(request.paths.id)
51+
action: (request, context) => deleteSomething(request.paths.id)
5252
}
5353
],
5454
debug: true,
@@ -68,7 +68,7 @@ exports.handler = router.handler(
6868
// a regex to match the content of the SNS-Subject:
6969
subject: /.*/,
7070
// Attention: the message is JSON-stringified
71-
action: sns => service.doSomething(JSON.parse(sns.Message))
71+
action: (sns, context) => service.doSomething(JSON.parse(sns.Message))
7272
}
7373
]
7474
}
@@ -101,13 +101,14 @@ return {
101101

102102
## local developement
103103

104-
The best is to work with ```npm link```
104+
The best is to work with ```yarn link```
105105

106-
See here: http://vansande.org/2015/03/20/npm-link/
106+
See here: https://yarnpkg.com/en/docs/cli/link
107107

108108

109109
## Release History
110110

111+
* 0.4.0 now [the Context Object](https://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-handler.html) pass through
111112
* 0.3.1 proxyIntegration: avoid error if response object is not set; add some debug logging
112113
* 0.3.0 proxyIntegration: add PATCH method; allow for custom status codes from route (thanks to [@mintuz](https://github.com/mintuz))
113114
* 0.2.2 proxyIntegration: set correct header values now for CORS

gulpfile.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ const gulp = require('gulp');
22
const del = require('del');
33
const install = require('gulp-install');
44
const jasmine = require('gulp-jasmine');
5-
const reporters = require('jasmine-reporters');
65

76
gulp.task('test', () =>
87
gulp.src('test/*.spec.js')

index.d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
// Type definitions for aws-lambda-router
2-
// Project: github.com/spring-mediaof 'RouteConfig/aws-lambda-router
2+
// Project: github.com/spring-media/aws-lambda-router
33

44
export function handler(routeConfig: RouteConfig): any;
55

66
export interface ProxyIntegrationRoute {
77
path: string;
88
method: string;
9-
action?: (request: any) => any;
9+
action?: (request: any, context: any) => any;
1010
}
1111

1212
export interface ProxyIntegrationConfig {
@@ -19,7 +19,7 @@ export interface ProxyIntegrationConfig {
1919

2020
export interface SnsRoute {
2121
subject: any;
22-
action?: (sns: any) => any;
22+
action?: (sns: any, context: any) => any;
2323
}
2424

2525
export interface SnsConfig {

index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ function handler(routeConfig) {
55

66
return (event, context, callback) => {
77
if (routeConfig.debug) {
8-
console.log("Lambda invoked with request:", event)
8+
console.log("Lambda invoked with request:", event);
9+
console.log("Lambda invoked with context:", context);
910
}
1011

1112
for (const eventProcessorName of eventProcessorMapping.keys()) {
@@ -18,7 +19,7 @@ function handler(routeConfig) {
1819
// - throws Error: the 'error.toString()' is taken as the error message of processing the event
1920
// - returns object: this is taken as the result of processing the event
2021
// - returns promise: when the promise is resolved, this is taken as the result of processing the event
21-
const result = eventProcessorMapping.get(eventProcessorName)(routeConfig[eventProcessorName], event);
22+
const result = eventProcessorMapping.get(eventProcessorName)(routeConfig[eventProcessorName], event, context);
2223
if (result) {
2324
// be resilient against a processor returning a value instead of a promise:
2425
return Promise.resolve(result)

lib/proxyIntegration.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ function addCorsHeaders(toAdd) {
1414
return toAdd;
1515
}
1616

17-
function processActionAndReturn(actionConfig, event, headers, errorMapping) {
18-
return Promise.resolve(actionConfig.action(event)).then(res => {
17+
function processActionAndReturn(actionConfig, event, context, headers, errorMapping) {
18+
return Promise.resolve(actionConfig.action(event, context)).then(res => {
1919

2020
if (res && res.body) {
2121
const mergedHeaders = Object.assign({}, headers, res.headers);
@@ -41,10 +41,11 @@ function processActionAndReturn(actionConfig, event, headers, errorMapping) {
4141
});
4242
}
4343

44-
function process(proxyIntegrationConfig, event) {
44+
function process(proxyIntegrationConfig, event, context) {
4545
if (proxyIntegrationConfig.debug) {
46-
console.log("Lambda proxyIntegrationConfig: ", proxyIntegrationConfig)
47-
console.log("Lambda request: ", event)
46+
console.log("Lambda proxyIntegrationConfig: ", proxyIntegrationConfig);
47+
console.log("Lambda event: ", event);
48+
console.log("Lambda context: ", context);
4849
}
4950

5051
//validate config
@@ -97,7 +98,7 @@ function process(proxyIntegrationConfig, event) {
9798
});
9899
}
99100
}
100-
return processActionAndReturn(actionConfig, event, headers, errorMapping);
101+
return processActionAndReturn(actionConfig, event, context, headers, errorMapping);
101102
} catch (error) {
102103
console.log('Error while evaluating matching action handler', error);
103104
return Promise.resolve(convertError(error, errorMapping, headers));

lib/sns.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
"use strict";
22

3-
function process(snsConfig, event) {
3+
function process(snsConfig, event, context) {
44
// detect if it's an sns-event at all:
5-
if(snsConfig.debug){
5+
if (snsConfig.debug) {
66
console.log('sns:Event', JSON.stringify(event));
7+
console.log('sns:context', context);
78
}
89

910
if (!Array.isArray(event.Records) || event.Records.length<1 || !event.Records[0].Sns) {
@@ -12,13 +13,13 @@ function process(snsConfig, event) {
1213
}
1314

1415
const sns = event.Records[0].Sns;
15-
for(let routeConfig of snsConfig.routes){
16-
if(routeConfig.subject instanceof RegExp){
17-
if(routeConfig.subject.test(sns.Subject)){
18-
const result = routeConfig.action(sns);
16+
for (let routeConfig of snsConfig.routes) {
17+
if (routeConfig.subject instanceof RegExp) {
18+
if (routeConfig.subject.test(sns.Subject)) {
19+
const result = routeConfig.action(sns, context);
1920
return result || {};
2021
}
21-
}else{
22+
} else {
2223
console.log(`SNS-Route with subject-regex '${routeConfig.subject}' is not a Regex; it is ignored!`);
2324
}
2425
}

package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "aws-lambda-router",
3-
"version": "0.3.1",
3+
"version": "0.4.0",
44
"description": "AWS lambda router",
55
"main": "index.js",
66
"types": "index.d.ts",
@@ -32,7 +32,6 @@
3232
"gulp-install": "1.1.0",
3333
"gulp-jasmine": "4.0.0",
3434
"gulp-zip": "4.2.0",
35-
"proxyquire": "2.1.0",
36-
"jasmine-reporters": "2.3.2"
35+
"proxyquire": "2.1.0"
3736
}
3837
}

0 commit comments

Comments
 (0)