Class: PetitioRequest#
PetitioRequest.PetitioRequest
Constructors#
constructor#
+ new PetitioRequest(url: string | URL, httpMethod?: HTTPMethod): PetitioRequest
Parameters:#
| Name | Type | Default value | Description |
|---|---|---|---|
url | string | URL | - | The URL to start composing a request for. |
httpMethod | HTTPMethod | "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
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:#
| Name | Type | Description |
|---|---|---|
data | string | Buffer | The 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:#
| Name | Type | Description |
|---|---|---|
data | Record<string, any> | The data to be set for the request body. |
sendAs? | json | If 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:#
| Name | Type | Description |
|---|---|---|
data | string | ParsedUrlQueryInput | The data to be set for the request body. |
sendAs | form | If 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:#
| Name | Type | Description |
|---|---|---|
data | Readable | The data to be set for the request body. |
sendAs | stream | If 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:#
| Name | Type | Description |
|---|---|---|
client | Client | The Undici client instance you wish to use. |
keepAlive? | boolean | Whether 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:#
| Name | Type | Description |
|---|---|---|
header | string | The encoded header name to set. |
value | string | The value to set the header to. |
Returns: PetitioRequest
Defined in: lib/PetitioRequest.ts:196
â–¸ header(header: Record<string, string>): PetitioRequest
Parameters:#
| Name | Type | Description |
|---|---|---|
header | Record<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:#
| Name | Default | Description |
|---|---|---|
T | any | Type 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:#
| Name | Type | Description |
|---|---|---|
method | HTTPMethod | The 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:#
| Name | Type | Description |
|---|---|---|
key | Options | An 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:#
| Name | Type |
|---|---|
T | keyof Options |
Parameters:#
| Name | Type | Description |
|---|---|---|
key | T | The client options key to set. |
value | Options[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:#
| Name | Type | Description |
|---|---|---|
relativePath | string | A 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:#
| Name | Type | Description |
|---|---|---|
key | string | The query key to use for the URL query parameters. |
value | any | The 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:#
| Name | Type | Description |
|---|---|---|
key | Record<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:#
| Name | Type | Description |
|---|---|---|
timeout | number | The 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:#
| Name | Type | Description |
|---|---|---|
timeout | keyof TimeoutOptions | The timeout option to change. |
time | number | The number of milliseconds to set the timeout to. |
Returns: PetitioRequest
Defined in: lib/PetitioRequest.ts:231