@orpc/vue-query 0.0.0-next.f56d2b3 → 0.0.0-next.f72e6b9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,119 @@
1
+ <div align="center">
2
+ <image align="center" src="https://orpc.unnoq.com/logo.webp" width=280 alt="oRPC logo" />
3
+ </div>
4
+
5
+ <h1></h1>
6
+
7
+ <div align="center">
8
+ <a href="https://codecov.io/gh/unnoq/orpc">
9
+ <img alt="codecov" src="https://codecov.io/gh/unnoq/orpc/branch/main/graph/badge.svg">
10
+ </a>
11
+ <a href="https://www.npmjs.com/package/@orpc/vue-query">
12
+ <img alt="weekly downloads" src="https://img.shields.io/npm/dw/%40orpc%2Fvue-query?logo=npm" />
13
+ </a>
14
+ <a href="https://github.com/unnoq/orpc/blob/main/LICENSE">
15
+ <img alt="MIT License" src="https://img.shields.io/github/license/unnoq/orpc?logo=open-source-initiative" />
16
+ </a>
17
+ <a href="https://discord.gg/TXEbwRBvQn">
18
+ <img alt="Discord" src="https://img.shields.io/discord/1308966753044398161?color=7389D8&label&logo=discord&logoColor=ffffff" />
19
+ </a>
20
+ </div>
21
+
22
+ <h3 align="center">Typesafe APIs Made Simple 🪄</h3>
23
+
24
+ **oRPC is a powerful combination of RPC and OpenAPI**, makes it easy to build APIs that are end-to-end type-safe and adhere to OpenAPI standards
25
+
26
+ ---
27
+
28
+ ## Highlights
29
+
30
+ - **🔗 End-to-End Type Safety**: Ensure type-safe inputs, outputs, and errors from client to server.
31
+ - **📘 First-Class OpenAPI**: Built-in support that fully adheres to the OpenAPI standard.
32
+ - **📝 Contract-First Development**: Optionally define your API contract before implementation.
33
+ - **⚙️ Framework Integrations**: Seamlessly integrate with TanStack Query (React, Vue, Solid, Svelte), Pinia Colada, and more.
34
+ - **🚀 Server Actions**: Fully compatible with React Server Actions on Next.js, TanStack Start, and other platforms.
35
+ - **🔠 Standard Schema Support**: Works out of the box with Zod, Valibot, ArkType, and other schema validators.
36
+ - **🗃️ Native Types**: Supports native types like Date, File, Blob, BigInt, URL, and more.
37
+ - **⏱️ Lazy Router**: Enhance cold start times with our lazy routing feature.
38
+ - **📡 SSE & Streaming**: Enjoy full type-safe support for SSE and streaming.
39
+ - **🌍 Multi-Runtime Support**: Fast and lightweight on Cloudflare, Deno, Bun, Node.js, and beyond.
40
+ - **🔌 Extendability**: Easily extend functionality with plugins, middleware, and interceptors.
41
+ - **🛡️ Reliability**: Well-tested, TypeScript-based, production-ready, and MIT licensed.
42
+
43
+ ## Documentation
44
+
45
+ You can find the full documentation [here](https://orpc.unnoq.com).
46
+
47
+ ## Packages
48
+
49
+ - [@orpc/contract](https://www.npmjs.com/package/@orpc/contract): Build your API contract.
50
+ - [@orpc/server](https://www.npmjs.com/package/@orpc/server): Build your API or implement API contract.
51
+ - [@orpc/client](https://www.npmjs.com/package/@orpc/client): Consume your API on the client with type-safety.
52
+ - [@orpc/react](https://www.npmjs.com/package/@orpc/react): Utilities for integrating oRPC with React and React Server Actions.
53
+ - [@orpc/react-query](https://www.npmjs.com/package/@orpc/react-query): Integration with [React Query](https://tanstack.com/query/latest/docs/framework/react/overview).
54
+ - [@orpc/vue-query](https://www.npmjs.com/package/@orpc/vue-query): Integration with [Vue Query](https://tanstack.com/query/latest/docs/framework/vue/overview).
55
+ - [@orpc/solid-query](https://www.npmjs.com/package/@orpc/solid-query): Integration with [Solid Query](https://tanstack.com/query/latest/docs/framework/solid/overview).
56
+ - [@orpc/svelte-query](https://www.npmjs.com/package/@orpc/svelte-query): Integration with [Svelte Query](https://tanstack.com/query/latest/docs/framework/svelte/overview).
57
+ - [@orpc/vue-colada](https://www.npmjs.com/package/@orpc/vue-colada): Integration with [Pinia Colada](https://pinia-colada.esm.dev/).
58
+ - [@orpc/openapi](https://www.npmjs.com/package/@orpc/openapi): Generate OpenAPI specs and handle OpenAPI requests.
59
+ - [@orpc/zod](https://www.npmjs.com/package/@orpc/zod): More schemas that [Zod](https://zod.dev/) doesn't support yet.
60
+ - [@orpc/valibot](https://www.npmjs.com/package/@orpc/valibot): OpenAPI spec generation from [Valibot](https://valibot.dev/).
61
+ - [@orpc/arktype](https://www.npmjs.com/package/@orpc/arktype): OpenAPI spec generation from [ArkType](https://arktype.io/).
62
+
63
+ ## `@orpc/vue-query`
64
+
65
+ Integration with [Vue Query](https://tanstack.com/query/latest/docs/framework/vue/overview). Read the [documentation](https://orpc.unnoq.com/docs/tanstack-query/vue) for more information.
66
+
67
+ ```ts
68
+ export function Setup() {
69
+ const query = useQuery(orpc.planet.find.queryOptions({
70
+ input: { id: 123 }, // Specify input if needed
71
+ context: { cache: true }, // Provide client context if needed
72
+ // additional options...
73
+ }))
74
+
75
+ const query = useInfiniteQuery(orpc.planet.list.infiniteOptions({
76
+ input: (pageParam: number | undefined) => ({ limit: 10, offset: pageParam }),
77
+ context: { cache: true }, // Provide client context if needed
78
+ initialPageParam: undefined,
79
+ getNextPageParam: lastPage => lastPage.nextPageParam,
80
+ // additional options...
81
+ }))
82
+
83
+ const mutation = useMutation(orpc.planet.create.mutationOptions({
84
+ context: { cache: true }, // Provide client context if needed
85
+ // additional options...
86
+ }))
87
+
88
+ mutation.mutate({ name: 'Earth' })
89
+
90
+ const queryClient = useQueryClient()
91
+
92
+ // Invalidate all planet queries
93
+ queryClient.invalidateQueries({
94
+ queryKey: orpc.planet.key(),
95
+ })
96
+
97
+ // Invalidate only regular (non-infinite) planet queries
98
+ queryClient.invalidateQueries({
99
+ queryKey: orpc.planet.key({ type: 'query' })
100
+ })
101
+
102
+ // Invalidate the planet find query with id 123
103
+ queryClient.invalidateQueries({
104
+ queryKey: orpc.planet.find.key({ input: { id: 123 } })
105
+ })
106
+ }
107
+ ```
108
+
109
+ ## Sponsors
110
+
111
+ <p align="center">
112
+ <a href="https://cdn.jsdelivr.net/gh/unnoq/unnoq/sponsors.svg">
113
+ <img src='https://cdn.jsdelivr.net/gh/unnoq/unnoq/sponsors.svg'/>
114
+ </a>
115
+ </p>
116
+
117
+ ## License
118
+
119
+ Distributed under the MIT License. See [LICENSE](https://github.com/unnoq/orpc/blob/main/LICENSE) for more information.
@@ -0,0 +1,130 @@
1
+ import { ClientContext, Client, NestedClient } from '@orpc/client';
2
+ import { QueryKey, QueryObserverOptions, QueryFunctionContext, InfiniteQueryObserverOptions, MutationObserverOptions, InfiniteData } from '@tanstack/vue-query';
3
+ import { PartialDeep, AnyFunction, MaybeOptionalOptions } from '@orpc/shared';
4
+ import { MaybeRef, MaybeRefOrGetter, ComputedRef, Ref } from 'vue';
5
+
6
+ type KeyType = 'query' | 'infinite' | 'mutation' | undefined;
7
+ interface BuildKeyOptions<TType extends KeyType, TInput> {
8
+ type?: TType;
9
+ input?: TType extends 'mutation' ? never : PartialDeep<TInput>;
10
+ }
11
+ declare function buildKey<TType extends KeyType, TInput>(path: string[], options?: BuildKeyOptions<TType, TInput>): QueryKey;
12
+
13
+ interface GeneralUtils<TInput> {
14
+ /**
15
+ * Generate a query/mutation key for checking status, invalidate, set, get, etc.
16
+ *
17
+ * @see {@link https://orpc.unnoq.com/docs/tanstack-query/basic#query-mutation-key Tanstack Query/Mutation Key Docs}
18
+ */
19
+ key<UType extends KeyType = undefined>(options?: BuildKeyOptions<UType, TInput>): QueryKey;
20
+ }
21
+ declare function createGeneralUtils<TInput>(path: string[]): GeneralUtils<TInput>;
22
+
23
+ /**
24
+ * Since `@tanstack/vue-query` is not exporting the type `MaybeRefDeep` we need to copy it from the source code.
25
+ * https://github.com/TanStack/query/blob/7ff544e12e79388e513b1cd886aeb946f80f0153/packages/vue-query/src/types.ts#L19C1-L27C2
26
+ */
27
+ type MaybeRefDeep<T> = MaybeRef<T extends AnyFunction ? T : T extends object ? {
28
+ [Property in keyof T]: MaybeRefDeep<T[Property]>;
29
+ } : T>;
30
+ type QueryOptionsIn<TClientContext extends ClientContext, TInput, TOutput, TError, TSelectData> = (undefined extends TInput ? {
31
+ input?: MaybeRefDeep<TInput>;
32
+ } : {
33
+ input: MaybeRefDeep<TInput>;
34
+ }) & (Record<never, never> extends TClientContext ? {
35
+ context?: MaybeRefDeep<TClientContext>;
36
+ } : {
37
+ context: MaybeRefDeep<TClientContext>;
38
+ }) & {
39
+ [P in keyof Omit<QueryObserverOptions<TOutput, TError, TSelectData, TOutput>, 'queryKey' | 'enabled'>]: MaybeRefDeep<QueryObserverOptions<TOutput, TError, TSelectData, TOutput>[P]>;
40
+ } & {
41
+ enabled?: MaybeRefOrGetter<QueryObserverOptions<TOutput, TError, TSelectData, TOutput>['enabled']>;
42
+ queryKey?: MaybeRefDeep<QueryObserverOptions<TOutput, TError, TSelectData, TOutput>['queryKey']>;
43
+ shallow?: boolean;
44
+ };
45
+ interface QueryOptionsBase<TOutput, TError> {
46
+ queryKey: ComputedRef<QueryKey>;
47
+ queryFn(ctx: QueryFunctionContext): Promise<TOutput>;
48
+ retry?(failureCount: number, error: TError): boolean;
49
+ }
50
+ type InfiniteOptionsIn<TClientContext extends ClientContext, TInput, TOutput, TError, TSelectData, TPageParam> = {
51
+ input: (pageParam: TPageParam) => MaybeRefDeep<TInput>;
52
+ } & (Record<never, never> extends TClientContext ? {
53
+ context?: MaybeRefDeep<TClientContext>;
54
+ } : {
55
+ context: MaybeRefDeep<TClientContext>;
56
+ }) & {
57
+ [Property in keyof Omit<InfiniteQueryObserverOptions<TOutput, TError, TSelectData, TOutput, QueryKey, TPageParam>, 'queryKey' | 'enabled'>]: MaybeRefDeep<InfiniteQueryObserverOptions<TOutput, TError, TSelectData, TOutput, QueryKey, TPageParam>[Property]>;
58
+ } & {
59
+ enabled?: MaybeRefOrGetter<InfiniteQueryObserverOptions<TOutput, TError, TSelectData, TOutput, QueryKey, TPageParam>['enabled']>;
60
+ queryKey?: MaybeRefDeep<InfiniteQueryObserverOptions<TOutput, TError, TSelectData, TOutput, QueryKey, TPageParam>['queryKey']>;
61
+ shallow?: boolean;
62
+ };
63
+ interface InfiniteOptionsBase<TOutput, TError, TPageParam> {
64
+ queryKey: ComputedRef<QueryKey>;
65
+ queryFn(ctx: QueryFunctionContext<QueryKey, TPageParam>): Promise<TOutput>;
66
+ retry?(failureCount: number, error: TError): boolean;
67
+ }
68
+ type MutationOptionsIn<TClientContext extends ClientContext, TInput, TOutput, TError, TMutationContext> = (Record<never, never> extends TClientContext ? {
69
+ context?: TClientContext;
70
+ } : {
71
+ context: TClientContext;
72
+ }) & MutationOptions<TInput, TOutput, TError, TMutationContext>;
73
+ type MutationOptions<TInput, TOutput, TError, TMutationContext> = {
74
+ [P in keyof MutationObserverOptions<TOutput, TError, TInput, TMutationContext>]: MaybeRefDeep<MutationObserverOptions<TOutput, TError, TInput, TMutationContext>[P]>;
75
+ } & {
76
+ shallow?: MaybeRef<boolean>;
77
+ };
78
+
79
+ interface ProcedureUtils<TClientContext extends ClientContext, TInput, TOutput, TError> {
80
+ /**
81
+ * Calling corresponding procedure client
82
+ *
83
+ * @see {@link https://orpc.unnoq.com/docs/tanstack-query/basic#calling-procedure-clients Tanstack Calling Procedure Client Docs}
84
+ */
85
+ call: Client<TClientContext, TInput, TOutput, TError>;
86
+ /**
87
+ * Generate options used for useQuery/prefetchQuery/...
88
+ *
89
+ * @see {@link https://orpc.unnoq.com/docs/tanstack-query/basic#query-options-utility Tanstack Query Options Utility Docs}
90
+ */
91
+ queryOptions<U, USelectData = TOutput>(...rest: MaybeOptionalOptions<U & QueryOptionsIn<TClientContext, TInput, TOutput, TError, USelectData>>): NoInfer<U & Omit<QueryOptionsBase<TOutput, TError>, keyof U>>;
92
+ /**
93
+ * Generate options used for useInfiniteQuery/prefetchInfiniteQuery/...
94
+ *
95
+ * @see {@link https://orpc.unnoq.com/docs/tanstack-query/basic#infinite-query-options-utility Tanstack Infinite Query Options Utility Docs}
96
+ */
97
+ 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>>;
98
+ /**
99
+ * Generate options used for useMutation/...
100
+ *
101
+ * @see {@link https://orpc.unnoq.com/docs/tanstack-query/basic#mutation-options Tanstack Mutation Options Docs}
102
+ */
103
+ mutationOptions<UMutationContext>(...rest: MaybeOptionalOptions<MutationOptionsIn<TClientContext, TInput, TOutput, TError, UMutationContext>>): MutationOptions<TInput, TOutput, TError, UMutationContext>;
104
+ }
105
+ interface CreateProcedureUtilsOptions {
106
+ path: string[];
107
+ }
108
+ declare function createProcedureUtils<TClientContext extends ClientContext, TInput, TOutput, TError>(client: Client<TClientContext, TInput, TOutput, TError>, options: CreateProcedureUtilsOptions): ProcedureUtils<TClientContext, TInput, TOutput, TError>;
109
+
110
+ type RouterUtils<T extends NestedClient<any>> = T extends Client<infer UClientContext, infer UInput, infer UOutput, infer UError> ? ProcedureUtils<UClientContext, UInput, UOutput, UError> & GeneralUtils<UInput> : {
111
+ [K in keyof T]: T[K] extends NestedClient<any> ? RouterUtils<T[K]> : never;
112
+ } & GeneralUtils<unknown>;
113
+ interface CreateRouterUtilsOptions {
114
+ path?: string[];
115
+ }
116
+ /**
117
+ * Create a router utils from a client.
118
+ *
119
+ * @info Both client-side and server-side clients are supported.
120
+ * @see {@link https://orpc.unnoq.com/docs/tanstack-query/vue Tanstack Query Vue Docs}
121
+ */
122
+ declare function createRouterUtils<T extends NestedClient<any>>(client: T, options?: CreateRouterUtilsOptions): RouterUtils<T>;
123
+
124
+ type UnrefDeep<T> = T extends Ref<infer U> ? UnrefDeep<U> : T extends AnyFunction ? T : T extends object ? {
125
+ [K in keyof T]: UnrefDeep<T[K]>;
126
+ } : T;
127
+ declare function unrefDeep<T>(value: T): UnrefDeep<T>;
128
+
129
+ export { buildKey, createGeneralUtils, createRouterUtils as createORPCVueQueryUtils, createProcedureUtils, createRouterUtils, unrefDeep };
130
+ export type { BuildKeyOptions, CreateProcedureUtilsOptions, CreateRouterUtilsOptions, GeneralUtils, InfiniteOptionsBase, InfiniteOptionsIn, KeyType, MaybeRefDeep, MutationOptions, MutationOptionsIn, ProcedureUtils, QueryOptionsBase, QueryOptionsIn, RouterUtils, UnrefDeep };
@@ -0,0 +1,130 @@
1
+ import { ClientContext, Client, NestedClient } from '@orpc/client';
2
+ import { QueryKey, QueryObserverOptions, QueryFunctionContext, InfiniteQueryObserverOptions, MutationObserverOptions, InfiniteData } from '@tanstack/vue-query';
3
+ import { PartialDeep, AnyFunction, MaybeOptionalOptions } from '@orpc/shared';
4
+ import { MaybeRef, MaybeRefOrGetter, ComputedRef, Ref } from 'vue';
5
+
6
+ type KeyType = 'query' | 'infinite' | 'mutation' | undefined;
7
+ interface BuildKeyOptions<TType extends KeyType, TInput> {
8
+ type?: TType;
9
+ input?: TType extends 'mutation' ? never : PartialDeep<TInput>;
10
+ }
11
+ declare function buildKey<TType extends KeyType, TInput>(path: string[], options?: BuildKeyOptions<TType, TInput>): QueryKey;
12
+
13
+ interface GeneralUtils<TInput> {
14
+ /**
15
+ * Generate a query/mutation key for checking status, invalidate, set, get, etc.
16
+ *
17
+ * @see {@link https://orpc.unnoq.com/docs/tanstack-query/basic#query-mutation-key Tanstack Query/Mutation Key Docs}
18
+ */
19
+ key<UType extends KeyType = undefined>(options?: BuildKeyOptions<UType, TInput>): QueryKey;
20
+ }
21
+ declare function createGeneralUtils<TInput>(path: string[]): GeneralUtils<TInput>;
22
+
23
+ /**
24
+ * Since `@tanstack/vue-query` is not exporting the type `MaybeRefDeep` we need to copy it from the source code.
25
+ * https://github.com/TanStack/query/blob/7ff544e12e79388e513b1cd886aeb946f80f0153/packages/vue-query/src/types.ts#L19C1-L27C2
26
+ */
27
+ type MaybeRefDeep<T> = MaybeRef<T extends AnyFunction ? T : T extends object ? {
28
+ [Property in keyof T]: MaybeRefDeep<T[Property]>;
29
+ } : T>;
30
+ type QueryOptionsIn<TClientContext extends ClientContext, TInput, TOutput, TError, TSelectData> = (undefined extends TInput ? {
31
+ input?: MaybeRefDeep<TInput>;
32
+ } : {
33
+ input: MaybeRefDeep<TInput>;
34
+ }) & (Record<never, never> extends TClientContext ? {
35
+ context?: MaybeRefDeep<TClientContext>;
36
+ } : {
37
+ context: MaybeRefDeep<TClientContext>;
38
+ }) & {
39
+ [P in keyof Omit<QueryObserverOptions<TOutput, TError, TSelectData, TOutput>, 'queryKey' | 'enabled'>]: MaybeRefDeep<QueryObserverOptions<TOutput, TError, TSelectData, TOutput>[P]>;
40
+ } & {
41
+ enabled?: MaybeRefOrGetter<QueryObserverOptions<TOutput, TError, TSelectData, TOutput>['enabled']>;
42
+ queryKey?: MaybeRefDeep<QueryObserverOptions<TOutput, TError, TSelectData, TOutput>['queryKey']>;
43
+ shallow?: boolean;
44
+ };
45
+ interface QueryOptionsBase<TOutput, TError> {
46
+ queryKey: ComputedRef<QueryKey>;
47
+ queryFn(ctx: QueryFunctionContext): Promise<TOutput>;
48
+ retry?(failureCount: number, error: TError): boolean;
49
+ }
50
+ type InfiniteOptionsIn<TClientContext extends ClientContext, TInput, TOutput, TError, TSelectData, TPageParam> = {
51
+ input: (pageParam: TPageParam) => MaybeRefDeep<TInput>;
52
+ } & (Record<never, never> extends TClientContext ? {
53
+ context?: MaybeRefDeep<TClientContext>;
54
+ } : {
55
+ context: MaybeRefDeep<TClientContext>;
56
+ }) & {
57
+ [Property in keyof Omit<InfiniteQueryObserverOptions<TOutput, TError, TSelectData, TOutput, QueryKey, TPageParam>, 'queryKey' | 'enabled'>]: MaybeRefDeep<InfiniteQueryObserverOptions<TOutput, TError, TSelectData, TOutput, QueryKey, TPageParam>[Property]>;
58
+ } & {
59
+ enabled?: MaybeRefOrGetter<InfiniteQueryObserverOptions<TOutput, TError, TSelectData, TOutput, QueryKey, TPageParam>['enabled']>;
60
+ queryKey?: MaybeRefDeep<InfiniteQueryObserverOptions<TOutput, TError, TSelectData, TOutput, QueryKey, TPageParam>['queryKey']>;
61
+ shallow?: boolean;
62
+ };
63
+ interface InfiniteOptionsBase<TOutput, TError, TPageParam> {
64
+ queryKey: ComputedRef<QueryKey>;
65
+ queryFn(ctx: QueryFunctionContext<QueryKey, TPageParam>): Promise<TOutput>;
66
+ retry?(failureCount: number, error: TError): boolean;
67
+ }
68
+ type MutationOptionsIn<TClientContext extends ClientContext, TInput, TOutput, TError, TMutationContext> = (Record<never, never> extends TClientContext ? {
69
+ context?: TClientContext;
70
+ } : {
71
+ context: TClientContext;
72
+ }) & MutationOptions<TInput, TOutput, TError, TMutationContext>;
73
+ type MutationOptions<TInput, TOutput, TError, TMutationContext> = {
74
+ [P in keyof MutationObserverOptions<TOutput, TError, TInput, TMutationContext>]: MaybeRefDeep<MutationObserverOptions<TOutput, TError, TInput, TMutationContext>[P]>;
75
+ } & {
76
+ shallow?: MaybeRef<boolean>;
77
+ };
78
+
79
+ interface ProcedureUtils<TClientContext extends ClientContext, TInput, TOutput, TError> {
80
+ /**
81
+ * Calling corresponding procedure client
82
+ *
83
+ * @see {@link https://orpc.unnoq.com/docs/tanstack-query/basic#calling-procedure-clients Tanstack Calling Procedure Client Docs}
84
+ */
85
+ call: Client<TClientContext, TInput, TOutput, TError>;
86
+ /**
87
+ * Generate options used for useQuery/prefetchQuery/...
88
+ *
89
+ * @see {@link https://orpc.unnoq.com/docs/tanstack-query/basic#query-options-utility Tanstack Query Options Utility Docs}
90
+ */
91
+ queryOptions<U, USelectData = TOutput>(...rest: MaybeOptionalOptions<U & QueryOptionsIn<TClientContext, TInput, TOutput, TError, USelectData>>): NoInfer<U & Omit<QueryOptionsBase<TOutput, TError>, keyof U>>;
92
+ /**
93
+ * Generate options used for useInfiniteQuery/prefetchInfiniteQuery/...
94
+ *
95
+ * @see {@link https://orpc.unnoq.com/docs/tanstack-query/basic#infinite-query-options-utility Tanstack Infinite Query Options Utility Docs}
96
+ */
97
+ 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>>;
98
+ /**
99
+ * Generate options used for useMutation/...
100
+ *
101
+ * @see {@link https://orpc.unnoq.com/docs/tanstack-query/basic#mutation-options Tanstack Mutation Options Docs}
102
+ */
103
+ mutationOptions<UMutationContext>(...rest: MaybeOptionalOptions<MutationOptionsIn<TClientContext, TInput, TOutput, TError, UMutationContext>>): MutationOptions<TInput, TOutput, TError, UMutationContext>;
104
+ }
105
+ interface CreateProcedureUtilsOptions {
106
+ path: string[];
107
+ }
108
+ declare function createProcedureUtils<TClientContext extends ClientContext, TInput, TOutput, TError>(client: Client<TClientContext, TInput, TOutput, TError>, options: CreateProcedureUtilsOptions): ProcedureUtils<TClientContext, TInput, TOutput, TError>;
109
+
110
+ type RouterUtils<T extends NestedClient<any>> = T extends Client<infer UClientContext, infer UInput, infer UOutput, infer UError> ? ProcedureUtils<UClientContext, UInput, UOutput, UError> & GeneralUtils<UInput> : {
111
+ [K in keyof T]: T[K] extends NestedClient<any> ? RouterUtils<T[K]> : never;
112
+ } & GeneralUtils<unknown>;
113
+ interface CreateRouterUtilsOptions {
114
+ path?: string[];
115
+ }
116
+ /**
117
+ * Create a router utils from a client.
118
+ *
119
+ * @info Both client-side and server-side clients are supported.
120
+ * @see {@link https://orpc.unnoq.com/docs/tanstack-query/vue Tanstack Query Vue Docs}
121
+ */
122
+ declare function createRouterUtils<T extends NestedClient<any>>(client: T, options?: CreateRouterUtilsOptions): RouterUtils<T>;
123
+
124
+ type UnrefDeep<T> = T extends Ref<infer U> ? UnrefDeep<U> : T extends AnyFunction ? T : T extends object ? {
125
+ [K in keyof T]: UnrefDeep<T[K]>;
126
+ } : T;
127
+ declare function unrefDeep<T>(value: T): UnrefDeep<T>;
128
+
129
+ export { buildKey, createGeneralUtils, createRouterUtils as createORPCVueQueryUtils, createProcedureUtils, createRouterUtils, unrefDeep };
130
+ export type { BuildKeyOptions, CreateProcedureUtilsOptions, CreateRouterUtilsOptions, GeneralUtils, InfiniteOptionsBase, InfiniteOptionsIn, KeyType, MaybeRefDeep, MutationOptions, MutationOptionsIn, ProcedureUtils, QueryOptionsBase, QueryOptionsIn, RouterUtils, UnrefDeep };
package/dist/index.mjs ADDED
@@ -0,0 +1,95 @@
1
+ import { isRef, computed } from 'vue';
2
+ import { isObject } from '@orpc/shared';
3
+
4
+ function buildKey(path, options) {
5
+ const withInput = options?.input !== void 0 ? { input: options?.input } : {};
6
+ const withType = options?.type !== void 0 ? { type: options?.type } : {};
7
+ return [path, {
8
+ ...withInput,
9
+ ...withType
10
+ }];
11
+ }
12
+
13
+ function createGeneralUtils(path) {
14
+ return {
15
+ key(options) {
16
+ return buildKey(path, options);
17
+ }
18
+ };
19
+ }
20
+
21
+ function unrefDeep(value) {
22
+ if (isRef(value)) {
23
+ return unrefDeep(value.value);
24
+ }
25
+ if (Array.isArray(value)) {
26
+ return value.map(unrefDeep);
27
+ }
28
+ if (isObject(value)) {
29
+ return Object.keys(value).reduce((acc, key) => {
30
+ acc[key] = unrefDeep(value[key]);
31
+ return acc;
32
+ }, {});
33
+ }
34
+ return value;
35
+ }
36
+
37
+ function createProcedureUtils(client, options) {
38
+ return {
39
+ call: client,
40
+ queryOptions(...[optionsIn = {}]) {
41
+ return {
42
+ queryKey: computed(() => buildKey(options.path, { type: "query", input: unrefDeep(optionsIn.input) })),
43
+ queryFn: ({ signal }) => client(unrefDeep(optionsIn.input), { signal, context: unrefDeep(optionsIn.context) }),
44
+ ...optionsIn
45
+ };
46
+ },
47
+ infiniteOptions(optionsIn) {
48
+ return {
49
+ queryKey: computed(() => {
50
+ return buildKey(options.path, { type: "infinite", input: unrefDeep(optionsIn.input(unrefDeep(optionsIn.initialPageParam))) });
51
+ }),
52
+ queryFn: ({ pageParam, signal }) => {
53
+ return client(unrefDeep(optionsIn.input(pageParam)), { signal, context: unrefDeep(optionsIn.context) });
54
+ },
55
+ ...optionsIn
56
+ };
57
+ },
58
+ mutationOptions(...[optionsIn = {}]) {
59
+ return {
60
+ mutationKey: buildKey(options.path, { type: "mutation" }),
61
+ mutationFn: (input) => client(input, { context: unrefDeep(optionsIn.context) }),
62
+ ...optionsIn
63
+ };
64
+ }
65
+ };
66
+ }
67
+
68
+ function createRouterUtils(client, options = {}) {
69
+ const path = options.path ?? [];
70
+ const generalUtils = createGeneralUtils(path);
71
+ const procedureUtils = createProcedureUtils(client, { path });
72
+ const recursive = new Proxy({
73
+ ...generalUtils,
74
+ ...procedureUtils
75
+ }, {
76
+ get(target, prop) {
77
+ const value = Reflect.get(target, prop);
78
+ if (typeof prop !== "string") {
79
+ return value;
80
+ }
81
+ const nextUtils = createRouterUtils(client[prop], { ...options, path: [...path, prop] });
82
+ if (typeof value !== "function") {
83
+ return nextUtils;
84
+ }
85
+ return new Proxy(value, {
86
+ get(_, prop2) {
87
+ return Reflect.get(nextUtils, prop2);
88
+ }
89
+ });
90
+ }
91
+ });
92
+ return recursive;
93
+ }
94
+
95
+ export { buildKey, createGeneralUtils, createRouterUtils as createORPCVueQueryUtils, createProcedureUtils, createRouterUtils, unrefDeep };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@orpc/vue-query",
3
3
  "type": "module",
4
- "version": "0.0.0-next.f56d2b3",
4
+ "version": "0.0.0-next.f72e6b9",
5
5
  "license": "MIT",
6
6
  "homepage": "https://orpc.unnoq.com",
7
7
  "repository": {
@@ -18,27 +18,25 @@
18
18
  ],
19
19
  "exports": {
20
20
  ".": {
21
- "types": "./dist/src/index.d.ts",
22
- "import": "./dist/index.js",
23
- "default": "./dist/index.js"
24
- },
25
- "./🔒/*": {
26
- "types": "./dist/src/*.d.ts"
21
+ "types": "./dist/index.d.mts",
22
+ "import": "./dist/index.mjs",
23
+ "default": "./dist/index.mjs"
27
24
  }
28
25
  },
29
26
  "files": [
30
- "!**/*.map",
31
- "!**/*.tsbuildinfo",
32
27
  "dist"
33
28
  ],
34
29
  "peerDependencies": {
35
- "@tanstack/vue-query": ">=5.50.0",
30
+ "@tanstack/vue-query": ">=5.55.0",
36
31
  "vue": ">=3.3.0",
37
- "@orpc/client": "0.0.0-next.f56d2b3",
38
- "@orpc/contract": "0.0.0-next.f56d2b3"
32
+ "@orpc/client": "0.0.0-next.f72e6b9"
33
+ },
34
+ "dependencies": {
35
+ "@tanstack/vue-query": "^5.72.2",
36
+ "@orpc/shared": "0.0.0-next.f72e6b9"
39
37
  },
40
38
  "scripts": {
41
- "build": "tsup --clean --sourcemap --entry.index=src/index.ts --format=esm --onSuccess='tsc -b --noCheck'",
39
+ "build": "unbuild",
42
40
  "build:watch": "pnpm run build --watch",
43
41
  "type:check": "tsc -b"
44
42
  }
package/dist/index.js DELETED
@@ -1,111 +0,0 @@
1
- // src/key.ts
2
- function buildKey(path, options) {
3
- const withInput = options?.input !== void 0 ? { input: options?.input } : {};
4
- const withType = options?.type !== void 0 ? { type: options?.type } : {};
5
- return [
6
- "__ORPC__",
7
- path,
8
- {
9
- ...withInput,
10
- ...withType
11
- }
12
- ];
13
- }
14
-
15
- // src/utils.ts
16
- import { isRef } from "vue";
17
- function deepUnref(value) {
18
- if (isRef(value)) {
19
- return deepUnref(value.value);
20
- }
21
- if (Array.isArray(value)) {
22
- return value.map(deepUnref);
23
- }
24
- if (value && typeof value === "object") {
25
- return Object.keys(value).reduce((acc, key) => {
26
- acc[key] = deepUnref(value[key]);
27
- return acc;
28
- }, {});
29
- }
30
- return value;
31
- }
32
-
33
- // src/utils-general.ts
34
- function createGeneralUtils(path) {
35
- return {
36
- key(options) {
37
- return buildKey(path, deepUnref(options));
38
- }
39
- };
40
- }
41
-
42
- // src/utils-procedure.ts
43
- import { computed } from "vue";
44
- function createProcedureUtils(client, path) {
45
- return {
46
- queryOptions(...[options]) {
47
- const input = options?.input;
48
- return {
49
- queryKey: computed(() => buildKey(path, { type: "query", input: deepUnref(input) })),
50
- queryFn: ({ signal }) => client(deepUnref(input), { signal, context: deepUnref(options?.context) }),
51
- ...options
52
- };
53
- },
54
- infiniteOptions(options) {
55
- const input = options.input;
56
- return {
57
- initialPageParam: void 0,
58
- queryKey: computed(() => buildKey(path, { type: "infinite", input: deepUnref(input) })),
59
- queryFn: ({ pageParam, signal }) => {
60
- return client({ ...deepUnref(input), cursor: pageParam }, { signal, context: deepUnref(options.context) });
61
- },
62
- ...options
63
- };
64
- },
65
- mutationOptions(...[options]) {
66
- return {
67
- mutationKey: buildKey(path, { type: "mutation" }),
68
- mutationFn: (input) => client(input, { context: deepUnref(options?.context) }),
69
- ...options
70
- };
71
- }
72
- };
73
- }
74
-
75
- // src/utils-router.ts
76
- function createRouterUtils(client, path = []) {
77
- const generalUtils = createGeneralUtils(path);
78
- const procedureUtils = createProcedureUtils(client, path);
79
- const recursive = new Proxy({
80
- ...generalUtils,
81
- ...procedureUtils
82
- }, {
83
- get(target, prop) {
84
- const value = Reflect.get(target, prop);
85
- if (typeof prop !== "string") {
86
- return value;
87
- }
88
- const nextUtils = createRouterUtils(client[prop], [...path, prop]);
89
- if (typeof value !== "function") {
90
- return nextUtils;
91
- }
92
- return new Proxy(value, {
93
- get(_, prop2) {
94
- return Reflect.get(nextUtils, prop2);
95
- }
96
- });
97
- }
98
- });
99
- return recursive;
100
- }
101
-
102
- // src/index.ts
103
- var createORPCVueQueryUtils = createRouterUtils;
104
- export {
105
- buildKey,
106
- createGeneralUtils,
107
- createORPCVueQueryUtils,
108
- createProcedureUtils,
109
- createRouterUtils
110
- };
111
- //# sourceMappingURL=index.js.map
@@ -1,8 +0,0 @@
1
- import { createRouterUtils } from './utils-router';
2
- export * from './key';
3
- export * from './types';
4
- export * from './utils-general';
5
- export * from './utils-procedure';
6
- export * from './utils-router';
7
- export declare const createORPCVueQueryUtils: typeof createRouterUtils;
8
- //# sourceMappingURL=index.d.ts.map
package/dist/src/key.d.ts DELETED
@@ -1,9 +0,0 @@
1
- import type { PartialDeep } from '@orpc/shared';
2
- import type { QueryKey } from '@tanstack/vue-query';
3
- export type KeyType = 'query' | 'infinite' | 'mutation' | undefined;
4
- export interface BuildKeyOptions<TType extends KeyType, TInput> {
5
- type?: TType;
6
- input?: TType extends 'mutation' ? never : PartialDeep<TInput>;
7
- }
8
- export declare function buildKey<TType extends KeyType, TInput>(path: string[], options?: BuildKeyOptions<TType, TInput>): QueryKey;
9
- //# sourceMappingURL=key.d.ts.map
@@ -1,59 +0,0 @@
1
- import type { AnyFunction, SetOptional } from '@orpc/shared';
2
- import type { MutationObserverOptions, QueryFunctionContext, QueryKey, QueryObserverOptions, UseInfiniteQueryOptions } from '@tanstack/vue-query';
3
- import type { ComputedRef, MaybeRef, MaybeRefOrGetter } from 'vue';
4
- export type MaybeDeepRef<T> = MaybeRef<T extends AnyFunction ? T : T extends object ? {
5
- [K in keyof T]: MaybeDeepRef<T[K]>;
6
- } : T>;
7
- export type NonUndefinedGuard<T> = T extends undefined ? never : T;
8
- export type InferCursor<T> = T extends {
9
- cursor?: any;
10
- } ? T['cursor'] : never;
11
- export type QueryOptionsExtra<TClientContext, TInput, TOutput, TError extends Error, TSelectData> = (undefined extends TInput ? {
12
- input?: MaybeDeepRef<TInput>;
13
- } : {
14
- input: MaybeDeepRef<TInput>;
15
- }) & (undefined extends TClientContext ? {
16
- context?: MaybeDeepRef<TClientContext>;
17
- } : {
18
- context: MaybeDeepRef<TClientContext>;
19
- }) & SetOptional<{
20
- [P in keyof QueryObserverOptions<TOutput, TError, TSelectData, TOutput, QueryKey>]: P extends 'enabled' ? MaybeRefOrGetter<QueryObserverOptions<TOutput, TError, TSelectData, TOutput, QueryKey>[P]> : MaybeDeepRef<QueryObserverOptions<TOutput, TError, TSelectData, TOutput, QueryKey>[P]>;
21
- }, 'queryKey'> & {
22
- shallow?: MaybeRef<boolean>;
23
- initialData?: NonUndefinedGuard<TOutput> | (() => NonUndefinedGuard<TOutput>) | undefined;
24
- };
25
- export interface QueryOptionsBase<TOutput, TError extends Error> {
26
- queryKey: ComputedRef<QueryKey>;
27
- queryFn(ctx: QueryFunctionContext): Promise<TOutput>;
28
- retry?(failureCount: number, error: TError): boolean;
29
- }
30
- export type InfiniteOptionsExtra<TClientContext, TInput, TOutput, TError extends Error, TSelectData> = (undefined extends TInput ? {
31
- input?: MaybeDeepRef<Omit<TInput, 'cursor'>>;
32
- } : {
33
- input: MaybeDeepRef<Omit<TInput, 'cursor'>>;
34
- }) & (undefined extends TClientContext ? {
35
- context?: MaybeDeepRef<TClientContext>;
36
- } : {
37
- context: MaybeDeepRef<TClientContext>;
38
- }) & SetOptional<UseInfiniteQueryOptions<TOutput, TError, TSelectData, TOutput, QueryKey, InferCursor<TInput>>, 'queryKey' | (undefined extends InferCursor<TInput> ? 'initialPageParam' : never)>;
39
- export interface InfiniteOptionsBase<TInput, TOutput, TError extends Error> {
40
- queryKey: ComputedRef<QueryKey>;
41
- queryFn(ctx: QueryFunctionContext<QueryKey, InferCursor<TInput>>): Promise<TOutput>;
42
- initialPageParam: undefined;
43
- retry?(failureCount: number, error: TError): boolean;
44
- }
45
- export type MutationOptionsExtra<TClientContext, TInput, TOutput, TError extends Error> = (undefined extends TClientContext ? {
46
- context?: MaybeDeepRef<TClientContext>;
47
- } : {
48
- context: MaybeDeepRef<TClientContext>;
49
- }) & {
50
- [P in keyof MutationObserverOptions<TOutput, TError, TInput, unknown>]: MaybeDeepRef<MutationObserverOptions<TOutput, TError, TInput, unknown>[P]>;
51
- } & {
52
- shallow?: MaybeRef<boolean>;
53
- };
54
- export interface MutationOptionsBase<TInput, TOutput, TError extends Error> {
55
- mutationKey: QueryKey;
56
- mutationFn(input: TInput): Promise<TOutput>;
57
- retry?(failureCount: number, error: TError): boolean;
58
- }
59
- //# sourceMappingURL=types.d.ts.map
@@ -1,11 +0,0 @@
1
- import type { QueryKey } from '@tanstack/vue-query';
2
- import type { BuildKeyOptions, KeyType } from './key';
3
- import type { MaybeDeepRef } from './types';
4
- /**
5
- * Utils at any level (procedure or router)
6
- */
7
- export interface GeneralUtils<TInput> {
8
- key<UType extends KeyType = undefined>(options?: MaybeDeepRef<BuildKeyOptions<UType, TInput>>): QueryKey;
9
- }
10
- export declare function createGeneralUtils<TInput>(path: string[]): GeneralUtils<TInput>;
11
- //# sourceMappingURL=utils-general.d.ts.map
@@ -1,13 +0,0 @@
1
- import type { Client } from '@orpc/contract';
2
- import type { IsEqual } from '@orpc/shared';
3
- import type { InfiniteOptionsBase, InfiniteOptionsExtra, MutationOptionsBase, MutationOptionsExtra, QueryOptionsBase, QueryOptionsExtra } from './types';
4
- /**
5
- * Utils at procedure level
6
- */
7
- export interface ProcedureUtils<TClientContext, TInput, TOutput, TError extends Error> {
8
- queryOptions<U extends QueryOptionsExtra<TClientContext, TInput, TOutput, TError, any>>(...opt: [options: U] | (undefined extends TInput & TClientContext ? [] : never)): IsEqual<U, QueryOptionsExtra<TClientContext, TInput, TOutput, TError, any>> extends true ? QueryOptionsBase<TOutput, TError> : Omit<QueryOptionsBase<TOutput, TError>, keyof U> & U;
9
- infiniteOptions<U extends InfiniteOptionsExtra<TClientContext, TInput, TOutput, TError, any>>(options: U): Omit<InfiniteOptionsBase<TInput, TOutput, TError>, keyof U> & U;
10
- mutationOptions<U extends MutationOptionsExtra<TClientContext, TInput, TOutput, TError>>(...opt: [options: U] | (undefined extends TClientContext ? [] : never)): IsEqual<U, MutationOptionsExtra<TClientContext, TInput, TOutput, TError>> extends true ? MutationOptionsBase<TInput, TOutput, TError> : Omit<MutationOptionsBase<TInput, TOutput, TError>, keyof U> & U;
11
- }
12
- export declare function createProcedureUtils<TClientContext, TInput, TOutput, TError extends Error>(client: Client<TClientContext, TInput, TOutput, TError>, path: string[]): ProcedureUtils<TClientContext, TInput, TOutput, TError>;
13
- //# sourceMappingURL=utils-procedure.d.ts.map
@@ -1,12 +0,0 @@
1
- import type { Client, NestedClient } from '@orpc/contract';
2
- import { type GeneralUtils } from './utils-general';
3
- import { type ProcedureUtils } from './utils-procedure';
4
- export type RouterUtils<T extends NestedClient<any>> = T extends Client<infer TClientContext, infer UInput, infer UOutput, infer UError> ? ProcedureUtils<TClientContext, UInput, UOutput, UError> & GeneralUtils<UInput> : {
5
- [K in keyof T]: T[K] extends NestedClient<any> ? RouterUtils<T[K]> : never;
6
- } & GeneralUtils<unknown>;
7
- /**
8
- * @param client - The client create form `@orpc/client`
9
- * @param path - The base path for query key
10
- */
11
- export declare function createRouterUtils<T extends NestedClient<any>>(client: T, path?: string[]): RouterUtils<T>;
12
- //# sourceMappingURL=utils-router.d.ts.map
@@ -1,7 +0,0 @@
1
- import type { AnyFunction } from '@orpc/shared';
2
- import type { Ref } from 'vue';
3
- export type DeepUnref<T> = T extends Ref<infer U> ? DeepUnref<U> : T extends AnyFunction ? T : T extends object ? {
4
- [K in keyof T]: DeepUnref<T[K]>;
5
- } : T;
6
- export declare function deepUnref<T>(value: T): DeepUnref<T>;
7
- //# sourceMappingURL=utils.d.ts.map