UNPKG

@exceptionless/fetchclient

Version:

A simple fetch client with middleware support for Deno and the browser.

150 lines (149 loc) 6.03 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.useFetchClient = useFetchClient; exports.getJSON = getJSON; exports.postJSON = postJSON; exports.putJSON = putJSON; exports.patchJSON = patchJSON; exports.deleteJSON = deleteJSON; exports.getCurrentProvider = getCurrentProvider; exports.setCurrentProviderFunc = setCurrentProviderFunc; exports.setBaseUrl = setBaseUrl; exports.setAccessTokenFunc = setAccessTokenFunc; exports.setModelValidator = setModelValidator; exports.useMiddleware = useMiddleware; exports.setRequestOptions = setRequestOptions; exports.useRateLimit = useRateLimit; exports.usePerDomainRateLimit = usePerDomainRateLimit; const FetchClientProvider_js_1 = require("./FetchClientProvider.js"); let getCurrentProviderFunc = () => null; /** * Gets a FetchClient instance from the current provider. * @returns The FetchClient instance. */ 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. */ 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. */ 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. */ 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. */ 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. */ function deleteJSON(url, options) { return useFetchClient().deleteJSON(url, options); } /** * Gets the current FetchClientProvider. * @returns The current FetchClientProvider. */ function getCurrentProvider() { if (getCurrentProviderFunc === null) { return FetchClientProvider_js_1.defaultInstance; } return getCurrentProviderFunc() ?? FetchClientProvider_js_1.defaultInstance; } /** * 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 */ 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. */ 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. */ 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. */ 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. */ 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. */ 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. */ 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. */ function usePerDomainRateLimit(options) { getCurrentProvider().usePerDomainRateLimit(options); }