undici-extra
- Version 1.2.0
- Published
- 43.1 kB
- 2 dependencies
- MIT license
Install
npm i undici-extrayarn add undici-extrapnpm add undici-extraOverview
Extra features for Undici with an elegant API, smart dispatcher, robust retries, and more.
Index
Variables
Functions
Classes
Interfaces
Type Aliases
Variables
variable DEFAULT_RETRY_OPTIONS
const DEFAULT_RETRY_OPTIONS: Required<RetryOptions>;variable undici
const undici: UndiciInstance;Functions
function normalizeRetryOptions
normalizeRetryOptions: (retry?: number | RetryOptions) => RetryOptions;Classes
class HTTPError
class HTTPError extends Error {}Thrown when a request fails with a non-2xx status code.
constructor
constructor(response: Response, request: Request);property request
readonly request: Request;property response
readonly response: Response;class Undici
class Undici {}constructor
constructor(options?: UndiciOptions);property options
readonly options: UndiciOptions;property queueSize
readonly queueSize: number;method execute
execute: ( input: string | URL | Request, options?: UndiciOptions) => Promise<Response>;method paginate
paginate: <T = any>( input: string | URL | Request, options?: UndiciOptions) => AsyncIterable<T>;Interfaces
interface Hooks
interface Hooks {}property afterResponse
afterResponse?: AfterResponseHook[];Hooks to run after the response is received.
property beforeRequest
beforeRequest?: BeforeRequestHook[];Hooks to run before the request is sent.
property beforeRetry
beforeRetry?: BeforeRetryHook[];Hooks to run before each retry attempt.
interface ResponsePromise
interface ResponsePromise extends Promise<Response> {}method arrayBuffer
arrayBuffer: () => Promise<ArrayBuffer>;Parse response as ArrayBuffer.
method blob
blob: () => Promise<Blob>;Parse response as Blob.
method formData
formData: () => Promise<FormData>;Parse response as FormData.
method json
json: <T = unknown>() => Promise<T>;Parse response as JSON.
method ndjson
ndjson: <T = unknown>() => AsyncIterable<T>;Iterate over Newline-Delimited JSON (NDJSON) stream.
Example 1
for await (const item of undici('...').ndjson()) { ... }
method pipe
pipe: (destination: NodeJS.WritableStream) => Promise<void>;Pipes the response body to a Node.js Writable stream. Resolves when the stream finishes or rejects on error.
Example 1
await undici('...').pipe(fs.createWriteStream('file.txt'));
method stream
stream: () => Promise<Readable>;Returns the response body as a Node.js Readable stream.
Example 1
const stream = await undici('...').stream(); stream.pipe(fs.createWriteStream('file.txt'));
method text
text: () => Promise<string>;Parse response as text.
interface RetryOptions
interface RetryOptions {}property backoffLimit
backoffLimit?: number;property delay
delay?: (retryCount: number) => number;property errorCodes
errorCodes?: string[];property limit
limit?: number;property methods
methods?: string[];property statusCodes
statusCodes?: number[];interface UndiciOptions
interface UndiciOptions extends RequestInit {}property debug
debug?: boolean;Log equivalent cURL command.
property dedup
dedup?: boolean;Coalesce concurrent requests to the same URL/method.
property hooks
hooks?: Hooks;Lifecycle hooks.
property json
json?: unknown;Shortcut for sending JSON body. Sets 'content-type' to 'application/json'.
property pagination
pagination?: { /** Transform response into an array of items. */ transform?: (response: Response) => any | Promise<any>; /** Return next URL or false to stop. */ paginate?: ( response: Response, allItems: any[], currentItems: any[] ) => string | URL | false | Promise<string | URL | false>;};Pagination configuration for
.paginate().
property prefixUrl
prefixUrl?: string | URL;Base URL for the request.
property proxy
proxy?: ProxyInput;HTTP/HTTPS Proxy URL or Options. Automatically creates and caches an
undici.ProxyAgent.
property retry
retry?: number | RetryOptions;Retry configuration. 2
property searchParams
searchParams?: | string | Record<string, string | number | boolean | undefined | null> | URLSearchParams;Search parameters to append to the URL.
property throttle
throttle?: Options;Rate limit configuration. If provided, requests created from this client instance will be throttled.
property throwHttpErrors
throwHttpErrors?: boolean;Throw HTTPError for non-2xx responses. true
property timeout
timeout?: number;Request timeout in milliseconds.
property unixSocket
unixSocket?: string;Path to a Unix domain socket.
Type Aliases
type AfterResponseHook
type AfterResponseHook = ( request: Request, options: UndiciOptions, response: Response) => Response | Promise<Response> | void | Promise<void>;Called after a response is received. Can return a Response to override it.
type BeforeRequestHook
type BeforeRequestHook = ( request: Request, options: UndiciOptions) => Request | Response | Promise<Request | Response> | void | Promise<void>;Called before a request is sent. Can return a Request to override it or a Response to short-circuit.
type BeforeRetryHook
type BeforeRetryHook = (options: { request: Request; options: UndiciOptions; error: Error; retryCount: number;}) => void | Promise<void>;Called before a retry attempt.
type UndiciInstance
type UndiciInstance = { /** * Make an HTTP request with extra features. * @example await undici('https://api.com/users').json() */ (input: string | URL | Request, options?: UndiciOptions): ResponsePromise; /** Make a GET request. */ get(input: string | URL | Request, options?: UndiciOptions): ResponsePromise; /** Make a POST request. */ post(input: string | URL | Request, options?: UndiciOptions): ResponsePromise; /** Make a PUT request. */ put(input: string | URL | Request, options?: UndiciOptions): ResponsePromise; /** Make a PATCH request. */ patch(input: string | URL | Request, options?: UndiciOptions): ResponsePromise; /** Make a DELETE request. */ delete(input: string | URL | Request, options?: UndiciOptions): ResponsePromise; /** Make a HEAD request. */ head(input: string | URL | Request, options?: UndiciOptions): ResponsePromise; /** Create a new instance with default options. */ create(defaultOptions: UndiciOptions): UndiciInstance; /** Create a new instance by merging options with the current instance. */ extend(extendedOptions: UndiciOptions): UndiciInstance; /** Returns the number of requests currently queued by the throttler. */ readonly queueSize: number; /** * Paginate through an API. * @example * for await (const item of undici.paginate('events', { * pagination: { * paginate: (res) => res.json().then(data => data.next_url) * } * })) { ... } */ paginate<T = any>( input: string | URL | Request, options?: UndiciOptions ): AsyncIterable<T>;};Package Files (1)
Dependencies (2)
Dev Dependencies (11)
Peer Dependencies (0)
No peer dependencies.
Badge
To add a badge like this oneto your package's README, use the codes available below.
You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/undici-extra.
- Markdown[](https://www.jsdocs.io/package/undici-extra)
- HTML<a href="https://www.jsdocs.io/package/undici-extra"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>
- Updated .
Package analyzed in 3208 ms. - Missing or incorrect documentation? Open an issue for this package.
