import * as swr from 'swr'; import { SWRConfiguration, SWRResponse, MutatorCallback, MutatorOptions } from 'swr'; import * as type_fest from 'type-fest'; import { Exact, PartialDeep } from 'type-fest'; import * as openapi_fetch from 'openapi-fetch'; import { MaybeOptionalInit, FetchResponse, Client } from 'openapi-fetch'; import * as openapi_typescript_helpers from 'openapi-typescript-helpers'; import { PathsWithMethod, HttpMethod, MediaType, RequiredKeysOf } from 'openapi-typescript-helpers'; import * as swr_infinite from 'swr/infinite'; import { SWRInfiniteConfiguration, SWRInfiniteKeyLoader } from 'swr/infinite'; type MaybeRequired = RequiredKeysOf extends never ? T | undefined : T; type TryKey = T extends { [Key in K]?: unknown; } ? T[K] : undefined; /** * Provides specific types used within a given request */ type TypesForRequest, Method extends Extract, Path extends PathsWithMethod, Init = MaybeOptionalInit, Params = Init extends { params?: unknown; } ? Init["params"] : undefined, Res = FetchResponse, Data = Extract["data"], Error = Extract["error"], PathParams = TryKey, Query = TryKey, Headers = TryKey, Cookies = TryKey, SWRConfig = SWRConfiguration> = { Init: Init; Data: Data; Error: Error; Path: MaybeRequired; Query: MaybeRequired; Headers: MaybeRequired; Cookies: Cookies; SWRConfig: SWRConfig; SWRResponse: SWRResponse; }; /** * Provides specific types for GET requests * * Uses {@link TypesForRequest} */ type TypesForGetRequest, Path extends PathsWithMethod>> = TypesForRequest, Path>; /** * Produces a typed wrapper for [`useSWRImmutable`](https://swr.vercel.app/docs/revalidation.en-US#disable-automatic-revalidations). * * ```ts * import createClient from "openapi-fetch"; * const client = createClient(); * * const useImmutable = createImmutableHook(client, ""); * * // Fetch the query * useImmutable("/pets"); * * // Skip the query * useImmutable("/pets", null); * * // Fetch the query with parameters * useImmutable("/pets", { * params: { query: { limit: 10 } } * }); * * // Fetch the query with parameters and SWR configuration * useImmutable( * "/pets", * { params: { query: { limit: 10 } } }, * { errorRetryCount: 2 }, * ); * * // Fetch the query with no parameters and SWR configuration * useImmutable( * "/pets", * {}, * { errorRetryCount: 2 }, * ); * ``` */ declare const createImmutableHook: (client: openapi_fetch.Client, prefix: Prefix) => , R extends TypesForGetRequest, Init extends type_fest.Exact, // Fetch the query with no parameters and SWR configuration Data extends R["Data"], Error extends R["Error"] | FetcherError, Config extends R["SWRConfig"]>(path: Path, ...[init, config]: openapi_typescript_helpers.RequiredKeysOf extends never ? [(Init | null)?, Config?] : [Init | null, Config?]) => swr.SWRResponse; /** * Produces a typed wrapper for [`useSWRInfinite`](https://swr.vercel.app/docs/pagination#useswrinfinite). * * ```ts * import createClient from "openapi-fetch"; * const client = createClient(); * * const useInfinite = createInfiniteHook(client, ""); * * useInfinite("/pets", (index, previousPage) => { * if (previousPage && !previousPage.hasMore) { * return null; * } * * return { * params: { * query: { * limit: 10, * offset: index * 10, * }, * }, * }; * }); * ``` */ declare function createInfiniteHook(client: Client, prefix: Prefix): , R extends TypesForGetRequest, Init extends Exact, Data extends R["Data"], Error extends R["Error"] | FetcherError, Config extends SWRInfiniteConfiguration>(path: Path, getInit: SWRInfiniteKeyLoader, config?: Config) => swr_infinite.SWRInfiniteResponse; type CompareFn = (init: any, partialInit: any) => boolean; /** * Produces a typed wrapper for [`useSWRConfig#mutate`](https://swr.vercel.app/docs/mutation). * * ```ts * import createClient from "openapi-fetch"; * import { isMatch } from "lodash"; * * const client = createClient(); * * const useMutate = createMutateHook(client, "", isMatch); * * const mutate = useMutate(); * * // Revalidate all keys matching this path * await mutate(["/pets"]); * await mutate(["/pets"], newData); * await mutate(["/pets"], undefined, { revalidate: true }); * * // Revlidate all keys matching this path and this subset of options * await mutate( * ["/pets", { query: { limit: 10 } }], * newData, * { revalidate: false } * ); * ``` */ declare function createMutateHook(client: Client, prefix: string, compare: CompareFn): () => , R extends TypesForGetRequest, Init extends Exact>([path, init]: [Path, PartialDeep?], data?: R["Data"] | Promise | MutatorCallback, opts?: boolean | MutatorOptions) => Promise<(R["Data"] | undefined)[]>; /** * Produces a typed wrapper for [`useSWR`](https://swr.vercel.app/docs/api). * * ```ts * import createClient from "openapi-fetch"; * * const client = createClient(); * * const useQuery = createQueryHook(client, ""); * * // Fetch the query * useQuery("/pets"); * * // Skip the query * useQuery("/pets", null); * * // Fetch the query with parameters * useQuery("/pets", { * params: { query: { limit: 10 } } * }); * * // Fetch the query with parameters and SWR configuration * useQuery( * "/pets", * { params: { query: { limit: 10 } } }, * { errorRetryCount: 2 }, * ); * * // Fetch the query with no parameters and SWR configuration * useQuery( * "/pets", * {}, * { errorRetryCount: 2 }, * ); * ``` */ declare const createQueryHook: (client: openapi_fetch.Client, prefix: Prefix) => , R extends TypesForGetRequest, Init extends type_fest.Exact, Data extends R["Data"], Error extends R["Error"] | FetcherError, Config extends R["SWRConfig"]>(path: Path, ...[init, config]: openapi_typescript_helpers.RequiredKeysOf extends never ? [(Init | null)?, Config?] : [Init | null, Config?]) => swr.SWRResponse; export { createImmutableHook, createInfiniteHook, createMutateHook, createQueryHook }; export type { CompareFn, TypesForGetRequest, TypesForRequest };