### Install FetchClient using npm Source: https://github.com/exceptionless/fetchclient/blob/main/readme.md Installs the FetchClient library as a project dependency using npm. ```shell npm install --save @exceptionless/fetchclient ``` -------------------------------- ### Deno Development Commands Source: https://github.com/exceptionless/fetchclient/blob/main/readme.md Provides common commands for developing with Deno, including running tests, linting code, formatting files, and type checking. ```shell deno run test deno lint deno fmt deno run check ``` -------------------------------- ### Implementing Request Middleware Source: https://github.com/exceptionless/fetchclient/blob/main/readme.md Demonstrates how to add custom middleware to the FetchClient request pipeline using `useMiddleware`. Middleware functions can intercept requests and responses, allowing for logging, request modification, or other side effects. ```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( `https://dummyjson.com/products/search?q=iphone&limit=10`, ); ``` -------------------------------- ### Caching API Responses Source: https://github.com/exceptionless/fetchclient/blob/main/readme.md Illustrates how to implement client-side caching for API requests using `cacheKey` and `cacheDuration` options. It also shows how to programmatically invalidate cached entries using `client.cache.delete`. ```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( `https://jsonplaceholder.typicode.com/todos/1`, { cacheKey: ["todos", "1"], cacheDuration: 1000 * 60, // expires in 1 minute } ); // invalidate programmatically client.cache.delete(["todos", "1"]); ``` -------------------------------- ### Fetch JSON Data with Typed Response Source: https://github.com/exceptionless/fetchclient/blob/main/readme.md Demonstrates fetching JSON data and typing the response using the `getJSON` method of FetchClient. It allows specifying a TypeScript type for the expected JSON structure, enhancing type safety. ```ts import { FetchClient } from '@exceptionless/fetchclient'; type Products = { products: Array<{ id: number; name: string }>; }; const client = new FetchClient(); const response = await client.getJSON( `https://dummyjson.com/products/search?q=iphone&limit=10`, ); const products = response.data; ``` -------------------------------- ### Fetch JSON Data with Typed Response (Function) Source: https://github.com/exceptionless/fetchclient/blob/main/readme.md Shows how to use the standalone `getJSON` function for fetching JSON data with type safety, without needing to instantiate a FetchClient object. ```ts import { getJSON } from '@exceptionless/fetchclient'; type Products = { products: Array<{ id: number; name: string }>; }; const response = await getJSON( `https://dummyjson.com/products/search?q=iphone&limit=10`, ); const products = response.data; ``` -------------------------------- ### Model Validation with Custom Validator Source: https://github.com/exceptionless/fetchclient/blob/main/readme.md Configures a global model validator using `setModelValidator` to perform custom data validation, such as checking password length. It demonstrates how to integrate validation logic and handle validation errors returned in the response. ```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); } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.