Registry / devops / fetch-http-client

fetch-http-client

JSON →
library1.1.0jsnpmunverified

A lightweight HTTP client wrapper around the Fetch API with middleware support, enabling synchronous and asynchronous pre-request and post-response hooks. Version 1.1.0 is the current stable release with no active development since 2017. It distinguishes itself from fetch-plus and http-client by allowing asynchronous middleware (e.g., for token retrieval) and includes built-in middleware for JSON handling, query string construction, and path variable substitution. Suitable for browser and React Native environments.

npm install fetch-http-client
INSTALL
IMPORT
SIG · FETCH-HTTP-CLIENT
F
fetch-http-client
devopsjavascriptv1.1.0
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.

FetchHttpClient
import FetchHttpClient from 'fetch-http-client'
const FetchHttpClient = require('fetch-http-client')
Default export. CommonJS require works if using module bundler that supports CJS interop.
json
import { json } from 'fetch-http-client'
import json from 'fetch-http-client'
Named export, not default. For built-in middleware like query() and json().
query
import { query } from 'fetch-http-client'
const { query } = require('fetch-http-client')
Both ESM and CJS named imports work; ensure bundler supports destructuring.

Creates a client with JSON and query middleware, adds an auth header, requests a user by path variable and query parameter.

import FetchHttpClient, { json, query } from 'fetch-http-client'; const client = new FetchHttpClient('https://api.example.com'); client.addMiddleware(json()); client.addMiddleware(query()); client.addMiddleware(request => { request.options.headers['Authorization'] = 'Bearer ' + (process.env.API_KEY ?? ''); return request; }); client.get('users/{id}', { uriParams: { id: 1 }, query: { active: true } }) .then(response => console.log(response.jsonData)) .catch(err => console.error(err));
Debug
Known issues
gotchaMiddleware must return the request object (or a Promise resolving to request) – forgetting causes hangs.
fix
Always return request from middleware: client.addMiddleware(req => { ...; return req; })
affects: >=0.0.0
gotchaThe json() middleware sets Content-Type header to application/json but does NOT stringify the body automatically – you must pass the object in options.body.
fix
client.post('data', { body: { key: 'value' } }) will fail; stringify manually: body: JSON.stringify({ key: 'value' })
affects: >=0.0.0
gotchaPath variables in URI must match exactly the keys in uriParams; mismatches throw silently (uri not replaced).
fix
Ensure uri params match: client.get('users/{userId}', { uriParams: { userId: 123 } })
affects: >=0.0.0
deprecatedPackage has not been updated since 2017; no support for newer Fetch API features (AbortSignal, keepalive, etc.).
fix
Consider using ky, got, or native fetch with manual middleware.
affects: >=1.1.0
Errors
Common errors & fixes
Uncaught (in promise) TypeError: Failed to execute 'fetch' on 'Window': Illegal invocation
Missing Content-Type header or body not properly formatted.
fix
Add json() middleware and ensure body is a string (JSON.stringify).
TypeError: request.options.headers is undefined
Middleware modifies request.options.headers but options may be undefined.
fix
Initialize headers: request.options = request.options || {}; request.options.headers = request.options.headers || {};
fetch is not defined / fetch is not a function
Fetch API not available in Node.js without polyfill.
fix
Install 'node-fetch' and set global.fetch = require('node-fetch') before using fetch-http-client.
Upgrade
Version history
1.1.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
2 hits · last 30 days
node
2
Resources
fetch-http-client — npm install fetch-http-client · libregistry