@acrool/rtk-query-codegen-openapi 0.0.2-test.7 → 0.0.2-test.9

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/lib/index.mjs CHANGED
@@ -458,12 +458,14 @@ async function generateApi(spec, {
458
458
  );
459
459
  const printer2 = ts4.createPrinter({ newLine: ts4.NewLineKind.LineFeed });
460
460
  const allTypeDefinitions = [];
461
+ const definedTypeNames = /* @__PURE__ */ new Set();
461
462
  const components = v3Doc.components;
462
463
  if (components) {
463
464
  const componentDefinitions = Object.entries(components).map(([componentType, componentDefs]) => {
464
465
  const typeEntries = Object.entries(componentDefs).map(([name, def]) => {
465
466
  addSchemeTypeName(name);
466
467
  const typeName = capitalize(camelCase(name));
468
+ definedTypeNames.add(typeName);
467
469
  const typeNode = wrapWithSchemeIfComponent(apiGen.getTypeFromSchema(def));
468
470
  return factory.createTypeAliasDeclaration(
469
471
  [factory.createModifier(ts4.SyntaxKind.ExportKeyword)],
@@ -481,10 +483,101 @@ async function generateApi(spec, {
481
483
  });
482
484
  allTypeDefinitions.push(...componentDefinitions);
483
485
  }
484
- if (useEnumType) {
485
- allTypeDefinitions.push(...apiGen.enumAliases);
486
+ const enumEntries = [
487
+ ...apiGen.enumAliases.filter((e) => ts4.isEnumDeclaration(e)),
488
+ ...apiGen.enumAliases.filter((e) => ts4.isTypeAliasDeclaration(e))
489
+ ].map((enumDecl) => {
490
+ if (ts4.isEnumDeclaration(enumDecl)) {
491
+ return factory.createEnumDeclaration(
492
+ [factory.createModifier(ts4.SyntaxKind.ExportKeyword)],
493
+ enumDecl.name,
494
+ enumDecl.members
495
+ );
496
+ } else if (ts4.isTypeAliasDeclaration(enumDecl)) {
497
+ return factory.createTypeAliasDeclaration(
498
+ [factory.createModifier(ts4.SyntaxKind.ExportKeyword)],
499
+ enumDecl.name,
500
+ enumDecl.typeParameters,
501
+ enumDecl.type
502
+ );
503
+ }
504
+ return enumDecl;
505
+ });
506
+ const unionTypeEnums = apiGen.aliases.filter((alias) => {
507
+ if (ts4.isTypeAliasDeclaration(alias) && alias.type) {
508
+ return ts4.isUnionTypeNode(alias.type);
509
+ }
510
+ return false;
511
+ }).map((alias) => {
512
+ if (ts4.isTypeAliasDeclaration(alias)) {
513
+ return factory.createTypeAliasDeclaration(
514
+ [factory.createModifier(ts4.SyntaxKind.ExportKeyword)],
515
+ alias.name,
516
+ alias.typeParameters,
517
+ alias.type
518
+ );
519
+ }
520
+ return alias;
521
+ });
522
+ const allEnumEntries = [...enumEntries, ...unionTypeEnums];
523
+ if (allEnumEntries.length > 0) {
524
+ allTypeDefinitions.push(
525
+ factory.createModuleDeclaration(
526
+ [factory.createModifier(ts4.SyntaxKind.ExportKeyword)],
527
+ factory.createIdentifier("Enum"),
528
+ factory.createModuleBlock(allEnumEntries),
529
+ ts4.NodeFlags.Namespace
530
+ )
531
+ );
532
+ }
533
+ if (apiGen.aliases.length > 0) {
534
+ const aliasEntries = apiGen.aliases.filter((alias) => {
535
+ if (ts4.isTypeAliasDeclaration(alias)) {
536
+ const isDefinedInComponents = definedTypeNames.has(alias.name.text);
537
+ const isUnionTypeEnum = ts4.isUnionTypeNode(alias.type);
538
+ return !isDefinedInComponents && !isUnionTypeEnum;
539
+ }
540
+ return false;
541
+ }).map((alias) => {
542
+ if (ts4.isTypeAliasDeclaration(alias)) {
543
+ return factory.createTypeAliasDeclaration(
544
+ [factory.createModifier(ts4.SyntaxKind.ExportKeyword)],
545
+ alias.name,
546
+ alias.typeParameters,
547
+ alias.type
548
+ );
549
+ }
550
+ return alias;
551
+ });
552
+ if (aliasEntries.length > 0) {
553
+ const existingSchemeIndex = allTypeDefinitions.findIndex(
554
+ (def) => ts4.isModuleDeclaration(def) && ts4.isIdentifier(def.name) && def.name.text === "Scheme"
555
+ );
556
+ if (existingSchemeIndex >= 0) {
557
+ const existingScheme = allTypeDefinitions[existingSchemeIndex];
558
+ const mergedMembers = [...existingScheme.body.statements, ...aliasEntries];
559
+ allTypeDefinitions[existingSchemeIndex] = factory.createModuleDeclaration(
560
+ [factory.createModifier(ts4.SyntaxKind.ExportKeyword)],
561
+ factory.createIdentifier("Scheme"),
562
+ factory.createModuleBlock(mergedMembers),
563
+ ts4.NodeFlags.Namespace
564
+ );
565
+ } else {
566
+ allTypeDefinitions.push(
567
+ factory.createModuleDeclaration(
568
+ [factory.createModifier(ts4.SyntaxKind.ExportKeyword)],
569
+ factory.createIdentifier("Scheme"),
570
+ factory.createModuleBlock(aliasEntries),
571
+ ts4.NodeFlags.Namespace
572
+ )
573
+ );
574
+ }
575
+ }
486
576
  }
487
- allTypeDefinitions.push(...apiGen.aliases);
577
+ const fs2 = await import("node:fs/promises");
578
+ const path4 = await import("node:path");
579
+ const sharedTypesDir = path4.dirname(sharedTypesFile);
580
+ await fs2.mkdir(sharedTypesDir, { recursive: true });
488
581
  const output = printer2.printNode(
489
582
  ts4.EmitHint.Unspecified,
490
583
  factory.createSourceFile(
@@ -494,7 +587,6 @@ async function generateApi(spec, {
494
587
  ),
495
588
  resultFile2
496
589
  );
497
- const fs2 = await import("node:fs/promises");
498
590
  await fs2.writeFile(sharedTypesFile, output, "utf-8");
499
591
  }
500
592
  if (apiGen.spec.components?.schemas) {
@@ -539,7 +631,12 @@ async function generateApi(spec, {
539
631
  [
540
632
  generateImportNode(apiFile, { [apiImport]: "api" }),
541
633
  generateImportNode("@acrool/react-fetcher", { IRestFulEndpointsQueryReturn: "IRestFulEndpointsQueryReturn" }),
542
- ...sharedTypesFile ? [generateImportNode(sharedTypesImportPath, { Scheme: "Scheme" })] : [],
634
+ ...sharedTypesFile ? [
635
+ generateImportNode(sharedTypesImportPath, {
636
+ Scheme: "Scheme",
637
+ ...useEnumType ? { Enum: "Enum" } : {}
638
+ })
639
+ ] : [],
543
640
  ...tag ? [generateTagTypes({ addTagTypes: extractAllTagTypes({ operationDefinitions }) })] : [],
544
641
  generateCreateApiCall({
545
642
  tag,
@@ -614,25 +711,6 @@ async function generateApi(spec, {
614
711
  ).filter(
615
712
  ([status, response]) => isDataResponse(status, includeDefault, apiGen.resolve(response), responses || {})
616
713
  ).filter(([_1, _2, type]) => type !== keywordType.void).map(([code, response, type]) => {
617
- if (sharedTypesFile2 && ts4.isTypeReferenceNode(type) && type.typeName) {
618
- if (ts4.isIdentifier(type.typeName)) {
619
- const typeName = type.typeName.text;
620
- if (typeName in apiGen.aliases || typeName in apiGen.enumAliases) {
621
- return ts4.addSyntheticLeadingComment(
622
- factory.createTypeReferenceNode(
623
- factory.createQualifiedName(
624
- factory.createIdentifier("sharedTypes"),
625
- factory.createIdentifier(camelCase(typeName))
626
- ),
627
- type.typeArguments
628
- ),
629
- ts4.SyntaxKind.MultiLineCommentTrivia,
630
- `* status ${code} ${response.description} `,
631
- false
632
- );
633
- }
634
- }
635
- }
636
714
  return ts4.addSyntheticLeadingComment(
637
715
  type,
638
716
  ts4.SyntaxKind.MultiLineCommentTrivia,
@@ -841,6 +919,28 @@ async function generateApi(spec, {
841
919
  function wrapWithSchemeIfComponent(typeNode) {
842
920
  if (ts4.isTypeReferenceNode(typeNode) && ts4.isIdentifier(typeNode.typeName)) {
843
921
  const typeName = typeNode.typeName.text;
922
+ const isEnumType = useEnumType && (apiGen.enumAliases.some((enumDecl) => {
923
+ if (ts4.isEnumDeclaration(enumDecl) || ts4.isTypeAliasDeclaration(enumDecl)) {
924
+ return enumDecl.name.text === typeName;
925
+ }
926
+ return false;
927
+ }) || apiGen.aliases.some((alias) => {
928
+ if (ts4.isTypeAliasDeclaration(alias) && alias.type) {
929
+ if (ts4.isUnionTypeNode(alias.type)) {
930
+ return alias.name.text === typeName;
931
+ }
932
+ }
933
+ return false;
934
+ }));
935
+ if (isEnumType) {
936
+ return factory.createTypeReferenceNode(
937
+ factory.createQualifiedName(
938
+ factory.createIdentifier("Enum"),
939
+ typeNode.typeName
940
+ ),
941
+ typeNode.typeArguments?.map(wrapWithSchemeIfComponent)
942
+ );
943
+ }
844
944
  if (schemeTypeNames.has(typeName)) {
845
945
  return factory.createTypeReferenceNode(
846
946
  factory.createQualifiedName(
@@ -861,6 +961,40 @@ async function generateApi(spec, {
861
961
  return factory.createArrayTypeNode(wrapWithSchemeIfComponent(typeNode.elementType));
862
962
  }
863
963
  if (ts4.isUnionTypeNode(typeNode)) {
964
+ const unionTypes = typeNode.types;
965
+ if (unionTypes.length > 0 && unionTypes.every(
966
+ (type) => ts4.isLiteralTypeNode(type) && (ts4.isStringLiteral(type.literal) || ts4.isNumericLiteral(type.literal))
967
+ )) {
968
+ const enumValues = unionTypes.map((type) => {
969
+ if (ts4.isLiteralTypeNode(type)) {
970
+ if (ts4.isStringLiteral(type.literal)) {
971
+ return type.literal.text;
972
+ } else if (ts4.isNumericLiteral(type.literal)) {
973
+ return type.literal.text;
974
+ }
975
+ }
976
+ return null;
977
+ }).filter(Boolean);
978
+ const matchingEnum = apiGen.aliases.find((alias) => {
979
+ if (ts4.isTypeAliasDeclaration(alias) && ts4.isUnionTypeNode(alias.type)) {
980
+ const aliasValues = alias.type.types.map((type) => {
981
+ if (ts4.isLiteralTypeNode(type)) {
982
+ if (ts4.isStringLiteral(type.literal)) {
983
+ return type.literal.text;
984
+ } else if (ts4.isNumericLiteral(type.literal)) {
985
+ return type.literal.text;
986
+ }
987
+ }
988
+ return null;
989
+ }).filter(Boolean);
990
+ return aliasValues.length === enumValues.length && aliasValues.every((val) => enumValues.includes(val));
991
+ }
992
+ return false;
993
+ });
994
+ if (matchingEnum && ts4.isTypeAliasDeclaration(matchingEnum)) {
995
+ return typeNode;
996
+ }
997
+ }
864
998
  return factory.createUnionTypeNode(typeNode.types.map(wrapWithSchemeIfComponent));
865
999
  }
866
1000
  if (ts4.isTypeLiteralNode(typeNode)) {
@@ -997,12 +1131,36 @@ function parseConfig(fullConfig) {
997
1131
  }, {});
998
1132
  Object.entries(groupedPaths).forEach(([groupName, paths2]) => {
999
1133
  const finalOutputPath = outputPath.replace("$1", groupName);
1000
- const filterEndpoints = filterEndpoint(groupName);
1001
- outFiles.push({
1002
- ...commonConfig,
1003
- outputFile: finalOutputPath,
1004
- filterEndpoints: [filterEndpoints]
1005
- });
1134
+ if (filterEndpoint) {
1135
+ const pathBasedFilter = (operationName, operationDefinition) => {
1136
+ const path4 = operationDefinition.path;
1137
+ const pathGroupName = getGroupNameFromPath(path4, pattern);
1138
+ if (pathGroupName !== groupName) {
1139
+ return false;
1140
+ }
1141
+ const endpointFilter = filterEndpoint(groupName);
1142
+ if (endpointFilter instanceof RegExp) {
1143
+ return endpointFilter.test(operationName);
1144
+ }
1145
+ return true;
1146
+ };
1147
+ outFiles.push({
1148
+ ...commonConfig,
1149
+ outputFile: finalOutputPath,
1150
+ filterEndpoints: pathBasedFilter
1151
+ });
1152
+ } else {
1153
+ const pathBasedFilter = (operationName, operationDefinition) => {
1154
+ const path4 = operationDefinition.path;
1155
+ const pathGroupName = getGroupNameFromPath(path4, pattern);
1156
+ return pathGroupName === groupName;
1157
+ };
1158
+ outFiles.push({
1159
+ ...commonConfig,
1160
+ outputFile: finalOutputPath,
1161
+ filterEndpoints: pathBasedFilter
1162
+ });
1163
+ }
1006
1164
  });
1007
1165
  } else {
1008
1166
  outFiles.push(fullConfig);