@zachacious/protoc-gen-connect-vue 1.0.4 → 1.0.6
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 +1 -1
- package/package.json +4 -2
- package/templates/api.ts.mustache +9 -8
- package/templates/client.ts.mustache +17 -9
- package/templates/rpc.ts.mustache +57 -48
package/README.md
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zachacious/protoc-gen-connect-vue",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.6",
|
|
4
4
|
"description": "Smart TanStack Query & ConnectRPC SDK generator for Vue.js",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -20,7 +20,9 @@
|
|
|
20
20
|
"files": [
|
|
21
21
|
"dist",
|
|
22
22
|
"bin",
|
|
23
|
-
"templates"
|
|
23
|
+
"templates",
|
|
24
|
+
"README.md",
|
|
25
|
+
"LICENSE"
|
|
24
26
|
],
|
|
25
27
|
"publishConfig": {
|
|
26
28
|
"access": "public"
|
|
@@ -1,14 +1,15 @@
|
|
|
1
|
-
import { computed } from "vue";
|
|
2
|
-
import {
|
|
1
|
+
import { computed, unref } from "vue";
|
|
2
|
+
import {
|
|
3
|
+
useQuery,
|
|
4
|
+
useMutation,
|
|
5
|
+
useInfiniteQuery,
|
|
6
|
+
useQueryClient,
|
|
7
|
+
useIsFetching,
|
|
8
|
+
useIsMutating
|
|
9
|
+
} from "@tanstack/vue-query";
|
|
3
10
|
import { ConnectError } from "@connectrpc/connect";
|
|
4
11
|
import { useGrpcClient } from "./client";
|
|
5
12
|
|
|
6
|
-
import {
|
|
7
|
-
{{#rpcs}}
|
|
8
|
-
{{queryDefinitionName}} as {{queryDefinitionName}}Def,
|
|
9
|
-
{{/rpcs}}
|
|
10
|
-
} from "./gen/{{{connectQueryFile}}}";
|
|
11
|
-
|
|
12
13
|
{{#wktImports}}import type { {{.}} } from "@bufbuild/protobuf";{{/wktImports}}
|
|
13
14
|
{{#localImports}}import type { {{.}} } from "./gen/{{{protoPbFile}}}";{{/localImports}}
|
|
14
15
|
{{#externalImports}}
|
|
@@ -1,14 +1,16 @@
|
|
|
1
|
+
import { ref, computed } from "vue";
|
|
1
2
|
import { {{serviceName}} } from './gen/{{{protoPbFile}}}';
|
|
2
3
|
import { createClient, ConnectError, type Interceptor } from "@connectrpc/connect";
|
|
3
4
|
import { createConnectTransport } from "@connectrpc/connect-web";
|
|
4
5
|
|
|
5
|
-
|
|
6
|
+
// --- REACTIVE STATE ---
|
|
7
|
+
const baseUrl = ref('http://localhost:3000');
|
|
6
8
|
|
|
7
9
|
/**
|
|
8
10
|
* Configure the API endpoint at runtime.
|
|
9
11
|
*/
|
|
10
12
|
export const setBaseUrl = (url: string) => {
|
|
11
|
-
baseUrl = url;
|
|
13
|
+
baseUrl.value = url;
|
|
12
14
|
};
|
|
13
15
|
|
|
14
16
|
// --- AUTH RESOLVER ---
|
|
@@ -35,12 +37,20 @@ const transportInterceptor: Interceptor = (next) => async (req) => {
|
|
|
35
37
|
}
|
|
36
38
|
};
|
|
37
39
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
40
|
+
/**
|
|
41
|
+
* useGrpcClient provides a reactive client.
|
|
42
|
+
* If baseUrl.value changes, the computed client updates automatically.
|
|
43
|
+
*/
|
|
44
|
+
export const useGrpcClient = () => {
|
|
45
|
+
const transport = computed(() => createConnectTransport({
|
|
46
|
+
baseUrl: baseUrl.value,
|
|
47
|
+
interceptors: [transportInterceptor],
|
|
48
|
+
}));
|
|
42
49
|
|
|
43
|
-
|
|
50
|
+
const client = computed(() => createClient({{serviceName}}, transport.value));
|
|
51
|
+
|
|
52
|
+
return { client };
|
|
53
|
+
};
|
|
44
54
|
|
|
45
55
|
export const globalQueryConfig = {
|
|
46
56
|
defaultOptions: {
|
|
@@ -52,8 +62,6 @@ export const globalQueryConfig = {
|
|
|
52
62
|
},
|
|
53
63
|
};
|
|
54
64
|
|
|
55
|
-
export const useGrpcClient = () => ({ client });
|
|
56
|
-
|
|
57
65
|
/*
|
|
58
66
|
|
|
59
67
|
// the following is an example of how to use the hybrid api
|
|
@@ -1,50 +1,59 @@
|
|
|
1
1
|
/**
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
2
|
+
* Standard Async: {{functionName}}
|
|
3
|
+
* Used for manual actions. Invalidates the cache for this resource on success.
|
|
4
|
+
*/
|
|
5
|
+
const {{functionName}} = async (req: {{inputType}}): Promise<APIResponse<{{outputType}}>> => {
|
|
6
|
+
const res = await callConnect(client.value.{{functionName}}.bind(client.value), req, (res) => res);
|
|
7
|
+
if (!res.error) {
|
|
8
|
+
queryClient.invalidateQueries({ queryKey: ["{{resource}}"] });
|
|
9
|
+
}
|
|
10
|
+
return res;
|
|
11
|
+
};
|
|
9
12
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
{{
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
})
|
|
48
|
-
{
|
|
49
|
-
|
|
50
|
-
|
|
13
|
+
/**
|
|
14
|
+
* TanStack Hook: {{hookName}}
|
|
15
|
+
* Provides reactive data binding with caching.
|
|
16
|
+
*/
|
|
17
|
+
const {{hookName}} = (
|
|
18
|
+
{{#isQuery}}
|
|
19
|
+
input: any, // Accepts Ref<{{inputType}}> or {{inputType}}
|
|
20
|
+
options: any = {}
|
|
21
|
+
{{/isQuery}}
|
|
22
|
+
{{^isQuery}}
|
|
23
|
+
options: any = {}
|
|
24
|
+
{{/isQuery}}
|
|
25
|
+
) => {
|
|
26
|
+
{{#isPaginated}}
|
|
27
|
+
return useInfiniteQuery({
|
|
28
|
+
queryKey: ["{{resource}}", "{{functionName}}", input],
|
|
29
|
+
queryFn: async ({ pageParam }) => {
|
|
30
|
+
const req = { ...unref(input), page: pageParam };
|
|
31
|
+
return client.value.{{functionName}}(req);
|
|
32
|
+
},
|
|
33
|
+
initialPageParam: 1,
|
|
34
|
+
getNextPageParam: (lastPage: any) => lastPage.nextPage ?? undefined,
|
|
35
|
+
...options,
|
|
36
|
+
});
|
|
37
|
+
{{/isPaginated}}
|
|
38
|
+
|
|
39
|
+
{{^isPaginated}}
|
|
40
|
+
{{#isQuery}}
|
|
41
|
+
return useQuery({
|
|
42
|
+
queryKey: ["{{resource}}", "{{functionName}}", input],
|
|
43
|
+
queryFn: () => client.value.{{functionName}}(unref(input)),
|
|
44
|
+
...options,
|
|
45
|
+
});
|
|
46
|
+
{{/isQuery}}
|
|
47
|
+
|
|
48
|
+
{{^isQuery}}
|
|
49
|
+
return useMutation({
|
|
50
|
+
mutationFn: (req: {{inputType}}) => client.value.{{functionName}}(req),
|
|
51
|
+
onSuccess: async (data, variables, context) => {
|
|
52
|
+
await queryClient.invalidateQueries({ queryKey: ["{{resource}}"] });
|
|
53
|
+
if (options.onSuccess) return options.onSuccess(data, variables, context);
|
|
54
|
+
},
|
|
55
|
+
...options,
|
|
56
|
+
});
|
|
57
|
+
{{/isQuery}}
|
|
58
|
+
{{/isPaginated}}
|
|
59
|
+
};
|