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.

110 lines (109 loc) 2.97 kB
import type { ICoreResponse } from '../core_base/types.js'; import type { HttpMethod, ResponseType, FormField } 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 SmartRequestClient<T = any> { private _url; private _options; private _retries; private _queryParams; private _paginationConfig?; /** * Create a new SmartRequestClient instance */ static create<T = any>(): SmartRequestClient<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 request timeout in milliseconds */ timeout(ms: number): this; /** * Set number of retry attempts */ retry(count: number): 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 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; }