Lambda event handler for API Gateway HTTP requests.
npm i @fiquu/lambda-http-event-handler
This will generate the necessary objects to handle an HTTP request graciously:
/configs/http-event.ts
import { conflict, badRequest } from '@fiquu/lambda-http-event-handler/lib/responses';
import { HTTPEventConfig } from '@fiquu/lambda-http-event-handler';
const handlers = new Map();
handlers.set(11000, conflict);
handlers.set('ValidationError', badRequest);
const config: HTTPEventConfig = {
res: {
handlers
}
};
export default config;
/service/my-function/handler.ts
import { noContent } from '@fiquu/lambda-http-event-handler/lib/responses';
import { APIGatewayProxyEvent, APIGatewayProxyResult } from 'aws-lambda';
import { createHTTPEvent } from '@fiquu/lambda-http-event-handler';
import { createLogger } from '@fiquu/logger';
import config from '../../configs/http-event';
const log = createLogger('HTTP /api/my-path');
/**
* My Function handler function.
*/
export default function handler(event: APIGatewayProxyEvent): APIGatewayProxyResult {
const { req, res } = createHTTPEvent(event, config);
log.debug(req.headers);
log.debug(req.params);
log.debug(req.query);
log.debug('Body:', req.body);
try {
return res.send(noContent());
} catch (err) {
log.error('It failed...', { err });
return res.handle(err);
}
}
/configs/http-event.ts
import { StaticFileHandlerConfig } from '@fiquu/lambda-http-event-handler/statics';
const config: StaticFileHandlerConfig = {
res: {
nonce: true,
views: {
basedir: join(__dirname, '..', 'fixtures', 'views'),
locals: {}
}
}
};
export default config;
/api/my-path/handler.ts
import { noContent } from '@fiquu/lambda-http-event-handler/lib/responses';
import { APIGatewayProxyEvent, APIGatewayProxyResult } from 'aws-lambda';
import { createHTTPEvent } from '@fiquu/lambda-http-event-handler';
import { createLogger } from '@fiquu/logger';
import config from './configs/http-event';
const log = createLogger('HTTP /view-path');
export default function handler(event: APIGatewayProxyEvent): APIGatewayProxyResult {
const { req, res } = createHTTPEvent(event, config);
try {
return res.render('the-view', {
body: req.body
});
} catch (err) {
log.error('It failed...', { err });
return res.render(err);
}
}
/configs/statics.ts
import { StaticFileHandlerConfig } from '@fiquu/lambda-http-event-handler/statics';
const config: StaticFileHandlerConfig = {
basedir: join('..', 'static')
};
export default config;
/statics/handler.ts
import { createStaticFileHandler, StaticFileHandler } from '@fiquu/lambda-http-event-handler/statics';
import { APIGatewayProxyEvent, APIGatewayProxyResult } from 'aws-lambda';
import config from '../configs/statics';
const staticFileHandler: StaticFileHandler = createStaticFileHandler(config);
export default function handler(event: APIGatewayProxyEvent): Promise<APIGatewayProxyResult> {
return staticFileHandler.get(event);
}
Default HTTP response params.
Bad Request HTTP response (400).
The HTTP response params.
The HTTP response object.
Conflict HTTP response (409).
The HTTP response params.
The HTTP response object.
Creates an instance of Response.
The HTTP response params.
The HTTP response object.
Creates the HTTP response params object.
The HTTP response params object.
Creates a new instance of the HTTP Event class.
The API Gateway HTTP proxy event.
The HTTP event configuration object.
The HTTP Event object.
Creates an HTTP request object.
The API Gateway HTTP event object.
The HTTP request object.
Creates a new HTTP response handler.
The HTTP response config.
The HTTP response handler instance.
Creates an HTTP response.
The HTTP response.
Configures the module.
The configuration object.
The statics file handler instance.
Creates a new instance of Views.
The component instance.
Creates the view locals object.
The objects to merge into the locals.
The merged locals values.
Created HTTP response (201).
The HTTP response params.
The HTTP response object.
Creates a file not found response.
The HTTP response.
Forbidden HTTP response (403).
The HTTP response params.
The HTTP response object.
Retrieves a view template by path.
The template basedir.
The view's template relative path.
The required template function.
Gets the HTTP request body as parsed JSON if possible.
The HTTP request event.
The HTTP request body.
Gets the HTTP request headers.
The HTTP request event.
The HTTP request headers object.
Generates a nonce value from the provided config.
The target size to generate of.
The nonce value.
Gets the HTTP request path parameters.
The HTTP request event.
The HTTP request path parameters object.
Gets the HTTP request query string parameters.
The HTTP request event.
The HTTP request query string parameters object.
Creates an HTTP response params object.
The response parameters object.
The file data object.
The HTTP response params object.
Handles response error.
The HTTP response configuration object.
The error to handle.
The handled HTTP response object.
Creates an Internal Server Error (500) response.
The HTTP response object.
Moved Permanently HTTP response (301).
The HTTP response params.
The HTTP response object.
No Content HTTP response (204).
The HTTP response params.
The HTTP response object.
Normalizes and merges the paramters and defaults
The HTTP response params.
The HTTP response params.
Not Acceptable HTTP response (406).
The HTTP response params.
The HTTP response object.
Not Found HTTP response (404).
The HTTP response params.
The HTTP response object.
Not Modified HTTP response (302).
The HTTP response params.
The HTTP response object.
Ok HTTP response (200).
The HTTP response params.
The HTTP response object.
Precondition Failed HTTP response (412).
The HTTP response params.
The HTTP response object.
Renders a view template.
The template basedir.
The view's path to render relative to the config.basedir
.
The locals object.
The rendered HTML string.
Sends a response to the request.
The HTTP response configuration object.
The response object to send.
The appropriate response object.
Creates a response as JSON.
The HTTP response object.
The JSON HTTP response object.
Unauthorized HTTP response (401).
The HTTP response params.
The HTTP response object.
Generated using TypeDoc
Default API Gateway response values.