Registry / devops / xhfetch

xhfetch

JSON →
library1.0.2jsnpmunverified

Minimal cancellable fetch wrapper (~600 bytes gzipped) providing a familiar fetch-like API over XMLHttpRequest. Current version 1.0.2 is stable with infrequent releases. Differentiators: tiny size, explicit cancellation via exposed XHR object, IE8+ support (with Promise polyfill). Alternative to unfetch or native fetch when cancellation is required.

npm install xhfetch
INSTALL
IMPORT
SIG · XHFETCH
X
xhfetch
devopsjavascriptv1.0.2
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.

createRequest
import { createRequest } from 'xhfetch'
import createRequest from 'xhfetch'
Only named export; default import will result in undefined.
createRequest
const { createRequest } = require('xhfetch')
const createRequest = require('xhfetch')
CJS destructuring required; package exposes ESM-style named export.
fetch
const { fetch } = createRequest(url, options)
createRequest(url, options).fetch()
Call fetch() on the returned object; no separate import.

Demonstrates creating a cancellable GET request with timeout using createRequest, including abort handling.

import { createRequest } from 'xhfetch'; async function getUser(id) { const { fetch, xhr } = createRequest(`https://api.example.com/users/${id}`, { method: 'GET', headers: { 'Accept': 'application/json' } }); // Cancel after 5 seconds (example) const timeout = setTimeout(() => xhr.abort(), 5000); try { const res = await fetch(); if (!res.ok) throw new Error(`HTTP ${res.status}`); const data = await res.json(); clearTimeout(timeout); return data; } catch (err) { clearTimeout(timeout); if (err.name === 'AbortError') console.log('Request aborted'); else throw err; } }
Debug
Known issues
gotchaThe .fetch() returned by createRequest does not accept any arguments; pass all options to createRequest.
fix
Move all parameters (URL, init) to createRequest, not .fetch().
affects: <=1.0.2
gotchacredentials: 'same-origin' is not supported; use 'include' for cross-origin with cookies or omit for same-origin (cookies ignored if omitted).
fix
Use credentials: 'include' only when needed; omit for same-origin requests without cookies.
affects: <=1.0.2
gotchaResponse object only implements subset of standard fetch Response: ok, status, statusText, clone, text, json, blob, headers. No body used/getReader/arrayBuffer.
fix
Use only documented methods; avoid methods like response.arrayBuffer() (not supported).
affects: <=1.0.2
Errors
Common errors & fixes
TypeError: Cannot destructure property 'fetch' of (intermediate value).fetch is not a function
Calling createRequest incorrectly, e.g., createRequest(url).fetch(...) instead of calling fetch() returned.
fix
const { fetch } = createRequest(url); const result = await fetch();
Uncaught TypeError: Failed to execute 'fetch' on 'Window': Illegal invocation
Attempting to use window.fetch from xhfetch; xhfetch is not a polyfill for window.fetch.
fix
Use only createRequest API, not window.fetch.
ReferenceError: Promise is not defined
Environment (e.g., IE11 without Promise polyfill) lacks Promise support.
fix
Include a Promise polyfill like es6-promise or core-js.
Upgrade
Version history
1.0.2latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
21 hits · last 30 days
node
18
Resources
xhfetch — npm install xhfetch · libregistry