Registry / devops / simple-json-fetch

simple-json-fetch

JSON →
library1.0.1jsnpmunverified

A lightweight wrapper around the WHATWG fetch() API that automatically parses JSON responses and resolves only for successful HTTP status codes (200-299) with valid JSON. Version 1.0.1 is the latest and only stable version. It has zero runtime dependencies but expects a fetch polyfill in environments without native fetch. Unlike raw fetch, it simplifies JSON handling by providing a resolved `.json` property on success and an `.error` property on rejection. Suitable for projects where you want concise JSON fetching without manual response parsing.

npm install simple-json-fetch
INSTALL
IMPORT
SIG · SIMPLE-JSON-FETCH
S
simple-json-fetch
devopsjavascriptv1.0.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.

jsonFetch
import jsonFetch from 'simple-json-fetch'
const jsonFetch = require('simple-json-fetch')
This package is ESM-only. It uses default export.
default export
import jsonFetch from 'simple-json-fetch'
import { jsonFetch } from 'simple-json-fetch'
The package exports a default function, not a named export.
fetch polyfill
import 'whatwg-fetch'
import fetch from 'whatwg-fetch'
The polyfill must be imported as a side-effect before using jsonFetch.

Demonstrates importing the polyfill and jsonFetch, making a GET request, and handling success/failure with automatic JSON parsing.

import 'whatwg-fetch' import jsonFetch from 'simple-json-fetch' const url = 'https://api.example.com/data' const options = { method: 'GET', headers: { 'Accept': 'application/json' } } jsonFetch(url, options) .then(response => { console.log('Success:', response.json) // response.json is the parsed JavaScript object }) .catch(response => { console.error('Error:', response.error) // response.error is the error (e.g., parse error) console.log('Status:', response.status, response.statusText) })
Debug
Known issues
gotchaRequires a fetch polyfill in environments without native fetch (e.g., older Node, some browsers). Install whatwg-fetch or similar and import it before jsonFetch.
fix
npm install whatwg-fetch and add import 'whatwg-fetch' at the top of your entry file.
affects: >=0.0.0
gotchaThe promise resolves only if HTTP status is 200-299 AND the response body is valid JSON. Any other status or malformed JSON causes rejection.
fix
Always handle rejection, and check response.status for additional context.
affects: >=0.0.0
gotchaNo automated tests; the package is a personal snippet. Use with caution in production.
fix
Consider adding tests or use a well-tested alternative like ky or got.
affects: >=0.0.0
Errors
Common errors & fixes
Uncaught TypeError: fetch is not a function
Missing fetch polyfill in an environment without native fetch (e.g., Node.js <18).
fix
Install whatwg-fetch (npm install whatwg-fetch) and add import 'whatwg-fetch' before using simple-json-fetch.
Uncaught SyntaxError: The requested module 'simple-json-fetch' does not provide an export named 'jsonFetch'
Using named import instead of default import.
fix
Use import jsonFetch from 'simple-json-fetch' (without curly braces).
Error: jsonFetch is not a function
Using require() on an ESM-only package.
fix
Change to import jsonFetch from 'simple-json-fetch' or use dynamic import().
Upgrade
Version history
1.0.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
5 hits · last 30 days
node
4
Amazon
1
Resources
simple-json-fetch — npm install simple-json-fetch · libregistry