Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@exceptionless/fetchclient

Package Overview
Dependencies
Maintainers
2
Versions
24
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@exceptionless/fetchclient - npm Package Compare versions

Comparing version 0.15.0 to 0.16.0

esm/src/DefaultHelpers.js

45

esm/mod.js

@@ -1,2 +0,1 @@

import { defaultInstance, } from "./src/FetchClientProvider.js";
export * from "./src/FetchClient.js";

@@ -6,44 +5,2 @@ export { ProblemDetails } from "./src/ProblemDetails.js";

export { FetchClientProvider } from "./src/FetchClientProvider.js";
/**
* A global singleton instance of the FetchClient.
*/
export const instance = defaultInstance.getFetchClient();
/**
* A global default singleton instance of the FetchClientProvider.
*/
export const defaultProvider = defaultInstance;
/**
* Sets the default base URL for the FetchClient.
* @param baseUrl - The base URL to use for requests.
*/
export function setDefaultBaseUrl(baseUrl) {
defaultProvider.setBaseUrl(baseUrl);
}
/**
* Sets the default access token function for the FetchClient.
* @param accessTokenFunc - The function that retrieves the access token.
*/
export function setDefaultAccessTokenFunc(accessTokenFunc) {
defaultProvider.setAccessTokenFunc(accessTokenFunc);
}
/**
* Sets the default model validator function for the FetchClient.
* @param validate - The function that validates the model.
*/
export function setDefaultModelValidator(validate) {
defaultProvider.setModelValidator(validate);
}
/**
* Adds a middleware to the FetchClient.
* @param middleware - The middleware function to be added.
*/
export function useDefaultMiddleware(middleware) {
defaultProvider.useMiddleware(middleware);
}
/**
* Sets the default request options for the FetchClient.
* @param options - The options to set as the default request options.
*/
export function setDefaultRequestOptions(options) {
defaultProvider.applyOptions({ defaultRequestOptions: options });
}
export * from "./src/DefaultHelpers.js";
import { Counter } from "./Counter.js";
import { ProblemDetails } from "./ProblemDetails.js";
import { parseLinkHeader } from "./LinkHeader.js";
import { defaultInstance, FetchClientProvider } from "./FetchClientProvider.js";
import { FetchClientProvider } from "./FetchClientProvider.js";
import { getCurrentProvider } from "./DefaultHelpers.js";
/**

@@ -23,3 +24,3 @@ * Represents a client for making HTTP requests using the Fetch API.

this.#options = optionsOrProvider;
this.#provider = optionsOrProvider?.provider ?? defaultInstance;
this.#provider = optionsOrProvider?.provider ?? getCurrentProvider();
}

@@ -107,3 +108,3 @@ }

* @param url - The URL to send the request to.
* @param body - The request body, can be an object or a string.
* @param body - The request body, can be an object, a string, or FormData.
* @param options - Additional options for the request.

@@ -117,2 +118,10 @@ * @returns A promise that resolves to a FetchClientResponse object.

};
if (body instanceof FormData) {
const response = await this.fetchInternal(url, options, {
method: "POST",
headers: { ...options?.headers },
body: body,
});
return response;
}
const problem = await this.validate(body, options);

@@ -129,22 +138,2 @@ if (problem)

/**
* Sends a POST request with form data to the specified URL.
*
* @param url - The URL to send the request to.
* @param formData - The form data to include in the request body.
* @param options - The optional request options.
* @returns A promise that resolves to the response of the request.
*/
async postForm(url, formData, options) {
options = {
...this.options.defaultRequestOptions,
...options,
};
const response = await this.fetchInternal(url, options, {
method: "POST",
headers: { ...options?.headers },
body: formData,
});
return response;
}
/**
* Sends a POST request with JSON payload to the specified URL.

@@ -164,3 +153,3 @@ *

* @param url - The URL to send the request to.
* @param body - The request body, can be an object or a string.
* @param body - The request body, can be an object, a string, or FormData.
* @param options - The request options.

@@ -174,2 +163,10 @@ * @returns A promise that resolves to a FetchClientResponse object.

};
if (body instanceof FormData) {
const response = await this.fetchInternal(url, options, {
method: "PUT",
headers: { ...options?.headers },
body: body,
});
return response;
}
const problem = await this.validate(body, options);

@@ -200,3 +197,3 @@ if (problem)

* @param url - The URL to send the PATCH request to.
* @param body - The body of the request. It can be an object or a string.
* @param body - The body of the request. It can be an object, a string, or FormData.
* @param options - The options for the request.

@@ -210,2 +207,10 @@ * @returns A Promise that resolves to the response of the PATCH request.

};
if (body instanceof FormData) {
const response = await this.fetchInternal(url, options, {
method: "PATCH",
headers: { ...options?.headers },
body: body,
});
return response;
}
const problem = await this.validate(body, options);

@@ -212,0 +217,0 @@ if (problem)

{
"name": "@exceptionless/fetchclient",
"version": "0.15.0",
"version": "0.16.0",
"description": "A simple fetch client with middleware support for Deno and the browser.",

@@ -5,0 +5,0 @@ "keywords": [

@@ -12,14 +12,122 @@ <!-- deno-fmt-ignore-file -->

```shell
npm install --save @exceptionless/fetchclient
```
$ npm install --save @exceptionless/fetchclient
```
## Docs
[API Documentation](https://jsr.io/@exceptionless/fetchclient/doc)
## Usage
Get a typed JSON response:
```ts
import { FetchClient } from '@exceptionless/fetchclient';
type Products = {
products: Array<{ id: number; name: string }>;
};
const client = new FetchClient();
const response = await client.getJSON<Products>(
`https://dummyjson.com/products/search?q=iphone&limit=10`,
);
const products = response.data;
```
Get a typed JSON response using a function:
```ts
import { getJSON } from '@exceptionless/fetchclient';
type Products = {
products: Array<{ id: number; name: string }>;
};
const response = await getJSON<Products>(
`https://dummyjson.com/products/search?q=iphone&limit=10`,
);
const products = response.data;
```
Use a model validator:
```ts
import { FetchClient, setModelValidator } from '@exceptionless/fetchclient';
setModelValidator(async (data: object | null) => {
// use zod or any other validator
const problem = new ProblemDetails();
const d = data as { password: string };
if (d?.password?.length < 6) {
problem.errors.password = [
"Password must be longer than or equal to 6 characters.",
];
}
return problem;
});
const client = new FetchClient();
const data = {
email: "test@test",
password: "test",
};
const response = await client.postJSON(
"https://jsonplaceholder.typicode.com/todos/1",
data,
);
if (!response.ok) {
// check response problem
console.log(response.problem.detail);
}
```
Use caching:
```ts
import { FetchClient } from '@exceptionless/fetchclient';
type Todo = { userId: number; id: number; title: string; completed: boolean };
const client = new FetchClient();
const response = await client.getJSON<Todo>(
`https://jsonplaceholder.typicode.com/todos/1`,
{
cacheKey: ["todos", "1"],
cacheDuration: 1000 * 60, // expires in 1 minute
}
);
// invalidate programmatically
client.cache.delete(["todos", "1"]);
```
Use middleware:
```ts
import { FetchClient, useMiddleware } from '@exceptionless/fetchclient';
type Products = {
products: Array<{ id: number; name: string }>;
};
useMiddleware(async (ctx, next) => {
console.log('starting request')
await next();
console.log('completed request')
});
const client = new FetchClient();
const response = await client.getJSON<Products>(
`https://dummyjson.com/products/search?q=iphone&limit=10`,
);
```
## License
MIT © [Exceptionless](https://exceptionless.com)

@@ -17,4 +17,3 @@ "use strict";

Object.defineProperty(exports, "__esModule", { value: true });
exports.setDefaultRequestOptions = exports.useDefaultMiddleware = exports.setDefaultModelValidator = exports.setDefaultAccessTokenFunc = exports.setDefaultBaseUrl = exports.defaultProvider = exports.instance = exports.FetchClientProvider = exports.FetchClientCache = exports.ProblemDetails = void 0;
const FetchClientProvider_js_1 = require("./src/FetchClientProvider.js");
exports.FetchClientProvider = exports.FetchClientCache = exports.ProblemDetails = void 0;
__exportStar(require("./src/FetchClient.js"), exports);

@@ -25,51 +24,4 @@ var ProblemDetails_js_1 = require("./src/ProblemDetails.js");

Object.defineProperty(exports, "FetchClientCache", { enumerable: true, get: function () { return FetchClientCache_js_1.FetchClientCache; } });
var FetchClientProvider_js_2 = require("./src/FetchClientProvider.js");
Object.defineProperty(exports, "FetchClientProvider", { enumerable: true, get: function () { return FetchClientProvider_js_2.FetchClientProvider; } });
/**
* A global singleton instance of the FetchClient.
*/
exports.instance = FetchClientProvider_js_1.defaultInstance.getFetchClient();
/**
* A global default singleton instance of the FetchClientProvider.
*/
exports.defaultProvider = FetchClientProvider_js_1.defaultInstance;
/**
* Sets the default base URL for the FetchClient.
* @param baseUrl - The base URL to use for requests.
*/
function setDefaultBaseUrl(baseUrl) {
exports.defaultProvider.setBaseUrl(baseUrl);
}
exports.setDefaultBaseUrl = setDefaultBaseUrl;
/**
* Sets the default access token function for the FetchClient.
* @param accessTokenFunc - The function that retrieves the access token.
*/
function setDefaultAccessTokenFunc(accessTokenFunc) {
exports.defaultProvider.setAccessTokenFunc(accessTokenFunc);
}
exports.setDefaultAccessTokenFunc = setDefaultAccessTokenFunc;
/**
* Sets the default model validator function for the FetchClient.
* @param validate - The function that validates the model.
*/
function setDefaultModelValidator(validate) {
exports.defaultProvider.setModelValidator(validate);
}
exports.setDefaultModelValidator = setDefaultModelValidator;
/**
* Adds a middleware to the FetchClient.
* @param middleware - The middleware function to be added.
*/
function useDefaultMiddleware(middleware) {
exports.defaultProvider.useMiddleware(middleware);
}
exports.useDefaultMiddleware = useDefaultMiddleware;
/**
* Sets the default request options for the FetchClient.
* @param options - The options to set as the default request options.
*/
function setDefaultRequestOptions(options) {
exports.defaultProvider.applyOptions({ defaultRequestOptions: options });
}
exports.setDefaultRequestOptions = setDefaultRequestOptions;
var FetchClientProvider_js_1 = require("./src/FetchClientProvider.js");
Object.defineProperty(exports, "FetchClientProvider", { enumerable: true, get: function () { return FetchClientProvider_js_1.FetchClientProvider; } });
__exportStar(require("./src/DefaultHelpers.js"), exports);

