@orpc/vue-query 0.0.0-next.18dacf3

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,108 @@
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 }),
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 }) => client({ ...deepUnref(input), cursor: pageParam }, { signal }),
59
+ ...options
60
+ };
61
+ },
62
+ mutationOptions(options) {
63
+ return {
64
+ mutationKey: buildKey(path, { type: "mutation" }),
65
+ mutationFn: (input) => client(input),
66
+ ...options
67
+ };
68
+ }
69
+ };
70
+ }
71
+
72
+ // src/utils-router.ts
73
+ function createRouterUtils(client, path = []) {
74
+ const generalUtils = createGeneralUtils(path);
75
+ const procedureUtils = createProcedureUtils(client, path);
76
+ const recursive = new Proxy({
77
+ ...generalUtils,
78
+ ...procedureUtils
79
+ }, {
80
+ get(target, prop) {
81
+ const value = Reflect.get(target, prop);
82
+ if (typeof prop !== "string") {
83
+ return value;
84
+ }
85
+ const nextUtils = createRouterUtils(client[prop], [...path, prop]);
86
+ if (typeof value !== "function") {
87
+ return nextUtils;
88
+ }
89
+ return new Proxy(value, {
90
+ get(_, prop2) {
91
+ return Reflect.get(nextUtils, prop2);
92
+ }
93
+ });
94
+ }
95
+ });
96
+ return recursive;
97
+ }
98
+
99
+ // src/index.ts
100
+ var createORPCVueQueryUtils = createRouterUtils;
101
+ export {
102
+ buildKey,
103
+ createGeneralUtils,
104
+ createORPCVueQueryUtils,
105
+ createProcedureUtils,
106
+ createRouterUtils
107
+ };
108
+ //# 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,31 @@
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, TSelectData> = (undefined extends TInput ? {
12
+ input?: MaybeDeepRef<TInput>;
13
+ } : {
14
+ input: MaybeDeepRef<TInput>;
15
+ }) & SetOptional<{
16
+ [P in keyof QueryObserverOptions<TOutput, DefaultError, TSelectData, TOutput, QueryKey>]: P extends 'enabled' ? MaybeRefOrGetter<MaybeDeepRef<QueryObserverOptions<TOutput, DefaultError, TSelectData, TOutput, QueryKey>[P]>> : MaybeDeepRef<QueryObserverOptions<TOutput, DefaultError, TSelectData, TOutput, QueryKey>[P]>;
17
+ }, 'queryKey'> & {
18
+ shallow?: MaybeRef<boolean>;
19
+ initialData?: NonUndefinedGuard<TOutput> | (() => NonUndefinedGuard<TOutput>) | undefined;
20
+ };
21
+ export type InfiniteOptions<TInput, TOutput, TSelectData> = (undefined extends TInput ? {
22
+ input?: MaybeDeepRef<Omit<TInput, 'cursor'>>;
23
+ } : {
24
+ input: MaybeDeepRef<Omit<TInput, 'cursor'>>;
25
+ }) & SetOptional<UseInfiniteQueryOptions<TOutput, DefaultError, TSelectData, TOutput, QueryKey, InferCursor<TInput>>, 'queryKey' | (undefined extends InferCursor<TInput> ? 'initialPageParam' : never)>;
26
+ export type MutationOptions<TInput, TOutput> = {
27
+ [P in keyof MutationObserverOptions<TOutput, DefaultError, TInput, unknown>]: MaybeDeepRef<MutationObserverOptions<TOutput, DefaultError, TInput, unknown>[P]>;
28
+ } & {
29
+ shallow?: MaybeRef<boolean>;
30
+ };
31
+ //# 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 { Caller } from '@orpc/server';
2
+ import type { IsEqual } from '@orpc/shared';
3
+ import type { QueryKey } from '@tanstack/vue-query';
4
+ import type { ComputedRef } from 'vue';
5
+ import type { InfiniteOptions, MutationOptions, QueryOptions } from './types';
6
+ /**
7
+ * Utils at procedure level
8
+ */
9
+ export interface ProcedureUtils<TInput, TOutput> {
10
+ queryOptions: <U extends QueryOptions<TInput, TOutput, any>>(...options: [U] | (undefined extends TInput ? [] : never)) => IsEqual<U, QueryOptions<TInput, TOutput, any>> extends true ? {
11
+ queryKey: QueryKey;
12
+ queryFn: () => Promise<TOutput>;
13
+ } : Omit<{
14
+ queryKey: ComputedRef<QueryKey>;
15
+ queryFn: () => Promise<TOutput>;
16
+ }, keyof U> & U;
17
+ infiniteOptions: <U extends InfiniteOptions<TInput, TOutput, any>>(options: U) => Omit<{
18
+ queryKey: ComputedRef<QueryKey>;
19
+ queryFn: () => Promise<TOutput>;
20
+ initialPageParam: undefined;
21
+ }, keyof U> & U;
22
+ mutationOptions: <U extends MutationOptions<TInput, TOutput>>(options?: U) => IsEqual<U, MutationOptions<TInput, TOutput>> 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>(client: Caller<TInput, TOutput>, path: string[]): ProcedureUtils<TInput, TOutput>;
31
+ //# sourceMappingURL=utils-procedure.d.ts.map
@@ -0,0 +1,14 @@
1
+ import type { RouterClient } from '@orpc/client';
2
+ import type { ContractProcedure, ContractRouter, SchemaInput, SchemaOutput } from '@orpc/contract';
3
+ import type { Lazy, Procedure, Router } from '@orpc/server';
4
+ import { type GeneralUtils } from './utils-general';
5
+ import { type ProcedureUtils } from './utils-procedure';
6
+ export type RouterUtils<T extends Router<any> | ContractRouter> = {
7
+ [K in keyof T]: T[K] extends ContractProcedure<infer UInputSchema, infer UOutputSchema> | Procedure<any, any, infer UInputSchema, infer UOutputSchema, infer UFuncOutput> | Lazy<Procedure<any, any, infer UInputSchema, infer UOutputSchema, infer UFuncOutput>> ? ProcedureUtils<SchemaInput<UInputSchema>, SchemaOutput<UOutputSchema, UFuncOutput>> & GeneralUtils<SchemaInput<UInputSchema>> : T[K] extends Router<any> | ContractRouter ? RouterUtils<T[K]> : never;
8
+ } & GeneralUtils<unknown>;
9
+ /**
10
+ * @param client - The client create form `@orpc/client`
11
+ * @param path - The base path for query key
12
+ */
13
+ export declare function createRouterUtils<T extends Router<any> | ContractRouter>(client: RouterClient<T>, path?: string[]): RouterUtils<T>;
14
+ //# sourceMappingURL=utils-router.d.ts.map
@@ -0,0 +1,3 @@
1
+ import type { MaybeDeepRef } from './types';
2
+ export declare function deepUnref<T>(value: MaybeDeepRef<T>): T;
3
+ //# 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.18dacf3",
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/client": "0.0.0-next.18dacf3",
38
+ "@orpc/contract": "0.0.0-next.18dacf3",
39
+ "@orpc/server": "0.0.0-next.18dacf3"
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
+ }