Class: Response

Response(resolve, reject)

new Response(resolve, reject)

Create a new Response object. This is called automatically when a new App is instatiated. You do not need to call this directly.

Parameters:
Name Type Description
resolve resolve

Resolve from a Promise

reject reject

Reject from a Promise

Members

body :string

Get or Set the body that is passed back to the event initiator (eg API Gateway, S3)

Type:
  • string
Properties:
Name Type Description
str string

Body that will be returned to the event initiator

Example
response.body = "hello world";

cacheHeader :number

Get and Set the Cache-Control header

Type:
  • number
Properties:
Name Type Description
n number

Seconds to set cache header to

Example
response.cacheHeader = 30;

contentType :string

Get or Set content type

Type:
  • string
Properties:
Name Type Description
str string

The content type

Default Value:
  • application/json
Example
response.contentType = 'text/html'

Set a cookie to be returned in the response headers

Type:
  • object
Properties:
Name Type Description
cookie object

Cookie object

Properties
Name Type Attributes Default Description
key string

Set the name of the cookie

value string

Set the value of the cookie

maxAge number <optional>
604800

Set the Max-Age param of cookie in seconds

expires date <optional>

Set the Expires param of cookie in toUTCString format. Only will get set if maxAge is not explicitly set

secure boolean <optional>
true

Set to false to serve cookie over http

httpOnly boolean <optional>
true

Set to false to allow cookie to be accessed in client javascript

domain string <optional>

Set the Domain param. If not set it will be automatically set to the serving domain

sameSite string <optional>
lax

Set the SameSite param. Possible values are lax, strict, none

path string <optional>
/

Set the Path param

Example
response.cookie = {"key": "userid", "value": "abc"}

(readonly) cookies :object

Cookies that have been set

Type:
  • object

csp :object|string

Get and Set the Content-Security-Policy header.

Type:
  • object | string
Properties:
Name Type Description
csp object | string

CSP object

Example
Set as an object:
response.csp = {"default-src": ["self", "example.com"], "script-src": ["scripts.example.com]};
OR set as a string response.csp = "default-src 'self'";

csrf :string

Set a csrf cookie

Type:
  • string
Properties:
Name Type Description
csrf string
Example
response.csrf = "abcd1234";

endTimer :string

End a timer. Used in conjunction with startTimer. If set, Response will include a header server-timing

Type:
  • string
Properties:
Name Type Description
key string

The name of the timer to start

Example
response.endTimer = "database";

Set a custom header

Type:
  • object
Properties:
Name Type Description
Header object
Properties
Name Type Description
key string
value string
Example
response.header = {"key": "foo", "value": "bar"};

(readonly) headers :object

headers that have been set

Type:
  • object

isBase64Encoded :boolean

Get and Set the isBase64Encoded property for API Gateway. Set to true if passing back binary data encoded in Base64.

Type:
  • boolean
Properties:
Name Type Description
b boolean

Boolean value for isBase64Encoded

Default Value:
  • false
Example
response.isBase64Encoded = true;

json :object

Convenience method to set the body to a JSON string from an object

Type:
  • object
Properties:
Name Type Description
obj object

Object to stringify and set to body

Example
response.json = {"status": "ok"};

location :string

Get or Set Location header

Type:
  • string
Properties:
Name Type Description
str string

The location to set

Example
response.location = 'https://example.com';

reject :function

returns the callback function provided to the handler by lambda to reject the promise

Type:
  • function

resolve :function

returns the callback function provided to the handler by lambda to resolve the promise

Type:
  • function

startTimer :string

Start a timer. Used in conjunction with endTimer. If set, Response will include a header server-timing

Type:
  • string
Properties:
Name Type Description
key string

The name of the timer to start

Example
response.startTimer = "database";

statusCode :number

Get or Set the status code that is passed back to the event initiator (eg API Gateway, S3)

Type:
  • number
Properties:
Name Type Description
n number

The status code

Default Value:
  • 500
Example
response.statusCode = 200;

timer :object

Set a timer to be returned in the server-timing response headers

Type:
  • object
Properties:
Name Type Description
timer object

Timer object

Properties
Name Type Description
key string

Set the name of the timer

value number

Set the elapsed time in milliseconds

Example
response.timer = {key: "database", value: 250}

(readonly) timers :object

timers that have been set

Type:
  • object

Methods

done() → {object}

Called when the request is done. The resolve of the Promise passed in to the constructor will be called with JSON

Returns:

{statusCode, body, headers}

Type
object