Skip to content

bconnorwhite/cross-fetch-json

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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:

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published