@orpc/vue-query 0.0.0-next.da84cda → 0.0.0-next.dcc6c29

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,123 @@
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, ensuring a smooth and enjoyable developer experience.
25
+
26
+ ---
27
+
28
+ ## Highlights
29
+
30
+ - **End-to-End Type Safety 🔒**: Ensure complete type safety from inputs to outputs and errors, bridging server and client seamlessly.
31
+ - **First-Class OpenAPI 📄**: Adheres to the OpenAPI standard out of the box, ensuring seamless integration and comprehensive API documentation.
32
+ - **Contract-First Development 📜**: (Optional) Define your API contract upfront and implement it with confidence.
33
+ - **Exceptional Developer Experience ✨**: Enjoy a streamlined workflow with robust typing and clear, in-code documentation.
34
+ - **Multi-Runtime Support 🌍**: Run your code seamlessly on Cloudflare, Deno, Bun, Node.js, and more.
35
+ - **Framework Integrations 🧩**: Supports Tanstack Query (React, Vue, Solid, Svelte), Pinia Colada, and more.
36
+ - **Server Actions ⚡️**: Fully compatible with React Server Actions on Next.js, TanStack Start, and more.
37
+ - **Standard Schema Support 🗂️**: Effortlessly work with Zod, Valibot, ArkType, and others right out of the box.
38
+ - **Fast & Lightweight 💨**: Built on native APIs across all runtimes – optimized for speed and efficiency.
39
+ - **Native Types 📦**: Enjoy built-in support for Date, File, Blob, BigInt, URL and more with no extra setup.
40
+ - **Lazy Router ⏱️**: Improve cold start times with our lazy routing feature.
41
+ - **SSE & Streaming 📡**: Provides SSE and streaming features – perfect for real-time notifications and AI-powered streaming responses.
42
+ - **Reusability 🔄**: Write once and reuse your code across multiple purposes effortlessly.
43
+ - **Extendability 🔌**: Easily enhance oRPC with plugins, middleware, and interceptors.
44
+ - **Reliability 🛡️**: Well-tested, fully TypeScript, production-ready, and MIT licensed for peace of mind.
45
+ - **Simplicity 💡**: Enjoy straightforward, clean code with no hidden magic.
46
+
47
+ ## Documentation
48
+
49
+ You can find the full documentation [here](https://orpc.unnoq.com).
50
+
51
+ ## Packages
52
+
53
+ - [@orpc/contract](https://www.npmjs.com/package/@orpc/contract): Build your API contract.
54
+ - [@orpc/server](https://www.npmjs.com/package/@orpc/server): Build your API or implement API contract.
55
+ - [@orpc/client](https://www.npmjs.com/package/@orpc/client): Consume your API on the client with type-safety.
56
+ - [@orpc/react](https://www.npmjs.com/package/@orpc/react): Utilities for integrating oRPC with React and React Server Actions.
57
+ - [@orpc/react-query](https://www.npmjs.com/package/@orpc/react-query): Integration with [React Query](https://tanstack.com/query/latest/docs/framework/react/overview).
58
+ - [@orpc/vue-query](https://www.npmjs.com/package/@orpc/vue-query): Integration with [Vue Query](https://tanstack.com/query/latest/docs/framework/vue/overview).
59
+ - [@orpc/solid-query](https://www.npmjs.com/package/@orpc/solid-query): Integration with [Solid Query](https://tanstack.com/query/latest/docs/framework/solid/overview).
60
+ - [@orpc/svelte-query](https://www.npmjs.com/package/@orpc/svelte-query): Integration with [Svelte Query](https://tanstack.com/query/latest/docs/framework/svelte/overview).
61
+ - [@orpc/vue-colada](https://www.npmjs.com/package/@orpc/vue-colada): Integration with [Pinia Colada](https://pinia-colada.esm.dev/).
62
+ - [@orpc/openapi](https://www.npmjs.com/package/@orpc/openapi): Generate OpenAPI specs and handle OpenAPI requests.
63
+ - [@orpc/zod](https://www.npmjs.com/package/@orpc/zod): More schemas that [Zod](https://zod.dev/) doesn't support yet.
64
+ - [@orpc/valibot](https://www.npmjs.com/package/@orpc/valibot): OpenAPI spec generation from [Valibot](https://valibot.dev/).
65
+ - [@orpc/arktype](https://www.npmjs.com/package/@orpc/arktype): OpenAPI spec generation from [ArkType](https://arktype.io/).
66
+
67
+ ## `@orpc/vue-query`
68
+
69
+ 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.
70
+
71
+ ```ts
72
+ export function Setup() {
73
+ const query = useQuery(orpc.planet.find.queryOptions({
74
+ input: { id: 123 }, // Specify input if needed
75
+ context: { cache: true }, // Provide client context if needed
76
+ // additional options...
77
+ }))
78
+
79
+ const query = useInfiniteQuery(orpc.planet.list.infiniteOptions({
80
+ input: (pageParam: number | undefined) => ({ limit: 10, offset: pageParam }),
81
+ context: { cache: true }, // Provide client context if needed
82
+ initialPageParam: undefined,
83
+ getNextPageParam: lastPage => lastPage.nextPageParam,
84
+ // additional options...
85
+ }))
86
+
87
+ const mutation = useMutation(orpc.planet.create.mutationOptions({
88
+ context: { cache: true }, // Provide client context if needed
89
+ // additional options...
90
+ }))
91
+
92
+ mutation.mutate({ name: 'Earth' })
93
+
94
+ const queryClient = useQueryClient()
95
+
96
+ // Invalidate all planet queries
97
+ queryClient.invalidateQueries({
98
+ queryKey: orpc.planet.key(),
99
+ })
100
+
101
+ // Invalidate only regular (non-infinite) planet queries
102
+ queryClient.invalidateQueries({
103
+ queryKey: orpc.planet.key({ type: 'query' })
104
+ })
105
+
106
+ // Invalidate the planet find query with id 123
107
+ queryClient.invalidateQueries({
108
+ queryKey: orpc.planet.find.key({ input: { id: 123 } })
109
+ })
110
+ }
111
+ ```
112
+
113
+ ## Sponsors
114
+
115
+ <p align="center">
116
+ <a href="https://cdn.jsdelivr.net/gh/unnoq/unnoq/sponsors.svg">
117
+ <img src='https://cdn.jsdelivr.net/gh/unnoq/unnoq/sponsors.svg'/>
118
+ </a>
119
+ </p>
120
+
121
+ ## License
122
+
123
+ Distributed under the MIT License. See [LICENSE](https://github.com/unnoq/orpc/blob/main/LICENSE) for more information.
@@ -0,0 +1,92 @@
1
+ import { ClientContext, Client, NestedClient } from '@orpc/client';
2
+ import { QueryKey, QueryObserverOptions, QueryFunctionContext, UseInfiniteQueryOptions, MutationObserverOptions, InfiniteData } from '@tanstack/vue-query';
3
+ import { PartialDeep, AnyFunction, SetOptional, 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
+ key<UType extends KeyType = undefined>(options?: BuildKeyOptions<UType, TInput>): QueryKey;
15
+ }
16
+ declare function createGeneralUtils<TInput>(path: string[]): GeneralUtils<TInput>;
17
+
18
+ /**
19
+ * Since `@tanstack/vue-query` is not exporting the type `MaybeRefDeep` we need to copy it from the source code.
20
+ * https://github.com/TanStack/query/blob/7ff544e12e79388e513b1cd886aeb946f80f0153/packages/vue-query/src/types.ts#L19C1-L27C2
21
+ */
22
+ type MaybeRefDeep<T> = MaybeRef<T extends AnyFunction ? T : T extends object ? {
23
+ [Property in keyof T]: MaybeRefDeep<T[Property]>;
24
+ } : T>;
25
+ type QueryOptionsIn<TClientContext extends ClientContext, TInput, TOutput, TError, TSelectData> = (undefined extends TInput ? {
26
+ input?: MaybeRefDeep<TInput>;
27
+ } : {
28
+ input: MaybeRefDeep<TInput>;
29
+ }) & (Record<never, never> extends TClientContext ? {
30
+ context?: MaybeRefDeep<TClientContext>;
31
+ } : {
32
+ context: MaybeRefDeep<TClientContext>;
33
+ }) & {
34
+ [P in keyof Omit<QueryObserverOptions<TOutput, TError, TSelectData, TOutput>, 'queryKey' | 'enabled'>]: MaybeRefDeep<QueryObserverOptions<TOutput, TError, TSelectData, TOutput>[P]>;
35
+ } & {
36
+ enabled?: MaybeRefOrGetter<QueryObserverOptions<TOutput, TError, TSelectData, TOutput>['enabled']>;
37
+ queryKey?: MaybeRefDeep<QueryObserverOptions<TOutput, TError, TSelectData, TOutput>['queryKey']>;
38
+ shallow?: MaybeRef<boolean>;
39
+ };
40
+ interface QueryOptionsBase<TOutput, TError> {
41
+ queryKey: ComputedRef<QueryKey>;
42
+ queryFn(ctx: QueryFunctionContext): Promise<TOutput>;
43
+ retry?(failureCount: number, error: TError): boolean;
44
+ }
45
+ type InfiniteOptionsIn<TClientContext extends ClientContext, TInput, TOutput, TError, TSelectData, TPageParam> = {
46
+ input: (pageParam: TPageParam) => MaybeRefDeep<TInput>;
47
+ } & (Record<never, never> extends TClientContext ? {
48
+ context?: MaybeRefDeep<TClientContext>;
49
+ } : {
50
+ context: MaybeRefDeep<TClientContext>;
51
+ }) & SetOptional<UseInfiniteQueryOptions<TOutput, TError, TSelectData, TOutput, QueryKey, TPageParam>, 'queryKey'>;
52
+ interface InfiniteOptionsBase<TOutput, TError, TPageParam> {
53
+ queryKey: ComputedRef<QueryKey>;
54
+ queryFn(ctx: QueryFunctionContext<QueryKey, TPageParam>): Promise<TOutput>;
55
+ retry?(failureCount: number, error: TError): boolean;
56
+ }
57
+ type MutationOptionsIn<TClientContext extends ClientContext, TInput, TOutput, TError, TMutationContext> = (Record<never, never> extends TClientContext ? {
58
+ context?: TClientContext;
59
+ } : {
60
+ context: TClientContext;
61
+ }) & MutationOptions<TInput, TOutput, TError, TMutationContext>;
62
+ type MutationOptions<TInput, TOutput, TError, TMutationContext> = {
63
+ [P in keyof MutationObserverOptions<TOutput, TError, TInput, TMutationContext>]: MaybeRefDeep<MutationObserverOptions<TOutput, TError, TInput, TMutationContext>[P]>;
64
+ } & {
65
+ shallow?: MaybeRef<boolean>;
66
+ };
67
+
68
+ interface ProcedureUtils<TClientContext extends ClientContext, TInput, TOutput, TError> {
69
+ call: Client<TClientContext, TInput, TOutput, TError>;
70
+ queryOptions<U, USelectData = TOutput>(...rest: MaybeOptionalOptions<U & QueryOptionsIn<TClientContext, TInput, TOutput, TError, USelectData>>): NoInfer<U & Omit<QueryOptionsBase<TOutput, TError>, keyof U>>;
71
+ 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>>;
72
+ mutationOptions<UMutationContext>(...rest: MaybeOptionalOptions<MutationOptionsIn<TClientContext, TInput, TOutput, TError, UMutationContext>>): MutationOptions<TInput, TOutput, TError, UMutationContext>;
73
+ }
74
+ interface CreateProcedureUtilsOptions {
75
+ path: string[];
76
+ }
77
+ declare function createProcedureUtils<TClientContext extends ClientContext, TInput, TOutput, TError>(client: Client<TClientContext, TInput, TOutput, TError>, options: CreateProcedureUtilsOptions): ProcedureUtils<TClientContext, TInput, TOutput, TError>;
78
+
79
+ type RouterUtils<T extends NestedClient<any>> = T extends Client<infer UClientContext, infer UInput, infer UOutput, infer UError> ? ProcedureUtils<UClientContext, UInput, UOutput, UError> & GeneralUtils<UInput> : {
80
+ [K in keyof T]: T[K] extends NestedClient<any> ? RouterUtils<T[K]> : never;
81
+ } & GeneralUtils<unknown>;
82
+ interface CreateRouterUtilsOptions {
83
+ path?: string[];
84
+ }
85
+ declare function createRouterUtils<T extends NestedClient<any>>(client: T, options?: CreateRouterUtilsOptions): RouterUtils<T>;
86
+
87
+ type UnrefDeep<T> = T extends Ref<infer U> ? UnrefDeep<U> : T extends AnyFunction ? T : T extends object ? {
88
+ [K in keyof T]: UnrefDeep<T[K]>;
89
+ } : T;
90
+ declare function unrefDeep<T>(value: T): UnrefDeep<T>;
91
+
92
+ 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 };
@@ -0,0 +1,92 @@
1
+ import { ClientContext, Client, NestedClient } from '@orpc/client';
2
+ import { QueryKey, QueryObserverOptions, QueryFunctionContext, UseInfiniteQueryOptions, MutationObserverOptions, InfiniteData } from '@tanstack/vue-query';
3
+ import { PartialDeep, AnyFunction, SetOptional, 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
+ key<UType extends KeyType = undefined>(options?: BuildKeyOptions<UType, TInput>): QueryKey;
15
+ }
16
+ declare function createGeneralUtils<TInput>(path: string[]): GeneralUtils<TInput>;
17
+
18
+ /**
19
+ * Since `@tanstack/vue-query` is not exporting the type `MaybeRefDeep` we need to copy it from the source code.
20
+ * https://github.com/TanStack/query/blob/7ff544e12e79388e513b1cd886aeb946f80f0153/packages/vue-query/src/types.ts#L19C1-L27C2
21
+ */
22
+ type MaybeRefDeep<T> = MaybeRef<T extends AnyFunction ? T : T extends object ? {
23
+ [Property in keyof T]: MaybeRefDeep<T[Property]>;
24
+ } : T>;
25
+ type QueryOptionsIn<TClientContext extends ClientContext, TInput, TOutput, TError, TSelectData> = (undefined extends TInput ? {
26
+ input?: MaybeRefDeep<TInput>;
27
+ } : {
28
+ input: MaybeRefDeep<TInput>;
29
+ }) & (Record<never, never> extends TClientContext ? {
30
+ context?: MaybeRefDeep<TClientContext>;
31
+ } : {
32
+ context: MaybeRefDeep<TClientContext>;
33
+ }) & {
34
+ [P in keyof Omit<QueryObserverOptions<TOutput, TError, TSelectData, TOutput>, 'queryKey' | 'enabled'>]: MaybeRefDeep<QueryObserverOptions<TOutput, TError, TSelectData, TOutput>[P]>;
35
+ } & {
36
+ enabled?: MaybeRefOrGetter<QueryObserverOptions<TOutput, TError, TSelectData, TOutput>['enabled']>;
37
+ queryKey?: MaybeRefDeep<QueryObserverOptions<TOutput, TError, TSelectData, TOutput>['queryKey']>;
38
+ shallow?: MaybeRef<boolean>;
39
+ };
40
+ interface QueryOptionsBase<TOutput, TError> {
41
+ queryKey: ComputedRef<QueryKey>;
42
+ queryFn(ctx: QueryFunctionContext): Promise<TOutput>;
43
+ retry?(failureCount: number, error: TError): boolean;
44
+ }
45
+ type InfiniteOptionsIn<TClientContext extends ClientContext, TInput, TOutput, TError, TSelectData, TPageParam> = {
46
+ input: (pageParam: TPageParam) => MaybeRefDeep<TInput>;
47
+ } & (Record<never, never> extends TClientContext ? {
48
+ context?: MaybeRefDeep<TClientContext>;
49
+ } : {
50
+ context: MaybeRefDeep<TClientContext>;
51
+ }) & SetOptional<UseInfiniteQueryOptions<TOutput, TError, TSelectData, TOutput, QueryKey, TPageParam>, 'queryKey'>;
52
+ interface InfiniteOptionsBase<TOutput, TError, TPageParam> {
53
+ queryKey: ComputedRef<QueryKey>;
54
+ queryFn(ctx: QueryFunctionContext<QueryKey, TPageParam>): Promise<TOutput>;
55
+ retry?(failureCount: number, error: TError): boolean;
56
+ }
57
+ type MutationOptionsIn<TClientContext extends ClientContext, TInput, TOutput, TError, TMutationContext> = (Record<never, never> extends TClientContext ? {
58
+ context?: TClientContext;
59
+ } : {
60
+ context: TClientContext;
61
+ }) & MutationOptions<TInput, TOutput, TError, TMutationContext>;
62
+ type MutationOptions<TInput, TOutput, TError, TMutationContext> = {
63
+ [P in keyof MutationObserverOptions<TOutput, TError, TInput, TMutationContext>]: MaybeRefDeep<MutationObserverOptions<TOutput, TError, TInput, TMutationContext>[P]>;
64
+ } & {
65
+ shallow?: MaybeRef<boolean>;
66
+ };
67
+
68
+ interface ProcedureUtils<TClientContext extends ClientContext, TInput, TOutput, TError> {
69
+ call: Client<TClientContext, TInput, TOutput, TError>;
70
+ queryOptions<U, USelectData = TOutput>(...rest: MaybeOptionalOptions<U & QueryOptionsIn<TClientContext, TInput, TOutput, TError, USelectData>>): NoInfer<U & Omit<QueryOptionsBase<TOutput, TError>, keyof U>>;
71
+ 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>>;
72
+ mutationOptions<UMutationContext>(...rest: MaybeOptionalOptions<MutationOptionsIn<TClientContext, TInput, TOutput, TError, UMutationContext>>): MutationOptions<TInput, TOutput, TError, UMutationContext>;
73
+ }
74
+ interface CreateProcedureUtilsOptions {
75
+ path: string[];
76
+ }
77
+ declare function createProcedureUtils<TClientContext extends ClientContext, TInput, TOutput, TError>(client: Client<TClientContext, TInput, TOutput, TError>, options: CreateProcedureUtilsOptions): ProcedureUtils<TClientContext, TInput, TOutput, TError>;
78
+
79
+ type RouterUtils<T extends NestedClient<any>> = T extends Client<infer UClientContext, infer UInput, infer UOutput, infer UError> ? ProcedureUtils<UClientContext, UInput, UOutput, UError> & GeneralUtils<UInput> : {
80
+ [K in keyof T]: T[K] extends NestedClient<any> ? RouterUtils<T[K]> : never;
81
+ } & GeneralUtils<unknown>;
82
+ interface CreateRouterUtilsOptions {
83
+ path?: string[];
84
+ }
85
+ declare function createRouterUtils<T extends NestedClient<any>>(client: T, options?: CreateRouterUtilsOptions): RouterUtils<T>;
86
+
87
+ type UnrefDeep<T> = T extends Ref<infer U> ? UnrefDeep<U> : T extends AnyFunction ? T : T extends object ? {
88
+ [K in keyof T]: UnrefDeep<T[K]>;
89
+ } : T;
90
+ declare function unrefDeep<T>(value: T): UnrefDeep<T>;
91
+
92
+ 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 };
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.da84cda",
4
+ "version": "0.0.0-next.dcc6c29",
5
5
  "license": "MIT",
6
6
  "homepage": "https://orpc.unnoq.com",
7
7
  "repository": {
@@ -18,27 +18,24 @@
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
30
  "@tanstack/vue-query": ">=5.50.0",
36
31
  "vue": ">=3.3.0",
37
- "@orpc/contract": "0.0.0-next.da84cda",
38
- "@orpc/client": "0.0.0-next.da84cda"
32
+ "@orpc/client": "0.0.0-next.dcc6c29"
33
+ },
34
+ "dependencies": {
35
+ "@orpc/shared": "0.0.0-next.dcc6c29"
39
36
  },
40
37
  "scripts": {
41
- "build": "tsup --clean --sourcemap --entry.index=src/index.ts --format=esm --onSuccess='tsc -b --noCheck'",
38
+ "build": "unbuild",
42
39
  "build:watch": "pnpm run build --watch",
43
40
  "type:check": "tsc -b"
44
41
  }
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