Skip to main content

Class: ApiRequest

Defined in: packages/api/src/lib/structures/api/ApiRequest.ts:11

Extends​

  • IncomingMessage

Constructors​

Constructor​

new ApiRequest(socket: Socket): ApiRequest

Defined in: node_modules/@types/node/http.d.ts:1198

Parameters​

ParameterType
socketSocket

Returns​

ApiRequest

Inherited from​

IncomingMessage.constructor

Properties​

#cachedRequest​

private #cachedRequest: null | Request = null

Defined in: packages/api/src/lib/structures/api/ApiRequest.ts:63

The response object. This field is cached to prevent multiple response objects from being created.


auth?​

optional auth: null | AuthData

Defined in: packages/api/src/lib/structures/api/ApiRequest.ts:29

The authorization information. This field indicates three possible values:

  • undefined: The authorization middleware has not been executed yet.
  • null: The user is not authorized.
  • AuthData: The user is authorized.

params​

params: Record<string, string> = {}

Defined in: packages/api/src/lib/structures/api/ApiRequest.ts:20

The URI parameters.


query​

query: Record<string, string | string[]> = {}

Defined in: packages/api/src/lib/structures/api/ApiRequest.ts:15

The query parameters.


route?​

optional route: null | Route<RouteOptions>

Defined in: packages/api/src/lib/structures/api/ApiRequest.ts:57

The route that matched the request. The field indicates three possible values:

  • undefined: The router handler has not been executed yet.
  • null: The router handler has been executed, but no route matched the request.
  • Route: The router handler has been executed and a route matched the request.

Since​

7.0.0


routerNode?​

optional routerNode: null | RouterNode

Defined in: packages/api/src/lib/structures/api/ApiRequest.ts:43

The router node that matched the request. The field indicates three possible values:

  • undefined: The router handler has not been executed yet.
  • null: The router handler has been executed, but no node matched the request.
  • RouterNode: The router handler has been executed and a node matched the request.

Since​

7.0.0

Accessors​

#isFormContentType​

Get Signature​

get private #isFormContentType(): boolean

Defined in: packages/api/src/lib/structures/api/ApiRequest.ts:65

Returns​

boolean

Methods​

asWeb()​

asWeb(): Request

Defined in: packages/api/src/lib/structures/api/ApiRequest.ts:78

The response object, used to validate the request's headers and body.

Returns​

Request


readBody()​

readBody(): Promise<unknown>

Defined in: packages/api/src/lib/structures/api/ApiRequest.ts:93

Reads the request body and tries to parse using JSON or form-urlencoded.

Returns​

Promise<unknown>

The result of the body parsing

Example​

const body = await request.readBody();

readBodyArrayBuffer()​

readBodyArrayBuffer(): Promise<ArrayBuffer>

Defined in: packages/api/src/lib/structures/api/ApiRequest.ts:102

Reads the request body as an ArrayBuffer.

Returns​

Promise<ArrayBuffer>

The result of the body parsing


readBodyBlob()​

readBodyBlob(): Promise<Blob>

Defined in: packages/api/src/lib/structures/api/ApiRequest.ts:111

Reads the request body as a Blob.

Returns​

Promise<Blob>

The result of the body parsing


readBodyFormData()​

readBodyFormData(): Promise<FormData>

Defined in: packages/api/src/lib/structures/api/ApiRequest.ts:127

Reads the request body as a FormData.

Returns​

Promise<FormData>

The result of the body parsing

Remarks​

This will throw an error if the content type is not one of the following:

  • application/x-www-form-urlencoded
  • multipart/form-data

readBodyJson()​

readBodyJson(): Promise<unknown>

Defined in: packages/api/src/lib/structures/api/ApiRequest.ts:137

Reads the request body as text, using TextDecoder. Afterward, it parses the body as JSON with JSON.parse.

Returns​

Promise<unknown>

The result of the body parsing


readBodyText()​

readBodyText(): Promise<string>

Defined in: packages/api/src/lib/structures/api/ApiRequest.ts:146

Reads the request body as text, using TextDecoder.

Returns​

Promise<string>

The result of the body parsing


readValidatedBody()​

readValidatedBody<Type>(validator: ValidatorFunction<unknown, Type>): Promise<Type>

Defined in: packages/api/src/lib/structures/api/ApiRequest.ts:156

Identical to ApiRequest.readBody, but it validates the result.

Type Parameters​

Type Parameter
Type

Parameters​

ParameterTypeDescription
validatorValidatorFunction<unknown, Type>The validator function to use on the body parsing result

Returns​

Promise<Type>

The validated body


readValidatedBodyFormData()​

readValidatedBodyFormData<Type>(validator: ValidatorFunction<FormData, Type>): Promise<Type>

Defined in: packages/api/src/lib/structures/api/ApiRequest.ts:167

Identical to ApiRequest.readBodyFormData, but it validates the result.

Type Parameters​

Type Parameter
Type

Parameters​

ParameterTypeDescription
validatorValidatorFunction<FormData, Type>The validator function to use on the body parsing result

Returns​

Promise<Type>

The validated body


readValidatedBodyJson()​

readValidatedBodyJson<Type>(validator: ValidatorFunction<unknown, Type>): Promise<Type>

Defined in: packages/api/src/lib/structures/api/ApiRequest.ts:177

Identical to ApiRequest.readBodyJson, but it validates the result.

Type Parameters​

Type Parameter
Type

Parameters​

ParameterTypeDescription
validatorValidatorFunction<unknown, Type>The validator function to use on the body parsing result

Returns​

Promise<Type>

The validated body


readValidatedBodyText()​

readValidatedBodyText<Type>(validator: ValidatorFunction<string, Type>): Promise<Type>

Defined in: packages/api/src/lib/structures/api/ApiRequest.ts:187

Identical to ApiRequest.readBodyText, but it validates the result.

Type Parameters​

Type Parameter
Type

Parameters​

ParameterTypeDescription
validatorValidatorFunction<string, Type>The validator function to use on the body parsing result

Returns​

Promise<Type>

The validated body