import type { AxiosError, Method } from "axios"; import type { UseMutationResult, DefaultError } from "@tanstack/react-query"; import type { PartialDeep } from "type-fest"; import type { FlexibleConciergeConfig } from "../../../types/concierge"; import type { FunctionlessUseMutationOptions } from "../../../types/tanstack-query"; import type { DatalessAxiosRequestConfig, ServerData, Methodless } from "../../../types/axios"; /** * Specifies the available requests that can be performed with * `useCreateUpdate`: * - `create` for `POST` requests * - `update` for `PUT` requests * - `partialUpdate` for `PATCH` requests */ export type UseCreateUpdateRequest = "create" | "update" | "partialUpdate"; /** * Ensures the passed payload's type is compatible with the request type. The * type is either recursively marked as optional for `partialUpdate` or left * as-is. * * @template Q The request type. * @template D The request's payload. */ export type UseCreateUpdatePayload = Q extends "partialUpdate" ? PartialDeep : D; /** Extracts the selected methos from Axios. */ export type UseCreateUpdateMethod = Extract; /** * Forces the `method` attribute based on the given `UseCreateUpdateRequest`. * * @template Q The request type. */ type _UseCreateUpdateMethodGuard = Q extends "create" ? Extract : Q extends "update" ? Extract : Extract; /** * Represents the error speficically raised by Axios. * * @template Q The request type. * @template D The request's payload. * @template E The error. */ export type UseCreateUpdateHTTPError = AxiosError>; /** * Represents the exception raised by `mutationFn` - either a standard error or * an Axios error. * * @template Q The request type. * @template D The request's payload. * @template E The error. */ export type UseCreateUpdateException = DefaultError | UseCreateUpdateHTTPError; /** * `useCreateUpdate` hook configuration. Offers `useMutation`, Axios and * concierge configurations separately. * * @template Q The request type. * @template R The response. * @template D The request's payload. * @template P The URL parameters. * @template E The error. */ export interface UseCreateUpdateOptions extends FlexibleConciergeConfig { /** `useMutation` configuration without `mutationFn`. */ reactQuery: FunctionlessUseMutationOptions, UseCreateUpdatePayload, UseCreateUpdateException>; /** Axios configuration with forced method and no data. */ axios: Methodless> & { method: _UseCreateUpdateMethodGuard; }; } /** * `useCreateUpdate` hook result. fromServeres TanStack Query's `useMutation` * result. * * @template Q The request type. * @template R The response. * @template D The request's payload. * @template E The error. */ export type UseCreateUpdateResult = UseMutationResult, UseCreateUpdateException, UseCreateUpdatePayload>; export {};