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 = ; { res} moduleexportshandler =
Path Parameter Example
// endpoint /hello/{value} // handler const endpoint = ; { res} moduleexportshandler =
Query Parameter Example
// endpoint /hello/?timestamp=true // handler const endpoint = ; { const timestamp = reqquerytimestamp ? : '' res} moduleexportshandler =
Body Example
// endpoint /hello/, { data: 'lorem' } // handler const endpoint = ; { res} moduleexportshandler =
Cors Response Example
// endpoint /hello // handler const endpoint = ; { res } moduleexportshandler =
Cors through Config Response Example
const endpoint = ; // Config Optionsconst opts = headers: "Access-Control-Allow-Origin": "*" // endpoint /hello { res} // endpoint /ping { res // returns // { // statusCode: 200, // body: message, // headers: { // "Access-Control-Allow-Origin": "*", // "Additional-Header": "example" // } // }} moduleexportsgetHelloWorld = moduleexportsping =
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 |
Object
req : 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 |
Object
res : 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 |
AWS Only
res.error(error) 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;res;res; // chainingres ;
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;res;// Returns 'bar'
Aliased as res.get()
.
Kind: public function
Returns: string
- Returns value for header key
.