cross-fetch-json
TypeScript icon, indicating that this package has built-in type declarations

2.0.0 • Public • Published

cross-fetch-json

NPM TypeScript

Universal fetch API that returns JSON.

If I should maintain this repo, please ⭐️ GitHub stars

DM me on Twitter if you have questions or suggestions. Twitter


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>

Dependenciesdependencies

  • 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

Dev Dependencies

  • autorepo: Autorepo abstracts away your dev dependencies, providing a single command to run all of your scripts.

License license

MIT


Related Packages:

Package Sidebar

Install

npm i cross-fetch-json

Weekly Downloads

31

Version

2.0.0

License

MIT

Unpacked Size

6.18 kB

Total Files

8

Last publish

Collaborators

  • bconnorwhite