Universal fetch API that returns JSON
npm install cross-fetch-jsonUniversal fetch API that returns JSON.
_If I should maintain this repo, please ⭐️_
_DM me on Twitter if you have questions or suggestions._
---
``bash`
yarn add cross-fetch-json
`bash`
npm install cross-fetch-json
`bash`
pnpm add cross-fetch-json
The fetchJSON function returns a Promise that resolves to a JSONValue or undefined if the response is not valid JSON.
`ts
import { fetchJSON } from "cross-fetch-json";
fetchJSON("https://example.com"); // Promise
`
A getFetchFn function is also exported that allows for custom parsing with Zod:
`ts
import { getFetchFn } from "cross-fetch-json";
import z from "zod";
const userSchema = z.object({
name: z.string(),
age: z.number()
});
const fetchUser = getFetchFn(schema);
fetchUser("https://example.com"); // Promise<{ name: string; age: number; } | undefined>
``
- cross-fetch: Universal WHATWG Fetch API for Node, Browsers and React Native
- parse-json-object: Parse a typed JSON object
- zod: TypeScript-first schema declaration and validation library with static type inference
- autorepo: Autorepo abstracts away your dev dependencies, providing a single command to run all of your scripts.