import { type MapSameFunction } 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: FetchJsonBody | undefined): string | undefined; export interface FetchJsonInput extends Omit { readonly method: FetchMethod; readonly body?: FetchJsonBody | undefined; /** * 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) => string | null | never; export declare const throwJsonResponseParseErrorFunction: HandleFetchJsonParseErrorFunction; export declare const returnNullHandleFetchJsonParseErrorFunction: HandleFetchJsonParseErrorFunction; 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; } /** * 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 */ defaultMethod?: string; /** * Optional map function to modify the FetchJsonInput before it is finalized into a RequestInit value. */ mapFetchJsonInput?: FetchJsonInputMapFunction; } export type FetchJsonRequestInitFunction = (methodOrInput?: string | FetchJsonInput | undefined, 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 - optional configuration specifying the default HTTP method and an input mapping function * @returns a function that produces a {@link RequestInit} from a method string or {@link FetchJsonInput} */ export declare function fetchJsonRequestInitFunction(config?: FetchJsonRequestInitFunctionConfig): FetchJsonRequestInitFunction; export declare const fetchJsonRequestInit: FetchJsonRequestInitFunction;