@ahoo-wang/fetcher-generator 2.10.0 → 2.10.2
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/cli.cjs +1 -1
- package/dist/cli.js +1 -1
- package/dist/client/apiClientGenerator.d.ts.map +1 -1
- package/dist/index.cjs +4 -4
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +71 -62
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Scope as le, VariableDeclarationKind as O, Project as pe } from "ts-morph";
|
|
2
2
|
import { ContentTypeValues as S, combineURLs as E } from "@ahoo-wang/fetcher";
|
|
3
|
-
import { ResourceAttributionPathSpec as
|
|
4
|
-
import { parse as
|
|
3
|
+
import { ResourceAttributionPathSpec as G } from "@ahoo-wang/fetcher-wow";
|
|
4
|
+
import { parse as J } from "yaml";
|
|
5
5
|
import { readFile as me } from "fs";
|
|
6
6
|
import { join as de, relative as fe } from "path";
|
|
7
7
|
function $(o) {
|
|
@@ -11,11 +11,11 @@ function v(o, e) {
|
|
|
11
11
|
const t = $(o);
|
|
12
12
|
return e.schemas?.[t];
|
|
13
13
|
}
|
|
14
|
-
function
|
|
14
|
+
function V(o, e) {
|
|
15
15
|
const t = $(o);
|
|
16
16
|
return e.requestBodies?.[t];
|
|
17
17
|
}
|
|
18
|
-
function
|
|
18
|
+
function K(o, e) {
|
|
19
19
|
const t = $(o);
|
|
20
20
|
return e.parameters?.[t];
|
|
21
21
|
}
|
|
@@ -25,11 +25,11 @@ function I(o, e) {
|
|
|
25
25
|
schema: v(o, e)
|
|
26
26
|
};
|
|
27
27
|
}
|
|
28
|
-
const
|
|
29
|
-
function
|
|
30
|
-
return Array.isArray(o) ? o.flatMap((e) =>
|
|
28
|
+
const W = /[-_\s.]+/;
|
|
29
|
+
function H(o) {
|
|
30
|
+
return Array.isArray(o) ? o.flatMap((e) => z(e.split(W))) : z(o.split(W));
|
|
31
31
|
}
|
|
32
|
-
function
|
|
32
|
+
function z(o) {
|
|
33
33
|
return o.flatMap((e) => {
|
|
34
34
|
if (e.length === 0)
|
|
35
35
|
return [];
|
|
@@ -43,7 +43,7 @@ function W(o) {
|
|
|
43
43
|
});
|
|
44
44
|
}
|
|
45
45
|
function x(o) {
|
|
46
|
-
return o === "" || o.length === 0 ? "" :
|
|
46
|
+
return o === "" || o.length === 0 ? "" : H(o).filter((t) => t.length > 0).map((t) => {
|
|
47
47
|
const n = t.charAt(0), r = t.slice(1);
|
|
48
48
|
return (/[a-zA-Z]/.test(n) ? n.toUpperCase() : n) + r.toLowerCase();
|
|
49
49
|
}).join("");
|
|
@@ -53,23 +53,23 @@ function h(o) {
|
|
|
53
53
|
return e.charAt(0).toLowerCase() + e.slice(1);
|
|
54
54
|
}
|
|
55
55
|
function he(o) {
|
|
56
|
-
return o === "" || Array.isArray(o) && o.length === 0 ? "" :
|
|
56
|
+
return o === "" || Array.isArray(o) && o.length === 0 ? "" : H(o).filter((t) => t.length > 0).map((t) => t.toUpperCase()).join("_");
|
|
57
57
|
}
|
|
58
58
|
function g(o) {
|
|
59
59
|
return !!(o && typeof o == "object" && "$ref" in o);
|
|
60
60
|
}
|
|
61
|
-
function
|
|
61
|
+
function q(o, e) {
|
|
62
62
|
if (e && !g(e) && e.content)
|
|
63
63
|
return e.content[o]?.schema;
|
|
64
64
|
}
|
|
65
|
-
function
|
|
66
|
-
return
|
|
65
|
+
function Y(o) {
|
|
66
|
+
return q(S.APPLICATION_JSON, o);
|
|
67
67
|
}
|
|
68
68
|
function ye(o) {
|
|
69
|
-
return
|
|
69
|
+
return q(S.TEXT_EVENT_STREAM, o);
|
|
70
70
|
}
|
|
71
71
|
function xe(o) {
|
|
72
|
-
return
|
|
72
|
+
return q("*/*", o);
|
|
73
73
|
}
|
|
74
74
|
const Ae = [
|
|
75
75
|
"string",
|
|
@@ -78,7 +78,7 @@ const Ae = [
|
|
|
78
78
|
"boolean",
|
|
79
79
|
"null"
|
|
80
80
|
];
|
|
81
|
-
function
|
|
81
|
+
function j(o) {
|
|
82
82
|
return Array.isArray(o) ? !0 : Ae.includes(o);
|
|
83
83
|
}
|
|
84
84
|
function N(o) {
|
|
@@ -87,22 +87,22 @@ function N(o) {
|
|
|
87
87
|
function Ce(o) {
|
|
88
88
|
return Array.isArray(o.enum) && o.enum.length > 0;
|
|
89
89
|
}
|
|
90
|
-
function
|
|
90
|
+
function X(o) {
|
|
91
91
|
return Array.isArray(o.anyOf) && o.anyOf.length > 0;
|
|
92
92
|
}
|
|
93
|
-
function
|
|
93
|
+
function Z(o) {
|
|
94
94
|
return Array.isArray(o.oneOf) && o.oneOf.length > 0;
|
|
95
95
|
}
|
|
96
96
|
function Pe(o) {
|
|
97
|
-
return
|
|
97
|
+
return X(o) || Z(o);
|
|
98
98
|
}
|
|
99
99
|
function $e(o) {
|
|
100
100
|
return Array.isArray(o.allOf) && o.allOf.length > 0;
|
|
101
101
|
}
|
|
102
|
-
function z(o) {
|
|
103
|
-
return Y(o) || X(o) || $e(o);
|
|
104
|
-
}
|
|
105
102
|
function L(o) {
|
|
103
|
+
return X(o) || Z(o) || $e(o);
|
|
104
|
+
}
|
|
105
|
+
function B(o) {
|
|
106
106
|
return o.includes("|") || o.includes("&") ? `(${o})[]` : `${o}[]`;
|
|
107
107
|
}
|
|
108
108
|
function Ie(o) {
|
|
@@ -125,7 +125,7 @@ function A(o) {
|
|
|
125
125
|
return "any";
|
|
126
126
|
}
|
|
127
127
|
}
|
|
128
|
-
function
|
|
128
|
+
function D(o) {
|
|
129
129
|
return [
|
|
130
130
|
{ method: "get", operation: o.get },
|
|
131
131
|
{ method: "put", operation: o.put },
|
|
@@ -137,19 +137,19 @@ function Z(o) {
|
|
|
137
137
|
{ method: "trace", operation: o.trace }
|
|
138
138
|
].filter(({ operation: e }) => e !== void 0);
|
|
139
139
|
}
|
|
140
|
-
function
|
|
140
|
+
function _(o) {
|
|
141
141
|
return o.responses[200];
|
|
142
142
|
}
|
|
143
|
-
function
|
|
144
|
-
const e =
|
|
145
|
-
return
|
|
143
|
+
function k(o) {
|
|
144
|
+
const e = _(o);
|
|
145
|
+
return Y(e);
|
|
146
146
|
}
|
|
147
147
|
function Te(o, e) {
|
|
148
|
-
return o.parameters ? o.parameters.map((t) => g(t) ?
|
|
148
|
+
return o.parameters ? o.parameters.map((t) => g(t) ? K(t, e) : t).filter((t) => t.in === "path") : [];
|
|
149
149
|
}
|
|
150
150
|
const Se = "string";
|
|
151
151
|
function ee(o) {
|
|
152
|
-
return !o.schema || g(o.schema) || !o.schema.type || !
|
|
152
|
+
return !o.schema || g(o.schema) || !o.schema.type || !j(o.schema.type) ? Se : A(o.schema.type);
|
|
153
153
|
}
|
|
154
154
|
function te(o) {
|
|
155
155
|
return o.startsWith("http://") || o.startsWith("https://") ? Ee(o) : ve(o);
|
|
@@ -170,7 +170,7 @@ async function Re(o) {
|
|
|
170
170
|
case "json":
|
|
171
171
|
return JSON.parse(e);
|
|
172
172
|
case "yaml":
|
|
173
|
-
return
|
|
173
|
+
return J(e);
|
|
174
174
|
default:
|
|
175
175
|
throw new Error(`Unsupported file format: ${o}`);
|
|
176
176
|
}
|
|
@@ -181,7 +181,7 @@ async function we(o) {
|
|
|
181
181
|
case "json":
|
|
182
182
|
return JSON.parse(e);
|
|
183
183
|
case "yaml":
|
|
184
|
-
return
|
|
184
|
+
return J(e);
|
|
185
185
|
default:
|
|
186
186
|
throw new Error(`Unsupported file format: ${o}`);
|
|
187
187
|
}
|
|
@@ -252,9 +252,9 @@ function w(o, e) {
|
|
|
252
252
|
e.title,
|
|
253
253
|
e.description
|
|
254
254
|
];
|
|
255
|
-
e.format && t.push(`- format: ${e.format}`),
|
|
255
|
+
e.format && t.push(`- format: ${e.format}`), Q(t, e, "default"), Q(t, e, "example"), be(t, e), Me(t, e), qe(t, e), R(o, t);
|
|
256
256
|
}
|
|
257
|
-
function
|
|
257
|
+
function Q(o, e, t) {
|
|
258
258
|
const n = e[t];
|
|
259
259
|
if (n) {
|
|
260
260
|
if (typeof n != "object") {
|
|
@@ -327,7 +327,7 @@ class Le {
|
|
|
327
327
|
*/
|
|
328
328
|
build() {
|
|
329
329
|
for (const [e, t] of Object.entries(this.openAPI.paths)) {
|
|
330
|
-
const n =
|
|
330
|
+
const n = D(t);
|
|
331
331
|
for (const r of n)
|
|
332
332
|
this.commands(e, r), this.state(r.operation), this.events(r.operation), this.fields(r.operation);
|
|
333
333
|
}
|
|
@@ -359,14 +359,14 @@ class Le {
|
|
|
359
359
|
const r = Ge(n.operationId);
|
|
360
360
|
if (!r)
|
|
361
361
|
return;
|
|
362
|
-
const i =
|
|
362
|
+
const i = _(n);
|
|
363
363
|
if (!i || !g(i) || i.$ref !== We || !n.requestBody)
|
|
364
364
|
return;
|
|
365
365
|
const s = n.parameters ?? [], a = s.filter((m) => g(m) && m.$ref === ze).at(0), c = s.filter(
|
|
366
366
|
(m) => !g(m) && m.in === "path"
|
|
367
367
|
);
|
|
368
368
|
if (a) {
|
|
369
|
-
const m =
|
|
369
|
+
const m = K(
|
|
370
370
|
a,
|
|
371
371
|
this.openAPI.components
|
|
372
372
|
);
|
|
@@ -388,8 +388,8 @@ class Le {
|
|
|
388
388
|
operation: n
|
|
389
389
|
};
|
|
390
390
|
n.tags?.forEach((m) => {
|
|
391
|
-
const
|
|
392
|
-
|
|
391
|
+
const F = this.aggregates.get(m);
|
|
392
|
+
F && F.commands.set(r, ue);
|
|
393
393
|
});
|
|
394
394
|
}
|
|
395
395
|
/**
|
|
@@ -399,7 +399,7 @@ class Le {
|
|
|
399
399
|
state(e) {
|
|
400
400
|
if (!e.operationId?.endsWith(".snapshot_state.single"))
|
|
401
401
|
return;
|
|
402
|
-
const t =
|
|
402
|
+
const t = k(e);
|
|
403
403
|
if (!g(t))
|
|
404
404
|
return;
|
|
405
405
|
const n = I(
|
|
@@ -418,7 +418,7 @@ class Le {
|
|
|
418
418
|
events(e) {
|
|
419
419
|
if (!this.openAPI.components || !e.operationId?.endsWith(".event.list_query"))
|
|
420
420
|
return;
|
|
421
|
-
const t =
|
|
421
|
+
const t = k(e);
|
|
422
422
|
if (g(t))
|
|
423
423
|
return;
|
|
424
424
|
const n = t?.items;
|
|
@@ -452,7 +452,7 @@ class Le {
|
|
|
452
452
|
fields(e) {
|
|
453
453
|
if (!this.openAPI.components || !e.operationId?.endsWith(".snapshot.count"))
|
|
454
454
|
return;
|
|
455
|
-
const n =
|
|
455
|
+
const n = V(
|
|
456
456
|
e.requestBody,
|
|
457
457
|
this.openAPI.components
|
|
458
458
|
).content[S.APPLICATION_JSON].schema, i = v(
|
|
@@ -616,7 +616,7 @@ class ke {
|
|
|
616
616
|
i
|
|
617
617
|
), t.addTypeAlias({
|
|
618
618
|
name: e.name,
|
|
619
|
-
type:
|
|
619
|
+
type: B(i.name),
|
|
620
620
|
isExported: !0
|
|
621
621
|
});
|
|
622
622
|
}
|
|
@@ -629,7 +629,7 @@ class ke {
|
|
|
629
629
|
e,
|
|
630
630
|
n,
|
|
631
631
|
r
|
|
632
|
-
) : (
|
|
632
|
+
) : (L(n) && (n.anyOf || n.oneOf || n.allOf).forEach((s) => {
|
|
633
633
|
if (g(s)) {
|
|
634
634
|
const a = y(s);
|
|
635
635
|
T(
|
|
@@ -695,11 +695,11 @@ class ke {
|
|
|
695
695
|
n,
|
|
696
696
|
r.items
|
|
697
697
|
);
|
|
698
|
-
return
|
|
698
|
+
return B(i);
|
|
699
699
|
}
|
|
700
|
-
if (r.type &&
|
|
700
|
+
if (r.type && j(r.type))
|
|
701
701
|
return A(r.type);
|
|
702
|
-
if (
|
|
702
|
+
if (L(r))
|
|
703
703
|
return this.resolvePropertyCompositionType(
|
|
704
704
|
e,
|
|
705
705
|
t,
|
|
@@ -754,10 +754,10 @@ const Qe = "@ahoo-wang/fetcher-decorator", Ue = [
|
|
|
754
754
|
], Je = {
|
|
755
755
|
type: "Promise<Response>",
|
|
756
756
|
metadata: "{resultExtractor: ResultExtractors.Response }"
|
|
757
|
-
},
|
|
757
|
+
}, U = {
|
|
758
758
|
type: "Promise<string>",
|
|
759
759
|
metadata: "{resultExtractor: ResultExtractors.Text }"
|
|
760
|
-
},
|
|
760
|
+
}, b = `{
|
|
761
761
|
headers: { Accept: ContentTypeValues.TEXT_EVENT_STREAM },
|
|
762
762
|
resultExtractor: JsonEventStreamResultExtractor,
|
|
763
763
|
}`;
|
|
@@ -797,7 +797,7 @@ function ce(o) {
|
|
|
797
797
|
function Ke(o) {
|
|
798
798
|
let e = 0, t = 0;
|
|
799
799
|
return o.commands.forEach((n) => {
|
|
800
|
-
n.path.startsWith(
|
|
800
|
+
n.path.startsWith(G.TENANT) && (e += 1), n.path.startsWith(G.OWNER) && (t += 1);
|
|
801
801
|
}), e === 0 && t === 0 ? "ResourceAttributionPathSpec.NONE" : e > t ? "ResourceAttributionPathSpec.TENANT" : "ResourceAttributionPathSpec.OWNER";
|
|
802
802
|
}
|
|
803
803
|
function ge(o, e, t, n) {
|
|
@@ -807,7 +807,7 @@ function ge(o, e, t, n) {
|
|
|
807
807
|
function He(o, e) {
|
|
808
808
|
return `${x(o.aggregateName)}${e}`;
|
|
809
809
|
}
|
|
810
|
-
function
|
|
810
|
+
function M(o) {
|
|
811
811
|
return o === "delete" ? "del" : o;
|
|
812
812
|
}
|
|
813
813
|
class Ye {
|
|
@@ -918,7 +918,7 @@ class Ye {
|
|
|
918
918
|
let n;
|
|
919
919
|
if (g(t.requestBody) ? (this.context.logger.info(
|
|
920
920
|
`Extracting request body from reference for operation: ${t.operationId}`
|
|
921
|
-
), n =
|
|
921
|
+
), n = V(
|
|
922
922
|
t.requestBody,
|
|
923
923
|
this.context.openAPI.components
|
|
924
924
|
)) : n = t.requestBody, !n)
|
|
@@ -1021,7 +1021,7 @@ class Ye {
|
|
|
1021
1021
|
return this.context.logger.info(
|
|
1022
1022
|
`Schema has no type, using default return type: ${n}`
|
|
1023
1023
|
), n;
|
|
1024
|
-
if (
|
|
1024
|
+
if (j(t.type)) {
|
|
1025
1025
|
const i = `Promise<${A(t.type)}>`;
|
|
1026
1026
|
return this.context.logger.info(`Resolved primitive return type: ${i}`), i;
|
|
1027
1027
|
}
|
|
@@ -1036,19 +1036,19 @@ class Ye {
|
|
|
1036
1036
|
* @returns Object containing type and optional stream flag
|
|
1037
1037
|
*/
|
|
1038
1038
|
resolveReturnType(e, t) {
|
|
1039
|
-
const n =
|
|
1039
|
+
const n = _(t);
|
|
1040
1040
|
if (!n)
|
|
1041
1041
|
return this.context.logger.info(
|
|
1042
1042
|
`No OK response found for operation ${t.operationId}, using default return type: ${this.defaultReturnType.type}`
|
|
1043
1043
|
), this.defaultReturnType;
|
|
1044
|
-
const r =
|
|
1044
|
+
const r = Y(n) || xe(n);
|
|
1045
1045
|
if (r) {
|
|
1046
1046
|
const s = this.resolveSchemaReturnType(e, r);
|
|
1047
1047
|
return this.context.logger.info(
|
|
1048
1048
|
`Resolved JSON/wildcard response return type for operation ${t.operationId}: ${s}`
|
|
1049
1049
|
), {
|
|
1050
1050
|
type: s,
|
|
1051
|
-
metadata: s ===
|
|
1051
|
+
metadata: s === U.type ? U.metadata : void 0
|
|
1052
1052
|
};
|
|
1053
1053
|
}
|
|
1054
1054
|
const i = ye(n);
|
|
@@ -1068,14 +1068,14 @@ class Ye {
|
|
|
1068
1068
|
`Resolved event stream return type for operation ${t.operationId}: ${l}`
|
|
1069
1069
|
), {
|
|
1070
1070
|
type: l,
|
|
1071
|
-
metadata:
|
|
1071
|
+
metadata: b
|
|
1072
1072
|
};
|
|
1073
1073
|
}
|
|
1074
1074
|
}
|
|
1075
1075
|
const s = "Promise<JsonServerSentEventStream<any>>";
|
|
1076
1076
|
return this.context.logger.info(
|
|
1077
1077
|
`Resolved generic event stream return type for operation ${t.operationId}: ${s}`
|
|
1078
|
-
), { type: s, metadata:
|
|
1078
|
+
), { type: s, metadata: b };
|
|
1079
1079
|
}
|
|
1080
1080
|
return this.context.logger.info(
|
|
1081
1081
|
`Using default return type for operation ${t.operationId}: ${this.defaultReturnType.type}`
|
|
@@ -1099,10 +1099,10 @@ class Ye {
|
|
|
1099
1099
|
t,
|
|
1100
1100
|
r.operation
|
|
1101
1101
|
), a = this.resolveReturnType(t, r.operation), c = a.metadata ? {
|
|
1102
|
-
name:
|
|
1102
|
+
name: M(r.method),
|
|
1103
1103
|
arguments: [`'${r.path}'`, a.metadata]
|
|
1104
1104
|
} : {
|
|
1105
|
-
name:
|
|
1105
|
+
name: M(r.method),
|
|
1106
1106
|
arguments: [`'${r.path}'`]
|
|
1107
1107
|
};
|
|
1108
1108
|
this.context.logger.info(
|
|
@@ -1136,7 +1136,7 @@ class Ye {
|
|
|
1136
1136
|
const t = /* @__PURE__ */ new Map();
|
|
1137
1137
|
let n = 0;
|
|
1138
1138
|
for (const [r, i] of Object.entries(this.context.openAPI.paths)) {
|
|
1139
|
-
const s =
|
|
1139
|
+
const s = D(i).filter(
|
|
1140
1140
|
(a) => {
|
|
1141
1141
|
if (!a.operation.operationId)
|
|
1142
1142
|
return !1;
|
|
@@ -1170,6 +1170,15 @@ class Ye {
|
|
|
1170
1170
|
"Resolving API client tags from OpenAPI specification"
|
|
1171
1171
|
);
|
|
1172
1172
|
const e = /* @__PURE__ */ new Map(), t = this.context.openAPI.tags?.length || 0;
|
|
1173
|
+
for (const r of Object.values(this.context.openAPI.paths))
|
|
1174
|
+
D(r).forEach((i) => {
|
|
1175
|
+
i.operation.tags?.forEach((s) => {
|
|
1176
|
+
e.has(s) || e.set(s, {
|
|
1177
|
+
name: s,
|
|
1178
|
+
description: ""
|
|
1179
|
+
});
|
|
1180
|
+
});
|
|
1181
|
+
});
|
|
1173
1182
|
let n = 0;
|
|
1174
1183
|
return this.context.openAPI.tags?.forEach((r) => {
|
|
1175
1184
|
r.name != "wow" && r.name != "Actuator" && !this.isAggregateTag(r) ? (e.set(r.name, r), n++, this.context.logger.info(`Included API client tag: ${r.name}`)) : this.context.logger.info(
|
|
@@ -1296,7 +1305,7 @@ class Xe {
|
|
|
1296
1305
|
let r = "CommandClient", i = [], s = "Promise<CommandResult>";
|
|
1297
1306
|
n && (r = "Stream" + r, i = [
|
|
1298
1307
|
"''",
|
|
1299
|
-
|
|
1308
|
+
b
|
|
1300
1309
|
], s = "Promise<CommandResultEventStream>");
|
|
1301
1310
|
const a = He(
|
|
1302
1311
|
t.aggregate,
|
|
@@ -1366,7 +1375,7 @@ class Xe {
|
|
|
1366
1375
|
name: h(r.name),
|
|
1367
1376
|
decorators: [
|
|
1368
1377
|
{
|
|
1369
|
-
name:
|
|
1378
|
+
name: M(r.method),
|
|
1370
1379
|
arguments: [`${this.getEndpointPath(r)}`]
|
|
1371
1380
|
}
|
|
1372
1381
|
],
|