@orpc/vue-query 0.0.0-next.011bc88

Sign up to get free protection for your applications and to get access to all the features.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 oRPC
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/dist/index.js ADDED
@@ -0,0 +1,110 @@
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
+ queryKey: computed(() => buildKey(path, { type: "infinite", input: deepUnref(input) })),
58
+ queryFn: ({ pageParam, signal }) => {
59
+ return client({ ...deepUnref(input), cursor: pageParam }, { signal, context: deepUnref(options.context) });
60
+ },
61
+ ...options
62
+ };
63
+ },
64
+ mutationOptions(...[options]) {
65
+ return {
66
+ mutationKey: buildKey(path, { type: "mutation" }),
67
+ mutationFn: (input) => client(input, { context: deepUnref(options?.context) }),
68
+ ...options
69
+ };
70
+ }
71
+ };
72
+ }
73
+
74
+ // src/utils-router.ts
75
+ function createRouterUtils(client, path = []) {
76
+ const generalUtils = createGeneralUtils(path);
77
+ const procedureUtils = createProcedureUtils(client, path);
78
+ const recursive = new Proxy({
79
+ ...generalUtils,
80
+ ...procedureUtils
81
+ }, {
82
+ get(target, prop) {
83
+ const value = Reflect.get(target, prop);
84
+ if (typeof prop !== "string") {
85
+ return value;
86
+ }
87
+ const nextUtils = createRouterUtils(client[prop], [...path, prop]);
88
+ if (typeof value !== "function") {
89
+ return nextUtils;
90
+ }
91
+ return new Proxy(value, {
92
+ get(_, prop2) {
93
+ return Reflect.get(nextUtils, prop2);
94
+ }
95
+ });
96
+ }
97
+ });
98
+ return recursive;
99
+ }
100
+
101
+ // src/index.ts
102
+ var createORPCVueQueryUtils = createRouterUtils;
103
+ export {
104
+ buildKey,
105
+ createGeneralUtils,
106
+ createORPCVueQueryUtils,
107
+ createProcedureUtils,
108
+ createRouterUtils
109
+ };
110
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,8 @@
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
@@ -0,0 +1,9 @@
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
@@ -0,0 +1,43 @@
1
+ import type { AnyFunction, SetOptional } from '@orpc/shared';
2
+ import type { DefaultError, MutationObserverOptions, QueryKey, QueryObserverOptions, UseInfiniteQueryOptions } from '@tanstack/vue-query';
3
+ import type { 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 QueryOptions<TInput, TOutput, TClientContext, 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, DefaultError, TSelectData, TOutput, QueryKey>]: P extends 'enabled' ? MaybeRefOrGetter<QueryObserverOptions<TOutput, DefaultError, TSelectData, TOutput, QueryKey>[P]> : MaybeDeepRef<QueryObserverOptions<TOutput, DefaultError, TSelectData, TOutput, QueryKey>[P]>;
21
+ }, 'queryKey'> & {
22
+ shallow?: MaybeRef<boolean>;
23
+ initialData?: NonUndefinedGuard<TOutput> | (() => NonUndefinedGuard<TOutput>) | undefined;
24
+ };
25
+ export type InfiniteOptions<TInput, TOutput, TClientContext, TSelectData> = (undefined extends TInput ? {
26
+ input?: MaybeDeepRef<Omit<TInput, 'cursor'>>;
27
+ } : {
28
+ input: MaybeDeepRef<Omit<TInput, 'cursor'>>;
29
+ }) & (undefined extends TClientContext ? {
30
+ context?: MaybeDeepRef<TClientContext>;
31
+ } : {
32
+ context: MaybeDeepRef<TClientContext>;
33
+ }) & SetOptional<UseInfiniteQueryOptions<TOutput, DefaultError, TSelectData, TOutput, QueryKey, InferCursor<TInput>>, 'queryKey' | (undefined extends InferCursor<TInput> ? 'initialPageParam' : never)>;
34
+ export type MutationOptions<TInput, TOutput, TClientContext> = (undefined extends TClientContext ? {
35
+ context?: MaybeDeepRef<TClientContext>;
36
+ } : {
37
+ context: MaybeDeepRef<TClientContext>;
38
+ }) & {
39
+ [P in keyof MutationObserverOptions<TOutput, DefaultError, TInput, unknown>]: MaybeDeepRef<MutationObserverOptions<TOutput, DefaultError, TInput, unknown>[P]>;
40
+ } & {
41
+ shallow?: MaybeRef<boolean>;
42
+ };
43
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1,11 @@
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
@@ -0,0 +1,31 @@
1
+ import type { ProcedureClient } from '@orpc/server';
2
+ import type { IsEqual } from '@orpc/shared';
3
+ import type { QueryFunctionContext, QueryKey } from '@tanstack/vue-query';
4
+ import type { ComputedRef } from 'vue';
5
+ import type { InferCursor, InfiniteOptions, MutationOptions, QueryOptions } from './types';
6
+ /**
7
+ * Utils at procedure level
8
+ */
9
+ export interface ProcedureUtils<TInput, TOutput, TClientContext> {
10
+ queryOptions: <U extends QueryOptions<TInput, TOutput, TClientContext, any>>(...opt: [options: U] | (undefined extends TInput & TClientContext ? [] : never)) => IsEqual<U, QueryOptions<TInput, TOutput, TClientContext, any>> extends true ? {
11
+ queryKey: ComputedRef<QueryKey>;
12
+ queryFn: (ctx: QueryFunctionContext) => Promise<TOutput>;
13
+ } : Omit<{
14
+ queryKey: ComputedRef<QueryKey>;
15
+ queryFn: (ctx: QueryFunctionContext) => Promise<TOutput>;
16
+ }, keyof U> & U;
17
+ infiniteOptions: <U extends InfiniteOptions<TInput, TOutput, TClientContext, any>>(options: U) => Omit<{
18
+ queryKey: ComputedRef<QueryKey>;
19
+ queryFn: (ctx: QueryFunctionContext<QueryKey, InferCursor<TInput>>) => Promise<TOutput>;
20
+ initialPageParam: undefined;
21
+ }, keyof U> & U;
22
+ mutationOptions: <U extends MutationOptions<TInput, TOutput, TClientContext>>(...opt: [options: U] | (undefined extends TClientContext ? [] : never)) => IsEqual<U, MutationOptions<TInput, TOutput, TClientContext>> extends true ? {
23
+ mutationKey: QueryKey;
24
+ mutationFn: (input: TInput) => Promise<TOutput>;
25
+ } : Omit<{
26
+ mutationKey: QueryKey;
27
+ mutationFn: (input: TInput) => Promise<TOutput>;
28
+ }, keyof U> & U;
29
+ }
30
+ export declare function createProcedureUtils<TInput, TOutput, TClientContext>(client: ProcedureClient<TInput, TOutput, TClientContext>, path: string[]): ProcedureUtils<TInput, TOutput, TClientContext>;
31
+ //# sourceMappingURL=utils-procedure.d.ts.map
@@ -0,0 +1,12 @@
1
+ import type { ProcedureClient, RouterClient } from '@orpc/server';
2
+ import { type GeneralUtils } from './utils-general';
3
+ import { type ProcedureUtils } from './utils-procedure';
4
+ export type RouterUtils<T extends RouterClient<any, any>> = T extends ProcedureClient<infer TInput, infer TOutput, infer TClientContext> ? ProcedureUtils<TInput, TOutput, TClientContext> & GeneralUtils<TInput> : {
5
+ [K in keyof T]: T[K] extends RouterClient<any, 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 RouterClient<any, any>>(client: T, path?: string[]): RouterUtils<T>;
12
+ //# sourceMappingURL=utils-router.d.ts.map
@@ -0,0 +1,7 @@
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
package/package.json ADDED
@@ -0,0 +1,46 @@
1
+ {
2
+ "name": "@orpc/vue-query",
3
+ "type": "module",
4
+ "version": "0.0.0-next.011bc88",
5
+ "license": "MIT",
6
+ "homepage": "https://orpc.unnoq.com",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/unnoq/orpc.git",
10
+ "directory": "packages/vue-query"
11
+ },
12
+ "keywords": [
13
+ "unnoq",
14
+ "orpc",
15
+ "vue",
16
+ "vue-query",
17
+ "tanstack"
18
+ ],
19
+ "exports": {
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"
27
+ }
28
+ },
29
+ "files": [
30
+ "!**/*.map",
31
+ "!**/*.tsbuildinfo",
32
+ "dist"
33
+ ],
34
+ "peerDependencies": {
35
+ "@tanstack/vue-query": ">=5.50.0",
36
+ "vue": ">=3.3.0",
37
+ "@orpc/server": "0.0.0-next.011bc88",
38
+ "@orpc/client": "0.0.0-next.011bc88",
39
+ "@orpc/contract": "0.0.0-next.011bc88"
40
+ },
41
+ "scripts": {
42
+ "build": "tsup --clean --sourcemap --entry.index=src/index.ts --format=esm --onSuccess='tsc -b --noCheck'",
43
+ "build:watch": "pnpm run build --watch",
44
+ "type:check": "tsc -b"
45
+ }
46
+ }