@foundatiofx/fetchclient
Version:
A typed JSON fetch client with middleware support for Deno, Node and the browser.
133 lines (132 loc) • 5.41 kB
JavaScript
import { defaultInstance as defaultProvider, } from "./FetchClientProvider.js";
let getCurrentProviderFunc = () => null;
/**
* Gets a FetchClient instance from the current provider.
* @returns The FetchClient instance.
*/
export function useFetchClient(options) {
return getCurrentProvider().getFetchClient(options);
}
/**
* 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 function getJSON(url, options) {
return useFetchClient().getJSON(url, options);
}
/**
* 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 function postJSON(url, body, options) {
return useFetchClient().postJSON(url, body, options);
}
/**
* 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 function putJSON(url, body, options) {
return useFetchClient().putJSON(url, body, options);
}
/**
* 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 function patchJSON(url, body, options) {
return useFetchClient().patchJSON(url, body, options);
}
/**
* 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 function deleteJSON(url, options) {
return useFetchClient().deleteJSON(url, options);
}
/**
* Gets the current FetchClientProvider.
* @returns The current FetchClientProvider.
*/
export function getCurrentProvider() {
if (getCurrentProviderFunc === null) {
return defaultProvider;
}
return getCurrentProviderFunc() ?? defaultProvider;
}
/**
* 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 function setCurrentProviderFunc(getProviderFunc) {
getCurrentProviderFunc = getProviderFunc;
}
/**
* Sets the base URL for any FetchClient instances created by the current provider.
* @param baseUrl - The base URL to use for requests.
*/
export function setBaseUrl(baseUrl) {
getCurrentProvider().setBaseUrl(baseUrl);
}
/**
* Sets the access token function for any FetchClient instances created by the current provider.
* @param accessTokenFunc - The function that retrieves the access token.
*/
export function setAccessTokenFunc(accessTokenFunc) {
getCurrentProvider().setAccessTokenFunc(accessTokenFunc);
}
/**
* Sets the model validator function for any FetchClient instances created by the current provider.
* @param validate - The function that validates the model.
*/
export function setModelValidator(validate) {
getCurrentProvider().setModelValidator(validate);
}
/**
* Adds a middleware to any FetchClient instances created by the current provider.
* @param middleware - The middleware function to be added.
*/
export function useMiddleware(middleware) {
getCurrentProvider().useMiddleware(middleware);
}
/**
* 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 function setRequestOptions(options) {
getCurrentProvider().applyOptions({ defaultRequestOptions: options });
}
/**
* Enables rate limiting for any FetchClient instances created by the current provider.
* @param options - The rate limiting configuration options.
*/
export function useRateLimit(options) {
getCurrentProvider().useRateLimit(options);
}
/**
* Enables per-domain rate limiting for any FetchClient instances created by the current provider.
* @param options - The rate limiting configuration options.
*/
export function usePerDomainRateLimit(options) {
getCurrentProvider().usePerDomainRateLimit(options);
}