@foundatiofx/fetchclient
Version:
A typed JSON fetch client with middleware support for Deno, Node and the browser.
106 lines • 5.76 kB
TypeScript
import type { FetchClient } from "./FetchClient.js";
import type { FetchClientMiddleware } from "./FetchClientMiddleware.js";
import type { FetchClientOptions } from "./FetchClientOptions.js";
import { type FetchClientProvider } from "./FetchClientProvider.js";
import type { FetchClientResponse } from "./FetchClientResponse.js";
import type { ProblemDetails } from "./ProblemDetails.js";
import type { RateLimitMiddlewareOptions } from "./RateLimitMiddleware.js";
import type { GetRequestOptions, RequestOptions } from "./RequestOptions.js";
/**
* Gets a FetchClient instance from the current provider.
* @returns The FetchClient instance.
*/
export declare function useFetchClient(options?: FetchClientOptions): FetchClient;
/**
* Sends a GET request to the specified URL using the default client and provider 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.
*/
export declare function getJSON<T>(url: string, options?: GetRequestOptions): Promise<FetchClientResponse<T>>;
/**
* Sends a POST request with JSON payload using the default client and provider 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.
*/
export declare function postJSON<T>(url: string, body?: object | string | FormData, options?: RequestOptions): Promise<FetchClientResponse<T>>;
/**
* Sends a PUT request with JSON payload using the default client and provider 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.
*/
export declare function putJSON<T>(url: string, body?: object | string, options?: RequestOptions): Promise<FetchClientResponse<T>>;
/**
* Sends a PATCH request with JSON payload using the default client and provider 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.
*/
export declare function patchJSON<T>(url: string, body?: object | string, options?: RequestOptions): Promise<FetchClientResponse<T>>;
/**
* Sends a DELETE request with JSON payload using the default client and provider 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.
*/
export declare function deleteJSON<T>(url: string, options?: RequestOptions): Promise<FetchClientResponse<T>>;
/**
* Gets the current FetchClientProvider.
* @returns The current FetchClientProvider.
*/
export declare function getCurrentProvider(): FetchClientProvider;
/**
* Sets the function that retrieves the current FetchClientProvider using whatever scoping mechanism is available.
* @param getProviderFunc - The function that retrieves the current FetchClientProvider.
* @returns void
*/
export declare function setCurrentProviderFunc(getProviderFunc: () => FetchClientProvider | null): void;
/**
* Sets the base URL for any FetchClient instances created by the current provider.
* @param baseUrl - The base URL to use for requests.
*/
export declare function setBaseUrl(baseUrl: string): void;
/**
* Sets the access token function for any FetchClient instances created by the current provider.
* @param accessTokenFunc - The function that retrieves the access token.
*/
export declare function setAccessTokenFunc(accessTokenFunc: () => string | null): void;
/**
* Sets the model validator function for any FetchClient instances created by the current provider.
* @param validate - The function that validates the model.
*/
export declare function setModelValidator(validate: (model: object | null) => Promise<ProblemDetails | null>): void;
/**
* Adds a middleware to any FetchClient instances created by the current provider.
* @param middleware - The middleware function to be added.
*/
export declare function useMiddleware(middleware: FetchClientMiddleware): void;
/**
* Sets the default request options for any FetchClient instances created by the current provider.
* @param options - The options to set as the default request options.
*/
export declare function setRequestOptions(options: RequestOptions): void;
/**
* Enables rate limiting for any FetchClient instances created by the current provider.
* @param options - The rate limiting configuration options.
*/
export declare function useRateLimit(options: RateLimitMiddlewareOptions): void;
/**
* Enables per-domain rate limiting for any FetchClient instances created by the current provider.
* @param options - The rate limiting configuration options.
*/
export declare function usePerDomainRateLimit(options: Omit<RateLimitMiddlewareOptions, "getGroupFunc">): void;
//# sourceMappingURL=DefaultHelpers.d.ts.map