Registry / devops / zhi-fetch-middleware

zhi-fetch-middleware

JSON →
library0.13.5jsnpmunverified

A lightweight middleware layer for fetch requests that allows adding interceptors (before, after) and error handling. Version 0.13.5 is the latest stable release, with no external dependencies. It provides a simple API to compose middleware functions for modifying requests and responses, similar to Express middleware but for the Fetch API. Ships TypeScript definitions. Suitable for small to medium projects needing request/response transforms or logging.

npm install zhi-fetch-middleware
INSTALL
IMPORT
SIG · ZHI-FETCH-MIDDLEWA
Z
zhi-fetch-middleware
devopsjavascriptv0.13.5
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

createMiddleware
import { createMiddleware } from 'zhi-fetch-middleware'
import createMiddleware from 'zhi-fetch-middleware'
Named export only, no default export.
FetchMiddleware
import { FetchMiddleware } from 'zhi-fetch-middleware'
import { FetchMiddleware } from 'zhi-fetch-middleware'
Type import for TypeScript: import type { FetchMiddleware } from 'zhi-fetch-middleware'
applyMiddleware
import { applyMiddleware } from 'zhi-fetch-middleware'
const { applyMiddleware } = require('zhi-fetch-middleware')
ESM-only package; CommonJS require is not supported.

Creates a simple logger middleware that logs requests and responses, then applies it to fetch to produce an enhanced fetch function.

import { createMiddleware, applyMiddleware } from 'zhi-fetch-middleware'; const loggerMiddleware = createMiddleware({ before: async (request) => { console.log('Fetching:', request.url); return request; }, after: async (response) => { console.log('Response status:', response.status); return response; } }); const enhancedFetch = applyMiddleware(fetch, [loggerMiddleware]); (async () => { const response = await enhancedFetch('https://api.example.com/data', { method: 'GET', headers: { 'Content-Type': 'application/json' } }); const data = await response.json(); console.log(data); })();
Debug
Known issues
breakingVersion 0.12.0 changed the API from an object-based configuration to a function-based one. Old usage with middleware config object no longer works.
fix
Migrate to using createMiddleware() and applyMiddleware() as described in the docs.
affects: >=0.12.0
deprecatedThe method 'useMiddleware' is deprecated and will be removed in v1.0.
fix
Replace with applyMiddleware() which is the new standard.
affects: >=0.13.0
gotchaMiddleware functions must be async or return a Promise, otherwise they may be skipped.
fix
Ensure all middleware functions are async.
affects: >=0.0.0
Errors
Common errors & fixes
TypeError: createMiddleware is not a function
Default import used instead of named import.
fix
Use `import { createMiddleware } from 'zhi-fetch-middleware'`
ERR_PACKAGE_PATH_NOT_EXPORTED: Package subpath './dist/index.js' is not defined
Trying to import from an internal path that is not exposed via package.json exports.
fix
Import only from 'zhi-fetch-middleware' directly.
Upgrade
Version history
0.13.5latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
26 hits · last 30 days
node
21
Amazon
1
Resources
zhi-fetch-middleware — npm install zhi-fetch-middleware · libregistry