AWS API Gateway SDK
A set of tools to help build applications integrated with AWS API Gateway.
Installing
$ npm i -SE @floteam/aws-api-gateway-sdk
Usage
Error handling
Error handler logs your error you provide to it and format response for API type you use. Tha handler based on @floteam/errors library. Any error which is not an instance of HttpError
will be converted to a HttpError
instance.
HTTP API error handling
To handle error in a lambda connected to HTTP API, use the following handler:
// Import handler
import {errorHandler} from '@floteam/aws-api-gateway-sdk/http-api/error-handler';
export function myHttpApiHandler(event, context) {
try {
// ...code
} catch (error) {
// Handle error. It will format response based on HttpError instance properties.
return errorHandler(error);
}
}
REST API error handling
To handle error in a lambda connected to REST API, use the following handler:
// Import handler
import {errorHandler} from '@floteam/aws-api-gateway-sdk/rest-api/error-handler';
export function myRestApiHandler(event, context) {
try {
// ...code
} catch (error) {
// Handle error. It will format and throw error.
errorHandler(error);
}
}
HTTP API response
To create an HTTP API response you can use the http-api response library.
Example
import * as httpApiResponse from '@floteam/aws-api-gateway-sdk/http-api/response';
// Simple response built via `createResponse` function
export function myHttpApiHandler1(event, context) {
try {
// ...code
return httpApiResponse.createResponse(200, 'Ok');
} catch (error) {
// ...Handle error
}
}
// Simple error response built via `errorResponse` function
export function myHttpApiHandler2(event, context) {
try {
// ...code
} catch (error) {
// ...Handle error
return httpApiResponse.errorResponse(500, 'Opps');
}
}
// Build response using custom Response instance
export function myHttpApiHandler3(event, context) {
// Create a custom instance of Response class with custom headers
const customResponse = new httpApiResponse.Response({
'My-Custom-Header': 'my-value'
});
try {
return customResponse.createResponse(200, 'Ok');
} catch (error) {
// ...Handle error
return customResponse.errorResponse(500, 'Opps');
}
}
Exports
The http-api library exports the following tools:
-
Response
- class to build response -
defaultResponse
- default(or global) instance ofResponse
class. The same asnew Response()
-
createResponse
- wrapper arounddefaultResponse.createResponse
method -
errorResponse
- wrapper arounddefaultResponse.errorResponse
method
Response
class
You can provide custom http headers as the first constructor`s argument. Those headers will be applied to each response.
const customResponse = new httpApiResponse.Response({
'My-Custom-Header': 'my-value'
});
const response = customResponse.createResponse(200, 'Hi!');
the result will be
{
statusCode: 200,
headers: {
'My-Custom-Header': 'my-value',
},
body: 'Hi!'
}
If you did not provide any custom headers then the following headers will be applied by default:
- Access-Control-Allow-Origin: *
- Access-Control-Allow-Credentials: false
- Content-Type': application/json
Methods
-
setHeaders(headers)
- Sets default headers for response -
create(status, body, customHeaders)
- Creates an HTTP API response -
error(status, message, detauls, customHeaders)
- - Creates an HTTP API error response
FAQ
Does the library provide any tooltips my IDE?
Yes. The library is written with TypeScript + comments, you can use that information as tooltips in your IDE.
Contribution
Publishing
Up version
$ npm version (path|minor|major)
Prepare package
$ npm run pre-publish
This step creates tarball that is ready for publishing
Publish
$ npm publish ./dist/NAME_OF_TARBALL.tgz