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
| Name | Type | Description |
|---|---|---|
incomingMessage | IncomingMessage | The wrapped Node.js incoming message. |
config | Required<WebServiceConfig> | WebService configuration specifiying how incomingMessage should be parsed. |
Defined in
Properties
id
• Readonly id: string
The request ID. It's either generated or extracted from the incoming message, if trustRequestID is configured.
Defined in
incomingMessage
• incomingMessage: IncomingMessage
The wrapped Node.js incoming message.
Defined in
log
• Readonly log: Console
A per-request logger. Decorated with request ID if logRequestID is true.
Defined in
method
• Readonly method: string
The request method.
Implementation of
AuthSchemeRequest.method
Defined in
params
• Readonly params: Params = {}
Custom parameters from filters etc may be stored here.
Defined in
remoteAddress
• Readonly remoteAddress: string
The IP address from which the request was issued.
Defined in
url
• Readonly url: URL
A reconstructed URL for this request
Implementation of
AuthSchemeRequest.url
Defined in
userAgent
• Readonly userAgent: UserAgent
The parsed user agent
Defined in
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
| Name | Type |
|---|---|
T | extends object |
Parameters
| Name | Type | Description |
|---|---|---|
finalizable | T & Finalizable | The 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
| Name | Type |
|---|---|
T | extends object |
Parameters
| Name | Type | Description |
|---|---|---|
contentType? | string | ContentType | What parser to use. Defaults to the content-type request header. |
maxContentLength | number | The 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
▸ 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
| Name | Type | Default value | Description |
|---|---|---|---|
name | keyof IncomingHttpHeaders | undefined | The name of the request header to fetch (case-insensitive). |
def? | string | string[] | undefined | The default value to return, in case the header was not found. If not specified, an exception will be thrown instead. |
concatenate | boolean | true | Specifies 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
| Name | Type | Description |
|---|---|---|
name | string | The name of the parameter to fetch. |
def? | BasicTypes | The 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
| Name | Type | Description |
|---|---|---|
param | string | The name of the parameter to set. |
value | BasicTypes | The parameter value. |
Returns
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