@zachacious/protoc-gen-connect-vue 1.0.14 → 1.0.16
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/dist/generator.js +44 -20
- package/package.json +1 -1
- package/templates/rpc.ts.mustache +6 -14
package/dist/generator.js
CHANGED
|
@@ -336805,23 +336805,47 @@ var templates = {
|
|
|
336805
336805
|
rpc: fs.readFileSync(path.join(templateDir, "rpc.ts.mustache"), "utf-8"),
|
|
336806
336806
|
index: fs.readFileSync(path.join(templateDir, "index.ts.mustache"), "utf-8")
|
|
336807
336807
|
};
|
|
336808
|
-
function
|
|
336808
|
+
function findBestPaginationCandidate(msg, isRequest, depth = 0) {
|
|
336809
336809
|
if (depth > 3)
|
|
336810
336810
|
return null;
|
|
336811
|
-
|
|
336812
|
-
for (const f of msg.fields) {
|
|
336813
|
-
const normalized = f.name.toLowerCase().replace(/_/g, "");
|
|
336814
|
-
if (terminals.includes(normalized))
|
|
336815
|
-
return [f.name];
|
|
336816
|
-
}
|
|
336811
|
+
let best = null;
|
|
336817
336812
|
for (const f of msg.fields) {
|
|
336813
|
+
let currentScore = 0;
|
|
336814
|
+
const name = f.name.toLowerCase().replace(/_/g, "");
|
|
336815
|
+
const reqKeywords = ["token", "page", "offset", "cursor", "start", "skip"];
|
|
336816
|
+
const resKeywords = ["next", "token", "more", "hasmore", "cursor", "total"];
|
|
336817
|
+
const targets = isRequest ? reqKeywords : resKeywords;
|
|
336818
|
+
if (targets.some((t) => name.includes(t)))
|
|
336819
|
+
currentScore += 10;
|
|
336820
|
+
if (name.includes("pagetoken") || name.includes("nextpage"))
|
|
336821
|
+
currentScore += 15;
|
|
336822
|
+
const isString = f.fieldKind === "scalar" && f.scalar === 9;
|
|
336823
|
+
const isNumber = f.fieldKind === "scalar" && [3, 4, 5, 13, 17, 18].includes(f.scalar);
|
|
336824
|
+
if (isString || isNumber) {
|
|
336825
|
+
currentScore += 5;
|
|
336826
|
+
if (!best || currentScore > best.score) {
|
|
336827
|
+
best = {
|
|
336828
|
+
path: [f.name],
|
|
336829
|
+
type: isString ? "string" : "number",
|
|
336830
|
+
score: currentScore
|
|
336831
|
+
};
|
|
336832
|
+
}
|
|
336833
|
+
}
|
|
336818
336834
|
if (f.fieldKind === "message") {
|
|
336819
|
-
const
|
|
336820
|
-
if (
|
|
336821
|
-
|
|
336835
|
+
const nested = findBestPaginationCandidate(f.message, isRequest, depth + 1);
|
|
336836
|
+
if (nested) {
|
|
336837
|
+
const nestedScore = nested.score + 12;
|
|
336838
|
+
if (!best || nestedScore > best.score) {
|
|
336839
|
+
best = {
|
|
336840
|
+
path: [f.name, ...nested.path],
|
|
336841
|
+
type: nested.type,
|
|
336842
|
+
score: nestedScore
|
|
336843
|
+
};
|
|
336844
|
+
}
|
|
336845
|
+
}
|
|
336822
336846
|
}
|
|
336823
336847
|
}
|
|
336824
|
-
return
|
|
336848
|
+
return best;
|
|
336825
336849
|
}
|
|
336826
336850
|
function processService(service) {
|
|
336827
336851
|
const importMap = new Map;
|
|
@@ -336856,12 +336880,11 @@ function processService(service) {
|
|
|
336856
336880
|
"Set",
|
|
336857
336881
|
"Add"
|
|
336858
336882
|
];
|
|
336859
|
-
const
|
|
336860
|
-
const
|
|
336861
|
-
const
|
|
336862
|
-
const
|
|
336863
|
-
const
|
|
336864
|
-
const isPaginated = isQuery && !!reqPath && !!resPath;
|
|
336883
|
+
const isMutation = mutationVerbs.some((v) => name.startsWith(v));
|
|
336884
|
+
const isQuery = isUnary && !isMutation;
|
|
336885
|
+
const reqCandidate = findBestPaginationCandidate(m.input, true);
|
|
336886
|
+
const resCandidate = findBestPaginationCandidate(m.output, false);
|
|
336887
|
+
const isPaginated = isQuery && reqCandidate && resCandidate && reqCandidate.score + resCandidate.score > 25;
|
|
336865
336888
|
return {
|
|
336866
336889
|
functionName: name.charAt(0).toLowerCase() + name.slice(1),
|
|
336867
336890
|
hookName: `use${name}`,
|
|
@@ -336870,8 +336893,9 @@ function processService(service) {
|
|
|
336870
336893
|
outputType: m.output.name,
|
|
336871
336894
|
isQuery,
|
|
336872
336895
|
isPaginated,
|
|
336873
|
-
reqPath:
|
|
336874
|
-
resPath:
|
|
336896
|
+
reqPath: reqCandidate?.path.join("."),
|
|
336897
|
+
resPath: resCandidate?.path.join("."),
|
|
336898
|
+
pageType: reqCandidate?.type || "string"
|
|
336875
336899
|
};
|
|
336876
336900
|
});
|
|
336877
336901
|
return {
|
|
@@ -336888,7 +336912,7 @@ function processService(service) {
|
|
|
336888
336912
|
}
|
|
336889
336913
|
var plugin = createEcmaScriptPlugin({
|
|
336890
336914
|
name: "protoc-gen-connect-vue",
|
|
336891
|
-
version: "v1.
|
|
336915
|
+
version: "v1.4.0",
|
|
336892
336916
|
generateTs: (schema) => {
|
|
336893
336917
|
const service = schema.files.flatMap((f) => f.services)[0];
|
|
336894
336918
|
if (!service)
|
package/package.json
CHANGED
|
@@ -3,9 +3,7 @@
|
|
|
3
3
|
*/
|
|
4
4
|
const {{functionName}} = async (req: {{inputType}}): Promise<APIResponse<{{outputType}}>> => {
|
|
5
5
|
const res = await callConnect(client.value.{{functionName}}.bind(client.value), req, (res) => res);
|
|
6
|
-
if (!res.error) {
|
|
7
|
-
queryClient.invalidateQueries({ queryKey: ["{{resource}}"] });
|
|
8
|
-
}
|
|
6
|
+
if (!res.error) queryClient.invalidateQueries({ queryKey: ["{{resource}}"] });
|
|
9
7
|
return res;
|
|
10
8
|
};
|
|
11
9
|
|
|
@@ -13,13 +11,8 @@ const {{functionName}} = async (req: {{inputType}}): Promise<APIResponse<{{outpu
|
|
|
13
11
|
* Hook: {{hookName}}
|
|
14
12
|
*/
|
|
15
13
|
const {{hookName}} = (
|
|
16
|
-
{{#isQuery}}
|
|
17
|
-
|
|
18
|
-
options: any = {}
|
|
19
|
-
{{/isQuery}}
|
|
20
|
-
{{^isQuery}}
|
|
21
|
-
options: any = {}
|
|
22
|
-
{{/isQuery}}
|
|
14
|
+
{{#isQuery}}input: any, options: any = {}{{/isQuery}}
|
|
15
|
+
{{^isQuery}}options: any = {}{{/isQuery}}
|
|
23
16
|
) => {
|
|
24
17
|
{{#isPaginated}}
|
|
25
18
|
return useInfiniteQuery({
|
|
@@ -34,17 +27,16 @@ const {{hookName}} = (
|
|
|
34
27
|
curr = curr[path[i]];
|
|
35
28
|
}
|
|
36
29
|
curr[path[path.length - 1]] = pageParam;
|
|
37
|
-
|
|
38
30
|
return client.value.{{functionName}}(req);
|
|
39
31
|
},
|
|
40
|
-
initialPageParam: options.initialPageParam ?? 1,
|
|
32
|
+
initialPageParam: options.initialPageParam ?? ( "{{pageType}}" === "string" ? "" : 1 ),
|
|
41
33
|
getNextPageParam: (lastPage: any) => {
|
|
42
34
|
const path = "{{resPath}}".split('.');
|
|
43
35
|
const val = path.reduce((o, i) => o?.[i], lastPage);
|
|
44
36
|
|
|
37
|
+
// If the discovered path is an object (like PageResponse), look for standard keys
|
|
45
38
|
if (typeof val === 'object' && val !== null) {
|
|
46
|
-
|
|
47
|
-
return val.nextPageToken || val.nextPage || (val.pageNumber ? val.pageNumber + 1 : undefined);
|
|
39
|
+
return val.nextPageToken || val.nextPage || val.pageToken || undefined;
|
|
48
40
|
}
|
|
49
41
|
return val || undefined;
|
|
50
42
|
},
|