0.1.0 • Published 3 years ago
umd-serverless-router v0.1.0
Very Lightweight framework for serverless APIs
The UMD serverless router is a lightweight framework for AWS API Gateway+Lambda. It has minimal dependencies, supports both callback and async+await and does not mask errors as success, so CloudWatch metrics shows accurate error counts for lambda invocations.
Example
# index.js
const APIROUTER = require ('umd-serverless-router');
const PEOPLE = require ('./lib/people');
/* Saved outside of the lambda entrypoint for cold start re-use/optimization. */
let apiRouter;
module.exports.routeProcessorLambdaEntrypoint = async (event, context) => {
if ( !apiRouter ) {
apiRouter = new APIROUTER ();
let people = new PEOPLE ();
apiRouter.registerRoutes ({
'setupFunction': people.routeSetup.bind (people),
'prefix': 'v1/people',
});
}
return await apiRouter.routeRequests (event, context);
};
# lib/people.js
let people = function ()
{
}
# setup routes for the people service
people.prototype.routeSetup = function (apiRouter, params)
{
/* GET /v1/people */
apiRouter.addRoute ({
'method': 'GET',
'path': '',
'handler': self.listPeople.bind (self),
});
/* POST /v1/people/${personId} */
apiRouter.addRoute ({
'method': 'POST',
'path': '{personId}',
'handler': self.addPerson.bind (self),
});
};
people.prototype.listPeople = function (request)
{
return new Promise ((resolve, reject) => {
return resolve ([
{
'name': 'homer',
'age': 42,
},
{
'name': 'marge',
'age': 41,
},
]);
});
};
module.exports = people;
References
Request Object Details
{
"version": "2.0",
"routeKey": "GET /{proxy+}",
"rawPath": "/v1/people",
"rawQueryString": "",
"headers": {
"accept": "*\/*",
"access-control-request-method": "GET",
"authorization": "Bearer SECRET_JWT",
"content-length": "0",
"host": "abcdefg.execute-api.us-east-1.amazonaws.com",
"origin": "http://localhost:8080",
"user-agent": "curl/7.19.6 (x86_64-unknown-linux-gnu) ...",
"x-amzn-trace-id": "Root=1-61645450-ABCDEFG",
"x-forwarded-for": "127.0.0.1",
"x-forwarded-port": "443",
"x-forwarded-proto": "https"
},
"requestContext": {
"accountId": "123456789012",
"apiId": "abcdefg1234",
"authorizer": {
"lambda": {
"authInfo": {
"stuff": "returned",
"by the": "authorizer"
}
}
},
"domainName": "abcdefg.execute-api.us-east-1.amazonaws.com",
"domainPrefix": "abcdefg",
"http": {
"method": "GET",
"path": "/v1/people",
"protocol": "HTTP/1.1",
"sourceIp": "127.0.0.1",
"userAgent": "curl/7.19.6 (x86_64-unknown-linux-gnu) ...",
},
"requestId": "ABCEDFGHIJ=",
"routeKey": "GET /{proxy+}",
"stage": "$default",
"time": "11/Oct/2021:15:12:16 +0000",
"timeEpoch": 1633965136901
},
"pathParameters": {
"proxy": "v1/people"
},
"body": "{ \"This\": \"is a request body\", \"a\": 3 }",
"isBase64Encoded": false
}
0.1.0
3 years ago