@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 CHANGED
@@ -59,7 +59,7 @@ plugins:
59
59
  out: gen
60
60
  opt: target=ts
61
61
  # 3. Smart SDK Generator
62
- - local: connect-vue
62
+ - local: protoc-gen-connect-vue
63
63
  out: src/api
64
64
  ```
65
65
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zachacious/protoc-gen-connect-vue",
3
- "version": "1.0.4",
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 { useQuery, useMutation, useInfiniteQuery, useQueryClient, useIsFetching, useIsMutating } from "@tanstack/vue-query";
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
- let baseUrl = 'http://localhost:3000';
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
- export const transport = createConnectTransport({
39
- baseUrl,
40
- interceptors: [transportInterceptor],
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
- export const client = createClient({{serviceName}}, transport);
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
- * Standard Async: {{functionName}}
3
- */
4
- const {{functionName}} = async (req: {{inputType}}): Promise<APIResponse<{{outputType}}>> => {
5
- const res = await callConnect(client.{{functionName}}, req, (res) => res);
6
- if (!res.error) queryClient.invalidateQueries({ queryKey: ["{{resource}}"] });
7
- return res;
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
- * TanStack Hook: {{hookName}}
12
- */
13
- const {{hookName}} = (
14
- {{#isQuery}}
15
- input: {{inputType}},
16
- options: any = {}
17
- {{/isQuery}}
18
- {{^isQuery}}
19
- options: any = {}
20
- {{/isQuery}}
21
- ) => {
22
- {{#isPaginated}}
23
- return useInfiniteQuery({
24
- ...{{queryDefinitionName}}Def,
25
- ...options,
26
- queryKey: ["{{resource}}", "{{functionName}}", input],
27
- initialPageParam: 1,
28
- getNextPageParam: options.getNextPageParam || ((lastPage: any) => lastPage.nextPage ?? undefined),
29
- });
30
- {{/isPaginated}}
31
- {{^isPaginated}}
32
- {{#isQuery}}
33
- return useQuery({
34
- ...{{queryDefinitionName}}Def,
35
- ...options,
36
- queryKey: ["{{resource}}", "{{functionName}}", input],
37
- });
38
- {{/isQuery}}
39
- {{^isQuery}}
40
- return useMutation({
41
- ...{{queryDefinitionName}}Def,
42
- ...options,
43
- onSuccess: async (data, variables, context) => {
44
- await queryClient.invalidateQueries({ queryKey: ["{{resource}}"] });
45
- if (options.onSuccess) return options.onSuccess(data, variables, context);
46
- },
47
- });
48
- {{/isQuery}}
49
- {{/isPaginated}}
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
+ };