Registry / testing / http-with-fetch

http-with-fetch

JSON →
library3.3.6jsnpmunverified

A tiny abstraction over the Fetch API for building featureful API clients. Current version 3.3.6. Released regularly on npm. Provides a CredentialsManager and visitor pattern for request/response handling, plus a TestHarness for mocking HTTP calls in tests using Node.js built-in test framework. Distinguishes itself by being lightweight (1.3KB bundle) and focused on the fetch API with no external runtime dependencies.

npm install http-with-fetch
INSTALL
IMPORT
SIG · HTTP-WITH-FETCH
H
http-with-fetch
testingjavascriptv3.3.6
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.

createApiClient
import { createApiClient } from 'http-with-fetch'
const createApiClient = require('http-with-fetch')
ESM-only package since v3. CommonJS require() will not work.
CredentialsManager
import { CredentialsManager } from 'http-with-fetch'
import { CredentialsManager } from 'http-with-fetch/harness'
CredentialsManager is exported from main entry, not from harness path.
TestHarness
import { TestHarness } from 'http-with-fetch/harness'
import { TestHarness } from 'http-with-fetch'
TestHarness is exported from a separate subpath 'http-with-fetch/harness'.

Shows how to create an API client with custom credentials manager and visitor for response handling, then make a GET request.

import { createApiClient, CredentialsManager } from 'http-with-fetch'; class DefaultCredentialsManager implements CredentialsManager { async attach(init: RequestInit, url: string) { init.credentials = 'include'; return { init, url }; } async isValid(_: Request, res: Response) { if (res.status === 401) return false; return true; } } const visitor = { async ok(response: Response) { return response.json(); }, async fail(request: Request, response: Response) { let error = await response.json(); if (process.env.NODE_ENV !== 'production') { console.error('API Call Failed: ', error, request); } throw new Error(error.message || 'Request failed'); }, }; const credentials = new DefaultCredentialsManager(); const client = createApiClient({ credentials, visitor }, false); // Usage const list = await client.get('/resource', { limit: 10 }); console.log(list);
Debug
Known issues
gotchaTestHarness only works when using client.execute() method, not other shortcut methods like client.get() or client.post().
fix
Use client.execute({ method: 'GET', url: '/path' }) instead of client.get('/path') when you need to mock requests with TestHarness.
affects: >=3.0.0
gotchaThe library is ESM-only starting from version 3. CommonJS require() will fail.
fix
Use import syntax or dynamic import('http-with-fetch'). Ensure your project is configured for ESM (type: 'module' in package.json).
affects: >=3.0.0
deprecatedThe 'TestHarness' previously required the mock to be passed as second argument; now it's the third argument after client and test context.
fix
Update TestHarness constructor calls: new TestHarness(client, t, handler) instead of old signature.
affects: >=3.2.0
Errors
Common errors & fixes
Cannot find module 'http-with-fetch/harness'
Import path for TestHarness is incorrect; using 'http-with-fetch' instead of 'http-with-fetch/harness'.
fix
import { TestHarness } from 'http-with-fetch/harness'
TypeError: client.get is not a function
Client created without visitors; get/post shortcuts require visitor object.
fix
Pass visitor to createApiClient: createApiClient({ credentials, visitor })
Upgrade
Version history
3.3.6latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
11 hits · last 30 days
node
10
Resources
http-with-fetch — npm install http-with-fetch · libregistry