Skip to main content

Class: WebArguments

@divine/web-service.WebArguments

A unified view all all possible arguments a filter or resource may receive when invoked.

Arguments may come from RegExp groups in the resource/filter path, query parameters, request headers and the parsed request body.

Constructors

constructor

new WebArguments(params, request)

Constructs a new WebArguments instance.

Parameters

NameTypeDescription
paramsStringParamsThe RegExp groups from the path matcher.
requestWebRequestThe request this class wraps.

Defined in

web-service/src/resource.ts:262

Properties

params

Readonly params: Object

A readonly map of all arguments with their unparsed values.

Since arguments may come from different sources, they are prefixed as follows:

  • RegExp group parameters (matched from the URL path) have a $ prefix.
  • URL query parameters have a ? prefix.
  • Request headers have a @ prefix.
  • Parameters from the request body have a . prefix. Note that these are only inserted once body has been called.
  • Custom request parameters manually set by setParam have a ~ prefix.

Index signature

[key: string]: string | object | undefined

Defined in

web-service/src/resource.ts:254


request

Readonly request: WebRequest

The request this class wraps.

Defined in

web-service/src/resource.ts:262

Accessors

log

get log(): Console

An alias/shortcut for log, which in turn is based on console.

Returns

Console

Defined in

web-service/src/resource.ts:277

Methods

body

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

Invokes body and then inserts all top-level properties of the parsed body into params with a . prefix (unless the parsed body is an array).

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

NameTypeDescription
Textends objectThe type this method should return.

Parameters

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

Returns

Promise<T>

The parsed request entity.

Defined in

web-service/src/resource.ts:295


boolean

boolean(param): boolean

Returns the value of a parameter parsed as a boolean.

The values true and t are accepted as true, while false and f represents false.

Throws

WebError(BAD_REQUEST) if a non-body parameter is missing or cannot be parsed.

Throws

WebError(UNPROCESSABLE_ENTITY) if a body parameter is missing or cannot be parsed.

Parameters

NameTypeDescription
paramstringThe name of the parameter to fetch (must include the desired prefix).

Returns

boolean

The parameter parsed as a boolean.

Defined in

web-service/src/resource.ts:334

boolean<T>(param, def): boolean | T

Returns the value of a parameter parsed as a boolean, or a default in case the parameter is missing.

The values true and t are accepted as true, while false and f represents false.

Throws

WebError(BAD_REQUEST) if a non-body parameter cannot be parsed.

Throws

WebError(UNPROCESSABLE_ENTITY) if a body parameter cannot be parsed.

Type parameters

NameTypeDescription
Textends undefined | null | booleanThe type of the def parameter.

Parameters

NameTypeDescription
paramstringThe name of the parameter to fetch (must include the desired prefix).
defTThe value that should be returned if the parameter could not be found.

Returns

boolean | T

The parameter parsed as a boolean, or the value of def.

Defined in

web-service/src/resource.ts:347


date

date(param): Date

Returns the value of a parameter parsed as an ISO date/timestamp.

The values true and t are accepted as true, while false and f represents false.

Throws

WebError(BAD_REQUEST) if a non-body parameter is missing or cannot be parsed.

Throws

WebError(UNPROCESSABLE_ENTITY) if a body parameter is missing or cannot be parsed.

Parameters

NameTypeDescription
paramstringThe name of the parameter to fetch (must include the desired prefix).

Returns

Date

The parameter parsed as an ISO date/timestamp.

Defined in

web-service/src/resource.ts:377

date<T>(param, def): Date | T

Returns the value of a parameter parsed as an ISO date/timestamp.

Any date/timestamp that begins with at least 4 digits and is supported by new Date() is accepted.

Throws

WebError(BAD_REQUEST) if a non-body parameter cannot be parsed.

Throws

WebError(UNPROCESSABLE_ENTITY) if a body parameter cannot be parsed.

Type parameters

NameTypeDescription
Textends undefined | null | DateThe type of the def parameter.

Parameters

