@orpc/react-query 0.0.0-next.f56d2b3 → 0.0.0-next.f710cd7

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/react-query">
12
+ <img alt="weekly downloads" src="https://img.shields.io/npm/dw/%40orpc%2Freact-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/react-query`
64
+
65
+ Integration with [React Query](https://tanstack.com/query/latest/docs/framework/react/overview). Read the [documentation](https://orpc.unnoq.com/docs/tanstack-query/react) for more information.
66
+
67
+ ```ts
68
+ export function Example() {
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,104 @@
1
+ import { ClientContext, Client, NestedClient } from '@orpc/client';
2
+ import { QueryKey, UseQueryOptions, QueryFunctionContext, UseInfiniteQueryOptions, UseMutationOptions, InfiniteData } from '@tanstack/react-query';
3
+ import { PartialDeep, SetOptional, MaybeOptionalOptions } from '@orpc/shared';
4
+
5
+ type KeyType = 'query' | 'infinite' | 'mutation' | undefined;
6
+ interface BuildKeyOptions<TType extends KeyType, TInput> {
7
+ type?: TType;
8
+ input?: TType extends 'mutation' ? never : PartialDeep<TInput>;
9
+ }
10
+ declare function buildKey<TType extends KeyType, TInput>(path: string[], options?: BuildKeyOptions<TType, TInput>): QueryKey;
11
+
12
+ /**
13
+ * Utils at any level (procedure or router)
14
+ */
15
+ interface GeneralUtils<TInput> {
16
+ /**
17
+ * Generate a query/mutation key for checking status, invalidate, set, get, etc.
18
+ *
19
+ * @see {@link https://orpc.unnoq.com/docs/tanstack-query/basic#query-mutation-key Tanstack Query/Mutation Key Docs}
20
+ */
21
+ key<UType extends KeyType = undefined>(options?: BuildKeyOptions<UType, TInput>): QueryKey;
22
+ }
23
+ declare function createGeneralUtils<TInput>(path: string[]): GeneralUtils<TInput>;
24
+
25
+ type QueryOptionsIn<TClientContext extends ClientContext, TInput, TOutput, TError, TSelectData> = (undefined extends TInput ? {
26
+ input?: TInput;
27
+ } : {
28
+ input: TInput;
29
+ }) & (Record<never, never> extends TClientContext ? {
30
+ context?: TClientContext;
31
+ } : {
32
+ context: TClientContext;
33
+ }) & SetOptional<UseQueryOptions<TOutput, TError, TSelectData>, 'queryKey'>;
34
+ interface QueryOptionsBase<TOutput, TError> {
35
+ queryKey: QueryKey;
36
+ queryFn(ctx: QueryFunctionContext): Promise<TOutput>;
37
+ retry?(failureCount: number, error: TError): boolean;
38
+ }
39
+ type InfiniteOptionsIn<TClientContext extends ClientContext, TInput, TOutput, TError, TSelectData, TPageParam> = {
40
+ input: (pageParam: TPageParam) => TInput;
41
+ } & (Record<never, never> extends TClientContext ? {
42
+ context?: TClientContext;
43
+ } : {
44
+ context: TClientContext;
45
+ }) & SetOptional<UseInfiniteQueryOptions<TOutput, TError, TSelectData, TOutput, QueryKey, TPageParam>, 'queryKey'>;
46
+ interface InfiniteOptionsBase<TOutput, TError, TPageParam> {
47
+ queryKey: QueryKey;
48
+ queryFn(ctx: QueryFunctionContext<QueryKey, TPageParam>): Promise<TOutput>;
49
+ retry?(failureCount: number, error: TError): boolean;
50
+ }
51
+ type MutationOptionsIn<TClientContext extends ClientContext, TInput, TOutput, TError, TMutationContext> = (Record<never, never> extends TClientContext ? {
52
+ context?: TClientContext;
53
+ } : {
54
+ context: TClientContext;
55
+ }) & MutationOptions<TInput, TOutput, TError, TMutationContext>;
56
+ type MutationOptions<TInput, TOutput, TError, TMutationContext> = UseMutationOptions<TOutput, TError, TInput, TMutationContext>;
57
+
58
+ interface ProcedureUtils<TClientContext extends ClientContext, TInput, TOutput, TError> {
59
+ /**
60
+ * Calling corresponding procedure client
61
+ *
62
+ * @see {@link https://orpc.unnoq.com/docs/tanstack-query/basic#calling-procedure-clients Tanstack Calling Procedure Client Docs}
63
+ */
64
+ call: Client<TClientContext, TInput, TOutput, TError>;
65
+ /**
66
+ * Generate options used for useQuery/useSuspenseQuery/prefetchQuery/...
67
+ *
68
+ * @see {@link https://orpc.unnoq.com/docs/tanstack-query/basic#query-options-utility Tanstack Query Options Utility Docs}
69
+ */
70
+ queryOptions<U, USelectData = TOutput>(...rest: MaybeOptionalOptions<U & QueryOptionsIn<TClientContext, TInput, TOutput, TError, USelectData>>): NoInfer<U & Omit<QueryOptionsBase<TOutput, TError>, keyof U>>;
71
+ /**
72
+ * Generate options used for useInfiniteQuery/useSuspenseInfiniteQuery/prefetchInfiniteQuery/...
73
+ *
74
+ * @see {@link https://orpc.unnoq.com/docs/tanstack-query/basic#infinite-query-options-utility Tanstack Infinite Query Options Utility Docs}
75
+ */
76
+ 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>>;
77
+ /**
78
+ * Generate options used for useMutation/...
79
+ *
80
+ * @see {@link https://orpc.unnoq.com/docs/tanstack-query/basic#mutation-options Tanstack Mutation Options Docs}
81
+ */
82
+ mutationOptions<UMutationContext>(...rest: MaybeOptionalOptions<MutationOptionsIn<TClientContext, TInput, TOutput, TError, UMutationContext>>): MutationOptions<TInput, TOutput, TError, UMutationContext>;
83
+ }
84
+ interface CreateProcedureUtilsOptions {
85
+ path: string[];
86
+ }
87
+ declare function createProcedureUtils<TClientContext extends ClientContext, TInput, TOutput, TError>(client: Client<TClientContext, TInput, TOutput, TError>, options: CreateProcedureUtilsOptions): ProcedureUtils<TClientContext, TInput, TOutput, TError>;
88
+
89
+ type RouterUtils<T extends NestedClient<any>> = T extends Client<infer UClientContext, infer UInput, infer UOutput, infer UError> ? ProcedureUtils<UClientContext, UInput, UOutput, UError> & GeneralUtils<UInput> : {
90
+ [K in keyof T]: T[K] extends NestedClient<any> ? RouterUtils<T[K]> : never;
91
+ } & GeneralUtils<unknown>;
92
+ interface CreateRouterUtilsOptions {
93
+ path?: string[];
94
+ }
95
+ /**
96
+ * Create a router utils from a client.
97
+ *
98
+ * @info Both client-side and server-side clients are supported.
99
+ * @see {@link https://orpc.unnoq.com/docs/tanstack-query/react Tanstack Query React Docs}
100
+ */
101
+ declare function createRouterUtils<T extends NestedClient<any>>(client: T, options?: CreateRouterUtilsOptions): RouterUtils<T>;
102
+
103
+ export { buildKey, createGeneralUtils, createRouterUtils as createORPCReactQueryUtils, createProcedureUtils, createRouterUtils };
104
+ export type { BuildKeyOptions, CreateProcedureUtilsOptions, CreateRouterUtilsOptions, GeneralUtils, InfiniteOptionsBase, InfiniteOptionsIn, KeyType, MutationOptions, MutationOptionsIn, ProcedureUtils, QueryOptionsBase, QueryOptionsIn, RouterUtils };
@@ -0,0 +1,104 @@
1
+ import { ClientContext, Client, NestedClient } from '@orpc/client';
2
+ import { QueryKey, UseQueryOptions, QueryFunctionContext, UseInfiniteQueryOptions, UseMutationOptions, InfiniteData } from '@tanstack/react-query';
3
+ import { PartialDeep, SetOptional, MaybeOptionalOptions } from '@orpc/shared';
4
+
5
+ type KeyType = 'query' | 'infinite' | 'mutation' | undefined;
6
+ interface BuildKeyOptions<TType extends KeyType, TInput> {
7
+ type?: TType;
8
+ input?: TType extends 'mutation' ? never : PartialDeep<TInput>;
9
+ }
10
+ declare function buildKey<TType extends KeyType, TInput>(path: string[], options?: BuildKeyOptions<TType, TInput>): QueryKey;
11
+
12
+ /**
13
+ * Utils at any level (procedure or router)
14
+ */
15
+ interface GeneralUtils<TInput> {
16
+ /**
17
+ * Generate a query/mutation key for checking status, invalidate, set, get, etc.
18
+ *
19
+ * @see {@link https://orpc.unnoq.com/docs/tanstack-query/basic#query-mutation-key Tanstack Query/Mutation Key Docs}
20
+ */
21
+ key<UType extends KeyType = undefined>(options?: BuildKeyOptions<UType, TInput>): QueryKey;
22
+ }
23
+ declare function createGeneralUtils<TInput>(path: string[]): GeneralUtils<TInput>;
24
+
25
+ type QueryOptionsIn<TClientContext extends ClientContext, TInput, TOutput, TError, TSelectData> = (undefined extends TInput ? {
26
+ input?: TInput;
27
+ } : {
28
+ input: TInput;
29
+ }) & (Record<never, never> extends TClientContext ? {
30
+ context?: TClientContext;
31
+ } : {
32
+ context: TClientContext;
33
+ }) & SetOptional<UseQueryOptions<TOutput, TError, TSelectData>, 'queryKey'>;
34
+ interface QueryOptionsBase<TOutput, TError> {
35
+ queryKey: QueryKey;
36
+ queryFn(ctx: QueryFunctionContext): Promise<TOutput>;
37
+ retry?(failureCount: number, error: TError): boolean;
38
+ }
39
+ type InfiniteOptionsIn<TClientContext extends ClientContext, TInput, TOutput, TError, TSelectData, TPageParam> = {
40
+ input: (pageParam: TPageParam) => TInput;
41
+ } & (Record<never, never> extends TClientContext ? {
42
+ context?: TClientContext;
43
+ } : {
44
+ context: TClientContext;
45
+ }) & SetOptional<UseInfiniteQueryOptions<TOutput, TError, TSelectData, TOutput, QueryKey, TPageParam>, 'queryKey'>;
46
+ interface InfiniteOptionsBase<TOutput, TError, TPageParam> {
47
+ queryKey: QueryKey;
48
+ queryFn(ctx: QueryFunctionContext<QueryKey, TPageParam>): Promise<TOutput>;
49
+ retry?(failureCount: number, error: TError): boolean;
50
+ }
51
+ type MutationOptionsIn<TClientContext extends ClientContext, TInput, TOutput, TError, TMutationContext> = (Record<never, never> extends TClientContext ? {
52
+ context?: TClientContext;
53
+ } : {
54
+ context: TClientContext;
55
+ }) & MutationOptions<TInput, TOutput, TError, TMutationContext>;
56
+ type MutationOptions<TInput, TOutput, TError, TMutationContext> = UseMutationOptions<TOutput, TError, TInput, TMutationContext>;
57
+
58
+ interface ProcedureUtils<TClientContext extends ClientContext, TInput, TOutput, TError> {
59
+ /**
60
+ * Calling corresponding procedure client
61
+ *
62
+ * @see {@link https://orpc.unnoq.com/docs/tanstack-query/basic#calling-procedure-clients Tanstack Calling Procedure Client Docs}
63
+ */
64
+ call: Client<TClientContext, TInput, TOutput, TError>;
65
+ /**
66
+ * Generate options used for useQuery/useSuspenseQuery/prefetchQuery/...
67
+ *
68
+ * @see {@link https://orpc.unnoq.com/docs/tanstack-query/basic#query-options-utility Tanstack Query Options Utility Docs}
69
+ */
70
+ queryOptions<U, USelectData = TOutput>(...rest: MaybeOptionalOptions<U & QueryOptionsIn<TClientContext, TInput, TOutput, TError, USelectData>>): NoInfer<U & Omit<QueryOptionsBase<TOutput, TError>, keyof U>>;
71
+ /**
72
+ * Generate options used for useInfiniteQuery/useSuspenseInfiniteQuery/prefetchInfiniteQuery/...
73
+ *
74
+ * @see {@link https://orpc.unnoq.com/docs/tanstack-query/basic#infinite-query-options-utility Tanstack Infinite Query Options Utility Docs}
75
+ */
76
+ 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>>;
77
+ /**
78
+ * Generate options used for useMutation/...
79
+ *
80
+ * @see {@link https://orpc.unnoq.com/docs/tanstack-query/basic#mutation-options Tanstack Mutation Options Docs}
81
+ */
82
+ mutationOptions<UMutationContext>(...rest: MaybeOptionalOptions<MutationOptionsIn<TClientContext, TInput, TOutput, TError, UMutationContext>>): MutationOptions<TInput, TOutput, TError, UMutationContext>;
83
+ }
84
+ interface CreateProcedureUtilsOptions {
85
+ path: string[];
86
+ }
87
+ declare function createProcedureUtils<TClientContext extends ClientContext, TInput, TOutput, TError>(client: Client<TClientContext, TInput, TOutput, TError>, options: CreateProcedureUtilsOptions): ProcedureUtils<TClientContext, TInput, TOutput, TError>;
88
+
89
+ type RouterUtils<T extends NestedClient<any>> = T extends Client<infer UClientContext, infer UInput, infer UOutput, infer UError> ? ProcedureUtils<UClientContext, UInput, UOutput, UError> & GeneralUtils<UInput> : {
90
+ [K in keyof T]: T[K] extends NestedClient<any> ? RouterUtils<T[K]> : never;
91
+ } & GeneralUtils<unknown>;
92
+ interface CreateRouterUtilsOptions {
93
+ path?: string[];
94
+ }
95
+ /**
96
+ * Create a router utils from a client.
97
+ *
98
+ * @info Both client-side and server-side clients are supported.
99
+ * @see {@link https://orpc.unnoq.com/docs/tanstack-query/react Tanstack Query React Docs}
100
+ */
101
+ declare function createRouterUtils<T extends NestedClient<any>>(client: T, options?: CreateRouterUtilsOptions): RouterUtils<T>;
102
+
103
+ export { buildKey, createGeneralUtils, createRouterUtils as createORPCReactQueryUtils, createProcedureUtils, createRouterUtils };
104
+ export type { BuildKeyOptions, CreateProcedureUtilsOptions, CreateRouterUtilsOptions, GeneralUtils, InfiniteOptionsBase, InfiniteOptionsIn, KeyType, MutationOptions, MutationOptionsIn, ProcedureUtils, QueryOptionsBase, QueryOptionsIn, RouterUtils };
package/dist/index.mjs ADDED
@@ -0,0 +1,74 @@
1
+ function buildKey(path, options) {
2
+ const withInput = options?.input !== void 0 ? { input: options?.input } : {};
3
+ const withType = options?.type !== void 0 ? { type: options?.type } : {};
4
+ return [path, {
5
+ ...withInput,
6
+ ...withType
7
+ }];
8
+ }
9
+
10
+ function createGeneralUtils(path) {
11
+ return {
12
+ key(options) {
13
+ return buildKey(path, options);
14
+ }
15
+ };
16
+ }
17
+
18
+ function createProcedureUtils(client, options) {
19
+ return {
20
+ call: client,
21
+ queryOptions(...[optionsIn = {}]) {
22
+ return {
23
+ queryKey: buildKey(options.path, { type: "query", input: optionsIn.input }),
24
+ queryFn: ({ signal }) => client(optionsIn.input, { signal, context: optionsIn.context }),
25
+ ...optionsIn
26
+ };
27
+ },
28
+ infiniteOptions(optionsIn) {
29
+ return {
30
+ queryKey: buildKey(options.path, { type: "infinite", input: optionsIn.input(optionsIn.initialPageParam) }),
31
+ queryFn: ({ pageParam, signal }) => {
32
+ return client(optionsIn.input(pageParam), { signal, context: optionsIn.context });
33
+ },
34
+ ...optionsIn
35
+ };
36
+ },
37
+ mutationOptions(...[optionsIn = {}]) {
38
+ return {
39
+ mutationKey: buildKey(options.path, { type: "mutation" }),
40
+ mutationFn: (input) => client(input, { context: optionsIn.context }),
41
+ ...optionsIn
42
+ };
43
+ }
44
+ };
45
+ }
46
+
47
+ function createRouterUtils(client, options = {}) {
48
+ const path = options.path ?? [];
49
+ const generalUtils = createGeneralUtils(path);
50
+ const procedureUtils = createProcedureUtils(client, { path });
51
+ const recursive = new Proxy({
52
+ ...generalUtils,
53
+ ...procedureUtils
54
+ }, {
55
+ get(target, prop) {
56
+ const value = Reflect.get(target, prop);
57
+ if (typeof prop !== "string") {
58
+ return value;
59
+ }
60
+ const nextUtils = createRouterUtils(client[prop], { ...options, path: [...path, prop] });
61
+ if (typeof value !== "function") {
62
+ return nextUtils;
63
+ }
64
+ return new Proxy(value, {
65
+ get(_, prop2) {
66
+ return Reflect.get(nextUtils, prop2);
67
+ }
68
+ });
69
+ }
70
+ });
71
+ return recursive;
72
+ }
73
+
74
+ export { buildKey, createGeneralUtils, createRouterUtils as createORPCReactQueryUtils, createProcedureUtils, createRouterUtils };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@orpc/react-query",
3
3
  "type": "module",
4
- "version": "0.0.0-next.f56d2b3",
4
+ "version": "0.0.0-next.f710cd7",
5
5
  "license": "MIT",
6
6
  "homepage": "https://orpc.unnoq.com",
7
7
  "repository": {
@@ -18,33 +18,29 @@
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/react-query": ">=5.59.0",
30
+ "@tanstack/react-query": ">=5.55.0",
36
31
  "react": ">=18.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.f710cd7"
39
33
  },
40
34
  "dependencies": {
41
- "@orpc/shared": "0.0.0-next.f56d2b3"
35
+ "@orpc/shared": "0.0.0-next.f710cd7"
42
36
  },
43
37
  "devDependencies": {
44
- "zod": "^3.24.1"
38
+ "@tanstack/vue-query": "^5.72.3",
39
+ "react": "^19.1.0",
40
+ "zod": "^3.24.2"
45
41
  },
46
42
  "scripts": {
47
- "build": "tsup --clean --sourcemap --entry.index=src/index.ts --format=esm --onSuccess='tsc -b --noCheck'",
43
+ "build": "unbuild",
48
44
  "build:watch": "pnpm run build --watch",
49
45
  "type:check": "tsc -b"
50
46
  }
package/dist/index.js DELETED
@@ -1,90 +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-general.ts
16
- function createGeneralUtils(path) {
17
- return {
18
- key(options) {
19
- return buildKey(path, options);
20
- }
21
- };
22
- }
23
-
24
- // src/utils-procedure.ts
25
- function createProcedureUtils(client, path) {
26
- return {
27
- queryOptions(...[options]) {
28
- const input = options?.input;
29
- return {
30
- queryKey: buildKey(path, { type: "query", input }),
31
- queryFn: ({ signal }) => client(input, { signal, context: options?.context }),
32
- ...options
33
- };
34
- },
35
- infiniteOptions(options) {
36
- const input = options.input;
37
- return {
38
- initialPageParam: void 0,
39
- queryKey: buildKey(path, { type: "infinite", input }),
40
- queryFn: ({ pageParam, signal }) => client({ ...input, cursor: pageParam }, { signal, context: options.context }),
41
- ...options
42
- };
43
- },
44
- mutationOptions(...[options]) {
45
- return {
46
- mutationKey: buildKey(path, { type: "mutation" }),
47
- mutationFn: (input) => client(input, { context: options?.context }),
48
- ...options
49
- };
50
- }
51
- };
52
- }
53
-
54
- // src/utils-router.ts
55
- function createRouterUtils(client, path = []) {
56
- const generalUtils = createGeneralUtils(path);
57
- const procedureUtils = createProcedureUtils(client, path);
58
- const recursive = new Proxy({
59
- ...generalUtils,
60
- ...procedureUtils
61
- }, {
62
- get(target, prop) {
63
- const value = Reflect.get(target, prop);
64
- if (typeof prop !== "string") {
65
- return value;
66
- }
67
- const nextUtils = createRouterUtils(client[prop], [...path, prop]);
68
- if (typeof value !== "function") {
69
- return nextUtils;
70
- }
71
- return new Proxy(value, {
72
- get(_, prop2) {
73
- return Reflect.get(nextUtils, prop2);
74
- }
75
- });
76
- }
77
- });
78
- return recursive;
79
- }
80
-
81
- // src/index.ts
82
- var createORPCReactQueryUtils = createRouterUtils;
83
- export {
84
- buildKey,
85
- createGeneralUtils,
86
- createORPCReactQueryUtils,
87
- createProcedureUtils,
88
- createRouterUtils
89
- };
90
- //# 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 createORPCReactQueryUtils: 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/react-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,45 +0,0 @@
1
- import type { SetOptional } from '@orpc/shared';
2
- import type { QueryFunctionContext, QueryKey, UndefinedInitialDataInfiniteOptions, UndefinedInitialDataOptions, UseMutationOptions } from '@tanstack/react-query';
3
- export type InferCursor<T> = T extends {
4
- cursor?: any;
5
- } ? T['cursor'] : never;
6
- export interface QueryOptionsBase<TOutput, TError extends Error> {
7
- queryKey: QueryKey;
8
- queryFn(ctx: QueryFunctionContext): Promise<TOutput>;
9
- retry?(failureCount: number, error: TError): boolean;
10
- }
11
- export type QueryOptionsExtra<TClientContext, TInput, TOutput, TError extends Error, TSelectData> = (undefined extends TInput ? {
12
- input?: TInput;
13
- } : {
14
- input: TInput;
15
- }) & (undefined extends TClientContext ? {
16
- context?: TClientContext;
17
- } : {
18
- context: TClientContext;
19
- }) & (SetOptional<UndefinedInitialDataOptions<TOutput, TError, TSelectData, QueryKey>, 'queryKey'>);
20
- export interface InfiniteOptionsBase<TInput, TOutput, TError extends Error> {
21
- queryKey: QueryKey;
22
- queryFn(ctx: QueryFunctionContext<QueryKey, InferCursor<TInput>>): Promise<TOutput>;
23
- retry?(failureCount: number, error: TError): boolean;
24
- initialPageParam: undefined;
25
- }
26
- export type InfiniteOptionsExtra<TClientContext, TInput, TOutput, TError extends Error, TSelectData> = (undefined extends TInput ? {
27
- input?: Omit<TInput, 'cursor'>;
28
- } : {
29
- input: Omit<TInput, 'cursor'>;
30
- }) & (undefined extends TClientContext ? {
31
- context?: TClientContext;
32
- } : {
33
- context: TClientContext;
34
- }) & (SetOptional<UndefinedInitialDataInfiniteOptions<TOutput, TError, TSelectData, QueryKey, InferCursor<TInput>>, 'queryKey' | (undefined extends InferCursor<TInput> ? 'initialPageParam' : never)>);
35
- export interface MutationOptionsBase<TInput, TOutput, TError extends Error> {
36
- mutationKey: QueryKey;
37
- mutationFn(input: TInput): Promise<TOutput>;
38
- retry?(failureCount: number, error: TError): boolean;
39
- }
40
- export type MutationOptionsExtra<TClientContext, TInput, TOutput, TError extends Error> = (undefined extends TClientContext ? {
41
- context?: TClientContext;
42
- } : {
43
- context: TClientContext;
44
- }) & UseMutationOptions<TOutput, TError, TInput>;
45
- //# sourceMappingURL=types.d.ts.map
@@ -1,10 +0,0 @@
1
- import type { QueryKey } from '@tanstack/react-query';
2
- import type { BuildKeyOptions, KeyType } from './key';
3
- /**
4
- * Utils at any level (procedure or router)
5
- */
6
- export interface GeneralUtils<TInput> {
7
- key<UType extends KeyType = undefined>(options?: BuildKeyOptions<UType, TInput>): QueryKey;
8
- }
9
- export declare function createGeneralUtils<TInput>(path: string[]): GeneralUtils<TInput>;
10
- //# 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>>(...opts: [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 UClientContext, infer UInput, infer UOutput, infer UError> ? ProcedureUtils<UClientContext, 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