@orpc/vue-query 0.0.0
Sign up to get free protection for your applications and to get access to all the features.
- package/LICENSE +21 -0
- package/dist/index.js +107 -0
- package/dist/src/index.d.ts +8 -0
- package/dist/src/key.d.ts +9 -0
- package/dist/src/types.d.ts +31 -0
- package/dist/src/utils-general.d.ts +10 -0
- package/dist/src/utils-procedure.d.ts +29 -0
- package/dist/src/utils-router.d.ts +14 -0
- package/dist/src/utils.d.ts +3 -0
- package/package.json +46 -0
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,107 @@
|
|
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.ts
|
25
|
+
import { isRef } from "vue";
|
26
|
+
function deepUnref(value) {
|
27
|
+
if (isRef(value)) {
|
28
|
+
return deepUnref(value.value);
|
29
|
+
}
|
30
|
+
if (Array.isArray(value)) {
|
31
|
+
return value.map(deepUnref);
|
32
|
+
}
|
33
|
+
if (value && typeof value === "object") {
|
34
|
+
return Object.keys(value).reduce((acc, key) => {
|
35
|
+
acc[key] = deepUnref(value[key]);
|
36
|
+
return acc;
|
37
|
+
}, {});
|
38
|
+
}
|
39
|
+
return value;
|
40
|
+
}
|
41
|
+
|
42
|
+
// src/utils-procedure.ts
|
43
|
+
function createProcedureUtils(client, path) {
|
44
|
+
return {
|
45
|
+
queryOptions(...[options]) {
|
46
|
+
const input = options?.input;
|
47
|
+
return {
|
48
|
+
queryKey: buildKey(path, { type: "query", input }),
|
49
|
+
queryFn: () => client(deepUnref(input)),
|
50
|
+
...options
|
51
|
+
};
|
52
|
+
},
|
53
|
+
infiniteOptions(options) {
|
54
|
+
const input = options.input;
|
55
|
+
return {
|
56
|
+
queryKey: buildKey(path, { type: "infinite", input }),
|
57
|
+
queryFn: ({ pageParam }) => client({ ...deepUnref(input), cursor: pageParam }),
|
58
|
+
...options
|
59
|
+
};
|
60
|
+
},
|
61
|
+
mutationOptions(options) {
|
62
|
+
return {
|
63
|
+
mutationKey: buildKey(path, { type: "mutation" }),
|
64
|
+
mutationFn: (input) => client(input),
|
65
|
+
...options
|
66
|
+
};
|
67
|
+
}
|
68
|
+
};
|
69
|
+
}
|
70
|
+
|
71
|
+
// src/utils-router.ts
|
72
|
+
function createRouterUtils(client, path = []) {
|
73
|
+
const generalUtils = createGeneralUtils(path);
|
74
|
+
const procedureUtils = createProcedureUtils(client, path);
|
75
|
+
const recursive = new Proxy({
|
76
|
+
...generalUtils,
|
77
|
+
...procedureUtils
|
78
|
+
}, {
|
79
|
+
get(target, prop) {
|
80
|
+
const value = Reflect.get(target, prop);
|
81
|
+
if (typeof prop !== "string") {
|
82
|
+
return value;
|
83
|
+
}
|
84
|
+
const nextUtils = createRouterUtils(client[prop], [...path, prop]);
|
85
|
+
if (typeof value !== "function") {
|
86
|
+
return nextUtils;
|
87
|
+
}
|
88
|
+
return new Proxy(value, {
|
89
|
+
get(_, prop2) {
|
90
|
+
return Reflect.get(nextUtils, prop2);
|
91
|
+
}
|
92
|
+
});
|
93
|
+
}
|
94
|
+
});
|
95
|
+
return recursive;
|
96
|
+
}
|
97
|
+
|
98
|
+
// src/index.ts
|
99
|
+
var createORPCVueQueryUtils = createRouterUtils;
|
100
|
+
export {
|
101
|
+
buildKey,
|
102
|
+
createGeneralUtils,
|
103
|
+
createORPCVueQueryUtils,
|
104
|
+
createProcedureUtils,
|
105
|
+
createRouterUtils
|
106
|
+
};
|
107
|
+
//# 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 { 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 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,10 @@
|
|
1
|
+
import type { QueryKey } from '@tanstack/vue-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
|
@@ -0,0 +1,29 @@
|
|
1
|
+
import type { IsEqual } from '@orpc/shared';
|
2
|
+
import type { QueryKey } from '@tanstack/vue-query';
|
3
|
+
import type { InfiniteOptions, MutationOptions, QueryOptions } from './types';
|
4
|
+
/**
|
5
|
+
* Utils at procedure level
|
6
|
+
*/
|
7
|
+
export interface ProcedureUtils<TInput, TOutput> {
|
8
|
+
queryOptions: <U extends QueryOptions<TInput, TOutput, TOutput>>(...options: [U] | (undefined extends TInput ? [] : never)) => IsEqual<U, QueryOptions<TInput, TOutput, TOutput>> extends true ? {
|
9
|
+
queryKey: QueryKey;
|
10
|
+
queryFn: () => Promise<TOutput>;
|
11
|
+
} : Omit<{
|
12
|
+
queryKey: QueryKey;
|
13
|
+
queryFn: () => Promise<TOutput>;
|
14
|
+
}, keyof U> & U;
|
15
|
+
infiniteOptions: <U extends InfiniteOptions<TInput, TOutput, any>>(options: U) => Omit<{
|
16
|
+
queryKey: QueryKey;
|
17
|
+
queryFn: () => Promise<TOutput>;
|
18
|
+
initialPageParam: undefined;
|
19
|
+
}, keyof U> & U;
|
20
|
+
mutationOptions: <U extends MutationOptions<TInput, TOutput>>(options?: U) => IsEqual<U, MutationOptions<TInput, TOutput>> extends true ? {
|
21
|
+
mutationKey: QueryKey;
|
22
|
+
mutationFn: (input: TInput) => Promise<TOutput>;
|
23
|
+
} : Omit<{
|
24
|
+
mutationKey: QueryKey;
|
25
|
+
mutationFn: (input: TInput) => Promise<TOutput>;
|
26
|
+
}, keyof U> & U;
|
27
|
+
}
|
28
|
+
export declare function createProcedureUtils<TInput, TOutput>(client: (input: TInput) => Promise<TOutput>, path: string[]): ProcedureUtils<TInput, TOutput>;
|
29
|
+
//# sourceMappingURL=utils-procedure.d.ts.map
|
@@ -0,0 +1,14 @@
|
|
1
|
+
import type { ContractProcedure, ContractRouter, SchemaInput, SchemaOutput } from '@orpc/contract';
|
2
|
+
import type { Lazy, Procedure, Router } from '@orpc/server';
|
3
|
+
import { type GeneralUtils } from './utils-general';
|
4
|
+
import { type ProcedureUtils } from './utils-procedure';
|
5
|
+
export type RouterUtils<T extends Router<any> | ContractRouter> = {
|
6
|
+
[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;
|
7
|
+
} & GeneralUtils<unknown>;
|
8
|
+
/**
|
9
|
+
* @param client - The client create form `@orpc/client`
|
10
|
+
* @param path - The base path for query key
|
11
|
+
*/
|
12
|
+
export declare function createRouterUtils<T extends Router<any> | ContractRouter>(client: any, // TODO typed
|
13
|
+
path?: string[]): RouterUtils<T>;
|
14
|
+
//# sourceMappingURL=utils-router.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",
|
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/contract": "0.14.0",
|
38
|
+
"@orpc/client": "0.14.0",
|
39
|
+
"@orpc/server": "0.14.0"
|
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
|
+
}
|