Registry / devops / fetch-factory

fetch-factory

JSON →
library0.2.1jsnpmunverified

A wrapper around the native fetch API to simplify creating service objects for REST APIs. Version 0.2.1 is the latest stable release. This library is in maintenance mode with no recent updates. It provides a declarative way to define API endpoints, supports parameter interpolation, JSON handling for POST requests, and request interceptors. Unlike heavier HTTP clients, fetch-factory is lightweight and relies on native fetch and Promise, requiring polyfills for older environments. It is consumable in Node.js and browsers via bundlers.

npm install fetch-factory
INSTALL
IMPORT
SIG · FETCH-FACTORY
F
fetch-factory
devopsjavascriptv0.2.1
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.

fetchFactory
import fetchFactory from 'fetch-factory'
const fetchFactory = require('fetch-factory')
Default import is used. CommonJS require works but TypeScript may prefer ES module syntax.
fetchFactory.create
import fetchFactory from 'fetch-factory'; const factory = fetchFactory.create(config)
import { create } from 'fetch-factory'
create is a method on the default export, not a named export.
global.fetchFactory
// In browser builds, use the global variable directly: const factory = window.fetchFactory.create(config)
import fetchFactory from 'fetch-factory' // Not needed in browser bundle
For browser builds using the distribution file, fetchFactory is available globally.

Demonstrates basic usage of fetch-factory to create a REST client with find and create methods, including parameter substitution and JSON body for POST.

import fetchFactory from 'fetch-factory'; const Users = fetchFactory.create({ url: 'https://api.example.com/users/:id', methods: ['find', 'create'] }); // GET https://api.example.com/users Users.find().then(response => response.json()).then(console.log); // GET https://api.example.com/users/123 Users.find({ params: { id: 123 } }).then(response => response.json()).then(console.log); // POST https://api.example.com/users with JSON body { name: 'Jack' } Users.create({ data: { name: 'Jack' } }).then(response => response.json()).then(console.log);
Debug
Known issues
gotchaYou must have a global fetch and Promise available. In Node.js versions <18 or older browsers, you need polyfills.
fix
Install and import 'whatwg-fetch' and 'es6-promise' polyfills before using fetch-factory.
affects: >=0
gotchaPOST requests automatically set Content-Type to application/json and stringify the data object. If you need to send form data or other types, you cannot override these default headers within the current configuration.
fix
Use a request interceptor to modify headers or consider a different library for non-JSON payloads.
affects: >=0
gotchaThe interceptors.request function must return an object with headers, body, and method properties. If you return the original request object directly, it may not include these properties correctly.
fix
Always return an object with the three required properties, or clone the request and modify.
affects: >=0
Errors
Common errors & fixes
fetchFactory is not defined
Attempting to use the library without importing it correctly (e.g., using the browser global without including the script).
fix
Ensure the fetch-factory script is loaded (dist/fetch-factory.js) or import the module correctly in your bundler.
Cannot find module 'fetch-factory'
The package is not installed or not available in node_modules.
fix
Run `npm install fetch-factory` to install the package.
Unhandled Promise rejection: TypeError: Failed to fetch
The fetch API is not available globally, possibly due to missing polyfill or running in an unsupported environment.
fix
Install a fetch polyfill like 'whatwg-fetch' and import it before using fetch-factory.
Upgrade
Version history
0.2.1latest on npm
Audit
Dependencies
whatwg-fetchoptionalRequired in older browsers that do not natively support the fetch API.
es6-promiseoptionalRequired in older environments that do not natively support Promises.
Agent activity
4 hits · last 30 days
node
4
Resources
fetch-factory — npm install fetch-factory · libregistry