//#region src/client/react/index.d.ts /** * A generated API client endpoint method that can be called by [`useQuery`]{@link useQuery}. * * @example * ```ts * const method: QueryMethod<[id: number], ApiResponse<{ id: number }>> = apiClient.users.get * ``` * * @typeParam TArgs - The endpoint argument tuple. * @typeParam TResponse - The resolved endpoint response type. */ type QueryMethod = (...args: TArgs) => Promise; /** * Resolves the response type returned by a [`QueryMethod`]{@link QueryMethod}. * * @example * ```ts * type Response = QueryResponse * ``` * * @typeParam TMethod - The endpoint method type. */ type QueryResponse = Awaited>; /** * Infers the default data value exposed by [`useQuery`]{@link useQuery}. * * @example * ```ts * type Data = QueryData> * ``` * * @typeParam TResponse - The endpoint response type. */ type QueryData = TResponse extends { body: infer TBody; } ? TBody : TResponse; /** * Current lifecycle status for a [`useQuery`]{@link useQuery} request. * * @example * ```ts * if (query.status === 'success') { * query.data * } * ``` */ type UseQueryStatus = 'idle' | 'loading' | 'success' | 'error'; /** * Options for [`useQuery`]{@link useQuery}. * * @example * ```tsx * const query = useQuery( * { enabled: userId !== undefined, keepPreviousData: true }, * apiClient.users.get, * userId, * ) * ``` * * @typeParam TResponse - The endpoint response type. * @typeParam TData - The selected data type exposed on the result. */ interface UseQueryOptions> { /** * Whether the hook should automatically run the endpoint. */ enabled?: boolean; /** * Keeps the previous response and data visible while the next request is loading. * @defaultValue true */ keepPreviousData?: boolean; /** * Optional query key used instead of the endpoint arguments when deciding whether to refetch. */ key?: unknown; /** * Selects the result data from the endpoint response. */ select?: (response: TResponse) => TData; } /** * Result returned by [`useQuery`]{@link useQuery}. * * @example * ```tsx * const query = useQuery(apiClient.health.get) * * if (query.isLoading) return 'Loading' * if (query.isError) return String(query.error) * * return query.data.ok * ``` * * @typeParam TResponse - The endpoint response type. * @typeParam TData - The selected data type exposed on the result. */ interface UseQueryResult> { /** * The current request lifecycle status. */ status: UseQueryStatus; /** * The latest selected data. By default this is `response.body`. */ data: TData | undefined; /** * The latest full endpoint response. */ response: TResponse | undefined; /** * The latest request error. */ error: unknown; /** * Whether the query has not run yet. */ isIdle: boolean; /** * Whether the query is loading without any successful response. */ isLoading: boolean; /** * Whether a request is currently in flight. */ isFetching: boolean; /** * Whether the query has a successful response. */ isSuccess: boolean; /** * Whether the latest request failed. */ isError: boolean; /** * Runs the endpoint again with the latest method arguments. * * @returns A promise for the endpoint response. */ refetch(): Promise; } /** * Runs a generated API endpoint method and refetches when its arguments change. * * @example * ```tsx * const query = useQuery(apiClient.usersById.get, 123, { includePosts: true }) * ``` * * @param method - Generated API client endpoint method to run. * @param args - Arguments passed to the endpoint method. * @returns Query state, the latest response body, the full response, and a manual refetch function. * @typeParam TMethod - The endpoint method type. */ declare function useQuery(method: TMethod, ...args: Parameters): UseQueryResult, QueryData>>; /** * Runs a generated API endpoint method with query options and refetches when its arguments change. * * @example * ```tsx * const query = useQuery( * { keepPreviousData: true, select: (response) => response.body.items }, * apiClient.search.get, * { q: 'routekit' }, * ) * ``` * * @param options - Query behavior options. * @param method - Generated API client endpoint method to run. * @param args - Arguments passed to the endpoint method. * @returns Query state, the selected data, the full response, and a manual refetch function. * @typeParam TMethod - The endpoint method type. * @typeParam TData - The selected data type. */ declare function useQuery>>(options: UseQueryOptions, TData>, method: TMethod, ...args: Parameters): UseQueryResult, TData>; //#endregion export { QueryData, QueryMethod, QueryResponse, UseQueryOptions, UseQueryResult, UseQueryStatus, useQuery };