Familiar Fetch API with support for custom middleware.
- Lightweight - only 235B Minified + Gzipped
- Zero dependencies
- Fetch-compatible API - No need to learn anything new.
- TypeScript types included
Use npm to install the package.
npm install fetch-with-middleware
import { buildFetch, type MiddlewareFn } from 'fetch-with-middleware';
// Custom middleware function that will
// log to console every request and response.
const logToConsole: MiddlewareFn = (next) => (request) => {
console.log("Request", request);
return next(request).then((response) => {
console.log("Response", response);
return response;
});
};
const myFetch = buildFetch({ middleware: [logToConsole] });
const response = await myFetch("https://localhost:3000");