@exceptionless/fetchclient
Advanced tools
Comparing version 0.16.0 to 0.17.0
import { defaultInstance as defaultProvider, } from "./FetchClientProvider.js"; | ||
let getCurrentProviderFunc = () => null; | ||
/** | ||
* Gets the FetchClientProvider for the current scope. | ||
* Gets a FetchClient instance. | ||
* @returns The FetchClient instance. | ||
*/ | ||
export function useFetchClient(options) { | ||
return getCurrentProvider().getFetchClient(options); | ||
} | ||
/** | ||
* Gets the current FetchClientProvider. | ||
* @returns The current FetchClientProvider. | ||
@@ -56,97 +63,1 @@ */ | ||
} | ||
/** | ||
* Sends a GET request to the specified URL. (Uses settings from the current provider) | ||
* | ||
* @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. | ||
*/ | ||
export function get(url, options) { | ||
return getCurrentProvider().getFetchClient().get(url, options); | ||
} | ||
/** | ||
* Sends a GET request to the specified URL and returns the response as JSON. (Uses settings from the current provider) | ||
* @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 getCurrentProvider().getFetchClient().getJSON(url, options); | ||
} | ||
/** | ||
* Sends a POST request to the specified URL. (Uses settings from the current provider) | ||
* | ||
* @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. | ||
*/ | ||
export function post(url, body, options) { | ||
return getCurrentProvider().getFetchClient().post(url, body, options); | ||
} | ||
/** | ||
* Sends a POST request with JSON payload to the specified URL. (Uses settings from the current provider) | ||
* | ||
* @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 postJSON(url, body, options) { | ||
return getCurrentProvider().getFetchClient().postJSON(url, body, options); | ||
} | ||
/** | ||
* Sends a PUT request to the specified URL with the given body and options. (Uses settings from the current provider) | ||
* @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. | ||
*/ | ||
export function put(url, body, options) { | ||
return getCurrentProvider().getFetchClient().put(url, body, options); | ||
} | ||
/** | ||
* Sends a PUT request with JSON payload to the specified URL. (Uses settings from the current provider) | ||
* | ||
* @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 getCurrentProvider().getFetchClient().putJSON(url, body, options); | ||
} | ||
/** | ||
* Sends a PATCH request to the specified URL with the provided body and options. (Uses settings from the current provider) | ||
* @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. | ||
*/ | ||
export function patch(url, body, options) { | ||
return getCurrentProvider().getFetchClient().patch(url, body, options); | ||
} | ||
/** | ||
* Sends a PATCH request with JSON payload to the specified URL. (Uses settings from the current provider) | ||
* | ||
* @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 getCurrentProvider().getFetchClient().patchJSON(url, body, options); | ||
} | ||
/** | ||
* Sends a DELETE request to the specified URL. (Uses settings from the current provider) | ||
* | ||
* @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. | ||
*/ | ||
export function deleteRequest(url, options) { | ||
return getCurrentProvider().getFetchClient().delete(url, options); | ||
} |
@@ -46,2 +46,18 @@ /** | ||
/** | ||
* Deletes all responses from the cache that have keys beginning with the specified key. | ||
* @param prefix - The cache key prefix. | ||
* @returns The number of responses that were deleted. | ||
*/ | ||
deleteAll(prefix) { | ||
let count = 0; | ||
for (const key of this.cache.keys()) { | ||
if (key.startsWith(this.getHash(prefix))) { | ||
if (this.cache.delete(key)) { | ||
count++; | ||
} | ||
} | ||
} | ||
return count; | ||
} | ||
/** | ||
* Checks if a response exists in the cache with the specified key. | ||
@@ -68,4 +84,7 @@ * @param key - The cache key. | ||
getHash(key) { | ||
return key.join(":"); | ||
if (key instanceof Array) { | ||
return key.join(":"); | ||
} | ||
return key; | ||
} | ||
} |
@@ -77,5 +77,14 @@ import { FetchClient } from "./FetchClient.js"; | ||
* Creates a new instance of FetchClient using the current provider. | ||
* @param options - The options to use for the FetchClient instance. | ||
* @returns A new instance of FetchClient. | ||
*/ | ||
getFetchClient() { | ||
getFetchClient(options) { | ||
if (options) { | ||
options = { | ||
...this.#options, | ||
...options, | ||
}; | ||
options.provider = this; | ||
return new FetchClient(options); | ||
} | ||
return new FetchClient(this); | ||
@@ -82,0 +91,0 @@ } |
{ | ||
"name": "@exceptionless/fetchclient", | ||
"version": "0.16.0", | ||
"version": "0.17.0", | ||
"description": "A simple fetch client with middleware support for Deno and the browser.", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -42,3 +42,3 @@ <!-- deno-fmt-ignore-file --> | ||
```ts | ||
import { getJSON } from '@exceptionless/fetchclient'; | ||
import { useFetchClient } from '@exceptionless/fetchclient'; | ||
@@ -49,3 +49,3 @@ type Products = { | ||
const response = await getJSON<Products>( | ||
const response = await useFetchClient().getJSON<Products>( | ||
`https://dummyjson.com/products/search?q=iphone&limit=10`, | ||
@@ -52,0 +52,0 @@ ); |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.deleteRequest = exports.patchJSON = exports.patch = exports.putJSON = exports.put = exports.postJSON = exports.post = exports.getJSON = exports.get = exports.setRequestOptions = exports.useMiddleware = exports.setModelValidator = exports.setAccessTokenFunc = exports.setBaseUrl = exports.setCurrentProviderFunc = exports.getCurrentProvider = void 0; | ||
exports.setRequestOptions = exports.useMiddleware = exports.setModelValidator = exports.setAccessTokenFunc = exports.setBaseUrl = exports.setCurrentProviderFunc = exports.getCurrentProvider = exports.useFetchClient = void 0; | ||
const FetchClientProvider_js_1 = require("./FetchClientProvider.js"); | ||
let getCurrentProviderFunc = () => null; | ||
/** | ||
* Gets the FetchClientProvider for the current scope. | ||
* Gets a FetchClient instance. | ||
* @returns The FetchClient instance. | ||
*/ | ||
function useFetchClient(options) { | ||
return getCurrentProvider().getFetchClient(options); | ||
} | ||
exports.useFetchClient = useFetchClient; | ||
/** | ||
* Gets the current FetchClientProvider. | ||
* @returns The current FetchClientProvider. | ||
@@ -66,106 +74,1 @@ */ | ||
exports.setRequestOptions = setRequestOptions; | ||
/** | ||
* Sends a GET request to the specified URL. (Uses settings from the current provider) | ||
* | ||
* @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. | ||
*/ | ||
function get(url, options) { | ||
return getCurrentProvider().getFetchClient().get(url, options); | ||
} | ||
exports.get = get; | ||
/** | ||
* Sends a GET request to the specified URL and returns the response as JSON. (Uses settings from the current provider) | ||
* @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 getCurrentProvider().getFetchClient().getJSON(url, options); | ||
} | ||
exports.getJSON = getJSON; | ||
/** | ||
* Sends a POST request to the specified URL. (Uses settings from the current provider) | ||
* | ||
* @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. | ||
*/ | ||
function post(url, body, options) { | ||
return getCurrentProvider().getFetchClient().post(url, body, options); | ||
} | ||
exports.post = post; | ||
/** | ||
* Sends a POST request with JSON payload to the specified URL. (Uses settings from the current provider) | ||
* | ||
* @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 postJSON(url, body, options) { | ||
return getCurrentProvider().getFetchClient().postJSON(url, body, options); | ||
} | ||
exports.postJSON = postJSON; | ||
/** | ||
* Sends a PUT request to the specified URL with the given body and options. (Uses settings from the current provider) | ||
* @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. | ||
*/ | ||
function put(url, body, options) { | ||
return getCurrentProvider().getFetchClient().put(url, body, options); | ||
} | ||
exports.put = put; | ||
/** | ||
* Sends a PUT request with JSON payload to the specified URL. (Uses settings from the current provider) | ||
* | ||
* @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 getCurrentProvider().getFetchClient().putJSON(url, body, options); | ||
} | ||
exports.putJSON = putJSON; | ||
/** | ||
* Sends a PATCH request to the specified URL with the provided body and options. (Uses settings from the current provider) | ||
* @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. | ||
*/ | ||
function patch(url, body, options) { | ||
return getCurrentProvider().getFetchClient().patch(url, body, options); | ||
} | ||
exports.patch = patch; | ||
/** | ||
* Sends a PATCH request with JSON payload to the specified URL. (Uses settings from the current provider) | ||
* | ||
* @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 getCurrentProvider().getFetchClient().patchJSON(url, body, options); | ||
} | ||
exports.patchJSON = patchJSON; | ||
/** | ||
* Sends a DELETE request to the specified URL. (Uses settings from the current provider) | ||
* | ||
* @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. | ||
*/ | ||
function deleteRequest(url, options) { | ||
return getCurrentProvider().getFetchClient().delete(url, options); | ||
} | ||
exports.deleteRequest = deleteRequest; |
@@ -49,2 +49,18 @@ "use strict"; | ||
/** | ||
* Deletes all responses from the cache that have keys beginning with the specified key. | ||
* @param prefix - The cache key prefix. | ||
* @returns The number of responses that were deleted. | ||
*/ | ||
deleteAll(prefix) { | ||
let count = 0; | ||
for (const key of this.cache.keys()) { | ||
if (key.startsWith(this.getHash(prefix))) { | ||
if (this.cache.delete(key)) { | ||
count++; | ||
} | ||
} | ||
} | ||
return count; | ||
} | ||
/** | ||
* Checks if a response exists in the cache with the specified key. | ||
@@ -71,5 +87,8 @@ * @param key - The cache key. | ||
getHash(key) { | ||
return key.join(":"); | ||
if (key instanceof Array) { | ||
return key.join(":"); | ||
} | ||
return key; | ||
} | ||
} | ||
exports.FetchClientCache = FetchClientCache; |
@@ -80,5 +80,14 @@ "use strict"; | ||
* Creates a new instance of FetchClient using the current provider. | ||
* @param options - The options to use for the FetchClient instance. | ||
* @returns A new instance of FetchClient. | ||
*/ | ||
getFetchClient() { | ||
getFetchClient(options) { | ||
if (options) { | ||
options = { | ||
...this.#options, | ||
...options, | ||
}; | ||
options.provider = this; | ||
return new FetchClient_js_1.FetchClient(options); | ||
} | ||
return new FetchClient_js_1.FetchClient(this); | ||
@@ -85,0 +94,0 @@ } |
@@ -0,8 +1,13 @@ | ||
import type { FetchClient, FetchClientOptions } from "./FetchClient.js"; | ||
import type { FetchClientMiddleware } from "./FetchClientMiddleware.js"; | ||
import { type FetchClientProvider } from "./FetchClientProvider.js"; | ||
import type { FetchClientResponse } from "./FetchClientResponse.js"; | ||
import type { ProblemDetails } from "./ProblemDetails.js"; | ||
import type { GetRequestOptions, RequestOptions } from "./RequestOptions.js"; | ||
import type { RequestOptions } from "./RequestOptions.js"; | ||
/** | ||
* Gets the FetchClientProvider for the current scope. | ||
* Gets a FetchClient instance. | ||
* @returns The FetchClient instance. | ||
*/ | ||
export declare function useFetchClient(options?: FetchClientOptions): FetchClient; | ||
/** | ||
* Gets the current FetchClientProvider. | ||
* @returns The current FetchClientProvider. | ||
@@ -42,80 +47,2 @@ */ | ||
export declare function setRequestOptions(options: RequestOptions): void; | ||
/** | ||
* Sends a GET request to the specified URL. (Uses settings from the current provider) | ||
* | ||
* @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. | ||
*/ | ||
export declare function get(url: string, options?: GetRequestOptions): Promise<FetchClientResponse<unknown>>; | ||
/** | ||
* Sends a GET request to the specified URL and returns the response as JSON. (Uses settings from the current provider) | ||
* @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 to the specified URL. (Uses settings from the current provider) | ||
* | ||
* @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. | ||
*/ | ||
export declare function post(url: string, body?: object | string | FormData, options?: RequestOptions): Promise<FetchClientResponse<unknown>>; | ||
/** | ||
* Sends a POST request with JSON payload to the specified URL. (Uses settings from the current provider) | ||
* | ||
* @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 postJSON<T>(url: string, body?: object | string, options?: RequestOptions): Promise<FetchClientResponse<T>>; | ||
/** | ||
* Sends a PUT request to the specified URL with the given body and options. (Uses settings from the current provider) | ||
* @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. | ||
*/ | ||
export declare function put(url: string, body?: object | string | FormData, options?: RequestOptions): Promise<FetchClientResponse<unknown>>; | ||
/** | ||
* Sends a PUT request with JSON payload to the specified URL. (Uses settings from the current provider) | ||
* | ||
* @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 to the specified URL with the provided body and options. (Uses settings from the current provider) | ||
* @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. | ||
*/ | ||
export declare function patch(url: string, body?: object | string | FormData, options?: RequestOptions): Promise<Response>; | ||
/** | ||
* Sends a PATCH request with JSON payload to the specified URL. (Uses settings from the current provider) | ||
* | ||
* @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 to the specified URL. (Uses settings from the current provider) | ||
* | ||
* @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. | ||
*/ | ||
export declare function deleteRequest(url: string, options?: RequestOptions): Promise<FetchClientResponse<unknown>>; | ||
//# sourceMappingURL=DefaultHelpers.d.ts.map |
/** | ||
* Represents a cache key used in the FetchClientCache. | ||
*/ | ||
export type CacheKey = string[]; | ||
export type CacheKey = string[] | string; | ||
/** | ||
@@ -39,2 +39,8 @@ * Represents an entry in the FetchClientCache. | ||
/** | ||
* Deletes all responses from the cache that have keys beginning with the specified key. | ||
* @param prefix - The cache key prefix. | ||
* @returns The number of responses that were deleted. | ||
*/ | ||
deleteAll(prefix: CacheKey): number; | ||
/** | ||
* Checks if a response exists in the cache with the specified key. | ||
@@ -41,0 +47,0 @@ * @param key - The cache key. |
@@ -51,5 +51,6 @@ import { FetchClient, type FetchClientOptions } from "./FetchClient.js"; | ||
* Creates a new instance of FetchClient using the current provider. | ||
* @param options - The options to use for the FetchClient instance. | ||
* @returns A new instance of FetchClient. | ||
*/ | ||
getFetchClient(): FetchClient; | ||
getFetchClient(options?: FetchClientOptions): FetchClient; | ||
/** | ||
@@ -56,0 +57,0 @@ * Applies the specified options by merging with the current options. |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
111468
2350