Skip to main content

Class: WebRequest

@divine/web-service.WebRequest

A wrapper around Node.js' IncomingMessage.

This class respects headers such as x-forwarded-* and x-http-method-override if configured to do so.

Implements​

  • AuthSchemeRequest

Constructors​

constructor​

• new WebRequest(incomingMessage, config)

Parses the Node.js request based on configuration.

Parameters​

NameTypeDescription
incomingMessageIncomingMessageThe wrapped Node.js incoming message.
configRequired<WebServiceConfig>WebService configuration specifiying how incomingMessage should be parsed.

Defined in​

web-service/src/request.ts:74

Properties​

id​

• Readonly id: string

The request ID. It's either generated or extracted from the incoming message, if trustRequestID is configured.

Defined in​

web-service/src/request.ts:56


incomingMessage​

• incomingMessage: IncomingMessage

The wrapped Node.js incoming message.

Defined in​

web-service/src/request.ts:74


log​

• Readonly log: Console

A per-request logger. Decorated with request ID if logRequestID is true.

Defined in​

web-service/src/request.ts:59


method​

• Readonly method: string

The request method.

Implementation of​

AuthSchemeRequest.method

Defined in​

web-service/src/request.ts:43


params​

• Readonly params: Params = {}

Custom parameters from filters etc may be stored here.

Defined in​

web-service/src/request.ts:62


remoteAddress​

• Readonly remoteAddress: string

The IP address from which the request was issued.

Defined in​

web-service/src/request.ts:49


url​

• Readonly url: URL

A reconstructed URL for this request

Implementation of​

AuthSchemeRequest.url

Defined in​

web-service/src/request.ts:46


userAgent​

• Readonly userAgent: UserAgent

The parsed user agent

Defined in​

web-service/src/request.ts:52

Accessors​

headers​

• get headers(): [string, string][]

All headers in a format compatible with the AuthSchemeRequest interface.

Returns​

[string, string][]

Implementation of​

AuthSchemeRequest.headers

Defined in​

web-service/src/request.ts:109


remoteUserAgent​

• get remoteUserAgent(): string

A short description of the remote client, including agent name, version and remote address.

Returns​

string

Defined in​

web-service/src/request.ts:102

Methods​

addFinalizer​

▸ addFinalizer<T>(finalizable): T

Registers a Finalizable object with this request.

Finalizers are functions that will be invoked as part of the close method and are used to free up temporary per-request resources.

Type parameters​

NameType
Textends object

Parameters​

NameTypeDescription
finalizableT & FinalizableThe Finalizable object whose finalizer should be called when this request is closed.

Returns​

T

The object that was passed is returned as-is.

Defined in​

web-service/src/request.ts:231


body​

▸ body<T>(contentType?, maxContentLength?): Promise<T>

Parses the incoming request body.

A reference to the parsed message is kept and will be returned directly if this method is called multiple times. The close method will free up temporary resources generated by this method, if any (for instance, file objects from the multipart/form-data parser).

Throws

WebError(PAYLOAD_TOO_LARGE) if the request body was larger than allowed.

Throws

WebError(UNSUPPORTED_MEDIA_TYPE) if the body could not be parsed.

Type parameters​

NameType
Textends object

Parameters​

NameTypeDescription
contentType?string | ContentTypeWhat parser to use. Defaults to the content-type request header.
maxContentLengthnumberThe maximum number of bytes to parse. Defaults to maxContentLength.

Returns​

Promise<T>

The parsed request entity.

Defined in​

web-service/src/request.ts:184


close​

▸ close(): Promise<void>

Closes this request and frees up all resources held by it by invoking all registered finalizers.

Returns​

Promise<void>

Defined in​

web-service/src/request.ts:244


▸ header(name, def?, concatenate?): string

Returns the value of a request header.

Throws

WebError(BAD_REQUEST) if the requested header is missing and no default was provided.

Parameters​

NameTypeDefault valueDescription
namekeyof IncomingHttpHeadersundefinedThe name of the request header to fetch (case-insensitive).
def?string | string[]undefinedThe default value to return, in case the header was not found. If not specified, an exception will be thrown instead.
concatenatebooleantrueSpecifies wheter to concatenate multiple headers with the same name into a single comma-separated string or not. If false, only the first header will be returned.

Returns​

string

The header value as a string.

Defined in​

web-service/src/request.ts:149


param​

▸ param(name, def?): BasicTypes

Returns the value of a custom parameter (set by setParam).

Throws

WebError(INTERNAL_SERVER_ERROR) if the requested header is missing and no default was provided.

Parameters​

NameTypeDescription
namestringThe name of the parameter to fetch.
def?BasicTypesThe default value to return, in case the parameter was not found. If not specified, an exception will be thrown instead.

Returns​

BasicTypes

The parameter value.

Defined in​

web-service/src/request.ts:123


setParam​

▸ setParam(param, value): WebRequest

Sets a custom parameter. Useful for providing resources with custom properties from a WebFilter, for instance.

Parameters​

NameTypeDescription
paramstringThe name of the parameter to set.
valueBasicTypesThe parameter value.

Returns​

WebRequest

This WebArguments.

Defined in​

web-service/src/request.ts:217


toString​

▸ toString(): string

Returns a short description about this request, including request method, URL and content type.

Returns​

string

Defined in​

web-service/src/request.ts:250