UNPKG

@zodios/solid

Version:
196 lines (193 loc) 15.2 kB
import * as _zodios_core_lib_zodios_types from '@zodios/core/lib/zodios.types'; import { ZodiosAliases, Aliases, MutationMethod } from '@zodios/core/lib/zodios.types'; import * as zod from 'zod'; import * as _zodios_core_lib_utils_types from '@zodios/core/lib/utils.types'; import { RequiredKeys, ReadonlyDeep, PathParamNames, IfEquals } from '@zodios/core/lib/utils.types'; import { QueryKey, CreateQueryResult, CreateInfiniteQueryResult, CreateMutationResult, CreateMutationOptions, CreateQueryOptions, CreateInfiniteQueryOptions } from '@tanstack/solid-query'; import { ZodiosEndpointDefinitions, ZodiosInstance, Method, ZodiosPathsByMethod, ZodiosRequestOptionsByPath, ZodiosRequestOptionsByAlias, ZodiosBodyByPath, ZodiosQueryParamsByPath, ZodiosError, ZodiosEndpointDefinition, ZodiosResponseByPath, ZodiosEndpointDefinitionByAlias, ZodiosBodyByAlias, ZodiosResponseByAlias } from '@zodios/core'; import { AxiosError } from 'axios'; type UndefinedIfNever<T> = IfEquals<T, never, undefined, T>; type Errors = Error | ZodiosError | AxiosError; type MutationOptions<Api extends ZodiosEndpointDefinition[], M extends Method, Path extends ZodiosPathsByMethod<Api, M>> = Omit<CreateMutationOptions<Awaited<ZodiosResponseByPath<Api, M, Path>>, Errors, UndefinedIfNever<ZodiosBodyByPath<Api, M, Path>>>, "mutationFn">; type MutationOptionsByAlias<Api extends ZodiosEndpointDefinitions, Alias extends string> = Omit<CreateMutationOptions<Awaited<ZodiosResponseByAlias<Api, Alias>>, Errors, UndefinedIfNever<ZodiosBodyByAlias<Api, Alias>>>, "mutationFn">; type QueryOptions<Api extends ZodiosEndpointDefinitions, Path extends ZodiosPathsByMethod<Api, "get">> = Awaited<CreateQueryOptions<ZodiosResponseByPath<Api, "get", Path>, Errors>>; type QueryOptionsByAlias<Api extends ZodiosEndpointDefinitions, Alias extends string> = Awaited<CreateQueryOptions<ZodiosResponseByAlias<Api, Alias>, Errors>>; type ImmutableQueryOptions<Api extends ZodiosEndpointDefinitions, M extends Method, Path extends ZodiosPathsByMethod<Api, M>> = Awaited<CreateQueryOptions<ZodiosResponseByPath<Api, M, Path>, Errors>>; type InfiniteQueryOptions<Api extends ZodiosEndpointDefinitions, Path extends ZodiosPathsByMethod<Api, "get">> = Awaited<CreateInfiniteQueryOptions<ZodiosResponseByPath<Api, "get", Path>, Errors>>; type ImmutableInfiniteQueryOptions<Api extends ZodiosEndpointDefinitions, M extends Method, Path extends ZodiosPathsByMethod<Api, M>> = Awaited<CreateInfiniteQueryOptions<ZodiosResponseByPath<Api, M, Path>, Errors>>; declare class ZodiosHooksClass<Api extends ZodiosEndpointDefinitions> { private readonly apiName; private readonly zodios; constructor(apiName: string, zodios: ZodiosInstance<Api>); private injectAliasEndpoints; private getEndpointByPath; private getEndpointByAlias; /** * compute the key for the provided endpoint * @param method - HTTP method of the endpoint * @param path - path for the endpoint * @param config - parameters of the api to the endpoint - when providing no parameters, will return the common key for the endpoint * @returns - Key */ getKeyByPath<M extends Method, Path extends ZodiosPathsByMethod<Api, Method>>(method: M, path: Path extends ZodiosPathsByMethod<Api, M> ? Path : never, config?: ZodiosRequestOptionsByPath<Api, M, Path>): QueryKey; /** * compute the key for the provided endpoint alias * @param alias - alias of the endpoint * @param config - parameters of the api to the endpoint * @returns - QueryKey */ getKeyByAlias<Alias extends keyof ZodiosAliases<Api>>(alias: Alias extends string ? Alias : never, config?: Alias extends string ? ZodiosRequestOptionsByAlias<Api, Alias> : never): QueryKey; createQuery<Path extends ZodiosPathsByMethod<Api, "get">, TConfig extends ZodiosRequestOptionsByPath<Api, "get", Path>>(path: Path, ...[config, queryOptions]: RequiredKeys<TConfig> extends never ? [ config?: ReadonlyDeep<TConfig>, queryOptions?: Omit<QueryOptions<Api, Path>, "queryKey" | "queryFn"> ] : [ config: ReadonlyDeep<TConfig>, queryOptions?: Omit<QueryOptions<Api, Path>, "queryKey" | "queryFn"> ]): CreateQueryResult<zod.output<_zodios_core_lib_utils_types.FilterArrayByValue<Api, { method: "get"; path: Path; }, []>[number]["response"]>, Errors> & { invalidate: () => Promise<void>; key: QueryKey; }; createImmutableQuery<Path extends ZodiosPathsByMethod<Api, "post">, TConfig extends ZodiosRequestOptionsByPath<Api, "post", Path>>(path: Path, body: ReadonlyDeep<UndefinedIfNever<ZodiosBodyByPath<Api, "post", Path>>>, ...[config, queryOptions]: RequiredKeys<TConfig> extends never ? [ config?: ReadonlyDeep<TConfig>, queryOptions?: Omit<ImmutableQueryOptions<Api, "post", Path>, "queryKey" | "queryFn"> ] : [ config: ReadonlyDeep<TConfig>, queryOptions?: Omit<ImmutableQueryOptions<Api, "post", Path>, "queryKey" | "queryFn"> ]): CreateQueryResult<zod.output<_zodios_core_lib_utils_types.FilterArrayByValue<Api, { method: "post"; path: Path; }, []>[number]["response"]>, Errors> & { invalidate: () => Promise<void>; key: QueryKey; }; createInfiniteQuery<Path extends ZodiosPathsByMethod<Api, "get">, TConfig extends ZodiosRequestOptionsByPath<Api, "get", Path>>(path: Path, ...[config, queryOptions]: RequiredKeys<TConfig> extends never ? [ config?: ReadonlyDeep<TConfig>, queryOptions?: Omit<InfiniteQueryOptions<Api, Path>, "queryKey" | "queryFn"> & { getPageParamList: () => ((ZodiosQueryParamsByPath<Api, "get", Path> extends never ? never : keyof ZodiosQueryParamsByPath<Api, "get", Path>) | PathParamNames<Path>)[]; } ] : [ config: ReadonlyDeep<TConfig>, queryOptions?: Omit<InfiniteQueryOptions<Api, Path>, "queryKey" | "queryFn"> & { getPageParamList: () => ((ZodiosQueryParamsByPath<Api, "get", Path> extends never ? never : keyof ZodiosQueryParamsByPath<Api, "get", Path>) | PathParamNames<Path>)[]; } ]): CreateInfiniteQueryResult<zod.output<_zodios_core_lib_utils_types.FilterArrayByValue<Api, { method: "get"; path: Path; }, []>[number]["response"]>, Errors> & { invalidate: () => Promise<void>; key: QueryKey; }; createImmutableInfiniteQuery<Path extends ZodiosPathsByMethod<Api, "post">, TConfig extends ZodiosRequestOptionsByPath<Api, "post", Path>>(path: Path, body: ReadonlyDeep<UndefinedIfNever<ZodiosBodyByPath<Api, "post", Path>>>, ...[config, queryOptions]: RequiredKeys<TConfig> extends never ? [ config?: ReadonlyDeep<TConfig>, queryOptions?: Omit<ImmutableInfiniteQueryOptions<Api, "post", Path>, "queryKey" | "queryFn"> & { getPageParamList: () => (keyof ZodiosBodyByPath<Api, "post", Path> | PathParamNames<Path> | (ZodiosQueryParamsByPath<Api, "post", Path> extends never ? never : keyof ZodiosQueryParamsByPath<Api, "post", Path>))[]; } ] : [ config: ReadonlyDeep<TConfig>, queryOptions?: Omit<ImmutableInfiniteQueryOptions<Api, "post", Path>, "queryKey" | "queryFn"> & { getPageParamList: () => (keyof ZodiosBodyByPath<Api, "post", Path> | PathParamNames<Path> | (ZodiosQueryParamsByPath<Api, "post", Path> extends never ? never : keyof ZodiosQueryParamsByPath<Api, "post", Path>))[]; } ]): CreateInfiniteQueryResult<zod.output<_zodios_core_lib_utils_types.FilterArrayByValue<Api, { method: "post"; path: Path; }, []>[number]["response"]>, Errors> & { invalidate: () => Promise<void>; key: QueryKey; }; createMutation<M extends Method, Path extends ZodiosPathsByMethod<Api, M>, TConfig extends ZodiosRequestOptionsByPath<Api, M, Path>>(method: M, path: Path, ...[config, mutationOptions]: RequiredKeys<TConfig> extends never ? [ config?: ReadonlyDeep<TConfig>, mutationOptions?: MutationOptions<Api, M, Path> ] : [ config: ReadonlyDeep<TConfig>, mutationOptions?: MutationOptions<Api, M, Path> ]): CreateMutationResult<zod.output<_zodios_core_lib_utils_types.FilterArrayByValue<Api, { method: M; path: Path; }, []>[number]["response"]>, Error | ZodiosError | AxiosError<unknown, any>, IfEquals<zod.input<_zodios_core_lib_zodios_types.BodySchema<Api, M, Path>>, never, undefined, zod.input<_zodios_core_lib_zodios_types.BodySchema<Api, M, Path>>>, unknown>; createGet<Path extends ZodiosPathsByMethod<Api, "get">, TConfig extends ZodiosRequestOptionsByPath<Api, "get", Path>>(path: Path, ...rest: RequiredKeys<TConfig> extends never ? [ config?: ReadonlyDeep<TConfig>, queryOptions?: Omit<QueryOptions<Api, Path>, "queryKey" | "queryFn"> ] : [ config: ReadonlyDeep<TConfig>, queryOptions?: Omit<QueryOptions<Api, Path>, "queryKey" | "queryFn"> ]): CreateQueryResult<zod.output<_zodios_core_lib_utils_types.FilterArrayByValue<Api, { method: "get"; path: Path; }, []>[number]["response"]>, Errors> & { invalidate: () => Promise<void>; key: QueryKey; }; createPost<Path extends ZodiosPathsByMethod<Api, "post">, TConfig extends ReadonlyDeep<ZodiosRequestOptionsByPath<Api, "post", Path>>>(path: Path, ...rest: RequiredKeys<TConfig> extends never ? [ config?: ReadonlyDeep<TConfig>, mutationOptions?: MutationOptions<Api, "post", Path> ] : [ config: ReadonlyDeep<TConfig>, mutationOptions?: MutationOptions<Api, "post", Path> ]): CreateMutationResult<zod.output<_zodios_core_lib_utils_types.FilterArrayByValue<Api, { method: "post"; path: Path; }, []>[number]["response"]>, Error | ZodiosError | AxiosError<unknown, any>, IfEquals<zod.input<_zodios_core_lib_zodios_types.BodySchema<Api, "post", Path>>, never, undefined, zod.input<_zodios_core_lib_zodios_types.BodySchema<Api, "post", Path>>>, unknown>; createPut<Path extends ZodiosPathsByMethod<Api, "put">, TConfig extends ReadonlyDeep<ZodiosRequestOptionsByPath<Api, "put", Path>>>(path: Path, ...rest: RequiredKeys<TConfig> extends never ? [ config?: ReadonlyDeep<TConfig>, mutationOptions?: MutationOptions<Api, "put", Path> ] : [ config: ReadonlyDeep<TConfig>, mutationOptions?: MutationOptions<Api, "put", Path> ]): CreateMutationResult<zod.output<_zodios_core_lib_utils_types.FilterArrayByValue<Api, { method: "put"; path: Path; }, []>[number]["response"]>, Error | ZodiosError | AxiosError<unknown, any>, IfEquals<zod.input<_zodios_core_lib_zodios_types.BodySchema<Api, "put", Path>>, never, undefined, zod.input<_zodios_core_lib_zodios_types.BodySchema<Api, "put", Path>>>, unknown>; createPatch<Path extends ZodiosPathsByMethod<Api, "patch">, TConfig extends ReadonlyDeep<ZodiosRequestOptionsByPath<Api, "patch", Path>>>(path: Path, ...rest: RequiredKeys<TConfig> extends never ? [ config?: ReadonlyDeep<TConfig>, mutationOptions?: MutationOptions<Api, "patch", Path> ] : [ config: ReadonlyDeep<TConfig>, mutationOptions?: MutationOptions<Api, "patch", Path> ]): CreateMutationResult<zod.output<_zodios_core_lib_utils_types.FilterArrayByValue<Api, { method: "patch"; path: Path; }, []>[number]["response"]>, Error | ZodiosError | AxiosError<unknown, any>, IfEquals<zod.input<_zodios_core_lib_zodios_types.BodySchema<Api, "patch", Path>>, never, undefined, zod.input<_zodios_core_lib_zodios_types.BodySchema<Api, "patch", Path>>>, unknown>; createDelete<Path extends ZodiosPathsByMethod<Api, "delete">, TConfig extends ReadonlyDeep<ZodiosRequestOptionsByPath<Api, "delete", Path>>>(path: Path, ...rest: RequiredKeys<TConfig> extends never ? [ config?: ReadonlyDeep<TConfig>, mutationOptions?: MutationOptions<Api, "post", Path> ] : [ config: ReadonlyDeep<TConfig>, mutationOptions?: MutationOptions<Api, "post", Path> ]): CreateMutationResult<zod.output<_zodios_core_lib_utils_types.FilterArrayByValue<Api, { method: "delete"; path: Path; }, []>[number]["response"]>, Error | ZodiosError | AxiosError<unknown, any>, IfEquals<zod.input<_zodios_core_lib_zodios_types.BodySchema<Api, "delete", Path>>, never, undefined, zod.input<_zodios_core_lib_zodios_types.BodySchema<Api, "delete", Path>>>, unknown>; } type ZodiosImmutableAliasHook<Body, Config, ImmutableOptions, Response> = RequiredKeys<Config> extends never ? (body: ReadonlyDeep<UndefinedIfNever<Body>>, configOptions?: ReadonlyDeep<Config>, queryOptions?: ImmutableOptions) => CreateQueryResult<Response, Errors> & { invalidate: () => Promise<void>; key: QueryKey; } : (body: ReadonlyDeep<UndefinedIfNever<Body>>, configOptions: ReadonlyDeep<Config>, queryOptions?: ImmutableOptions) => CreateQueryResult<Response, Errors> & { invalidate: () => Promise<void>; key: QueryKey; }; type ZodiosMutationAliasHook<Body, Config, MutationOptions, Response> = RequiredKeys<Config> extends never ? (configOptions?: ReadonlyDeep<Config>, mutationOptions?: MutationOptions) => CreateMutationResult<Response, Errors, UndefinedIfNever<Body>, unknown> : (configOptions: ReadonlyDeep<Config>, mutationOptions?: MutationOptions) => CreateMutationResult<Response, Errors, UndefinedIfNever<Body>, unknown>; type ZodiosAliasHook<Config, QueryOptions, Response> = RequiredKeys<Config> extends never ? (configOptions?: ReadonlyDeep<Config>, queryOptions?: QueryOptions) => CreateQueryResult<Response, Errors> & { invalidate: () => Promise<void>; key: QueryKey; } : (configOptions: ReadonlyDeep<Config>, queryOptions?: QueryOptions) => CreateQueryResult<Response, Errors> & { invalidate: () => Promise<void>; key: QueryKey; }; type ZodiosHooksAliases<Api extends ZodiosEndpointDefinitions> = { [Alias in Aliases<Api> as `create${Capitalize<Alias>}`]: ZodiosEndpointDefinitionByAlias<Api, Alias>[number]["method"] extends infer AliasMethod ? AliasMethod extends MutationMethod ? { immutable: ZodiosEndpointDefinitionByAlias<Api, Alias>[number]["immutable"]; method: AliasMethod; } extends { immutable: true; method: "post"; } ? ZodiosImmutableAliasHook<ZodiosBodyByAlias<Api, Alias>, ZodiosRequestOptionsByAlias<Api, Alias>, Omit<QueryOptionsByAlias<Api, Alias>, "queryKey" | "queryFn">, ZodiosResponseByAlias<Api, Alias>> : ZodiosMutationAliasHook<ZodiosBodyByAlias<Api, Alias>, ZodiosRequestOptionsByAlias<Api, Alias>, MutationOptionsByAlias<Api, Alias>, ZodiosResponseByAlias<Api, Alias>> : ZodiosAliasHook<ZodiosRequestOptionsByAlias<Api, Alias>, Omit<QueryOptionsByAlias<Api, Alias>, "queryKey" | "queryFn">, ZodiosResponseByAlias<Api, Alias>> : never; }; type ZodiosHooksInstance<Api extends ZodiosEndpointDefinitions> = ZodiosHooksClass<Api> & ZodiosHooksAliases<Api>; type ZodiosHooksConstructor = { new <Api extends ZodiosEndpointDefinitions>(name: string, zodios: ZodiosInstance<Api>): ZodiosHooksInstance<Api>; }; declare const ZodiosHooks: ZodiosHooksConstructor; export { ZodiosHooks, ZodiosHooksClass, ZodiosHooksConstructor, ZodiosHooksInstance };