@moccona/apicodegen 0.0.1 → 0.0.3

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/npm/index.cjs CHANGED
@@ -260,10 +260,10 @@ var Base = class _Base {
260
260
  * @param [operationId] - Unique identifier for the operation.
261
261
  * @returns - The generated function name.
262
262
  */
263
- static pathToFnName(path, method, operationId) {
264
- const name = this.camelCase(this.normalize(path));
263
+ static pathToFnName(path, method, _operationId = "") {
264
+ const name = this.normalize(this.camelCase(this.normalize(path)));
265
265
  const suffix = method ? this.capitalize(this.upperCamelCase(`using_${method}`)) : "";
266
- return (operationId ? this.camelCase(this.normalize(operationId)) : name) + suffix;
266
+ return name + suffix;
267
267
  }
268
268
  /**
269
269
  * Normalizes a string by replacing special characters and avoiding TypeScript keywords.
@@ -274,7 +274,7 @@ var Base = class _Base {
274
274
  if (typescriptKeywords.has(text)) {
275
275
  text += "_";
276
276
  }
277
- return text.replace(/[/\-_{}():\s`,*<>$]/gm, "_").replaceAll("...", "");
277
+ return text.replace(/[/\-_{}():\s`,*<>$#.]/gm, "_").replace(/^\d./gm, "").replaceAll("...", "");
278
278
  }
279
279
  /**
280
280
  * Capitalizes the first character of a string.
@@ -310,19 +310,24 @@ var Base = class _Base {
310
310
  */
311
311
  static async fetchDoc(url, requestInit = {}) {
312
312
  const agent = new import_undici.Agent({
313
- connect: {
314
- rejectUnauthorized: false
315
- }
313
+ connect: { rejectUnauthorized: false }
314
+ });
315
+ const { body, statusCode } = await (0, import_undici.request)(url, {
316
+ method: "GET",
317
+ dispatcher: agent,
318
+ ...requestInit
316
319
  });
320
+ if (statusCode >= 400) {
321
+ throw new Error(
322
+ `Failed to fetch OpenAPI documentation from ${url}: HTTP ${statusCode}`
323
+ );
324
+ }
317
325
  try {
318
- const { body } = await (0, import_undici.request)(url, {
319
- method: "GET",
320
- dispatcher: agent,
321
- ...requestInit
322
- });
323
326
  return body.json();
324
327
  } catch (error) {
325
- throw error;
328
+ throw new Error(
329
+ `Failed to parse JSON response from ${url}: ${error instanceof Error ? error.message : String(error)}`
330
+ );
326
331
  }
327
332
  }
328
333
  /**
@@ -331,12 +336,11 @@ var Base = class _Base {
331
336
  * @returns - The matched MediaTypes or null.
332
337
  */
333
338
  static getMediaType(mediaType) {
334
- for (const type in Object.values(MediaTypes)) {
335
- if (new RegExp(type).test(mediaType)) {
336
- return type;
337
- }
338
- }
339
- return;
339
+ const mediaTypeValues = Object.values(MediaTypes);
340
+ const found = mediaTypeValues.find(
341
+ (type) => mediaType.includes(type)
342
+ );
343
+ return found;
340
344
  }
341
345
  /**
342
346
  * Checks if a schema is a valid enum type that isn't boolean.
@@ -371,17 +375,21 @@ var Base = class _Base {
371
375
  * @returns - Array of unique enum schemas.
372
376
  */
373
377
  static uniqueEnums(enums) {
374
- const uniqueEnums_ = [];
375
- for (const enumObject of enums) {
376
- if (uniqueEnums_.length === 0) {
377
- uniqueEnums_.push(enumObject);
378
- } else {
379
- if (!uniqueEnums_.some((a) => this.isSameEnum(a, enumObject))) {
380
- uniqueEnums_.push(enumObject);
378
+ const enumMap = /* @__PURE__ */ new Map();
379
+ for (const e of enums) {
380
+ const existing = enumMap.get(e.name);
381
+ if (existing) {
382
+ for (const value of e.enum) {
383
+ existing.add(value);
381
384
  }
385
+ } else {
386
+ enumMap.set(e.name, new Set(e.enum));
382
387
  }
383
388
  }
384
- return uniqueEnums_;
389
+ return Array.from(enumMap.entries()).map(([name, values]) => ({
390
+ name,
391
+ enum: Array.from(values)
392
+ }));
385
393
  }
386
394
  /**
387
395
  * Finds the first occurrence of a matching enum schema in an array.
@@ -398,7 +406,7 @@ var Base = class _Base {
398
406
  * @returns - True if the object is a reference.
399
407
  */
400
408
  static isRef(schema) {
401
- return "$ref" in schema && typeof schema.$ref === "string";
409
+ return typeof schema === "object" && schema !== null && "$ref" in schema && typeof schema.$ref === "string";
402
410
  }
403
411
  };
404
412
 
@@ -484,7 +492,7 @@ var Generator = class _Generator {
484
492
  * @param path - The base path string containing placeholders.
485
493
  * @param parameters - Array of parameter objects defining the parameters.
486
494
  * @param basePath - Optional base path to prepend (default: "").
487
- * @returns A TypeScript template expression node.
495
+ * @returns A TypeScript template expressi
488
496
  */
489
497
  static toUrlTemplate(path, parameters, basePath = "") {
490
498
  const queryParameters = parameters.filter(
@@ -492,7 +500,7 @@ var Generator = class _Generator {
492
500
  );
493
501
  if (queryParameters.length > 0) {
494
502
  const queryString = queryParameters.map(
495
- (qp, index) => `${index === 0 ? "?" : "&"}${encodeURIComponent(qp.name)}={${qp.name}}`
503
+ (qp, index) => `${index === 0 ? "?" : "&"}${encodeURIComponent(qp.name)}={${Base.camelCase(Base.normalize(qp.name))}}`
496
504
  ).join("");
497
505
  path += queryString;
498
506
  }
@@ -607,7 +615,7 @@ var Generator = class _Generator {
607
615
  case "boolean" /* boolean */:
608
616
  return import_typescript.factory.createToken(import_typescript.SyntaxKind.BooleanKeyword);
609
617
  case "file" /* file */:
610
- return import_typescript.factory.createTypeReferenceNode(import_typescript.factory.createIdentifier("File"));
618
+ return import_typescript.factory.createTypeReferenceNode(import_typescript.factory.createIdentifier("Blob"));
611
619
  default:
612
620
  const {
613
621
  format: format2,
@@ -626,7 +634,7 @@ var Generator = class _Generator {
626
634
  return import_typescript.factory.createToken(import_typescript.SyntaxKind.BooleanKeyword);
627
635
  case "blob" /* blob */:
628
636
  case "binary" /* binary */:
629
- return import_typescript.factory.createTypeReferenceNode(import_typescript.factory.createIdentifier("File"));
637
+ return import_typescript.factory.createTypeReferenceNode(import_typescript.factory.createIdentifier("Blob"));
630
638
  default:
631
639
  }
632
640
  if (enum_) {
@@ -650,36 +658,36 @@ var Generator = class _Generator {
650
658
  );
651
659
  }
652
660
  if (allOf) {
653
- return import_typescript.factory.createUnionTypeNode(
661
+ return import_typescript.factory.createIntersectionTypeNode(
654
662
  allOf.map((schema2) => this.toTypeNode(schema2))
655
663
  );
656
664
  }
657
665
  if (type2 && typeof type2 === "string") {
658
666
  return import_typescript.factory.createTypeReferenceNode(
659
- type2 !== "unknown" ? import_typescript.factory.createIdentifier(Base.upperCamelCase(type2)) : type2
667
+ type2 !== "unknown" && type2 !== "null" ? import_typescript.factory.createIdentifier(Base.upperCamelCase(type2)) : type2
660
668
  );
661
669
  }
662
670
  }
663
671
  return import_typescript.factory.createToken(import_typescript.SyntaxKind.UnknownKeyword);
664
672
  }
665
- static toDeclarationNode(parameters) {
673
+ static toDeclarationNodes(parameters) {
666
674
  const objectElements = [];
667
675
  const typeObjectElements = [];
676
+ const refParameters = [];
668
677
  for (const parameter of parameters) {
669
678
  if (parameter.ref) {
670
- import_typescript.factory.createParameterDeclaration(
671
- void 0,
672
- void 0,
673
- import_typescript.factory.createIdentifier(
674
- Base.camelCase(Base.normalize(Base.ref2name(parameter.ref)))
675
- ),
676
- void 0,
677
- import_typescript.factory.createTypeReferenceNode(
678
- import_typescript.factory.createIdentifier(
679
- Base.upperCamelCase(Base.normalize(Base.ref2name(parameter.ref)))
680
- )
681
- ),
682
- void 0
679
+ const refName = Base.ref2name(parameter.ref);
680
+ refParameters.push(
681
+ import_typescript.factory.createParameterDeclaration(
682
+ void 0,
683
+ void 0,
684
+ import_typescript.factory.createIdentifier(Base.camelCase(Base.normalize(refName))),
685
+ void 0,
686
+ import_typescript.factory.createTypeReferenceNode(
687
+ import_typescript.factory.createIdentifier(Base.upperCamelCase(Base.normalize(refName)))
688
+ ),
689
+ void 0
690
+ )
683
691
  );
684
692
  } else {
685
693
  const { name, schema, required } = parameter;
@@ -700,14 +708,18 @@ var Generator = class _Generator {
700
708
  );
701
709
  }
702
710
  }
703
- return import_typescript.factory.createParameterDeclaration(
704
- void 0,
705
- void 0,
706
- import_typescript.factory.createObjectBindingPattern(objectElements),
707
- void 0,
708
- import_typescript.factory.createTypeLiteralNode(typeObjectElements),
709
- void 0
710
- );
711
+ if (objectElements.length > 0) {
712
+ const objectParam = import_typescript.factory.createParameterDeclaration(
713
+ void 0,
714
+ void 0,
715
+ import_typescript.factory.createObjectBindingPattern(objectElements),
716
+ void 0,
717
+ import_typescript.factory.createTypeLiteralNode(typeObjectElements),
718
+ void 0
719
+ );
720
+ return [objectParam, ...refParameters];
721
+ }
722
+ return refParameters;
711
723
  }
712
724
  static toFormDataStatement(parameters, requestBody) {
713
725
  const statements = [];
@@ -730,16 +742,11 @@ var Generator = class _Generator {
730
742
  )
731
743
  );
732
744
  statements.push(fdDeclaration);
733
- parameters.filter(
734
- (parameter) => parameter.ref !== void 0 && (parameter.in === "formData" /* formData */ || parameter.schema && this.isBinarySchema(parameter.schema))
735
- ).forEach((parameter) => {
745
+ parameters.forEach((parameter) => {
736
746
  statements.push(
737
747
  import_typescript.factory.createExpressionStatement(
738
748
  import_typescript.factory.createBinaryExpression(
739
- import_typescript.factory.createElementAccessExpression(
740
- import_typescript.factory.createIdentifier("req"),
741
- import_typescript.factory.createStringLiteral(parameter.name)
742
- ),
749
+ import_typescript.factory.createIdentifier(parameter.name),
743
750
  import_typescript.factory.createToken(import_typescript.SyntaxKind.AmpersandAmpersandToken),
744
751
  import_typescript.factory.createCallExpression(
745
752
  import_typescript.factory.createPropertyAccessExpression(
@@ -783,7 +790,13 @@ var Generator = class _Generator {
783
790
  import_typescript.factory.createStringLiteral(key),
784
791
  import_typescript.factory.createIdentifier("file"),
785
792
  import_typescript.factory.createPropertyAccessExpression(
786
- import_typescript.factory.createIdentifier("file"),
793
+ import_typescript.factory.createAsExpression(
794
+ import_typescript.factory.createIdentifier("file"),
795
+ import_typescript.factory.createTypeReferenceNode(
796
+ import_typescript.factory.createIdentifier("File"),
797
+ void 0
798
+ )
799
+ ),
787
800
  import_typescript.factory.createIdentifier("name")
788
801
  )
789
802
  ]
@@ -868,25 +881,28 @@ var Generator = class _Generator {
868
881
  );
869
882
  const shouldParseResponseToJSON = "application/json" === response?.type;
870
883
  const isRequestBodyBinary = requestBody?.schema && requestBody.schema.type === "array" /* array */ && this.isBinarySchema(requestBody.schema);
871
- const inFormDataParameters = parameters.filter(
872
- (p) => p.in === "formData" /* formData */
884
+ const parametersShouldPutInFormData = parameters.filter(
885
+ (p) => p.in === "formData" /* formData */ || p.schema && this.isBinarySchema(p.schema)
873
886
  );
874
- const notInFormDataParameters = parameters.filter(
875
- (p) => p.in !== "formData" /* formData */
887
+ const parametersShouldNotPutInFormData = parameters.filter(
888
+ (p) => !parametersShouldPutInFormData.includes(p)
876
889
  );
877
- const isRequestBodyContainsBinary = requestBody?.schema && "properties" in requestBody.schema && Object.values(requestBody.schema.properties).some(
890
+ const isRequestBodyContainsBinary = requestBody?.schema && "properties" in requestBody.schema && Object.values(requestBody.schema?.properties ?? {}).some(
878
891
  (p) => this.isBinarySchema(p)
879
892
  );
880
893
  const hasBinaryInParameters = parameters.some(
881
894
  (p) => p?.schema && this.isBinarySchema(p.schema)
882
895
  );
883
- const shouldPutParametersOrBodyInFormData = isFormDataRequest || isRequestBodyBinary || hasBinaryInParameters || isRequestBodyContainsBinary || inFormDataParameters.length > 0;
896
+ const shouldPutParametersOrBodyInFormData = isFormDataRequest || isRequestBodyBinary || hasBinaryInParameters || isRequestBodyContainsBinary || parametersShouldPutInFormData.length > 0;
884
897
  return import_typescript.factory.createBlock([
885
- ...shouldPutParametersOrBodyInFormData ? this.toFormDataStatement(inFormDataParameters, requestBody?.schema) : [],
898
+ ...shouldPutParametersOrBodyInFormData ? this.toFormDataStatement(
899
+ parametersShouldPutInFormData,
900
+ requestBody?.schema
901
+ ) : [],
886
902
  ...adapter.client(
887
903
  uri,
888
904
  method,
889
- notInFormDataParameters,
905
+ parametersShouldNotPutInFormData,
890
906
  requestBody,
891
907
  response,
892
908
  adapter,
@@ -957,8 +973,8 @@ var Generator = class _Generator {
957
973
  Base.pathToFnName(uri, method, operationId) + (shouldAddExtraMethodNameSuffix ? Base.capitalize(req.type.split("/")[1]) : ""),
958
974
  void 0,
959
975
  [
960
- parameters.length > 0 ? _Generator.toDeclarationNode(parameters) : void 0,
961
- req?.schema ? _Generator.toRequestBodyTypeNode(req.schema) : void 0
976
+ ...parameters.length > 0 ? _Generator.toDeclarationNodes(parameters) : [],
977
+ ...req?.schema ? [_Generator.toRequestBodyTypeNode(req.schema)] : []
962
978
  ].filter(Boolean),
963
979
  void 0,
964
980
  this.bodyBlock(
@@ -1078,7 +1094,6 @@ var AxiosAdapter = class extends Adapter {
1078
1094
  )
1079
1095
  ) : []
1080
1096
  ).concat(
1081
- // Add body if needed
1082
1097
  shouldUseFormData || inBody.length > 0 || requestBody?.schema ? import_typescript2.factory.createPropertyAssignment(
1083
1098
  import_typescript2.factory.createIdentifier(adapter.bodyFieldName),
1084
1099
  shouldUseFormData ? import_typescript2.factory.createIdentifier("fd") : inBody.length > 0 || requestBody?.schema && !Generator.isBinarySchema(requestBody.schema) ? import_typescript2.factory.createIdentifier("req") : import_typescript2.factory.createIdentifier("req")
@@ -1164,7 +1179,6 @@ var FetchAdapter = class extends Adapter {
1164
1179
  )
1165
1180
  ) : []
1166
1181
  ).concat(
1167
- // Add body if needed
1168
1182
  shouldUseFormData || inBody.length > 0 || requestBody?.schema ? import_typescript3.factory.createPropertyAssignment(
1169
1183
  import_typescript3.factory.createIdentifier(adapter.bodyFieldName),
1170
1184
  shouldUseFormData ? import_typescript3.factory.createIdentifier("fd") : inBody.length > 0 || requestBody?.schema && !Generator.isBinarySchema(requestBody.schema) ? import_typescript3.factory.createCallExpression(
@@ -1222,7 +1236,7 @@ var FetchAdapter = class extends Adapter {
1222
1236
  ],
1223
1237
  void 0,
1224
1238
  import_typescript3.factory.createToken(import_typescript3.SyntaxKind.EqualsGreaterThanToken),
1225
- import_typescript3.factory.createAsExpression(
1239
+ response?.schema ? import_typescript3.factory.createAsExpression(
1226
1240
  import_typescript3.factory.createParenthesizedExpression(
1227
1241
  import_typescript3.factory.createAwaitExpression(
1228
1242
  import_typescript3.factory.createCallExpression(
@@ -1236,6 +1250,17 @@ var FetchAdapter = class extends Adapter {
1236
1250
  )
1237
1251
  ),
1238
1252
  response?.schema ? Generator.toTypeNode(response.schema) : import_typescript3.factory.createToken(import_typescript3.SyntaxKind.UnknownKeyword)
1253
+ ) : import_typescript3.factory.createParenthesizedExpression(
1254
+ import_typescript3.factory.createAwaitExpression(
1255
+ import_typescript3.factory.createCallExpression(
1256
+ import_typescript3.factory.createPropertyAccessExpression(
1257
+ import_typescript3.factory.createIdentifier("response"),
1258
+ import_typescript3.factory.createIdentifier("json")
1259
+ ),
1260
+ void 0,
1261
+ []
1262
+ )
1263
+ )
1239
1264
  )
1240
1265
  )
1241
1266
  ]
@@ -1346,13 +1371,25 @@ var V2 = class {
1346
1371
  enum: enum_,
1347
1372
  format: format2,
1348
1373
  allOf: allOf?.map(
1349
- (s) => Base.isRef(s) ? { type: Base.upperCamelCase(Base.ref2name(s.$ref, this.doc)) } : this.toBaseSchema(s, enums)
1374
+ (s) => Base.isRef(s) ? {
1375
+ ...s,
1376
+ ref: s.$ref,
1377
+ type: Base.upperCamelCase(Base.ref2name(s.$ref, this.doc))
1378
+ } : this.toBaseSchema(s, enums)
1350
1379
  ),
1351
1380
  anyOf: anyOf?.map(
1352
- (s) => Base.isRef(s) ? { type: Base.upperCamelCase(Base.ref2name(s.$ref, this.doc)) } : this.toBaseSchema(s, enums)
1381
+ (s) => Base.isRef(s) ? {
1382
+ ...s,
1383
+ ref: s.$ref,
1384
+ type: Base.upperCamelCase(Base.ref2name(s.$ref, this.doc))
1385
+ } : this.toBaseSchema(s, enums)
1353
1386
  ),
1354
1387
  oneOf: oneOf?.map(
1355
- (s) => Base.isRef(s) ? { type: Base.upperCamelCase(Base.ref2name(s.$ref, this.doc)) } : this.toBaseSchema(s, enums)
1388
+ (s) => Base.isRef(s) ? {
1389
+ ...s,
1390
+ ref: s.$ref,
1391
+ type: Base.upperCamelCase(Base.ref2name(s.$ref, this.doc))
1392
+ } : this.toBaseSchema(s, enums)
1356
1393
  ),
1357
1394
  properties: Object.keys(properties).reduce((acc, p) => {
1358
1395
  const propSchema = properties[p];
@@ -1382,7 +1419,8 @@ var V2 = class {
1382
1419
  type,
1383
1420
  items,
1384
1421
  enum: enum_,
1385
- properties
1422
+ properties,
1423
+ schema
1386
1424
  } = parameter;
1387
1425
  if (enum_) {
1388
1426
  const type2 = Base.upperCamelCase(upLevelSchemaKey) + Base.upperCamelCase(name);
@@ -1404,17 +1442,36 @@ var V2 = class {
1404
1442
  }
1405
1443
  };
1406
1444
  }
1445
+ if (items) {
1446
+ return {
1447
+ name,
1448
+ required,
1449
+ description,
1450
+ in: parameter.in,
1451
+ schema: {
1452
+ type,
1453
+ items
1454
+ }
1455
+ };
1456
+ }
1457
+ if (schema && Base.isRef(schema)) {
1458
+ return {
1459
+ name,
1460
+ required,
1461
+ description,
1462
+ in: parameter.in,
1463
+ schema: {
1464
+ type: Base.upperCamelCase(Base.ref2name(schema.$ref))
1465
+ }
1466
+ };
1467
+ }
1407
1468
  return {
1408
1469
  name,
1409
1470
  required,
1410
1471
  description,
1411
1472
  in: parameter.in,
1412
- schema: items ? {
1413
- type,
1414
- items
1415
- } : {
1473
+ schema: {
1416
1474
  type,
1417
- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
1418
1475
  properties
1419
1476
  }
1420
1477
  };
@@ -1493,6 +1550,7 @@ var V2 = class {
1493
1550
  if (code in responses2) {
1494
1551
  const response = responses2[code];
1495
1552
  const responseSchema = this.getResponseByRef(response);
1553
+ const inBodyOnlyHasBody = inBody && inBody.length === 1 && inBody[0].in === "body" && inBody[0].name === "body";
1496
1554
  methodApis.push({
1497
1555
  method,
1498
1556
  operationId,
@@ -1501,7 +1559,12 @@ var V2 = class {
1501
1559
  description: description_,
1502
1560
  parameters: uniqueParameterName.map((name) => notInBody.find((p) => p.name === name)).filter(Boolean),
1503
1561
  responses: responseSchema,
1504
- requestBody: inBody.length > 0 ? [
1562
+ requestBody: inBody.length > 0 ? inBodyOnlyHasBody ? [
1563
+ {
1564
+ type: "application/json" /* JSON */,
1565
+ schema: inBody[0].schema
1566
+ }
1567
+ ] : [
1505
1568
  {
1506
1569
  type: "application/json" /* JSON */,
1507
1570
  schema: {
@@ -1569,7 +1632,11 @@ var V3 = class {
1569
1632
  type: upLevelSchemaKey + refName
1570
1633
  };
1571
1634
  }
1572
- schema = this.doc.components?.schemas?.[Base.ref2name(schema.$ref, this.doc)];
1635
+ const resolvedSchema = this.doc.components?.schemas?.[Base.ref2name(schema.$ref, this.doc)];
1636
+ if (!resolvedSchema) {
1637
+ return { type: "unknown" };
1638
+ }
1639
+ schema = resolvedSchema;
1573
1640
  }
1574
1641
  return this.toBaseSchema(schema, enums, "", upLevelSchemaKey + refName);
1575
1642
  }
@@ -1578,7 +1645,14 @@ var V3 = class {
1578
1645
  */
1579
1646
  getParameterByRef(schema, enums = [], upLevelSchemaKey = "") {
1580
1647
  if (Base.isRef(schema)) {
1581
- schema = this.doc.components?.parameters?.[Base.ref2name(schema.$ref, this.doc)];
1648
+ const resolvedSchema = this.doc.components?.parameters?.[Base.ref2name(schema.$ref, this.doc)];
1649
+ if (!resolvedSchema) {
1650
+ return {
1651
+ name: "unknown",
1652
+ in: "query"
1653
+ };
1654
+ }
1655
+ schema = resolvedSchema;
1582
1656
  }
1583
1657
  const {
1584
1658
  name,
@@ -1627,7 +1701,11 @@ var V3 = class {
1627
1701
  */
1628
1702
  getResponseByRef(schema) {
1629
1703
  if (Base.isRef(schema)) {
1630
- schema = this.doc.components?.responses?.[Base.ref2name(schema.$ref, this.doc)];
1704
+ const resolvedSchema = this.doc.components?.responses?.[Base.ref2name(schema.$ref, this.doc)];
1705
+ if (!resolvedSchema) {
1706
+ return [];
1707
+ }
1708
+ schema = resolvedSchema;
1631
1709
  }
1632
1710
  const { content = {} } = schema;
1633
1711
  return Object.keys(content).map((c) => ({
@@ -1640,7 +1718,11 @@ var V3 = class {
1640
1718
  */
1641
1719
  getRequestBodyByRef(schema, enums = []) {
1642
1720
  if (Base.isRef(schema)) {
1643
- schema = this.doc.components?.requestBodies?.[Base.ref2name(schema.$ref, this.doc)];
1721
+ const resolvedSchema = this.doc.components?.requestBodies?.[Base.ref2name(schema.$ref, this.doc)];
1722
+ if (!resolvedSchema) {
1723
+ return [];
1724
+ }
1725
+ schema = resolvedSchema;
1644
1726
  }
1645
1727
  const { content = {} } = schema;
1646
1728
  return Object.keys(content).map((c) => ({
@@ -1709,13 +1791,25 @@ var V3 = class {
1709
1791
  enum: enum_,
1710
1792
  format: format2,
1711
1793
  allOf: allOf?.map(
1712
- (s) => Base.isRef(s) ? { type: Base.capitalize(Base.ref2name(s.$ref, this.doc)) } : this.toBaseSchema(s, enums)
1794
+ (s) => Base.isRef(s) ? {
1795
+ ...s,
1796
+ ref: s.$ref,
1797
+ type: Base.capitalize(Base.ref2name(s.$ref, this.doc))
1798
+ } : this.toBaseSchema(s, enums)
1713
1799
  ),
1714
1800
  anyOf: anyOf?.map(
1715
- (s) => Base.isRef(s) ? { type: Base.capitalize(Base.ref2name(s.$ref, this.doc)) } : this.toBaseSchema(s, enums)
1801
+ (s) => Base.isRef(s) ? {
1802
+ ...s,
1803
+ ref: s.$ref,
1804
+ type: Base.capitalize(Base.ref2name(s.$ref, this.doc))
1805
+ } : this.toBaseSchema(s, enums)
1716
1806
  ),
1717
1807
  oneOf: oneOf?.map(
1718
- (s) => Base.isRef(s) ? { type: Base.capitalize(Base.ref2name(s.$ref, this.doc)) } : this.toBaseSchema(s, enums)
1808
+ (s) => Base.isRef(s) ? {
1809
+ ...s,
1810
+ ref: s.$ref,
1811
+ type: Base.capitalize(Base.ref2name(s.$ref, this.doc))
1812
+ } : this.toBaseSchema(s, enums)
1719
1813
  ),
1720
1814
  properties: Object.keys(properties).reduce((acc, p) => {
1721
1815
  const propSchema = properties[p];
@@ -2012,13 +2106,25 @@ var V3_1 = class {
2012
2106
  enum: enum_,
2013
2107
  format: format2,
2014
2108
  allOf: allOf?.map(
2015
- (s) => Base.isRef(s) ? { type: Base.upperCamelCase(Base.ref2name(s.$ref, this.doc)) } : this.toBaseSchema(s, enums)
2109
+ (s) => Base.isRef(s) ? {
2110
+ ...s,
2111
+ ref: s.$ref,
2112
+ type: Base.upperCamelCase(Base.ref2name(s.$ref, this.doc))
2113
+ } : this.toBaseSchema(s, enums)
2016
2114
  ),
2017
2115
  anyOf: anyOf?.map(
2018
- (s) => Base.isRef(s) ? { type: Base.upperCamelCase(Base.ref2name(s.$ref, this.doc)) } : this.toBaseSchema(s, enums)
2116
+ (s) => Base.isRef(s) ? {
2117
+ ...s,
2118
+ ref: s.$ref,
2119
+ type: Base.upperCamelCase(Base.ref2name(s.$ref, this.doc))
2120
+ } : this.toBaseSchema(s, enums)
2019
2121
  ),
2020
2122
  oneOf: oneOf?.map(
2021
- (s) => Base.isRef(s) ? { type: Base.upperCamelCase(Base.ref2name(s.$ref, this.doc)) } : this.toBaseSchema(s, enums)
2123
+ (s) => Base.isRef(s) ? {
2124
+ ...s,
2125
+ ref: s.$ref,
2126
+ type: Base.upperCamelCase(Base.ref2name(s.$ref, this.doc))
2127
+ } : this.toBaseSchema(s, enums)
2022
2128
  ),
2023
2129
  properties: Object.keys(properties).reduce((acc, p) => {
2024
2130
  const propSchema = properties[p];
@@ -2240,8 +2346,8 @@ var import_execa = require("execa");
2240
2346
  var import_fs_extra = __toESM(require("fs-extra"), 1);
2241
2347
  var PLUGIN_NAME = "apiCodeGen";
2242
2348
  var logger2 = (0, import_logger2.createScopedLogger)("api-codegen-vite-plugin");
2243
- var tsc = async () => {
2244
- await (0, import_execa.execaCommand)("npx tsc -b");
2349
+ var tsc = async (path) => {
2350
+ await (0, import_execa.execaCommand)(`npx tsc ${path} --noEmit`);
2245
2351
  };
2246
2352
  function apiCodeGenPlugin(options) {
2247
2353
  let firstRun = true;
@@ -2259,9 +2365,13 @@ function apiCodeGenPlugin(options) {
2259
2365
  const code = await codeGen(codeGenInitOptions);
2260
2366
  await import_fs_extra.default.createFile(codeGenInitOptions.output);
2261
2367
  await import_fs_extra.default.writeFile(codeGenInitOptions.output, code);
2368
+ try {
2369
+ await tsc(codeGenInitOptions.output);
2370
+ } catch (error) {
2371
+ logger2.error(error);
2372
+ }
2262
2373
  } catch (error) {
2263
- logger2.error(`Failed to generate api ${name}`);
2264
- console.error(error);
2374
+ logger2.error(`Failed to generate api ${name}: ${error}`);
2265
2375
  }
2266
2376
  return {
2267
2377
  ...proxies_,
@@ -2271,17 +2381,11 @@ function apiCodeGenPlugin(options) {
2271
2381
  Promise.resolve({})
2272
2382
  );
2273
2383
  logger2.info("-------> api codegen finished <--------");
2274
- try {
2275
- await tsc();
2276
- } catch (error) {
2277
- logger2.error(error);
2278
- process.exit(1);
2279
- }
2280
2384
  return {
2281
2385
  ...config,
2282
2386
  server: {
2283
2387
  ...config.server ?? {},
2284
- proxy: proxies
2388
+ proxy: Object.assign({}, config.server?.proxy ?? {}, proxies)
2285
2389
  }
2286
2390
  };
2287
2391
  }