@ahoo-wang/fetcher-generator 2.2.5 → 2.2.8

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/index.js CHANGED
@@ -1,11 +1,11 @@
1
- import { VariableDeclarationKind as O, Scope as Q } from "ts-morph";
2
- import { parse as k } from "yaml";
1
+ import { VariableDeclarationKind as E, Scope as k } from "ts-morph";
2
+ import { parse as Q } from "yaml";
3
3
  import * as v from "fs";
4
4
  import { readFile as L } from "fs";
5
5
  import { ContentTypeValues as S, combineURLs as q } from "@ahoo-wang/fetcher";
6
6
  import * as K from "path";
7
7
  import { join as U, relative as J } from "path";
8
- import { ResourceAttributionPathSpec as R } from "@ahoo-wang/fetcher-wow";
8
+ import { ResourceAttributionPathSpec as b } from "@ahoo-wang/fetcher-wow";
9
9
  function f(n) {
10
10
  return n.$ref.split("/").pop();
11
11
  }
@@ -27,12 +27,12 @@ function C(n, e) {
27
27
  schema: I(n, e)
28
28
  };
29
29
  }
30
- const b = /[-_\s.]+|(?=[A-Z])/;
30
+ const R = /[-_\s.]+|(?=[A-Z])/;
31
31
  function w(n) {
32
32
  if (n === "" || n.length === 0)
33
33
  return "";
34
34
  let e;
35
- return Array.isArray(n) ? e = n.flatMap((t) => t.split(b)) : e = n.split(b), e.filter((t) => t.length > 0).map((t) => {
35
+ return Array.isArray(n) ? e = n.flatMap((t) => t.split(R)) : e = n.split(R), e.filter((t) => t.length > 0).map((t) => {
36
36
  if (t.length === 0) return "";
37
37
  const o = t.charAt(0), r = t.slice(1);
38
38
  return (/[a-zA-Z]/.test(o) ? o.toUpperCase() : o) + r.toLowerCase();
@@ -61,7 +61,7 @@ async function ee(n) {
61
61
  case "json":
62
62
  return JSON.parse(e);
63
63
  case "yaml":
64
- return k(e);
64
+ return Q(e);
65
65
  default:
66
66
  throw new Error(`Unsupported file format: ${n}`);
67
67
  }
@@ -101,7 +101,7 @@ function ne(n) {
101
101
  function G(n) {
102
102
  return n.responses[200];
103
103
  }
104
- function M(n) {
104
+ function j(n) {
105
105
  const e = G(n);
106
106
  return oe(e);
107
107
  }
@@ -133,12 +133,15 @@ function ce(n) {
133
133
  function ge(n) {
134
134
  return Array.isArray(n.allOf) && n.allOf.length > 0;
135
135
  }
136
- function j(n) {
136
+ function M(n) {
137
137
  return F(n) || W(n) || ge(n);
138
138
  }
139
139
  function le(n) {
140
140
  return n.includes("|") || n.includes("&") ? `(${n})[]` : `${n}[]`;
141
141
  }
142
+ function pe(n) {
143
+ return n.type !== "object" ? !1 : n.properties ? Object.keys(n.properties).length === 0 : !0;
144
+ }
142
145
  function N(n) {
143
146
  if (Array.isArray(n))
144
147
  return n.map((e) => N(e)).join(" | ");
@@ -156,8 +159,8 @@ function N(n) {
156
159
  return "any";
157
160
  }
158
161
  }
159
- const _ = "types.ts", pe = "@";
160
- function me(n) {
162
+ const _ = "types.ts", me = "@";
163
+ function ue(n) {
161
164
  return q(n.path, _);
162
165
  }
163
166
  function T(n, e, t) {
@@ -179,7 +182,7 @@ function $(n, e, t) {
179
182
  });
180
183
  }
181
184
  function y(n, e, t) {
182
- if (t.path.startsWith(pe)) {
185
+ if (t.path.startsWith(me)) {
183
186
  $(n, t.path, [t.name]);
184
187
  return;
185
188
  }
@@ -187,7 +190,7 @@ function y(n, e, t) {
187
190
  let s = J(o, r);
188
191
  s = s.replace(/\.ts$/, ""), s.startsWith(".") || (s = "./" + s), $(n, s, [t.name]);
189
192
  }
190
- function E(n, e, t, o) {
193
+ function O(n, e, t, o) {
191
194
  n.path !== o.path && y(e, t, o);
192
195
  }
193
196
  function de(n, e) {
@@ -203,20 +206,20 @@ function P(n, e, t) {
203
206
  description: o
204
207
  });
205
208
  }
206
- function ue(n) {
209
+ function fe(n) {
207
210
  const e = n.split(".");
208
211
  return e.length != 2 || e[0].length === 0 || e[1].length === 0 ? null : e;
209
212
  }
210
- function fe(n) {
211
- const e = ue(n.name);
213
+ function he(n) {
214
+ const e = fe(n.name);
212
215
  return e ? {
213
216
  tag: n,
214
217
  contextAlias: e[0],
215
218
  aggregateName: e[1]
216
219
  } : null;
217
220
  }
218
- function he(n) {
219
- const e = n?.map((o) => fe(o)).filter((o) => o !== null);
221
+ function ye(n) {
222
+ const e = n?.map((o) => he(o)).filter((o) => o !== null);
220
223
  if (!e)
221
224
  return /* @__PURE__ */ new Map();
222
225
  const t = /* @__PURE__ */ new Map();
@@ -228,20 +231,20 @@ function he(n) {
228
231
  });
229
232
  }), t;
230
233
  }
231
- function ye(n) {
234
+ function Ae(n) {
232
235
  if (!n)
233
236
  return null;
234
237
  const e = n.split(".");
235
238
  return e.length != 3 ? null : e[2];
236
239
  }
237
- const Ae = "#/components/responses/wow.CommandOk", Ce = "#/components/parameters/wow.id";
238
- class Pe {
240
+ const Ce = "#/components/responses/wow.CommandOk", Pe = "#/components/parameters/wow.id";
241
+ class $e {
239
242
  /**
240
243
  * Creates a new AggregateResolver instance.
241
244
  * @param openAPI - The OpenAPI specification to resolve aggregates from
242
245
  */
243
246
  constructor(e) {
244
- this.openAPI = e, this.aggregates = he(e.tags), this.build();
247
+ this.openAPI = e, this.aggregates = ye(e.tags), this.build();
245
248
  }
246
249
  aggregates;
247
250
  /**
@@ -279,21 +282,21 @@ class Pe {
279
282
  const o = t.operation;
280
283
  if (o.operationId === "wow.command.send")
281
284
  return;
282
- const r = ye(o.operationId);
285
+ const r = Ae(o.operationId);
283
286
  if (!r)
284
287
  return;
285
288
  const s = G(o);
286
- if (!s || !g(s) || s.$ref !== Ae || !o.requestBody)
289
+ if (!s || !g(s) || s.$ref !== Ce || !o.requestBody)
287
290
  return;
288
- const a = o.parameters ?? [], c = a.filter((d) => g(d) && d.$ref === Ce).at(0), i = a.filter(
289
- (d) => !g(d) && d.in === "path"
291
+ const a = o.parameters ?? [], c = a.filter((u) => g(u) && u.$ref === Pe).at(0), i = a.filter(
292
+ (u) => !g(u) && u.in === "path"
290
293
  );
291
294
  if (c) {
292
- const d = H(
295
+ const u = H(
293
296
  c,
294
297
  this.openAPI.components
295
298
  );
296
- i.push(d);
299
+ i.push(u);
297
300
  }
298
301
  const m = o.requestBody.content[S.APPLICATION_JSON].schema, l = C(
299
302
  m,
@@ -310,8 +313,8 @@ class Pe {
310
313
  schema: l,
311
314
  operation: o
312
315
  };
313
- o.tags?.forEach((d) => {
314
- const D = this.aggregates.get(d);
316
+ o.tags?.forEach((u) => {
317
+ const D = this.aggregates.get(u);
315
318
  D && D.commands.set(r, z);
316
319
  });
317
320
  }
@@ -322,7 +325,7 @@ class Pe {
322
325
  state(e) {
323
326
  if (!e.operationId?.endsWith(".snapshot_state.single"))
324
327
  return;
325
- const t = M(e);
328
+ const t = j(e);
326
329
  if (!g(t))
327
330
  return;
328
331
  const o = C(
@@ -341,7 +344,7 @@ class Pe {
341
344
  events(e) {
342
345
  if (!this.openAPI.components || !e.operationId?.endsWith(".event.list_query"))
343
346
  return;
344
- const t = M(e);
347
+ const t = j(e);
345
348
  if (g(t))
346
349
  return;
347
350
  const o = t?.items;
@@ -388,7 +391,7 @@ class Pe {
388
391
  });
389
392
  }
390
393
  }
391
- const A = "@ahoo-wang/fetcher-wow", $e = {
394
+ const A = "@ahoo-wang/fetcher-wow", we = {
392
395
  "wow.command.CommandResult": "CommandResult",
393
396
  "wow.MessageHeaderSqlType": "MessageHeaderSqlType",
394
397
  "wow.api.BindingError": "BindingError",
@@ -417,10 +420,10 @@ const A = "@ahoo-wang/fetcher-wow", $e = {
417
420
  "wow.openapi.BatchResult": "BatchResult",
418
421
  "wow.messaging.CompensationTarget": "CompensationTarget"
419
422
  };
420
- function u(n) {
423
+ function d(n) {
421
424
  if (!n)
422
425
  return { name: "", path: "/" };
423
- const e = $e[n];
426
+ const e = we[n];
424
427
  if (e)
425
428
  return { name: e, path: A };
426
429
  const t = n.split(".");
@@ -454,12 +457,12 @@ class x {
454
457
  this.project = e.project, this.openAPI = e.openAPI, this.outputDir = e.outputDir, this.contextAggregates = e.contextAggregates, this.logger = e.logger;
455
458
  }
456
459
  }
457
- class we extends x {
460
+ class xe extends x {
458
461
  constructor(e) {
459
462
  super(e);
460
463
  }
461
464
  getOrCreateSourceFile(e) {
462
- const t = me(e);
465
+ const t = ue(e);
463
466
  return T(this.project, this.outputDir, t);
464
467
  }
465
468
  /**
@@ -514,7 +517,7 @@ class we extends x {
514
517
  * 4. Type alias processing
515
518
  */
516
519
  generateKeyedSchema({ key: e, schema: t }) {
517
- const o = u(e), r = this.getOrCreateSourceFile(o), s = this.process(o, r, t);
520
+ const o = d(e), r = this.getOrCreateSourceFile(o), s = this.process(o, r, t);
518
521
  s && P(s, t.title, t.description);
519
522
  }
520
523
  process(e, t, o) {
@@ -536,12 +539,12 @@ class we extends x {
536
539
  e,
537
540
  o,
538
541
  r
539
- ) : (j(o) && (o.anyOf || o.oneOf || o.allOf).forEach((a) => {
542
+ ) : (M(o) && (o.anyOf || o.oneOf || o.allOf).forEach((a) => {
540
543
  if (g(a)) {
541
- const c = u(
544
+ const c = d(
542
545
  f(a)
543
546
  );
544
- E(
547
+ O(
545
548
  e,
546
549
  t,
547
550
  this.outputDir,
@@ -580,15 +583,16 @@ class we extends x {
580
583
  let i = r.getProperty(s);
581
584
  i ? i.setType(c) : i = r.addProperty({
582
585
  name: s,
583
- type: c
586
+ type: c,
587
+ hasQuestionToken: !o.required?.includes(s)
584
588
  }), g(a) || P(i, a.title, a.description);
585
589
  }
586
590
  return r;
587
591
  }
588
592
  resolvePropertyType(e, t, o, r) {
589
593
  if (g(r)) {
590
- const s = u(f(r));
591
- return E(
594
+ const s = d(f(r));
595
+ return O(
592
596
  e,
593
597
  t,
594
598
  this.outputDir,
@@ -608,7 +612,7 @@ class we extends x {
608
612
  }
609
613
  if (r.type && se(r.type))
610
614
  return N(r.type);
611
- if (j(r))
615
+ if (M(r))
612
616
  return this.resolvePropertyCompositionType(
613
617
  e,
614
618
  t,
@@ -631,10 +635,10 @@ class we extends x {
631
635
  const r = o.anyOf || o.oneOf || o.allOf, s = /* @__PURE__ */ new Set();
632
636
  r.forEach((c) => {
633
637
  if (g(c)) {
634
- const i = u(
638
+ const i = d(
635
639
  f(c)
636
640
  );
637
- E(
641
+ O(
638
642
  e,
639
643
  t,
640
644
  this.outputDir,
@@ -648,10 +652,10 @@ class we extends x {
648
652
  return Array.from(s).join(a);
649
653
  }
650
654
  }
651
- function xe(n) {
655
+ function Oe(n) {
652
656
  let e = 0, t = 0;
653
657
  return n.commands.forEach((o) => {
654
- o.path.startsWith(R.TENANT) && (e += 1), o.path.startsWith(R.OWNER) && (t += 1);
658
+ o.path.startsWith(b.TENANT) && (e += 1), o.path.startsWith(b.OWNER) && (t += 1);
655
659
  }), e === 0 && t === 0 ? "ResourceAttributionPathSpec.NONE" : e > t ? "ResourceAttributionPathSpec.TENANT" : "ResourceAttributionPathSpec.OWNER";
656
660
  }
657
661
  function B(n, e, t, o) {
@@ -661,7 +665,7 @@ function B(n, e, t, o) {
661
665
  function Ee(n, e) {
662
666
  return `${w(n.aggregateName)}${e}`;
663
667
  }
664
- class Oe extends x {
668
+ class Se extends x {
665
669
  /**
666
670
  * Creates a new QueryClientGenerator instance.
667
671
  * @param context - The generation context containing OpenAPI spec and project details
@@ -730,7 +734,7 @@ class Oe extends x {
730
734
  this.logger.info(
731
735
  `Creating default query client options: ${o}`
732
736
  ), t.addVariableStatement({
733
- declarationKind: O.Const,
737
+ declarationKind: E.Const,
734
738
  declarations: [
735
739
  {
736
740
  name: o,
@@ -738,7 +742,7 @@ class Oe extends x {
738
742
  initializer: `{
739
743
  contextAlias: '${e.aggregate.contextAlias}',
740
744
  aggregateName: '${e.aggregate.aggregateName}',
741
- resourceAttribution: ${xe(e)},
745
+ resourceAttribution: ${Oe(e)},
742
746
  }`
743
747
  }
744
748
  ],
@@ -749,7 +753,7 @@ class Oe extends x {
749
753
  `Processing ${e.events.size} domain events for aggregate: ${e.aggregate.aggregateName}`
750
754
  );
751
755
  for (const m of e.events.values()) {
752
- const l = u(m.schema.key);
756
+ const l = d(m.schema.key);
753
757
  this.logger.info(
754
758
  `Adding import for event model: ${l.name} from path: ${l.path}`
755
759
  ), y(t, this.outputDir, l), r.push(l);
@@ -761,13 +765,13 @@ class Oe extends x {
761
765
  name: s,
762
766
  type: a
763
767
  });
764
- const c = `${h(e.aggregate.aggregateName)}QueryClientFactory`, i = u(e.state.key), p = u(e.fields.key);
768
+ const c = `${h(e.aggregate.aggregateName)}QueryClientFactory`, i = d(e.state.key), p = d(e.fields.key);
765
769
  this.logger.info(
766
770
  `Adding import for state model: ${i.name} from path: ${i.path}`
767
771
  ), y(t, this.outputDir, i), this.logger.info(
768
772
  `Adding import for fields model: ${p.name} from path: ${p.path}`
769
773
  ), y(t, this.outputDir, p), this.logger.info(`Creating query client factory: ${c}`), t.addVariableStatement({
770
- declarationKind: O.Const,
774
+ declarationKind: E.Const,
771
775
  declarations: [
772
776
  {
773
777
  name: c,
@@ -780,7 +784,7 @@ class Oe extends x {
780
784
  );
781
785
  }
782
786
  }
783
- class Se extends x {
787
+ class Ie extends x {
784
788
  commandEndpointPathsName = "COMMAND_ENDPOINT_PATHS";
785
789
  defaultCommandClientOptionsName = "DEFAULT_COMMAND_CLIENT_OPTIONS";
786
790
  /**
@@ -831,7 +835,7 @@ class Se extends x {
831
835
  ), this.processCommandEndpointPaths(t, e), this.logger.info(
832
836
  `Creating default command client options: ${this.defaultCommandClientOptionsName}`
833
837
  ), t.addVariableStatement({
834
- declarationKind: O.Const,
838
+ declarationKind: E.Const,
835
839
  declarations: [
836
840
  {
837
841
  name: this.defaultCommandClientOptionsName,
@@ -929,7 +933,7 @@ class Se extends x {
929
933
  {
930
934
  name: "apiMetadata",
931
935
  type: "ApiMetadata",
932
- scope: Q.Public,
936
+ scope: k.Public,
933
937
  isReadonly: !0,
934
938
  initializer: `${this.defaultCommandClientOptionsName}`
935
939
  }
@@ -948,7 +952,7 @@ class Se extends x {
948
952
  * @param definition - The command definition
949
953
  */
950
954
  processCommandMethod(e, t, o, r) {
951
- const s = u(o.schema.key);
955
+ const s = d(o.schema.key);
952
956
  this.logger.info(
953
957
  `Adding import for command model: ${s.name} from path: ${s.path}`
954
958
  ), y(e, this.outputDir, s), this.logger.info(
@@ -973,7 +977,7 @@ class Se extends x {
973
977
  `Adding command request parameter: commandRequest (type: CommandRequest<${s.name}>)`
974
978
  ), a.push({
975
979
  name: "commandRequest",
976
- hasQuestionToken: !0,
980
+ hasQuestionToken: pe(o.schema.schema),
977
981
  type: `CommandRequest<${s.name}>`,
978
982
  decorators: [
979
983
  {
@@ -1015,7 +1019,7 @@ class Se extends x {
1015
1019
  );
1016
1020
  }
1017
1021
  }
1018
- class Ie extends x {
1022
+ class Ne extends x {
1019
1023
  queryClientGenerator;
1020
1024
  commandClientGenerator;
1021
1025
  /**
@@ -1023,7 +1027,7 @@ class Ie extends x {
1023
1027
  * @param context - The generation context containing OpenAPI spec and project details
1024
1028
  */
1025
1029
  constructor(e) {
1026
- super(e), this.queryClientGenerator = new Oe(e), this.commandClientGenerator = new Se(e);
1030
+ super(e), this.queryClientGenerator = new Se(e), this.commandClientGenerator = new Ie(e);
1027
1031
  }
1028
1032
  /**
1029
1033
  * Generates client classes for all aggregates.
@@ -1079,7 +1083,7 @@ class Me {
1079
1083
  ), this.options.logger.info(`Input path: ${this.options.inputPath}`), this.options.logger.info(`Output directory: ${this.options.outputDir}`), this.options.logger.info("Parsing OpenAPI specification");
1080
1084
  const e = await ee(this.options.inputPath);
1081
1085
  this.options.logger.info("OpenAPI specification parsed successfully"), this.options.logger.info("Resolving bounded context aggregates");
1082
- const o = new Pe(e).resolve();
1086
+ const o = new $e(e).resolve();
1083
1087
  this.options.logger.info(
1084
1088
  `Resolved ${o.size} bounded context aggregates`
1085
1089
  );
@@ -1090,7 +1094,7 @@ class Me {
1090
1094
  contextAggregates: o,
1091
1095
  logger: this.options.logger
1092
1096
  };
1093
- this.options.logger.info("Generating models"), new we(r).generate(), this.options.logger.info("Models generated successfully"), this.options.logger.info("Generating clients"), new Ie(r).generate(), this.options.logger.info("Clients generated successfully"), this.options.logger.info("Generating index files"), this.generateIndex(), this.options.logger.info("Index files generated successfully"), this.options.logger.info("Optimizing source files"), this.optimizeSourceFiles(), this.options.logger.info("Source files optimized successfully"), this.options.logger.info("Saving project to disk"), await this.project.save(), this.options.logger.info("Code generation completed successfully");
1097
+ this.options.logger.info("Generating models"), new xe(r).generate(), this.options.logger.info("Models generated successfully"), this.options.logger.info("Generating clients"), new Ne(r).generate(), this.options.logger.info("Clients generated successfully"), this.options.logger.info("Generating index files"), this.generateIndex(), this.options.logger.info("Index files generated successfully"), this.options.logger.info("Optimizing source files"), this.optimizeSourceFiles(), this.options.logger.info("Source files optimized successfully"), this.options.logger.info("Saving project to disk"), await this.project.save(), this.options.logger.info("Code generation completed successfully");
1094
1098
  }
1095
1099
  /**
1096
1100
  * Generates index.ts files for all subdirectories in the output directory.