包详细信息

cross-fetch-json

bconnorwhite122MIT2.0.0

Universal fetch API that returns JSON

cross, fetch, json, http

自述文件

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:

更新日志

2.0.0 (2023-01-30)

Bug Fixes

  • include content-type header (7a8eee5)