Skip to content

fetchJson

Fetches a URL and returns its parsed JSON body, retrying transient failures (network errors, 5xx and 429).

Available since v3.1.5

import { fetchJson } from '@helpers4/promise';
// or, from the all-in-one package (same code, one install):
import { fetchJson } from 'helpers4/promise';
fetchJson<T>(input: RequestInfo | URL, init?: RequestInit, options: FetchJsonOptions): Promise<T>
ParameterTypeDescription
inputRequestInfo | URLURL or `Request` object passed to `fetch`
initRequestInitOptional `RequestInit` options passed to `fetch` (optional)
optionsFetchJsonOptionsRetry configuration — see FetchJsonOptions

Promise<T> — The parsed JSON body

const repo = await fetchJson<{ stars: number }>('https://api.example.com/repo');

Options for fetchJson.

type FetchJsonOptions = FetchFetchJsonOptions

View source on GitHub