Registry / devops / return-fetch-json

return-fetch-json

JSON →
library0.4.8jsnpmunverified

An extended fetch function that automatically serializes request bodies as JSON and deserializes response bodies as JSON objects. Built on top of return-fetch, it provides a thin, typed wrapper around the native fetch API. Current stable version is 0.4.8 (released 2024). The package is actively maintained, releases are versioned on npm, and it ships TypeScript definitions. Key differentiators: zero dependencies, small bundle size, custom parser support, and integration with base URL and default headers from return-fetch. Ideal for developers who want a lightweight JSON-aware fetch without the overhead of Axios or ky.

npm install return-fetch-json
INSTALL
IMPORT
SIG · RETURN-FETCH-JSON
R
return-fetch-json
devopsjavascriptv0.4.8
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.

default (returnFetchJson)
import returnFetchJson from 'return-fetch-json'
const returnFetchJson = require('return-fetch-json')
Package is ESM-first; CommonJS require works in some environments but is not recommended.
returnFetchJson type (for TypeScript)
import type { ReturnFetchJson } from 'return-fetch-json'
import { ReturnFetchJson } from 'return-fetch-json'
Type imports should use import type to avoid runtime side effects.
Full API (rare)
import * as returnFetchJson from 'return-fetch-json'
This pattern imports all exports; generally unnecessary as the default export is the primary use.

Demonstrates creating an extended fetch with base URL, making POST and GET requests, and automatically serializing/deserializing JSON objects.

import returnFetchJson from 'return-fetch-json'; const fetchExtended = returnFetchJson({ baseUrl: 'https://jsonplaceholder.typicode.com', }); async function main() { // POST with automatic JSON serialization const postResponse = await fetchExtended<{ id: number }>('/posts', { method: 'POST', body: { title: 'foo', body: 'bar', userId: 1 }, }); console.log('Created post:', postResponse.body); // GET with automatic JSON deserialization const getResponse = await fetchExtended<{ userId: number; id: number; title: string; completed: boolean }[]>('/todos/1'); console.log('Todo:', getResponse.body); } main().catch(console.error);
Debug
Known issues
gotchaBody must be a plain object or array; other types (e.g., FormData, Blob) are not serialized and may cause runtime errors.
fix
Ensure body is JSON-serializable (pass null or undefined for non-JSON bodies).
affects: >=0.1.0
breakingIn version 0.3.0, the API changed from synchronous to returning a Promise. Legacy code using synchronous call will break.
fix
Use async/await or .then() when calling the extended fetch function.
affects: >=0.3.0
deprecatedThe option `jsonParser` is deprecated in favor of the new `deserialize` option. Use `deserialize` instead.
fix
Replace `jsonParser` with `deserialize` in the configuration object.
affects: >=0.4.0
gotchaIf no `baseUrl` is provided, you must pass an absolute URL to the fetch call. Relative URLs will cause a TypeError.
fix
Either provide a `baseUrl` option or use absolute URLs in fetch calls.
affects: >=0.1.0
gotchaTypeScript generic parameter `T` only types the `.body` property; the response object itself still has status, headers, etc.
fix
Use `fetchExtended<ExpectedBody>(...)` only for the body shape; do not expect the entire response to be typed as T.
affects: >=0.1.0
Errors
Common errors & fixes
Error: Cannot find module 'return-fetch-json'
Package not installed or missing in node_modules.
fix
Run `npm install return-fetch-json` (or yarn/pnpm equivalent) in your project.
TypeError: fetchExtended is not a function
Incorrect import: using named import when only default export exists.
fix
Use `import returnFetchJson from 'return-fetch-json'` (default import).
TypeError: Body passed as JSON is not supported
Body is set to a non-serializable value (e.g., FormData).
fix
Only pass plain objects or arrays as body; for FormData, use the native fetch directly.
Upgrade
Version history
0.4.8latest on npm
Audit
Dependencies
return-fetchrequiredreturn-fetch-json is an extension of return-fetch; it relies on return-fetch for base URL and default headers functionality.
Agent activity
7 hits · last 30 days
node
6
Amazon
1
Resources
return-fetch-json — npm install return-fetch-json · libregistry