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

Req

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

Constructor Summary

Public Constructor
public

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

Method Summary

Public Methods
public

Gets the result of merging the original data (see #getOriginalData) with the request data.

public

Gets the request payload.

public

Gets the given header.

public

Gets the requested record id.

public

Gets the model instance.

public

For update queries, gets the data of the original record to update.

public

Gets the request query.

public

getRaw(): http.IncomingMessage

Gets the raw request.

public

Gets the data that was originally on the request body.

public

Whether this request has a body.

public

Whether this is a request for a particular record.

public

Whether this is a create request.

public

Whether this is a find request.

public

Whether this is a findOne request.

public

Whether this is a findOneAndDelete request.

public

Whether this is a findOneAndUpdate request.

public

Whether this is a readonly request to fetch data.

public

Whether this is a request that will return response data.

public

Whether this step is occurring with safe data, i.e.

public

Whether this is a request that will modify the data store.

public

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

Merges the request data with the given data, without modifying the original request.

Public Constructors

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

Params:

NameTypeAttributeDescription
raw http.IncomingMessage

The raw IncomingMessage object.

model Model

The Autonym model instance.

meta Meta

The meta object aggregated by policies during the request.

Example:

const req = new AutonymReq(request, Post, meta)

Public Methods

public async getCompleteData(): Promise<Record, AutonymError> source

Gets the result of merging the original data (see #getOriginalData) with the request data.

Return:

Promise<Record, AutonymError>

The merged data.

Example:

console.log(req.getData()) // { title: 'Test' }
const originalData = await req.getCompleteData()
console.log(originalData) // { title: 'Test', body: 'This is my first post.' }

public getData(): Record source

Gets the request payload.

Return:

Record

The data.

Throw:

ReferenceError

If the request does not have a body.

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 getId(): string source

Gets the requested record id.

Return:

string

The record id.

Throw:

ReferenceError

If it is a create or find request.

public getModel(): Model source

Gets the model instance.

Return:

Model

The model.

public async getOriginalData(): Promise<Record, AutonymError> source

For update queries, gets the data of the original record to update. For create queries, gets an empty object.

Return:

Promise<Record, AutonymError>

The original record data.

Throw:

ReferenceError

If the request is not a create or update request.

Example:

console.log(req.getData()) // { title: 'Test' }
const originalData = await req.getOriginalData()
console.log(originalData) // { title: 'Hello World', body: 'This is my first post.' }

public getQuery(): object source

Gets the request query.

Return:

object

The query.

public getRaw(): http.IncomingMessage source

Gets the raw request.

Return:

http.IncomingMessage

The raw request.

public getRequestData(): Record source

Gets the data that was originally on the request body.

Return:

Record

The data.

Throw:

ReferenceError

If the request does not have a body.

public hasBody(): boolean source

Whether this request has a body.

Return:

boolean

True if it is a create or findOneAndUpdate request.

public hasId(): boolean source

Whether this is a request for a particular record.

Return:

boolean

True if it is a findOne, findOneAndUpdate, or findOneAndDelete request.

public isCreating(): boolean source

Whether this is a create request.

Return:

boolean

True if it is a create request.

public isFinding(): boolean source

Whether this is a find request.

Return:

boolean

True if it is a find request.

public isFindingOne(): boolean source

Whether this is a findOne request.

Return:

boolean

True if it is a findOne request.

public isFindingOneAndDeleting(): boolean source

Whether this is a findOneAndDelete request.

Return:

boolean

True if it is a findOneAndDelete request.

public isFindingOneAndUpdating(): boolean source

Whether this is a findOneAndUpdate request.

Return:

boolean

True if it is a findOneAndUpdate request.

public isGetting(): boolean source

Whether this is a readonly request to fetch data.

Return:

boolean

True if it is a find or findOne request.

public isReading(): boolean source

Whether this is a request that will return response data.

Return:

boolean

True if it is a create, find, findOne, or findOneAndUpdate request.

public isValidated(): boolean source

Whether this step is occurring with safe data, i.e. the data has been validated, filtered, and populated with defaults.

Return:

boolean

True if it has passed the preSchema and validateAgainstSchema steps.

public isWriting(): boolean source

Whether this is a request that will modify the data store.

Return:

boolean

True if it is a create, findOneAndUpdate, or findOneAndDelete request.

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

Merges the request data with the given data, without modifying the original request.

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 request does not have a body.

Example:

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