import { type Factory, type MapFunction, type Maybe, type WebsiteUrl, type PromiseOrValue, type GetterOrValue } from '@dereekb/util'; import { type ConfiguredFetchWithTimeout, type RequestInitWithTimeout } from './fetch.type'; /** * Interface used for creating fetch related resource factories. */ export interface FetchService { readonly makeFetch: (config?: ConfigureFetchInput) => ConfiguredFetchWithTimeout; readonly makeRequest: FetchRequestFactory; readonly fetchRequestFactory: typeof fetchRequestFactory; } /** * fetchService() configuration. */ export type FetchServiceConfig = Required> & Pick; /** * Used to create a FetchService. * * @param config * @returns */ export declare function fetchService(config: FetchServiceConfig): FetchService; export type MapFetchResponseFunction = MapFunction, Promise>; /** * Custom fetch handler that takes in a Request and fetch function and returns a Response. */ export type FetchHandler = (request: Request, makeFetch: typeof fetch) => Promise; export interface ConfigureFetchInput extends FetchRequestFactoryInput { readonly makeFetch?: typeof fetch; /** * Whether or not to add timeout handling using timeoutFetch(). * * Default: false */ readonly useTimeout?: boolean; /** * Whether or not to map the fetch response using requireOkResponse(). * * Default: false */ readonly requireOkResponse?: boolean; /** * (Optional) Custom fetch handler. */ readonly fetchHandler?: FetchHandler; /** * (Optional) MapFetchResponseFunction * * If requireOkResponse is true, this mapping occurs afterwards. */ readonly mapResponse?: MapFetchResponseFunction; } /** * Default FetchHabdler * * @param request * @param makeFetch * @returns */ export declare const DEFAULT_FETCH_HANDLER: FetchHandler; /** * Creates a function that wraps fetch and uses a FetchRequestFactory to generate a Request before invoking Fetch. * * @param config * @returns */ export declare function configureFetch(config: ConfigureFetchInput): ConfiguredFetchWithTimeout; export interface FetchRequestFactoryInput { /** * Request factory to use. If not defined, will default to window/global. */ readonly makeRequest?: FetchRequestFactory; /** * Appends this URL to every fetch request. */ readonly baseUrl?: WebsiteUrl; /** * Whether or not to append the base url to RequestInfo that is already configured, instead of only URL or strings. */ readonly useBaseUrlForConfiguredFetchRequests?: boolean; /** * Whether or not to force always using the base url even when a WebsiteUrlWithPrefix value is provided. * * Defaults to useBaseUrlForConfiguredFetchRequests's value. */ readonly forceBaseUrlForWebsiteUrlWithPrefix?: boolean; /** * Base request info to add to each value. */ readonly baseRequest?: GetterOrValue>; /** * Default timeout to add to requestInit values. * * NOTE: This timeout is not used by this fetchRequest directly, but is added to the baseRequest. */ readonly timeout?: number; /** * Maps the input RequestInit value to another. * * If baseRequest is provided, the values will already be appended before reaching this factory. */ readonly requestInitFactory?: FetchRequestInitFactory; } export type FetchRequestInitFactory = (currRequest: PromiseOrValue, init?: PromiseOrValue) => PromiseOrValue; export type FetchRequestFactory = (input: RequestInfo | URL, init?: RequestInit | undefined) => PromiseOrValue; export type AbortControllerFactory = Factory; /** * The deafult FetchRequestFactory implementation that uses window/global Request. * * @param input * @param init * @returns */ export declare const DEFAULT_FETCH_REQUEST_FACTORY: FetchRequestFactory; /** * Creates a FetchRequestFactory that builds Request objects by applying base URL resolution, * base request init merging, timeout configuration, and custom request init transformations. * * @param config - configuration for URL resolution, base request defaults, timeout, and request init transformations * @returns a FetchRequestFactory that produces fully configured Request objects */ export declare function fetchRequestFactory(config: FetchRequestFactoryInput): FetchRequestFactory; /** * Merges two RequestInit objects, combining their headers and spreading remaining properties * so that values from the second init override the base. * * @param base - the base RequestInit to merge onto * @param requestInit - optional RequestInit whose values override the base * @returns the merged RequestInit */ export declare function mergeRequestInits(base: T, requestInit?: T | undefined): T; /** * Merges an array of HeadersInit values into a single array of key-value tuples. * Later headers override earlier ones on a per-key basis while preserving multi-value support. * * @param inputHeadersArray - array of HeadersInit values to merge, where later entries take precedence * @returns an array of [key, value] tuples representing the merged headers */ export declare function mergeRequestHeaders(inputHeadersArray: Maybe[]): [string, string][]; /** * Converts a HeadersInit value (tuple array, Headers object, or plain object) into * a normalized array of [key, value] tuples. * * @param headers - the HeadersInit value to convert * @returns an array of [key, value] string tuples */ export declare function headersToHeadersTuple(headers: HeadersInit): [string, string][]; /** * Type guard that checks whether the given input is a fetch Request object * by testing for the presence of a url property. * * @param input - the value to test * @returns true if the input is a Request */ export declare function isFetchRequest(input: unknown): input is Request;