Class: LambdaController

LambdaController

The base class for Lambda Controllers intended to be extended


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

Author:

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

Author:

method :string

Returns the HTTP Method (aka Verb) i.e. POST, GET, PATCH, DELETE, PUT

Type:
  • string
Author:

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

Author:
Example
// route /foo/:id, with url /foo/bar
this.params // => {id: "bar"}

path :string

Gets the current path

Type:
  • string
Author:

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

Author:

Methods


add(mixin)

Adds a Mixin in to the base class

Parameters:
Name Type Description
mixin object

The object to be mixed in

Author:
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

Author:
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

Author:
Example
lambdaController.badRequest();

body(string)

Sets the response body

Parameters:
Name Type Description
string *

The response body

Author:
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

Author:
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

Author:
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

Author:
Example
lambdaController.notFound();

send()

Sends the response

Author:
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

Author:
Example
lambdaController.sendJson({foo: "bar", a: 1});

serverError()

Sends a prefab Internal Server Error (500) error back to the end user

Author:
Example
lambdaController.serverError();

status(code)

Changes the responses http Status code

Parameters:
Name Type Description
code number

The HTTP reponse codes

Author:
See:
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

Author:
Returns:

An instance of the class

Type
LambdaController
Example
lambdaController.type("applicaiton/json");