serverless-endpoint
v0.1.0
Published
Dependency-less express like wrapper for serverless functions.
Downloads
11
Readme
serverless-endpoint
Dependency-less express like wrapper for serverless functions.
Support
✔️️ AWS Api Gateway | ✔️ Google Cloud Functions | ❌ Azure functions | ❌ IBM OpenWhisk
Install
npm install --save serverless-endpoint
usage
Basic Example
// handler
const endpoint = require('serverless-endpoint');
function getHelloWorld(req, res) {
res.send(200, { message: 'Hello World!' })
}
module.exports.handler = endpoint(getHelloWorld)
Path Parameter Example
// endpoint /hello/{value}
// handler
const endpoint = require('serverless-endpoint');
function getHelloWorld(req, res) {
res.send(200, { message: `Hello world! ${req.params.value}` })
}
module.exports.handler = endpoint(getHelloWorld)
Query Parameter Example
// endpoint /hello/?timestamp=true
// handler
const endpoint = require('serverless-endpoint');
function getHelloWorld(req, res) {
const timestamp = req.query.timestamp ? new Date() : ''
res.send(200, { message: `Hello World! ${timestamp}` })
}
module.exports.handler = endpoint(getHelloWorld)
Body Example
// endpoint /hello/, { data: 'lorem' }
// handler
const endpoint = require('serverless-endpoint');
function getHelloWorld(req, res) {
res.send(200, { message: `Hello World! ${req.body.data}` })
}
module.exports.handler = endpoint(getHelloWorld)
Cors Response Example
// endpoint /hello
// handler
const endpoint = require('serverless-endpoint');
function getHelloWorld(req, res) {
res.header({ "Access-Control-Allow-Origin" : "*" })
.send(200, { message: `Hello World!` })
}
module.exports.handler = endpoint(getHelloWorld)
Cors through Config Response Example
const endpoint = require('serverless-endpoint');
// Config Options
const opts = {
headers: { "Access-Control-Allow-Origin": "*" }
}
// endpoint /hello
function getHelloWorld(req, res) {
res.send(200, { message: `Hello World!` })
}
// endpoint /ping
function ping(req, res) {
res.header({ "Additional-Header": "example" })
.send(200, { message: `Ping!` })
// returns
// {
// statusCode: 200,
// body: message,
// headers: {
// "Access-Control-Allow-Origin": "*",
// "Additional-Header": "example"
// }
// }
}
module.exports.getHelloWorld = endpoint(getHelloWorld, opts)
module.exports.ping = endpoint(ping, opts)
Api
endpoint(cloudFunctionHandler, options)
Higher Order Function that abstracts the different cloud function parameters into a single express-like api. Is configurable by options parameter.
options
Properties
| Name | Type | Description |
| --- | --- | --- |
| headers | Object | default headers to be sent with res.send
|
req : Object
Properties
| Name | Type | Description |
| --- | --- | --- |
| body | Object | http body object sent by request |
| method | string | Http method - GET, PUT, POST, DELETE, etc.. |
| path | string | A cleaned url string |
| resource | string | base resource of url |
| headers | Object | header object containing all header information |
| params | Object | parameters object from url path - /resource/{id}
= { id: <value> }
|
| query | Object | query parameters object from url - /resource?sort=asc = { sort: 'asc' } |
| id | string | AWS Only
string id of the request: AWS.event.requestContext.requestId |
| apiId | string | AWS Only
string apiId: AWS.event.requestContext.apiId |
| stage | string | AWS Only
api stage from url - /dev/resource = 'dev' |
| identity | Object | AWS Only
identity of user: event.requestContext.identity |
| authorizer | Object | AWS Only
object returned from custom authorizer: event.requestContext.authorizer |
| header | function | value for the header key - header(headerKey) |
| get | function | value for the header key - get(headerKey)
| getOriginalRequest | function | AWS Only
returns the arguments provided to the http function |
res : Object
Properties
| Name | Type | Description |
| --- | --- | --- |
| send | function | Sends the HTTP response. |
| error | function | AWS Only
Returns an error to api gateway. |
| header | function | Set header key
to value
, or pass an object of header fields. |
| set | function | Alias for header
|
| getHeader | function | Get value for header key
. |
| get | function | Alias for getHeader
|
res.send(statusCode, body)
Formats statusCode, body to be sent as a HTTP response back to api consumer (Api Gateway, Google Endpoint). The body parameter can be a a String, an object, or an Array.
Kind: public function Returns: Object - response HTTP response object formatted for Api Gateway.
| Param | Type | Description | | --- | --- | --- | | statusCode | number | Http Response code | | body | string | Object | Array | Response body
res.error(error) AWS Only
returns error to api gateway
Kind: public function Returns: Object - Error to be handed to ApiGateway
| Param | Type | Description | | --- | --- | --- | | err | Object | Caught javascript error |
res.headers(key [, value])
Set header key
to value
, or pass
an object of header fields.
Examples:
res.header('Foo', ['bar', 'bot']);
res.header('Content-Type', 'application/json');
res.header({ 'Content-Type': 'text/html', 'X-API-Key': 'buildkey' });
// chaining
res.header({ 'Content-Type': 'text/html' })
.send(200, html);
Aliased as res.set()
.
Kind: public function
Returns: Res - Returns the same Res
for chaining
res.getHeader(key)
Get value for header key
.
Examples:
res.header('Foo', 'bar');
res.getHeader('Foo');
// Returns 'bar'
Aliased as res.get()
.
Kind: public function
Returns: string - Returns value for header key
.