UNPKG

@push.rocks/smartrequest

Version:

A module for modern HTTP/HTTPS requests with support for form data, file uploads, JSON, binary data, streams, and more.

134 lines (133 loc) 3.86 kB
import type { ICoreResponse } from '../core_base/types.js'; import type { ICoreRequestOptions } from '../core_base/types.js'; import type { HttpMethod, ResponseType, FormField, RateLimitConfig } from './types/common.js'; import { type TPaginationConfig, type OffsetPaginationConfig, type CursorPaginationConfig, type CustomPaginationConfig, type TPaginatedResponse } from './types/pagination.js'; /** * Modern fluent client for making HTTP requests */ export declare class SmartRequest<T = any> { private _url; private _options; private _retries; private _queryParams; private _paginationConfig?; private _rateLimitConfig?; /** * Create a new SmartRequest instance */ static create<T = any>(): SmartRequest<T>; /** * Set the URL for the request */ url(url: string): this; /** * Set the HTTP method */ method(method: HttpMethod): this; /** * Set JSON body for the request */ json(data: any): this; /** * Set form data for the request */ formData(data: FormField[]): this; /** * Set raw buffer data for the request */ buffer(data: Buffer | Uint8Array, contentType?: string): this; /** * Stream data for the request * Accepts Node.js Readable streams or web ReadableStream */ stream(stream: NodeJS.ReadableStream | ReadableStream<Uint8Array>, contentType?: string): this; /** * Set request timeout in milliseconds */ timeout(ms: number): this; /** * Set number of retry attempts */ retry(count: number): this; /** * Enable automatic 429 (Too Many Requests) handling with configurable backoff */ handle429Backoff(config?: RateLimitConfig): this; /** * Set HTTP headers */ headers(headers: Record<string, string>): this; /** * Set a single HTTP header */ header(name: string, value: string): this; /** * Set query parameters */ query(params: Record<string, string>): this; /** * Set additional request options */ options(options: Partial<ICoreRequestOptions>): this; /** * Enable or disable auto-drain for unconsumed response bodies (Node.js only) * Default is true to prevent socket hanging */ autoDrain(enabled: boolean): this; /** * Set the Accept header to indicate what content type is expected */ accept(type: ResponseType): this; /** * Configure pagination for requests */ pagination(config: TPaginationConfig): this; /** * Configure offset-based pagination (page & limit) */ withOffsetPagination(config?: Omit<OffsetPaginationConfig, 'strategy'>): this; /** * Configure cursor-based pagination */ withCursorPagination(config?: Omit<CursorPaginationConfig, 'strategy'>): this; /** * Configure Link header-based pagination */ withLinkPagination(): this; /** * Configure custom pagination */ withCustomPagination(config: Omit<CustomPaginationConfig, 'strategy'>): this; /** * Make a GET request */ get<R = T>(): Promise<ICoreResponse<R>>; /** * Make a POST request */ post<R = T>(): Promise<ICoreResponse<R>>; /** * Make a PUT request */ put<R = T>(): Promise<ICoreResponse<R>>; /** * Make a DELETE request */ delete<R = T>(): Promise<ICoreResponse<R>>; /** * Make a PATCH request */ patch<R = T>(): Promise<ICoreResponse<R>>; /** * Get paginated response */ getPaginated<ItemType = T>(): Promise<TPaginatedResponse<ItemType>>; /** * Get all pages at once (use with caution for large datasets) */ getAllPages<ItemType = T>(): Promise<ItemType[]>; /** * Execute the HTTP request */ private execute; }