@@ -8,2 +8,3 @@ "use strict";

const FetchClientProvider_js_1 = require("./FetchClientProvider.js");
const DefaultHelpers_js_1 = require("./DefaultHelpers.js");
/**

@@ -27,3 +28,3 @@ * Represents a client for making HTTP requests using the Fetch API.

this.#options = optionsOrProvider;
this.#provider = optionsOrProvider?.provider ?? FetchClientProvider_js_1.defaultInstance;
this.#provider = optionsOrProvider?.provider ?? (0, DefaultHelpers_js_1.getCurrentProvider)();
}

@@ -111,3 +112,3 @@ }

* @param url - The URL to send the request to.
* @param body - The request body, can be an object or a string.
* @param body - The request body, can be an object, a string, or FormData.
* @param options - Additional options for the request.

@@ -121,2 +122,10 @@ * @returns A promise that resolves to a FetchClientResponse object.

};
if (body instanceof FormData) {
const response = await this.fetchInternal(url, options, {
method: "POST",
headers: { ...options?.headers },
body: body,
});
return response;
}
const problem = await this.validate(body, options);

@@ -133,22 +142,2 @@ if (problem)

/**
* Sends a POST request with form data to the specified URL.
*
* @param url - The URL to send the request to.
* @param formData - The form data to include in the request body.
* @param options - The optional request options.
* @returns A promise that resolves to the response of the request.
*/
async postForm(url, formData, options) {
options = {
...this.options.defaultRequestOptions,
...options,
};
const response = await this.fetchInternal(url, options, {
method: "POST",
headers: { ...options?.headers },
body: formData,
});
return response;
}
/**
* Sends a POST request with JSON payload to the specified URL.

@@ -168,3 +157,3 @@ *

* @param url - The URL to send the request to.
* @param body - The request body, can be an object or a string.
* @param body - The request body, can be an object, a string, or FormData.
* @param options - The request options.

@@ -178,2 +167,10 @@ * @returns A promise that resolves to a FetchClientResponse object.

};
if (body instanceof FormData) {
const response = await this.fetchInternal(url, options, {
method: "PUT",
headers: { ...options?.headers },
body: body,
});
return response;
}
const problem = await this.validate(body, options);

@@ -204,3 +201,3 @@ if (problem)

* @param url - The URL to send the PATCH request to.
* @param body - The body of the request. It can be an object or a string.
* @param body - The body of the request. It can be an object, a string, or FormData.
* @param options - The options for the request.

@@ -214,2 +211,10 @@ * @returns A Promise that resolves to the response of the PATCH request.

};
if (body instanceof FormData) {
const response = await this.fetchInternal(url, options, {
method: "PATCH",
headers: { ...options?.headers },
body: body,
});
return response;
}
const problem = await this.validate(body, options);

@@ -216,0 +221,0 @@ if (problem)

@@ -1,6 +0,1 @@

import { type FetchClientProvider } from "./src/FetchClientProvider.js";
import type { FetchClient } from "./src/FetchClient.js";
import type { FetchClientMiddleware } from "./src/FetchClientMiddleware.js";
import type { ProblemDetails } from "./src/ProblemDetails.js";
import type { RequestOptions } from "./src/RequestOptions.js";
export * from "./src/FetchClient.js";

@@ -14,35 +9,3 @@ export type { FetchClientResponse } from "./src/FetchClientResponse.js";

export { FetchClientProvider } from "./src/FetchClientProvider.js";
/**
* A global singleton instance of the FetchClient.
*/
export declare const instance: FetchClient;
/**
* A global default singleton instance of the FetchClientProvider.
*/
export declare const defaultProvider: FetchClientProvider;
/**
* Sets the default base URL for the FetchClient.
* @param baseUrl - The base URL to use for requests.
*/
export declare function setDefaultBaseUrl(baseUrl: string): void;
/**
* Sets the default access token function for the FetchClient.
* @param accessTokenFunc - The function that retrieves the access token.
*/
export declare function setDefaultAccessTokenFunc(accessTokenFunc: () => string | null): void;
/**
* Sets the default model validator function for the FetchClient.
* @param validate - The function that validates the model.
*/
export declare function setDefaultModelValidator(validate: (model: object | null) => Promise<ProblemDetails | null>): void;
/**
* Adds a middleware to the FetchClient.
* @param middleware - The middleware function to be added.
*/
export declare function useDefaultMiddleware(middleware: FetchClientMiddleware): void;
/**
* Sets the default request options for the FetchClient.
* @param options - The options to set as the default request options.
*/
export declare function setDefaultRequestOptions(options: RequestOptions): void;
export * from "./src/DefaultHelpers.js";
//# sourceMappingURL=mod.d.ts.map

