Home Reference Source
import Res from 'autonym/lib/Res'
public class | source

Res

A wrapper for the response object with helper methods and access to Autonym model data.

Static Member Summary

Static Public Members
public static

The status code for successful create calls.

public static

The status code for successful find, findOne, findOneAndUpdate, and findOneAndDelete calls.

Constructor Summary

Public Constructor
public

constructor(raw: http.ServerResponse, model: Model, meta: Meta)

Method Summary

Public Methods
public

Gets the data currently set for the response body.

public

Gets the given header.

public

Gets the model instance.

public

getRaw(): http.ServerResponse

Gets the raw response.

public

Gets the currently set status code.

public

Whether the store method has populated the response data yet.

public

Whether the response has been sent to the client.

public

setData(data: Record, replace: boolean): void

Merges the currently set response data with the given data.

public

setHeader(header: string, value: string): void

Sets the given header.

public

setStatus(status: number): void

Sets the status code.

Static Public Members

public static CREATED: number source

The status code for successful create calls.

public static OK: number source

The status code for successful find, findOne, findOneAndUpdate, and findOneAndDelete calls.

Public Constructors

public constructor(raw: http.ServerResponse, model: Model, meta: Meta) source

Params:

NameTypeAttributeDescription
raw http.ServerResponse

The raw ServerResponse object.

model Model

The Autonym model instance.

meta Meta

The meta object aggregated by policies during the request.

Example:

const res = new AutonymRes(response, Post, meta)

Public Methods

public getData(): Record source

Gets the data currently set for the response body.

Return:

Record

The data.

Throw:

ReferenceError

If the store method has not been called yet.

public getHeader(header: string): string | undefined source

Gets the given header.

Params:

NameTypeAttributeDescription
header string

The header to find.

Return:

string | undefined

The header value.

public getModel(): Model source

Gets the model instance.

Return:

Model

The model.

public getRaw(): http.ServerResponse source

Gets the raw response.

Return:

http.ServerResponse

The raw response.

public getStatus(): number | null source

Gets the currently set status code.

Return:

number | null

The status code.

public isPopulated(): boolean source

Whether the store method has populated the response data yet.

Return:

boolean

True if the store method has been called.

public isSent(): boolean source

Whether the response has been sent to the client.

Return:

boolean

True if the response has been sent.

public setData(data: Record, replace: boolean): void source

Merges the currently set response data with the given data.

Params:

NameTypeAttributeDescription
data Record

The new properties to set.

replace boolean
  • optional

If true, replaces the data on the response instead of merging it.

Return:

void

Throw:

ReferenceError

If the store method has not been called yet.

Example:

console.log(res.getData()) // { title: 'Hello World' }
res.setData({ name: 'Test' })
console.log(res.getData()) // { name: 'Test', title: 'Hello World' }
console.log(res.getData()) // { title: 'Hello World' }
res.setData({ name: 'Test' }, true)
console.log(res.getData()) // { name: 'Test' }

public setHeader(header: string, value: string): void source

Sets the given header.

Params:

NameTypeAttributeDescription
header string

The header to set.

value string

The value to set to.

Return:

void

public setStatus(status: number): void source

Sets the status code.

Params:

NameTypeAttributeDescription
status number

The status code.

Return:

void