@orpc/vue-query
Version:
<div align="center"> <image align="center" src="https://orpc.unnoq.com/logo.webp" width=280 alt="oRPC logo" /> </div>
93 lines (85 loc) • 5.9 kB
TypeScript
import { ClientContext, Client, NestedClient } from '@orpc/client';
import { QueryKey, QueryObserverOptions, QueryFunctionContext, UseInfiniteQueryOptions, MutationObserverOptions, InfiniteData } from '@tanstack/vue-query';
import { PartialDeep, AnyFunction, SetOptional, MaybeOptionalOptions } from '@orpc/shared';
import { MaybeRef, MaybeRefOrGetter, ComputedRef, Ref } from 'vue';
type KeyType = 'query' | 'infinite' | 'mutation' | undefined;
interface BuildKeyOptions<TType extends KeyType, TInput> {
type?: TType;
input?: TType extends 'mutation' ? never : PartialDeep<TInput>;
}
declare function buildKey<TType extends KeyType, TInput>(path: string[], options?: BuildKeyOptions<TType, TInput>): QueryKey;
interface GeneralUtils<TInput> {
key<UType extends KeyType = undefined>(options?: BuildKeyOptions<UType, TInput>): QueryKey;
}
declare function createGeneralUtils<TInput>(path: string[]): GeneralUtils<TInput>;
/**
* Since `@tanstack/vue-query` is not exporting the type `MaybeRefDeep` we need to copy it from the source code.
* https://github.com/TanStack/query/blob/7ff544e12e79388e513b1cd886aeb946f80f0153/packages/vue-query/src/types.ts#L19C1-L27C2
*/
type MaybeRefDeep<T> = MaybeRef<T extends AnyFunction ? T : T extends object ? {
[Property in keyof T]: MaybeRefDeep<T[Property]>;
} : T>;
type QueryOptionsIn<TClientContext extends ClientContext, TInput, TOutput, TError, TSelectData> = (undefined extends TInput ? {
input?: MaybeRefDeep<TInput>;
} : {
input: MaybeRefDeep<TInput>;
}) & (Record<never, never> extends TClientContext ? {
context?: MaybeRefDeep<TClientContext>;
} : {
context: MaybeRefDeep<TClientContext>;
}) & {
[P in keyof Omit<QueryObserverOptions<TOutput, TError, TSelectData, TOutput>, 'queryKey' | 'enabled'>]: MaybeRefDeep<QueryObserverOptions<TOutput, TError, TSelectData, TOutput>[P]>;
} & {
enabled?: MaybeRefOrGetter<QueryObserverOptions<TOutput, TError, TSelectData, TOutput>['enabled']>;
queryKey?: MaybeRefDeep<QueryObserverOptions<TOutput, TError, TSelectData, TOutput>['queryKey']>;
shallow?: MaybeRef<boolean>;
};
interface QueryOptionsBase<TOutput, TError> {
queryKey: ComputedRef<QueryKey>;
queryFn(ctx: QueryFunctionContext): Promise<TOutput>;
retry?(failureCount: number, error: TError): boolean;
}
type InfiniteOptionsIn<TClientContext extends ClientContext, TInput, TOutput, TError, TSelectData, TPageParam> = {
input: (pageParam: TPageParam) => MaybeRefDeep<TInput>;
} & (Record<never, never> extends TClientContext ? {
context?: MaybeRefDeep<TClientContext>;
} : {
context: MaybeRefDeep<TClientContext>;
}) & SetOptional<UseInfiniteQueryOptions<TOutput, TError, TSelectData, TOutput, QueryKey, TPageParam>, 'queryKey'>;
interface InfiniteOptionsBase<TOutput, TError, TPageParam> {
queryKey: ComputedRef<QueryKey>;
queryFn(ctx: QueryFunctionContext<QueryKey, TPageParam>): Promise<TOutput>;
retry?(failureCount: number, error: TError): boolean;
}
type MutationOptionsIn<TClientContext extends ClientContext, TInput, TOutput, TError, TMutationContext> = (Record<never, never> extends TClientContext ? {
context?: TClientContext;
} : {
context: TClientContext;
}) & MutationOptions<TInput, TOutput, TError, TMutationContext>;
type MutationOptions<TInput, TOutput, TError, TMutationContext> = {
[P in keyof MutationObserverOptions<TOutput, TError, TInput, TMutationContext>]: MaybeRefDeep<MutationObserverOptions<TOutput, TError, TInput, TMutationContext>[P]>;
} & {
shallow?: MaybeRef<boolean>;
};
interface ProcedureUtils<TClientContext extends ClientContext, TInput, TOutput, TError> {
call: Client<TClientContext, TInput, TOutput, TError>;
queryOptions<U, USelectData = TOutput>(...rest: MaybeOptionalOptions<U & QueryOptionsIn<TClientContext, TInput, TOutput, TError, USelectData>>): NoInfer<U & Omit<QueryOptionsBase<TOutput, TError>, keyof U>>;
infiniteOptions<U, UPageParam, USelectData = InfiniteData<TOutput, UPageParam>>(options: U & InfiniteOptionsIn<TClientContext, TInput, TOutput, TError, USelectData, UPageParam>): NoInfer<U & Omit<InfiniteOptionsBase<TOutput, TError, UPageParam>, keyof U>>;
mutationOptions<UMutationContext>(...rest: MaybeOptionalOptions<MutationOptionsIn<TClientContext, TInput, TOutput, TError, UMutationContext>>): MutationOptions<TInput, TOutput, TError, UMutationContext>;
}
interface CreateProcedureUtilsOptions {
path: string[];
}
declare function createProcedureUtils<TClientContext extends ClientContext, TInput, TOutput, TError>(client: Client<TClientContext, TInput, TOutput, TError>, options: CreateProcedureUtilsOptions): ProcedureUtils<TClientContext, TInput, TOutput, TError>;
type RouterUtils<T extends NestedClient<any>> = T extends Client<infer UClientContext, infer UInput, infer UOutput, infer UError> ? ProcedureUtils<UClientContext, UInput, UOutput, UError> & GeneralUtils<UInput> : {
[K in keyof T]: T[K] extends NestedClient<any> ? RouterUtils<T[K]> : never;
} & GeneralUtils<unknown>;
interface CreateRouterUtilsOptions {
path?: string[];
}
declare function createRouterUtils<T extends NestedClient<any>>(client: T, options?: CreateRouterUtilsOptions): RouterUtils<T>;
type UnrefDeep<T> = T extends Ref<infer U> ? UnrefDeep<U> : T extends AnyFunction ? T : T extends object ? {
[K in keyof T]: UnrefDeep<T[K]>;
} : T;
declare function unrefDeep<T>(value: T): UnrefDeep<T>;
export { type BuildKeyOptions, type CreateProcedureUtilsOptions, type CreateRouterUtilsOptions, type GeneralUtils, type InfiniteOptionsBase, type InfiniteOptionsIn, type KeyType, type MaybeRefDeep, type MutationOptions, type MutationOptionsIn, type ProcedureUtils, type QueryOptionsBase, type QueryOptionsIn, type RouterUtils, type UnrefDeep, buildKey, createGeneralUtils, createRouterUtils as createORPCVueQueryUtils, createProcedureUtils, createRouterUtils, unrefDeep };