cross-fetch-json
v2.0.0
Published
Universal fetch API that returns JSON
Downloads
87
Maintainers
Readme
If I should maintain this repo, please ⭐️
DM me on Twitter if you have questions or suggestions.
Installation
yarn add cross-fetch-json
npm install cross-fetch-json
pnpm add cross-fetch-json
Usage
The fetchJSON
function returns a Promise
that resolves to a JSONValue
or undefined
if the response is not valid JSON.
import { fetchJSON } from "cross-fetch-json";
fetchJSON("https://example.com"); // Promise<JSONValue | undefined>
A getFetchFn
function is also exported that allows for custom parsing with Zod:
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.
Related Packages:
- parse-json-object: Parse a typed JSON object.
- types-json: Type checking for JSON objects