@exceptionless/fetchclient@0.40.0Built and signed on GitHub ActionsBuilt and signed on GitHub Actions
Built and signed on GitHub Actions
Fetch helper wrapper that supports middleware and the problem details standard
This package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers




JSR Score
100%
Published
3 months ago (0.40.0)
FetchClient

FetchClient is a library that makes it easier to use the fetch API for JSON APIs. It provides the following features:
- Makes fetch easier to use for JSON APIs
- Automatic model validation
- Caching
- Middleware
- Problem Details support
- Option to parse dates in responses
Install
npm install --save @exceptionless/fetchclient
Docs
Usage
Typed Response
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;
Typed Response Using a Function
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;
Model Validator
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); }
Caching
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"]);
Middleware
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`, );
Also, take a look at the tests:
Contributing
Run tests:
deno run test
Lint code:
deno lint
Format code:
deno fmt
Type check code:
deno run check
License
MIT © Exceptionless
Built and signed on
View transparency logGitHub Actions
Add Package
deno add jsr:@exceptionless/fetchclient
Import symbol
import * as fetchclient from "@exceptionless/fetchclient";
---- OR ----
Import directly with a jsr specifier
import * as fetchclient from "jsr:@exceptionless/fetchclient";
Add Package
npx jsr add @exceptionless/fetchclient
Import symbol
import * as fetchclient from "@exceptionless/fetchclient";
Add Package
yarn dlx jsr add @exceptionless/fetchclient
Import symbol
import * as fetchclient from "@exceptionless/fetchclient";
Add Package
pnpm dlx jsr add @exceptionless/fetchclient
Import symbol
import * as fetchclient from "@exceptionless/fetchclient";
Add Package
bunx jsr add @exceptionless/fetchclient
Import symbol
import * as fetchclient from "@exceptionless/fetchclient";