NameTypeDescription
paramstringThe name of the parameter to fetch (must include the desired prefix).
defTThe value that should be returned if the parameter could not be found.

Returns

Date | T

The parameter parsed as an ISO date/timestamp, or the value of def.

Defined in

web-service/src/resource.ts:390


has

has(param): boolean

Checks if the specified parameter exists.

Parameters

NameTypeDescription
paramstringThe name of the parameter to check (must include the desired prefix).

Returns

boolean

true if the parameter exists, else false.

Defined in

web-service/src/resource.ts:318


number

number(param): number

Returns the value of a parameter parsed as a number.

Any number supported by Number() is accepted. This means that 0x, 0b and 0o prefices are respected.

Throws

WebError(BAD_REQUEST) if a non-body parameter is missing or cannot be parsed.

Throws

WebError(UNPROCESSABLE_ENTITY) if a body parameter is missing or cannot be parsed.

Parameters

NameTypeDescription
paramstringThe name of the parameter to fetch (must include the desired prefix).

Returns

number

The parameter parsed as a number.

Defined in

web-service/src/resource.ts:428

number<T>(param, def): number | T

Returns the value of a parameter parsed as a number.

Any number supported by Number() is accepted. This means that 0x, 0b and 0o prefices are respected.

Throws

WebError(BAD_REQUEST) if a non-body parameter cannot be parsed.

Throws

WebError(UNPROCESSABLE_ENTITY) if a body parameter cannot be parsed.

Type parameters

NameTypeDescription
Textends undefined | null | numberThe type of the def parameter.

Parameters

NameTypeDescription
paramstringThe name of the parameter to fetch (must include the desired prefix).
defTThe value that should be returned if the parameter could not be found.

Returns

number | T

The parameter parsed as a number, or the value of def.

Defined in

web-service/src/resource.ts:441


object

object<T>(param): T

Returns the value of a parameter as an object.

Note that only parameters coming from the request body can actually be objects.

Throws

WebError(BAD_REQUEST) if a non-body parameter is missing or cannot be parsed.

Throws

WebError(UNPROCESSABLE_ENTITY) if a body parameter is missing or cannot be parsed.

Type parameters

NameType
Textends object

Parameters

NameTypeDescription
paramstringThe name of the parameter to fetch (must include the desired prefix).

Returns

T

The parameter as an object.

Defined in

web-service/src/resource.ts:471

object<T>(param, def): object | T

Returns the value of a parameter as an object.

Note that only parameters coming from the request body can actually be objects.

Throws

WebError(BAD_REQUEST) if a non-body parameter cannot be parsed.

Throws

WebError(UNPROCESSABLE_ENTITY) if a body parameter cannot be parsed.

Type parameters

NameTypeDescription
Textends undefined | null | objectThe type of the def parameter.

Parameters

NameTypeDescription
paramstringThe name of the parameter to fetch (must include the desired prefix).
defTThe value that should be returned if the parameter could not be found.

Returns

object | T

The parameter as an object, or the value of def.

Defined in

web-service/src/resource.ts:484


string

string(param): string

Returns the value of a parameter as a string.

Throws

WebError(BAD_REQUEST) if a non-body parameter is missing or cannot be parsed.

Throws

WebError(UNPROCESSABLE_ENTITY) if a body parameter is missing or cannot be parsed.

Parameters

NameTypeDescription
paramstringThe name of the parameter to fetch (must include the desired prefix).

Returns

string

The parameter as a string.

Defined in

web-service/src/resource.ts:510

string<T>(param, def): string | T

Returns the value of a parameter as a string.

Throws

WebError(BAD_REQUEST) if a non-body parameter cannot be parsed.

Throws

WebError(UNPROCESSABLE_ENTITY) if a body parameter cannot be parsed.

Type parameters

NameTypeDescription
Textends undefined | null | stringThe type of the def parameter.

Parameters

NameTypeDescription
paramstringThe name of the parameter to fetch (must include the desired prefix).
defTThe value that should be returned if the parameter could not be found.

Returns

string | T

The parameter as a string, or the value of def.

Defined in

web-service/src/resource.ts:521