d2l-fetch is a JavaScript library that provides a middleware layer for the standard `window.fetch` API. It enables developers to intercept and modify requests and responses before they reach the network or are returned to the caller. This allows for centralized handling of concerns like authentication, caching, logging, and error handling. The current stable version is 2.7.0. While the release cadence is not explicitly stated, the package appears to be actively maintained, with regular updates. Its key differentiator lies in its straightforward, chainable middleware pattern, offering a flexible way to extend `fetch` behavior without deep polyfilling or direct modification of `window.fetch` itself, making it suitable for environments where `fetch` is natively available.
npm install d2l-fetchVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to register two middleware functions (authentication and logging) with `d2lfetch` and then use `d2lfetch.fetch` to make an API request, showing how the middleware intercepts and modifies the request/response flow.
Always include `return next(request);` at the end of middleware functions that should continue the chain, or `return Promise.resolve(myResponse);` for early exits.
Choose one import method (either global script or ES module import) and stick to it consistently throughout your application to ensure a single, shared `d2lfetch` instance.
Carefully design your middleware order. Place early-exit middleware (like caching) strategically so that they only intercept requests where an early exit is appropriate and desired.
If using ES modules, ensure `import { d2lfetch } from 'd2l-fetch/src/index.js';` is at the top of your file. If using a global script, verify the `<script>` tag's `src` path is correct and that it has loaded before `d2lfetch` is accessed.Ensure middleware functions are defined as `(request, next) => {...}` and that `next(request)` is called. If you modify the request/response, ensure you return a valid `Request` or `Response` object or a Promise resolving to one.Review middleware functions for early exits that might be unintentionally preventing `window.fetch` from running. Check the `request` object's URL, headers, and method within a middleware to ensure it's well-formed before `next(request)` is called.
No dependency data recorded yet.