@orpc/vue-colada 0.21.0

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,96 @@
1
+ // src/key.ts
2
+ import { SuperJSON } from "@orpc/server/fetch";
3
+ function buildKey(path, options) {
4
+ return [
5
+ "__ORPC__",
6
+ ...path,
7
+ ...options?.input !== void 0 ? [{ input: JSON.stringify(SuperJSON.serialize(options.input)) }] : []
8
+ ];
9
+ }
10
+
11
+ // src/utils.ts
12
+ import { isRef } from "vue";
13
+ function deepUnref(value) {
14
+ if (isRef(value)) {
15
+ return deepUnref(value.value);
16
+ }
17
+ if (Array.isArray(value)) {
18
+ return value.map(deepUnref);
19
+ }
20
+ if (value && typeof value === "object") {
21
+ return Object.keys(value).reduce((acc, key) => {
22
+ acc[key] = deepUnref(value[key]);
23
+ return acc;
24
+ }, {});
25
+ }
26
+ return value;
27
+ }
28
+
29
+ // src/utils-general.ts
30
+ function createGeneralUtils(path) {
31
+ return {
32
+ key(options) {
33
+ return buildKey(path, deepUnref(options));
34
+ }
35
+ };
36
+ }
37
+
38
+ // src/utils-procedure.ts
39
+ import { computed } from "vue";
40
+ function createProcedureUtils(client, path) {
41
+ return {
42
+ queryOptions(...[options]) {
43
+ const input = options?.input;
44
+ return {
45
+ key: computed(() => buildKey(path, { input: deepUnref(input) })),
46
+ query: ({ signal }) => client(deepUnref(input), { signal, context: deepUnref(options?.context) }),
47
+ ...options
48
+ };
49
+ },
50
+ mutationOptions(...[options]) {
51
+ return {
52
+ key: (input) => buildKey(path, { input }),
53
+ mutation: (input, _) => client(input, { context: deepUnref(options?.context) }),
54
+ ...options
55
+ };
56
+ }
57
+ };
58
+ }
59
+
60
+ // src/utils-router.ts
61
+ function createRouterUtils(client, path = []) {
62
+ const generalUtils = createGeneralUtils(path);
63
+ const procedureUtils = createProcedureUtils(client, path);
64
+ const recursive = new Proxy({
65
+ ...generalUtils,
66
+ ...procedureUtils
67
+ }, {
68
+ get(target, prop) {
69
+ const value = Reflect.get(target, prop);
70
+ if (typeof prop !== "string") {
71
+ return value;
72
+ }
73
+ const nextUtils = createRouterUtils(client[prop], [...path, prop]);
74
+ if (typeof value !== "function") {
75
+ return nextUtils;
76
+ }
77
+ return new Proxy(value, {
78
+ get(_, prop2) {
79
+ return Reflect.get(nextUtils, prop2);
80
+ }
81
+ });
82
+ }
83
+ });
84
+ return recursive;
85
+ }
86
+
87
+ // src/index.ts
88
+ var createORPCVueColadaUtils = createRouterUtils;
89
+ export {
90
+ buildKey,
91
+ createGeneralUtils,
92
+ createORPCVueColadaUtils,
93
+ createProcedureUtils,
94
+ createRouterUtils
95
+ };
96
+ //# 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 createORPCVueColadaUtils: typeof createRouterUtils;
8
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1,6 @@
1
+ import type { EntryKey } from '@pinia/colada';
2
+ export interface BuildKeyOptions<TInput> {
3
+ input?: TInput;
4
+ }
5
+ export declare function buildKey<TInput>(path: string[], options?: BuildKeyOptions<TInput>): EntryKey;
6
+ //# sourceMappingURL=key.d.ts.map
@@ -0,0 +1,25 @@
1
+ import type { AnyFunction, SetOptional } from '@orpc/shared';
2
+ import type { UseMutationOptions, UseQueryOptions } from '@pinia/colada';
3
+ import type { MaybeRef } 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 UseQueryFnContext = Parameters<UseQueryOptions<any>['query']>[0];
8
+ export type InferCursor<T> = T extends {
9
+ cursor?: any;
10
+ } ? T['cursor'] : never;
11
+ export type QueryOptions<TInput, TOutput, TClientContext> = (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<UseQueryOptions<TOutput>, 'key' | 'query'>;
20
+ export type MutationOptions<TInput, TOutput, TClientContext> = (undefined extends TClientContext ? {
21
+ context?: MaybeDeepRef<TClientContext>;
22
+ } : {
23
+ context: MaybeDeepRef<TClientContext>;
24
+ }) & SetOptional<UseMutationOptions<TOutput, TInput>, 'mutation'>;
25
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1,11 @@
1
+ import type { EntryKey } from '@pinia/colada';
2
+ import type { BuildKeyOptions } 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: (options?: MaybeDeepRef<BuildKeyOptions<TInput>>) => EntryKey;
9
+ }
10
+ export declare function createGeneralUtils<TInput>(path: string[]): GeneralUtils<TInput>;
11
+ //# sourceMappingURL=utils-general.d.ts.map
@@ -0,0 +1,26 @@
1
+ import type { ProcedureClient } from '@orpc/server';
2
+ import type { IsEqual } from '@orpc/shared';
3
+ import type { EntryKey } from '@pinia/colada';
4
+ import type { ComputedRef } from 'vue';
5
+ import type { MutationOptions, QueryOptions, UseQueryFnContext } from './types';
6
+ /**
7
+ * Utils at procedure level
8
+ */
9
+ export interface ProcedureUtils<TInput, TOutput, TClientContext> {
10
+ queryOptions: <U extends QueryOptions<TInput, TOutput, TClientContext>>(...opt: [options: U] | (undefined extends TInput & TClientContext ? [] : never)) => IsEqual<U, QueryOptions<TInput, TOutput, TClientContext>> extends true ? {
11
+ key: ComputedRef<EntryKey>;
12
+ query: (ctx: UseQueryFnContext) => Promise<TOutput>;
13
+ } : Omit<{
14
+ key: ComputedRef<EntryKey>;
15
+ query: (ctx: UseQueryFnContext) => Promise<TOutput>;
16
+ }, keyof U> & U;
17
+ mutationOptions: <U extends MutationOptions<TInput, TOutput, TClientContext>>(...opt: [options: U] | (undefined extends TClientContext ? [] : never)) => IsEqual<U, MutationOptions<TInput, TOutput, TClientContext>> extends true ? {
18
+ key: (input: TInput) => EntryKey;
19
+ mutation: (input: TInput) => Promise<TOutput>;
20
+ } : Omit<{
21
+ key: (input: TInput) => EntryKey;
22
+ mutation: (input: TInput) => Promise<TOutput>;
23
+ }, keyof U> & U;
24
+ }
25
+ export declare function createProcedureUtils<TInput, TOutput, TClientContext>(client: ProcedureClient<TInput, TOutput, TClientContext>, path: string[]): ProcedureUtils<TInput, TOutput, TClientContext>;
26
+ //# 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,55 @@
1
+ {
2
+ "name": "@orpc/vue-colada",
3
+ "type": "module",
4
+ "version": "0.21.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-colada"
11
+ },
12
+ "keywords": [
13
+ "unnoq",
14
+ "orpc",
15
+ "vue",
16
+ "vue-colada",
17
+ "pinia-colada",
18
+ "tanstack"
19
+ ],
20
+ "exports": {
21
+ ".": {
22
+ "types": "./dist/src/index.d.ts",
23
+ "import": "./dist/index.js",
24
+ "default": "./dist/index.js"
25
+ },
26
+ "./🔒/*": {
27
+ "types": "./dist/src/*.d.ts"
28
+ }
29
+ },
30
+ "files": [
31
+ "!**/*.map",
32
+ "!**/*.tsbuildinfo",
33
+ "dist"
34
+ ],
35
+ "peerDependencies": {
36
+ "@pinia/colada": ">=0.13.1",
37
+ "vue": ">=3.3.0",
38
+ "@orpc/client": "0.21.0",
39
+ "@orpc/contract": "0.21.0",
40
+ "@orpc/server": "0.21.0"
41
+ },
42
+ "dependencies": {
43
+ "@orpc/server": "0.21.0"
44
+ },
45
+ "devDependencies": {
46
+ "@pinia/colada": "^0.13.1",
47
+ "@vue/test-utils": "^2.4.6",
48
+ "pinia": "^2.3.0"
49
+ },
50
+ "scripts": {
51
+ "build": "tsup --clean --sourcemap --entry.index=src/index.ts --format=esm --onSuccess='tsc -b --noCheck'",
52
+ "build:watch": "pnpm run build --watch",
53
+ "type:check": "tsc -b"
54
+ }
55
+ }