@foundatiofx/fetchclient
Version:
A typed JSON fetch client with middleware support for Deno, Node and the browser.
156 lines • 7.27 kB
TypeScript
import type { GetRequestOptions, RequestOptions } from "./RequestOptions.js";
import type { FetchClientResponse } from "./FetchClientResponse.js";
import type { FetchClientMiddleware } from "./FetchClientMiddleware.js";
import type { FetchClientCache } from "./FetchClientCache.js";
import { FetchClientProvider } from "./FetchClientProvider.js";
import type { FetchClientOptions } from "./FetchClientOptions.js";
import { type IObjectEvent } from "./ObjectEvent.js";
type Fetch = typeof globalThis.fetch;
/**
* Represents a client for making HTTP requests using the Fetch API.
*/
export declare class FetchClient {
#private;
/**
* Represents a FetchClient that handles HTTP requests using the Fetch API.
* @param options - The options to use for the FetchClient.
*/
constructor(optionsOrProvider?: FetchClientOptions | FetchClientProvider);
/**
* Gets the provider used by this FetchClient instance. The provider contains shared options that can be used by multiple FetchClient instances.
*/
get provider(): FetchClientProvider;
/**
* Gets the options used by this FetchClient instance.
*/
get options(): FetchClientOptions;
/**
* Gets the cache used for storing HTTP responses.
*/
get cache(): FetchClientCache;
/**
* Gets the fetch implementation used for making HTTP requests.
*/
get fetch(): Fetch | undefined;
/**
* Gets the number of inflight requests for this FetchClient instance.
*/
get requestCount(): number;
/**
* Gets a value indicating whether the client is currently loading.
* @returns {boolean} A boolean value indicating whether the client is loading.
*/
get isLoading(): boolean;
/**
* Gets an event that is triggered when the loading state changes.
*/
get loading(): IObjectEvent<boolean>;
/**
* Adds one or more middleware functions to the FetchClient's middleware pipeline.
* Middleware functions are executed in the order they are added.
*
* @param mw - The middleware functions to add.
*/
use(...mw: FetchClientMiddleware[]): FetchClient;
/**
* Sends a GET request to the specified URL.
*
* @param url - The URL to send the GET request to.
* @param options - The optional request options.
* @returns A promise that resolves to the response of the GET request.
*/
get(url: string, options?: GetRequestOptions): Promise<FetchClientResponse<unknown>>;
/**
* Sends a GET request to the specified URL and returns the response as JSON.
* @param url - The URL to send the GET request to.
* @param options - Optional request options.
* @returns A promise that resolves to the response as JSON.
*/
getJSON<T>(url: string, options?: GetRequestOptions): Promise<FetchClientResponse<T>>;
/**
* Sends a POST request to the specified URL.
*
* @param url - The URL to send the request to.
* @param body - The request body, can be an object, a string, or FormData.
* @param options - Additional options for the request.
* @returns A promise that resolves to a FetchClientResponse object.
*/
post(url: string, body?: object | string | FormData, options?: RequestOptions): Promise<FetchClientResponse<unknown>>;
/**
* Sends a POST request with JSON payload to the specified URL.
*
* @template T - The type of the response data.
* @param {string} url - The URL to send the request to.
* @param {object | string | FormData} [body] - The JSON payload or form data to send with the request.
* @param {RequestOptions} [options] - Additional options for the request.
* @returns {Promise<FetchClientResponse<T>>} - A promise that resolves to the response data.
*/
postJSON<T>(url: string, body?: object | string | FormData, options?: RequestOptions): Promise<FetchClientResponse<T>>;
/**
* Sends a PUT request to the specified URL with the given body and options.
* @param url - The URL to send the request to.
* @param body - The request body, can be an object, a string, or FormData.
* @param options - The request options.
* @returns A promise that resolves to a FetchClientResponse object.
*/
put(url: string, body?: object | string | FormData, options?: RequestOptions): Promise<FetchClientResponse<unknown>>;
/**
* Sends a PUT request with JSON payload to the specified URL.
*
* @template T - The type of the response data.
* @param {string} url - The URL to send the request to.
* @param {object | string} [body] - The JSON payload to send with the request.
* @param {RequestOptions} [options] - Additional options for the request.
* @returns {Promise<FetchClientResponse<T>>} - A promise that resolves to the response data.
*/
putJSON<T>(url: string, body?: object | string, options?: RequestOptions): Promise<FetchClientResponse<T>>;
/**
* Sends a PATCH request to the specified URL with the provided body and options.
* @param url - The URL to send the PATCH request to.
* @param body - The body of the request. It can be an object, a string, or FormData.
* @param options - The options for the request.
* @returns A Promise that resolves to the response of the PATCH request.
*/
patch(url: string, body?: object | string | FormData, options?: RequestOptions): Promise<Response>;
/**
* Sends a PATCH request with JSON payload to the specified URL.
*
* @template T - The type of the response data.
* @param {string} url - The URL to send the request to.
* @param {object | string} [body] - The JSON payload to send with the request.
* @param {RequestOptions} [options] - Additional options for the request.
* @returns {Promise<FetchClientResponse<T>>} - A promise that resolves to the response data.
*/
patchJSON<T>(url: string, body?: object | string, options?: RequestOptions): Promise<FetchClientResponse<T>>;
/**
* Sends a DELETE request to the specified URL.
*
* @param url - The URL to send the DELETE request to.
* @param options - The options for the request.
* @returns A promise that resolves to a `FetchClientResponse` object.
*/
delete(url: string, options?: RequestOptions): Promise<FetchClientResponse<unknown>>;
/**
* Sends a DELETE request with JSON payload to the specified URL.
*
* @template T - The type of the response data.
* @param {string} url - The URL to send the request to.
* @param {RequestOptions} [options] - Additional options for the request.
* @returns {Promise<FetchClientResponse<T>>} - A promise that resolves to the response data.
*/
deleteJSON<T>(url: string, options?: RequestOptions): Promise<FetchClientResponse<T>>;
private validate;
private fetchInternal;
private invokeMiddleware;
private mergeAbortSignals;
private getJSONResponse;
private reviveJsonValue;
private tryParseDate;
private buildRequestInit;
private buildJsonRequestOptions;
private problemToResponse;
private buildUrl;
private validateResponse;
}
export {};
//# sourceMappingURL=FetchClient.d.ts.map