new LambdaController(event, ctx, callback)
Parameters:
Name | Type | Description |
---|---|---|
event |
object | An Object supplied by AWS Lambda containing the event data from API Gateway |
ctx |
object | The Context object supplied by AWS Lambda |
callback |
function | The callback to trigger and send the response back to API Gateway |
Members
-
headers :object
-
Gets the Request headers, these will always be in lowercase i.e content-type
Type:
- object
Properties:
Name Type Description header
string the header name i.e. content-type
value
* the value of the header
-
method :string
-
Returns the HTTP Method (aka Verb) i.e. POST, GET, PATCH, DELETE, PUT
Type:
- string
-
params :object
-
Get the Path paramteres as a key value pair object
Type:
- object
Properties:
Name Type Description key
string the name of the path parameter
value
string the value of the path parameter
Example
// route /foo/:id, with url /foo/bar this.params // => {id: "bar"}
-
path :string
-
Gets the current path
Type:
- string
-
query :object
-
Gets the Query String as an Object
Type:
- object
Properties:
Name Type Description key
string the key of Query string
value
string the value in the query string
Methods
-
add(mixin)
-
Adds a Mixin in to the base class
Parameters:
Name Type Description mixin
object The object to be mixed in
Returns:
An instance of the class
- Type
- LambdaController
Example
this.add({ get a() { return "a"; }, b() { return "b"; } });
-
addHeader(header, value)
-
Adds a response header
Parameters:
Name Type Description header
string The header name
value
* The header value
Returns:
An instance of the class
- Type
- LambdaController
Example
lambdaController.addHeader("content-type", "application/json")
-
badRequest()
-
Sends a prefab Bad Request(400) error back to the end user
Example
lambdaController.badRequest();
-
body(string)
-
Sets the response body
Parameters:
Name Type Description string
* The response body
Returns:
An instance of the class
- Type
- LambdaController
Example
lambdaController.body(` <html> <head> <title>foobar</title> </head> <body> hello </body> </html> `);
-
error(code, message)
-
Sends an HTTP error response
Parameters:
Name Type Description code
number The http status code to use
message
string The error message for the end user
Example
lambdaController.error(404, "Resource was not found")
-
json(object)
-
Sets up the response to respond in JSON
Parameters:
Name Type Description object
object | array The Object or Array to serialize in to the response
Returns:
An instance of the class
- Type
- LambdaController
Example
lambdaController.json({foo: "bar", a: 1});
-
notFound()
-
Sends a prefab Not Found (404) error back to the end user
Example
lambdaController.notFound();
-
send()
-
Sends the response
Example
lambdaController.send();
-
sendJson(object)
-
Sets up and sends the response in JSON,
Parameters:
Name Type Description object
object | array The Object or Array to serialize in to the response
Example
lambdaController.sendJson({foo: "bar", a: 1});
-
serverError()
-
Sends a prefab Internal Server Error (500) error back to the end user
Example
lambdaController.serverError();
-
status(code)
-
Changes the responses http Status code
Parameters:
Name Type Description code
number The HTTP reponse codes
Returns:
An instance of the class
- Type
- LambdaController
Example
lambdaController.status(200);
-
type(type)
-
Sets the content type on the response
Sets the body to a JSON serialised string, as well sets the content-type correctly
Parameters:
Name Type Description type
string A valid mime type to set the content type to
Returns:
An instance of the class
- Type
- LambdaController
Example
lambdaController.type("applicaiton/json");