Skip to main content
Version: 1.2.0

Class: PetitioRequest#

PetitioRequest.PetitioRequest

Constructors#

constructor#

+ new PetitioRequest(url: string | URL, httpMethod?: HTTPMethod): PetitioRequest

Parameters:#

NameTypeDefault valueDescription
urlstring | URL-The URL to start composing a request for.
httpMethodHTTPMethod"GET"-

Returns: PetitioRequest

The Petitio request instance for your URL.

Defined in: lib/PetitioRequest.ts:66

Properties#

coreOptions#

• coreOptions: Options

Options to use for Undici under the hood.

see Undici ClientOptions documentation

Defined in: lib/PetitioRequest.ts:34


data#

• Optional data: string | Buffer | Readable

The data to be sent as the request body. This will be a buffer or string for normal requests, or a stream.Readable if the request is to be sent as a stream.

Defined in: lib/PetitioRequest.ts:40


httpMethod#

• httpMethod: HTTPMethod= "GET"

see HTTPMethod

Defined in: lib/PetitioRequest.ts:44


kClient#

• Optional kClient: Client

see PetitioRequest.client

Defined in: lib/PetitioRequest.ts:48


keepClient#

• Optional keepClient: boolean

Whether PetitioRequest.kClient will persist between PetitioRequest.send calls. It is recommended to enable this for superior performance.

Defined in: lib/PetitioRequest.ts:53


reqHeaders#

• reqHeaders: IncomingHttpHeaders

The headers to attach to the request.

Defined in: lib/PetitioRequest.ts:57


timeoutOptions#

• timeoutOptions: TimeoutOptions

The timeout options for the Undici client.

see TimeoutOptions

Defined in: lib/PetitioRequest.ts:62


url#

• url: URL

The URL destination for the request, targeted in PetitioRequest.send.

Defined in: lib/PetitioRequest.ts:66

Methods#

body#

â–¸ body(data: string | Buffer): PetitioRequest

Parameters:#

NameTypeDescription
datastring | BufferThe data to be set for the request body.

Returns: PetitioRequest

Defined in: lib/PetitioRequest.ts:129

â–¸ body(data: Record<string, any>, sendAs?: json): PetitioRequest

Parameters:#

NameTypeDescription
dataRecord<string, any>The data to be set for the request body.
sendAs?jsonIf data is set to any object type value other than a buffer or this is set to json, the Content-Type header will be set to application/json and the request data will be set to the stringified JSON form of the supplied data.

Returns: PetitioRequest

Defined in: lib/PetitioRequest.ts:137

â–¸ body(data: string | ParsedUrlQueryInput, sendAs: form): PetitioRequest

Parameters:#

NameTypeDescription
datastring | ParsedUrlQueryInputThe data to be set for the request body.
sendAsformIf data is a string or a parsed object of query parameters AND this is set to form, the Content-Type header will be set to application/x-www-form-urlencoded and the request data will be set to the URL encoded version of the query string.

Returns: PetitioRequest

Defined in: lib/PetitioRequest.ts:145

â–¸ body(data: Readable, sendAs: stream): PetitioRequest

Parameters:#

NameTypeDescription
dataReadableThe data to be set for the request body.
sendAsstreamIf data is a stream.Readable AND this is set to stream, the body will be sent as the stream with no modifications to it or the headers.

Returns: PetitioRequest

Defined in: lib/PetitioRequest.ts:152


client#

â–¸ client(client: Client, keepAlive?: boolean): PetitioRequest

see Undici Client documentation

Parameters:#

NameTypeDescription
clientClientThe Undici client instance you wish to use.
keepAlive?booleanWhether to persist the client across requests or not.

Returns: PetitioRequest

The request object for further composition.

Defined in: lib/PetitioRequest.ts:85


header#

â–¸ header(header: string, value: string): PetitioRequest

Parameters:#

