Skip to main content

Class: ApiRequest

Extends​

  • IncomingMessage

Constructors​

new ApiRequest()​

new ApiRequest(socket: Socket): ApiRequest

Parameters​

ParameterType
socketSocket

Returns​

ApiRequest

Inherited from​

IncomingMessage.constructor

Defined in​

node_modules/@types/node/http.d.ts:1172

Properties​

#cachedRequest​

private #cachedRequest: null | Request = null

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

Defined in​

projects/plugins/packages/api/src/lib/structures/api/ApiRequest.ts:63


auth?​

optional auth: null | AuthData

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.

Defined in​

projects/plugins/packages/api/src/lib/structures/api/ApiRequest.ts:29


params​

params: Record<string, string> = {}

The URI parameters.

Defined in​

projects/plugins/packages/api/src/lib/structures/api/ApiRequest.ts:20


query​

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

The query parameters.

Defined in​

projects/plugins/packages/api/src/lib/structures/api/ApiRequest.ts:15


route?​

optional route: null | Route<RouteOptions>

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

Defined in​

projects/plugins/packages/api/src/lib/structures/api/ApiRequest.ts:57


routerNode?​

optional routerNode: null | RouterNode

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

Defined in​

projects/plugins/packages/api/src/lib/structures/api/ApiRequest.ts:43

Accessors​

#isFormContentType​

Get Signature​

get private #isFormContentType(): boolean

Returns​

boolean

Defined in​

projects/plugins/packages/api/src/lib/structures/api/ApiRequest.ts:65

Methods​

asWeb()​

asWeb(): Request

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

Returns​

Request

Defined in​

projects/plugins/packages/api/src/lib/structures/api/ApiRequest.ts:78


readBody()​

readBody(): Promise<unknown>

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();

Defined in​

projects/plugins/packages/api/src/lib/structures/api/ApiRequest.ts:93


readBodyArrayBuffer()​

readBodyArrayBuffer(): Promise<ArrayBuffer>

Reads the request body as an ArrayBuffer.

Returns​

Promise<ArrayBuffer>

The result of the body parsing

Defined in​

projects/plugins/packages/api/src/lib/structures/api/ApiRequest.ts:102


readBodyBlob()​

readBodyBlob(): Promise<Blob>

Reads the request body as a Blob.

Returns​

Promise<Blob>

The result of the body parsing

Defined in​

projects/plugins/packages/api/src/lib/structures/api/ApiRequest.ts:111


readBodyFormData()​

readBodyFormData(): Promise<FormData>

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

Defined in​

projects/plugins/packages/api/src/lib/structures/api/ApiRequest.ts:127


readBodyJson()​

readBodyJson(): Promise<unknown>

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

Defined in​

projects/plugins/packages/api/src/lib/structures/api/ApiRequest.ts:137


readBodyText()​

readBodyText(): Promise<string>

Reads the request body as text, using TextDecoder.

Returns​

Promise<string>

The result of the body parsing

Defined in​

projects/plugins/packages/api/src/lib/structures/api/ApiRequest.ts:146


readValidatedBody()​

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

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

Defined in​

projects/plugins/packages/api/src/lib/structures/api/ApiRequest.ts:156


readValidatedBodyFormData()​

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

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

Defined in​

projects/plugins/packages/api/src/lib/structures/api/ApiRequest.ts:167


readValidatedBodyJson()​

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

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

Defined in​

projects/plugins/packages/api/src/lib/structures/api/ApiRequest.ts:177


readValidatedBodyText()​

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

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

Defined in​

projects/plugins/packages/api/src/lib/structures/api/ApiRequest.ts:187