import { type MapSameFunction, type Maybe } from '@dereekb/util'; import { type FetchMethod, type ConfiguredFetch } from './fetch.type'; import { type FetchURLInput } from './url'; export type FetchJsonBody = string | object; export declare class JsonResponseParseError extends Error { readonly response: Response; constructor(response: Response); } /** * Converts the input to a JSON string, or undefined if not provided. * * @param body * @returns */ export declare function fetchJsonBodyString(body: Maybe): Maybe; export interface FetchJsonInput extends Omit { readonly method: FetchMethod; readonly body?: Maybe; /** * Optional intercept function to intercept/transform the response. * * Does not override any other configured interceptor and occurs after those configured interceptors. */ readonly interceptResponse?: FetchJsonInterceptJsonResponseFunction; } export type FetchJsonInputMapFunction = MapSameFunction; export type FetchJsonGetFunction = (url: FetchURLInput) => Promise; export type FetchJsonMethodAndBodyFunction = (url: FetchURLInput, method: string, body?: FetchJsonBody) => Promise; export type FetchJsonWithInputFunction = (url: FetchURLInput, input: FetchJsonInput) => Promise; /** * Used to fetch from the input url and retrieve a JSON response. */ export type FetchJsonFunction = FetchJsonGetFunction & FetchJsonMethodAndBodyFunction & FetchJsonWithInputFunction; export type FetchJsonInterceptJsonResponseFunction = (json: unknown, response: Response) => unknown; export type HandleFetchJsonParseErrorFunction = (response: Response) => Maybe; export declare const throwJsonResponseParseErrorFunction: HandleFetchJsonParseErrorFunction; export declare const returnNullHandleFetchJsonParseErrorFunction: HandleFetchJsonParseErrorFunction; /** * Turns a {@link Response} into its decoded value. Rejects when the body cannot be decoded, which hands the response to the * configured {@link HandleFetchJsonParseErrorFunction}. */ export type FetchJsonDecodeResponseFunction = (response: Response) => Promise; /** * The default {@link FetchJsonDecodeResponseFunction}: `response.json()`. * * @param response - The response to decode. * @returns The decoded JSON. */ export declare const DEFAULT_FETCH_JSON_DECODE_RESPONSE_FUNCTION: FetchJsonDecodeResponseFunction; /** * The result of {@link readFetchJsonText}: the response, its body text (read once) and the decoded value. */ export interface FetchJsonTextResult { readonly response: Response; /** * The raw body text. Always present; the body is read exactly once. */ readonly text: string; /** * The decoded value, or undefined when the text was not JSON. * * `T` is the caller's claim about the body. It is never validated, exactly like {@link FetchJsonFunction}'s result type. */ readonly json: Maybe; /** * False when the text was not JSON. Tells a non-JSON body apart from one that decoded to `null`. */ readonly parsed: boolean; } /** * Decodes body text into a value. Returning undefined, or throwing, both mean "not JSON". */ export type ReadFetchJsonTextParseJsonFunction = (text: string) => unknown; export interface ReadFetchJsonTextConfig { /** * Text decoder. Defaults to `@dereekb/util`'s `parseJsonOrUndefined`. * * JSON repair is never on by default; opt in by passing a repairing decoder, such as * `(text) => repairJsonObjectOrUndefined(text)`. */ readonly parseJson?: Maybe; } /** * Awaits a fetch, reads its body as text once, and decodes that text as JSON. * * Never throws for a non-ok status or a non-JSON body: the caller reads `response.ok`, `parsed` and `text`. Rejects only * when the request itself or the body read fails, with that original error, so a later classifier still sees it. * * @param responsePromise - The pending fetch. A caller already holding a Response passes `Promise.resolve(response)`. * @param config - Optional decoder override. * @returns The response, its raw text and the decoded value. * * @dbxUtil * @dbxUtilCategory fetch * @dbxUtilKind function * @dbxUtilTags fetch, json, response, text, body, parse, read */ export declare function readFetchJsonText(responsePromise: Promise, config?: Maybe): Promise>; /** * Creates a {@link FetchJsonDecodeResponseFunction} built on {@link readFetchJsonText}. * * Rejects with a {@link JsonResponseParseError} when the text is not JSON, so the configured parse-error handler still runs. * The decoder consumes the body, so the handler cannot re-read it; `response.json()` behaves the same way. * * @param config - Optional decoder override, e.g. a repairing `parseJson`. * @returns A decoder to pass as {@link FetchJsonFunctionConfig.decodeResponse}. * * @dbxUtil * @dbxUtilCategory fetch * @dbxUtilKind factory * @dbxUtilTags fetch, json, decode, response, repair, factory * * @__NO_SIDE_EFFECTS__ */ export declare function fetchJsonTextDecodeResponseFunction(config?: Maybe): FetchJsonDecodeResponseFunction; export interface FetchJsonFunctionConfig extends FetchJsonRequestInitFunctionConfig { /** * Optional intercept function to transform all response JSON before returning the result. * * Useful for cases where an API returns errors with a 200 response. */ readonly interceptJsonResponse?: FetchJsonInterceptJsonResponseFunction; /** * Optional function to handle JSON parsing errors. */ readonly handleFetchJsonParseErrorFunction?: HandleFetchJsonParseErrorFunction; /** * How a response becomes a value. Defaults to {@link DEFAULT_FETCH_JSON_DECODE_RESPONSE_FUNCTION} (`response.json()`). * * A rejection is handed to {@link handleFetchJsonParseErrorFunction} together with the response. */ readonly decodeResponse?: FetchJsonDecodeResponseFunction; } /** * Creates a FetchJsonFunction from the input ConfiguredFetch. * * @param fetch - The configured fetch function to use for making HTTP requests. * @param inputConfig - Optional configuration or error handler for JSON parsing; when a function is provided it is used as the parse error handler. * @returns A FetchJsonFunction that performs requests and parses JSON responses. */ export declare function fetchJsonFunction(fetch: ConfiguredFetch, inputConfig?: FetchJsonFunctionConfig | HandleFetchJsonParseErrorFunction): FetchJsonFunction; export interface FetchJsonRequestInitFunctionConfig { /** * Default request method. * * Defaults to GET */ readonly defaultMethod?: string; /** * Optional map function to modify the FetchJsonInput before it is finalized into a RequestInit value. */ readonly mapFetchJsonInput?: FetchJsonInputMapFunction; } export type FetchJsonRequestInitFunction = (methodOrInput?: Maybe, body?: FetchJsonBody) => RequestInit; /** * Creates a {@link FetchJsonRequestInitFunction} that converts method/body inputs into a fully formed {@link RequestInit}, * applying the configured default method and optional input mapping. * * @param config - Default-method and input-mapping overrides applied by the generated function. * @returns Reusable builder that resolves a `RequestInit` for each request. */ export declare function fetchJsonRequestInitFunction(config?: FetchJsonRequestInitFunctionConfig): FetchJsonRequestInitFunction; export declare const fetchJsonRequestInit: FetchJsonRequestInitFunction;