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; 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 */ 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;