import type { ApiEndpoint } from '../endpoint.js'; import type { IEndpointInfo } from '../endpoint.types.js'; import type { CallerHooks } from '../hooks.js'; /** * Serializer function that converts a plain object into a FormData instance (or similar body). */ export type IFormDataSerializer = (data: Record) => unknown; /** * Options for the default FormData serializer factory. */ export interface FormDataSerializerOptions { /** * FormData constructor to use. Defaults to `globalThis.FormData`. * * Useful for: * - Older Node.js versions using `form-data` npm package * - Testing with mocks */ FormData?: new () => FormData; /** * How to serialize non-primitive, non-Blob values (e.g., nested objects, arrays). * - `'json'` (default): `JSON.stringify(value)` * - Custom function: `(key, value) => string | Blob` */ serializeValue?: 'json' | ((key: string, value: unknown) => string | Blob); } /** * Creates a default FormData serializer. * * Iterates over own enumerable properties of the data object and appends them to a new FormData instance. * * - `Blob`/`File` values are appended as-is * - `null`/`undefined` values are skipped * - Primitive values are converted to strings * - Objects/arrays are JSON-stringified by default (configurable via `serializeValue`) */ export declare function createFormDataSerializer(options?: FormDataSerializerOptions): IFormDataSerializer; /** * FormData serialization extension for endpoint. * * Marks an endpoint to have its request body serialized as FormData before sending. * Works with any HTTP transport (fetch, axios, etc.) — the library converts the plain object * to FormData via a configurable serializer, so the transport receives a ready-to-send FormData body. * * @example * ```typescript * const Endpoint = ApiEndpoint.create.extend(IEndpointFormData.extender); * * const upload = Endpoint('Upload') * .post<{ name: string, file: File }, { url: string }>() * .asFormData(); * ``` */ export interface IEndpointFormData { /** Whether this endpoint should serialize body data as FormData. `true` uses the default serializer; a function uses a custom one. */ readonly formData?: boolean | IFormDataSerializer; /** Marks this endpoint to serialize request body as FormData using the provided (or default) serializer. */ asFormData(serializer?: IFormDataSerializer): this; } export declare namespace IEndpointFormData { const extender: ApiEndpoint.IBuilderExtender; function guard(api: IEndpointInfo): api is (IEndpointInfo & IEndpointFormData); /** * Creates caller hooks for FormData serialization. * * The hook converts `config.data` (plain object) into a FormData instance using the provided serializer. * * @param serializer - Default serializer used when endpoint has `.asFormData()` without a custom serializer. * Use {@link createFormDataSerializer} for the built-in one, or provide a fully custom function. * * @example * ```typescript * // Simplest setup (global FormData, JSON-stringify objects) * IEndpointFormData.createHooks(createFormDataSerializer()) * * // Custom FormData constructor * import FormDataNode from 'form-data'; * IEndpointFormData.createHooks(createFormDataSerializer({ FormData: FormDataNode as any })) * * // Fully custom serializer * IEndpointFormData.createHooks((data) => { * const fd = new FormData(); * // custom logic... * return fd; * }) * ``` */ function createHooks(serializer: IFormDataSerializer): CallerHooks; }