@@ -114,17 +114,8 @@ import { Counter } from "./Counter.js";

* @param url - The URL to send the request to.
* @param body - The request body, can be an object or a string.
* @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, options?: RequestOptions): Promise<FetchClientResponse<unknown>>;
post(url: string, body?: object | string | FormData, options?: RequestOptions): Promise<FetchClientResponse<unknown>>;
/**
* Sends a POST request with form data to the specified URL.
*
* @param url - The URL to send the request to.
* @param formData - The form data to include in the request body.
* @param options - The optional request options.
* @returns A promise that resolves to the response of the request.
*/
postForm(url: string, formData: FormData, options?: RequestOptions): Promise<FetchClientResponse<unknown>>;
/**
* Sends a POST request with JSON payload to the specified URL.

@@ -142,7 +133,7 @@ *

* @param url - The URL to send the request to.
* @param body - The request body, can be an object or a string.
* @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, options?: RequestOptions): Promise<FetchClientResponse<unknown>>;
put(url: string, body?: object | string | FormData, options?: RequestOptions): Promise<FetchClientResponse<unknown>>;
/**

@@ -161,7 +152,7 @@ * Sends a PUT request with JSON payload to the specified URL.

* @param url - The URL to send the PATCH request to.
* @param body - The body of the request. It can be an object or a string.
* @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, options?: RequestOptions): Promise<Response>;
patch(url: string, body?: object | string | FormData, options?: RequestOptions): Promise<Response>;
/**

@@ -168,0 +159,0 @@ * Sends a PATCH request with JSON payload to the specified URL.

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc