Skip to content
This repository has been archived by the owner on Aug 8, 2024. It is now read-only.

Commit

Permalink
Merge pull request #12 from spring-media/passthrough-context
Browse files Browse the repository at this point in the history
Context Object is available now
  • Loading branch information
jzlai authored Sep 13, 2018
2 parents f3fd3f8 + 72b4e5f commit e3972c5
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 48 deletions.
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ exports.handler = router.handler(
// activate CORS on all http-methods (OPTIONS requests are handled automagically);
// if set to true, these default headers will be sent on every response:
// "Access-Control-Allow-Origin" = "'*'"
// "Access-Control-Allow-Methods" = "'GET,POST,PUT,DELETE,HEAD'"
// "Access-Control-Allow-Methods" = "'GET,POST,PUT,DELETE,HEAD,PATCH'"
// "Access-Control-Allow-Headers" = "'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token'"
cors: true,
routes: [
Expand All @@ -36,19 +36,19 @@ exports.handler = router.handler(
// http method to match
method: 'POST',
// provide a function to be called with the propriate data
action: request=>doAnything(request.body)
action: (request, context) => doAnything(request.body)
},
{
// request-path-pattern with a path variable:
path: '/article/:id',
method: 'GET',
// we can use the path param 'id' in the action call:
action: request=>getSomething(request.paths.id)
action: (request, context) => getSomething(request.paths.id)
},
{
path: '/:id',
method: 'DELETE',
action: request=>deleteSomething(request.paths.id)
action: (request, context) => deleteSomething(request.paths.id)
}
],
debug: true,
Expand All @@ -68,7 +68,7 @@ exports.handler = router.handler(
// a regex to match the content of the SNS-Subject:
subject: /.*/,
// Attention: the message is JSON-stringified
action: sns => service.doSomething(JSON.parse(sns.Message))
action: (sns, context) => service.doSomething(JSON.parse(sns.Message))
}
]
}
Expand Down Expand Up @@ -101,13 +101,14 @@ return {

## local developement

The best is to work with ```npm link```
The best is to work with ```yarn link```

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


## Release History

* 0.4.0 now [the Context Object](https://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-handler.html) pass through
* 0.3.1 proxyIntegration: avoid error if response object is not set; add some debug logging
* 0.3.0 proxyIntegration: add PATCH method; allow for custom status codes from route (thanks to [@mintuz](https://github.com/mintuz))
* 0.2.2 proxyIntegration: set correct header values now for CORS
Expand Down
1 change: 0 additions & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ const gulp = require('gulp');
const del = require('del');
const install = require('gulp-install');
const jasmine = require('gulp-jasmine');
const reporters = require('jasmine-reporters');

gulp.task('test', () =>
gulp.src('test/*.spec.js')
Expand Down
6 changes: 3 additions & 3 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// Type definitions for aws-lambda-router
// Project: github.com/spring-mediaof 'RouteConfig/aws-lambda-router
// Project: github.com/spring-media/aws-lambda-router

export function handler(routeConfig: RouteConfig): any;

export interface ProxyIntegrationRoute {
path: string;
method: string;
action?: (request: any) => any;
action?: (request: any, context: any) => any;
}

export interface ProxyIntegrationConfig {
Expand All @@ -19,7 +19,7 @@ export interface ProxyIntegrationConfig {

export interface SnsRoute {
subject: any;
action?: (sns: any) => any;
action?: (sns: any, context: any) => any;
}

export interface SnsConfig {
Expand Down
5 changes: 3 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ function handler(routeConfig) {

return (event, context, callback) => {
if (routeConfig.debug) {
console.log("Lambda invoked with request:", event)
console.log("Lambda invoked with request:", event);
console.log("Lambda invoked with context:", context);
}

for (const eventProcessorName of eventProcessorMapping.keys()) {
Expand All @@ -18,7 +19,7 @@ function handler(routeConfig) {
// - throws Error: the 'error.toString()' is taken as the error message of processing the event
// - returns object: this is taken as the result of processing the event
// - returns promise: when the promise is resolved, this is taken as the result of processing the event
const result = eventProcessorMapping.get(eventProcessorName)(routeConfig[eventProcessorName], event);
const result = eventProcessorMapping.get(eventProcessorName)(routeConfig[eventProcessorName], event, context);
if (result) {
// be resilient against a processor returning a value instead of a promise:
return Promise.resolve(result)
Expand Down
13 changes: 7 additions & 6 deletions lib/proxyIntegration.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ function addCorsHeaders(toAdd) {
return toAdd;
}

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

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

function process(proxyIntegrationConfig, event) {
function process(proxyIntegrationConfig, event, context) {
if (proxyIntegrationConfig.debug) {
console.log("Lambda proxyIntegrationConfig: ", proxyIntegrationConfig)
console.log("Lambda request: ", event)
console.log("Lambda proxyIntegrationConfig: ", proxyIntegrationConfig);
console.log("Lambda event: ", event);
console.log("Lambda context: ", context);
}

//validate config
Expand Down Expand Up @@ -97,7 +98,7 @@ function process(proxyIntegrationConfig, event) {
});
}
}
return processActionAndReturn(actionConfig, event, headers, errorMapping);
return processActionAndReturn(actionConfig, event, context, headers, errorMapping);
} catch (error) {
console.log('Error while evaluating matching action handler', error);
return Promise.resolve(convertError(error, errorMapping, headers));
Expand Down
15 changes: 8 additions & 7 deletions lib/sns.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
"use strict";

function process(snsConfig, event) {
function process(snsConfig, event, context) {
// detect if it's an sns-event at all:
if(snsConfig.debug){
if (snsConfig.debug) {
console.log('sns:Event', JSON.stringify(event));
console.log('sns:context', context);
}

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

const sns = event.Records[0].Sns;
for(let routeConfig of snsConfig.routes){
if(routeConfig.subject instanceof RegExp){
if(routeConfig.subject.test(sns.Subject)){
const result = routeConfig.action(sns);
for (let routeConfig of snsConfig.routes) {
if (routeConfig.subject instanceof RegExp) {
if (routeConfig.subject.test(sns.Subject)) {
const result = routeConfig.action(sns, context);
return result || {};
}
}else{
} else {
console.log(`SNS-Route with subject-regex '${routeConfig.subject}' is not a Regex; it is ignored!`);
}
}
Expand Down
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "aws-lambda-router",
"version": "0.3.1",
"version": "0.4.0",
"description": "AWS lambda router",
"main": "index.js",
"types": "index.d.ts",
Expand Down Expand Up @@ -32,7 +32,6 @@
"gulp-install": "1.1.0",
"gulp-jasmine": "4.0.0",
"gulp-zip": "4.2.0",
"proxyquire": "2.1.0",
"jasmine-reporters": "2.3.2"
"proxyquire": "2.1.0"
}
}
Loading

0 comments on commit e3972c5

Please sign in to comment.