0.0.1 • Published 6 years ago

sagr v0.0.1

Weekly downloads
3
License
Apache2
Repository
github
Last release
6 years ago

Simple-Api-Gateway-Router

An ES6 rewrite of the Alpr package.

This package aims to make request routing in a lambda function simple and effective. Used in pair with the serverless framework you can create a resource API with a single handler.

Content

Usage

Add this Simple-Api-Gateway-Router to your project.

$> npm install --save sagr  
  
or  
  
$> yarn add sagr  

In your handler:

const Router = require('sagr');

function Handler(event, context, callback) {
	const router = new Router({ event, context, callback });

	router.route({
		method: 'POST',
		path: '/api/test',
		handler: (req, res) => {
			res({
				statusCode: 200,
				body: {
					message: 'hello world !'
				},
				headers: {
					'x-header': 'hello',
				},
			}),
		}
	});
	
	router.route({
		// You can provide multiple methods
		method: ['POST', 'GET', 'PUT'],
		// And different paths
		path: ['/api/test', '/api/test/{variable}'],
		handler: (req, res) => {
			res({
				statusCode: 200,
				body: {
					message: 'hello world !'
				},
				headers: {
					'x-header': 'hello',
				},
			}),
		}
	});

	// If no route has been matched, the router will answer with a common 404 response.
	router.done();
}

module.exports = {
	handler
};

Routes

KeyTypeValue
methodstring/ArrayThe http method the route should match for. More than one can be specified
pathstring/ArrayThis should match the value of the route specified in API gateway including path parameter names
handlerFunctionThe handler function for the given route. Should take two parameters of request and response.

Handler

The handler is need to match the route specified and process the answer. it takes a request parameter which help you reach the request information needed in your computing, and a response callback which answer back with a formatted response to the API-Gateway.

Request

KeyTypeValue
request.getContextFunctionReturn the whole lambda context object.
request.getStageFunctionReturn the API Gateway stage variables.
request.getQueryFunctionReturn the query string parameters.
request.getBodyFunctionReturn the JSON parsed body.
request.getRawFunctionReturn the raw event object.
request.getPathFunctionReturn the request path parameters.
request.getHeadersFunctionReturn the request headers.

Response

The response callback send back a formatted response to the API-Gateway.

KeyTypeValueDefault
paramsObjectParameters object{}
params.statusCodeIntegerThe HTTP status code200
params.headersObjectAny headers to be returned in the response.{}
params.bodyMixedYour response body, whatever is specified will be JSON.stringify'd. If body is not set the body will be defined as the params object.JSON.stringify(params)
params.isBase64EncodedBooleanThis is usually used for serving binary data from an API.false

Here is the recommended way to call the response method.

response({
    statusCode: 200,
    headers: { "x-your-header": "header value" },
    body: { "response-object-key": "data" },
});

And response('hello world') would work out as:

{
    statusCode: 200,
    headers: {},
    body: "hello world"
}

Credits to aceew for it's router.