NameTypeDescription
headerstringThe encoded header name to set.
valuestringThe value to set the header to.

Returns: PetitioRequest

Defined in: lib/PetitioRequest.ts:196

â–¸ header(header: Record<string, string>): PetitioRequest

Parameters:#

NameTypeDescription
headerRecord<string, string>An object of keys and values to set headers to.

Returns: PetitioRequest

Defined in: lib/PetitioRequest.ts:200


json#

â–¸ json<T>(): Promise<T>

Type parameters:#

NameDefaultDescription
TanyType casting parameter for the JSON result.

Returns: Promise<T>

A serialized object result from sending the request.

Defined in: lib/PetitioRequest.ts:262


method#

â–¸ method(method: HTTPMethod): PetitioRequest

Parameters:#

NameTypeDescription
methodHTTPMethodThe HTTP method to change the request to.

Returns: PetitioRequest

The request object for further composition.

Defined in: lib/PetitioRequest.ts:214


option#

â–¸ option(key: Options): PetitioRequest

see Undici Client documentation

Parameters:#

NameTypeDescription
keyOptionsAn object of key-value options to set for Undici.

Returns: PetitioRequest

Defined in: lib/PetitioRequest.ts:243

â–¸ option<T>(key: T, value: Options[T]): PetitioRequest

see Undici Client documentation

Type parameters:#

NameType
Tkeyof Options

Parameters:#

NameTypeDescription
keyTThe client options key to set.
valueOptions[T]The value to set the client option to (type checked).

Returns: PetitioRequest

Defined in: lib/PetitioRequest.ts:250


path#

â–¸ path(relativePath: string): PetitioRequest

example https://example.org/hello/world with .path("../petitio") would resolve to https://example.org/hello/petitio.

Parameters:#

NameTypeDescription
relativePathstringA path to resolve relative to the current URL.

Returns: PetitioRequest

The request object for further composition.

Defined in: lib/PetitioRequest.ts:120


query#

â–¸ query(key: string, value: any): PetitioRequest

example If you wish to make a query at https://example.com/index?query=parameter you can use .query("query", "parameter").

Parameters:#

NameTypeDescription
keystringThe query key to use for the URL query parameters.
valueanyThe value to set the query key to.

Returns: PetitioRequest

Defined in: lib/PetitioRequest.ts:99

â–¸ query(key: Record<string, any>): PetitioRequest

example If you wish to make multiple queries at once, you can use .query({"keyOne": "hello", "keyTwo": "world!"}).

Parameters:#

NameTypeDescription
keyRecord<string, any>An object of query keys and their respective values.

Returns: PetitioRequest

Defined in: lib/PetitioRequest.ts:106


raw#

â–¸ raw(): Promise<Buffer>

Returns: Promise<Buffer>

The raw response body as a buffer.

Defined in: lib/PetitioRequest.ts:270


send#

â–¸ send(): Promise<PetitioResponse>

Finalizes and sends the composable request to the target server.

Returns: Promise<PetitioResponse>

The response object.

Defined in: lib/PetitioRequest.ts:288


text#

â–¸ text(): Promise<string>

Returns: Promise<string>

The raw response body as a string.

Defined in: lib/PetitioRequest.ts:278


timeout#

â–¸ timeout(timeout: number): PetitioRequest

see TimeoutOptions.bodyTimeout

Parameters:#

NameTypeDescription
timeoutnumberThe timeout (in milliseconds) to set the bodyTimeout to.

Returns: PetitioRequest

Defined in: lib/PetitioRequest.ts:225

â–¸ timeout(timeout: keyof TimeoutOptions, time: number): PetitioRequest

see TimeoutOptions

Parameters:#

NameTypeDescription
timeoutkeyof TimeoutOptionsThe timeout option to change.
timenumberThe number of milliseconds to set the timeout to.

Returns: PetitioRequest

Defined in: lib/PetitioRequest.ts:231

Last updated on