@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.d.mts +1 -1
- package/lib/index.d.ts +1 -1
- package/lib/index.js +188 -30
- package/lib/index.js.map +1 -1
- package/lib/index.mjs +188 -30
- package/lib/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/generate.ts +203 -37
- package/src/index.ts +51 -20
- package/src/types.ts +1 -1
package/lib/index.d.mts
CHANGED
|
@@ -133,7 +133,7 @@ type EndpointOverrides = {
|
|
|
133
133
|
type OutputFilesConfig = {
|
|
134
134
|
[outputFile: string]: {
|
|
135
135
|
groupMatch: RegExp;
|
|
136
|
-
filterEndpoint
|
|
136
|
+
filterEndpoint?: (groupName: string) => RegExp;
|
|
137
137
|
};
|
|
138
138
|
};
|
|
139
139
|
type ConfigFile = Id<Require<CommonOptions & OutputFileOptions, 'outputFile'>> | Id<Omit<CommonOptions, 'outputFile'> & {
|
package/lib/index.d.ts
CHANGED
|
@@ -133,7 +133,7 @@ type EndpointOverrides = {
|
|
|
133
133
|
type OutputFilesConfig = {
|
|
134
134
|
[outputFile: string]: {
|
|
135
135
|
groupMatch: RegExp;
|
|
136
|
-
filterEndpoint
|
|
136
|
+
filterEndpoint?: (groupName: string) => RegExp;
|
|
137
137
|
};
|
|
138
138
|
};
|
|
139
139
|
type ConfigFile = Id<Require<CommonOptions & OutputFileOptions, 'outputFile'>> | Id<Omit<CommonOptions, 'outputFile'> & {
|
package/lib/index.js
CHANGED
|
@@ -479,12 +479,14 @@ async function generateApi(spec, {
|
|
|
479
479
|
);
|
|
480
480
|
const printer2 = import_typescript4.default.createPrinter({ newLine: import_typescript4.default.NewLineKind.LineFeed });
|
|
481
481
|
const allTypeDefinitions = [];
|
|
482
|
+
const definedTypeNames = /* @__PURE__ */ new Set();
|
|
482
483
|
const components = v3Doc.components;
|
|
483
484
|
if (components) {
|
|
484
485
|
const componentDefinitions = Object.entries(components).map(([componentType, componentDefs]) => {
|
|
485
486
|
const typeEntries = Object.entries(componentDefs).map(([name, def]) => {
|
|
486
487
|
addSchemeTypeName(name);
|
|
487
488
|
const typeName = capitalize((0, import_lodash.default)(name));
|
|
489
|
+
definedTypeNames.add(typeName);
|
|
488
490
|
const typeNode = wrapWithSchemeIfComponent(apiGen.getTypeFromSchema(def));
|
|
489
491
|
return factory.createTypeAliasDeclaration(
|
|
490
492
|
[factory.createModifier(import_typescript4.default.SyntaxKind.ExportKeyword)],
|
|
@@ -502,10 +504,101 @@ async function generateApi(spec, {
|
|
|
502
504
|
});
|
|
503
505
|
allTypeDefinitions.push(...componentDefinitions);
|
|
504
506
|
}
|
|
505
|
-
|
|
506
|
-
|
|
507
|
+
const enumEntries = [
|
|
508
|
+
...apiGen.enumAliases.filter((e) => import_typescript4.default.isEnumDeclaration(e)),
|
|
509
|
+
...apiGen.enumAliases.filter((e) => import_typescript4.default.isTypeAliasDeclaration(e))
|
|
510
|
+
].map((enumDecl) => {
|
|
511
|
+
if (import_typescript4.default.isEnumDeclaration(enumDecl)) {
|
|
512
|
+
return factory.createEnumDeclaration(
|
|
513
|
+
[factory.createModifier(import_typescript4.default.SyntaxKind.ExportKeyword)],
|
|
514
|
+
enumDecl.name,
|
|
515
|
+
enumDecl.members
|
|
516
|
+
);
|
|
517
|
+
} else if (import_typescript4.default.isTypeAliasDeclaration(enumDecl)) {
|
|
518
|
+
return factory.createTypeAliasDeclaration(
|
|
519
|
+
[factory.createModifier(import_typescript4.default.SyntaxKind.ExportKeyword)],
|
|
520
|
+
enumDecl.name,
|
|
521
|
+
enumDecl.typeParameters,
|
|
522
|
+
enumDecl.type
|
|
523
|
+
);
|
|
524
|
+
}
|
|
525
|
+
return enumDecl;
|
|
526
|
+
});
|
|
527
|
+
const unionTypeEnums = apiGen.aliases.filter((alias) => {
|
|
528
|
+
if (import_typescript4.default.isTypeAliasDeclaration(alias) && alias.type) {
|
|
529
|
+
return import_typescript4.default.isUnionTypeNode(alias.type);
|
|
530
|
+
}
|
|
531
|
+
return false;
|
|
532
|
+
}).map((alias) => {
|
|
533
|
+
if (import_typescript4.default.isTypeAliasDeclaration(alias)) {
|
|
534
|
+
return factory.createTypeAliasDeclaration(
|
|
535
|
+
[factory.createModifier(import_typescript4.default.SyntaxKind.ExportKeyword)],
|
|
536
|
+
alias.name,
|
|
537
|
+
alias.typeParameters,
|
|
538
|
+
alias.type
|
|
539
|
+
);
|
|
540
|
+
}
|
|
541
|
+
return alias;
|
|
542
|
+
});
|
|
543
|
+
const allEnumEntries = [...enumEntries, ...unionTypeEnums];
|
|
544
|
+
if (allEnumEntries.length > 0) {
|
|
545
|
+
allTypeDefinitions.push(
|
|
546
|
+
factory.createModuleDeclaration(
|
|
547
|
+
[factory.createModifier(import_typescript4.default.SyntaxKind.ExportKeyword)],
|
|
548
|
+
factory.createIdentifier("Enum"),
|
|
549
|
+
factory.createModuleBlock(allEnumEntries),
|
|
550
|
+
import_typescript4.default.NodeFlags.Namespace
|
|
551
|
+
)
|
|
552
|
+
);
|
|
553
|
+
}
|
|
554
|
+
if (apiGen.aliases.length > 0) {
|
|
555
|
+
const aliasEntries = apiGen.aliases.filter((alias) => {
|
|
556
|
+
if (import_typescript4.default.isTypeAliasDeclaration(alias)) {
|
|
557
|
+
const isDefinedInComponents = definedTypeNames.has(alias.name.text);
|
|
558
|
+
const isUnionTypeEnum = import_typescript4.default.isUnionTypeNode(alias.type);
|
|
559
|
+
return !isDefinedInComponents && !isUnionTypeEnum;
|
|
560
|
+
}
|
|
561
|
+
return false;
|
|
562
|
+
}).map((alias) => {
|
|
563
|
+
if (import_typescript4.default.isTypeAliasDeclaration(alias)) {
|
|
564
|
+
return factory.createTypeAliasDeclaration(
|
|
565
|
+
[factory.createModifier(import_typescript4.default.SyntaxKind.ExportKeyword)],
|
|
566
|
+
alias.name,
|
|
567
|
+
alias.typeParameters,
|
|
568
|
+
alias.type
|
|
569
|
+
);
|
|
570
|
+
}
|
|
571
|
+
return alias;
|
|
572
|
+
});
|
|
573
|
+
if (aliasEntries.length > 0) {
|
|
574
|
+
const existingSchemeIndex = allTypeDefinitions.findIndex(
|
|
575
|
+
(def) => import_typescript4.default.isModuleDeclaration(def) && import_typescript4.default.isIdentifier(def.name) && def.name.text === "Scheme"
|
|
576
|
+
);
|
|
577
|
+
if (existingSchemeIndex >= 0) {
|
|
578
|
+
const existingScheme = allTypeDefinitions[existingSchemeIndex];
|
|
579
|
+
const mergedMembers = [...existingScheme.body.statements, ...aliasEntries];
|
|
580
|
+
allTypeDefinitions[existingSchemeIndex] = factory.createModuleDeclaration(
|
|
581
|
+
[factory.createModifier(import_typescript4.default.SyntaxKind.ExportKeyword)],
|
|
582
|
+
factory.createIdentifier("Scheme"),
|
|
583
|
+
factory.createModuleBlock(mergedMembers),
|
|
584
|
+
import_typescript4.default.NodeFlags.Namespace
|
|
585
|
+
);
|
|
586
|
+
} else {
|
|
587
|
+
allTypeDefinitions.push(
|
|
588
|
+
factory.createModuleDeclaration(
|
|
589
|
+
[factory.createModifier(import_typescript4.default.SyntaxKind.ExportKeyword)],
|
|
590
|
+
factory.createIdentifier("Scheme"),
|
|
591
|
+
factory.createModuleBlock(aliasEntries),
|
|
592
|
+
import_typescript4.default.NodeFlags.Namespace
|
|
593
|
+
)
|
|
594
|
+
);
|
|
595
|
+
}
|
|
596
|
+
}
|
|
507
597
|
}
|
|
508
|
-
|
|
598
|
+
const fs2 = await import("node:fs/promises");
|
|
599
|
+
const path4 = await import("node:path");
|
|
600
|
+
const sharedTypesDir = path4.dirname(sharedTypesFile);
|
|
601
|
+
await fs2.mkdir(sharedTypesDir, { recursive: true });
|
|
509
602
|
const output = printer2.printNode(
|
|
510
603
|
import_typescript4.default.EmitHint.Unspecified,
|
|
511
604
|
factory.createSourceFile(
|
|
@@ -515,7 +608,6 @@ async function generateApi(spec, {
|
|
|
515
608
|
),
|
|
516
609
|
resultFile2
|
|
517
610
|
);
|
|
518
|
-
const fs2 = await import("node:fs/promises");
|
|
519
611
|
await fs2.writeFile(sharedTypesFile, output, "utf-8");
|
|
520
612
|
}
|
|
521
613
|
if (apiGen.spec.components?.schemas) {
|
|
@@ -560,7 +652,12 @@ async function generateApi(spec, {
|
|
|
560
652
|
[
|
|
561
653
|
generateImportNode(apiFile, { [apiImport]: "api" }),
|
|
562
654
|
generateImportNode("@acrool/react-fetcher", { IRestFulEndpointsQueryReturn: "IRestFulEndpointsQueryReturn" }),
|
|
563
|
-
...sharedTypesFile ? [
|
|
655
|
+
...sharedTypesFile ? [
|
|
656
|
+
generateImportNode(sharedTypesImportPath, {
|
|
657
|
+
Scheme: "Scheme",
|
|
658
|
+
...useEnumType ? { Enum: "Enum" } : {}
|
|
659
|
+
})
|
|
660
|
+
] : [],
|
|
564
661
|
...tag ? [generateTagTypes({ addTagTypes: extractAllTagTypes({ operationDefinitions }) })] : [],
|
|
565
662
|
generateCreateApiCall({
|
|
566
663
|
tag,
|
|
@@ -635,25 +732,6 @@ async function generateApi(spec, {
|
|
|
635
732
|
).filter(
|
|
636
733
|
([status, response]) => isDataResponse(status, includeDefault, apiGen.resolve(response), responses || {})
|
|
637
734
|
).filter(([_1, _2, type]) => type !== import_generate3.keywordType.void).map(([code, response, type]) => {
|
|
638
|
-
if (sharedTypesFile2 && import_typescript4.default.isTypeReferenceNode(type) && type.typeName) {
|
|
639
|
-
if (import_typescript4.default.isIdentifier(type.typeName)) {
|
|
640
|
-
const typeName = type.typeName.text;
|
|
641
|
-
if (typeName in apiGen.aliases || typeName in apiGen.enumAliases) {
|
|
642
|
-
return import_typescript4.default.addSyntheticLeadingComment(
|
|
643
|
-
factory.createTypeReferenceNode(
|
|
644
|
-
factory.createQualifiedName(
|
|
645
|
-
factory.createIdentifier("sharedTypes"),
|
|
646
|
-
factory.createIdentifier((0, import_lodash.default)(typeName))
|
|
647
|
-
),
|
|
648
|
-
type.typeArguments
|
|
649
|
-
),
|
|
650
|
-
import_typescript4.default.SyntaxKind.MultiLineCommentTrivia,
|
|
651
|
-
`* status ${code} ${response.description} `,
|
|
652
|
-
false
|
|
653
|
-
);
|
|
654
|
-
}
|
|
655
|
-
}
|
|
656
|
-
}
|
|
657
735
|
return import_typescript4.default.addSyntheticLeadingComment(
|
|
658
736
|
type,
|
|
659
737
|
import_typescript4.default.SyntaxKind.MultiLineCommentTrivia,
|
|
@@ -862,6 +940,28 @@ async function generateApi(spec, {
|
|
|
862
940
|
function wrapWithSchemeIfComponent(typeNode) {
|
|
863
941
|
if (import_typescript4.default.isTypeReferenceNode(typeNode) && import_typescript4.default.isIdentifier(typeNode.typeName)) {
|
|
864
942
|
const typeName = typeNode.typeName.text;
|
|
943
|
+
const isEnumType = useEnumType && (apiGen.enumAliases.some((enumDecl) => {
|
|
944
|
+
if (import_typescript4.default.isEnumDeclaration(enumDecl) || import_typescript4.default.isTypeAliasDeclaration(enumDecl)) {
|
|
945
|
+
return enumDecl.name.text === typeName;
|
|
946
|
+
}
|
|
947
|
+
return false;
|
|
948
|
+
}) || apiGen.aliases.some((alias) => {
|
|
949
|
+
if (import_typescript4.default.isTypeAliasDeclaration(alias) && alias.type) {
|
|
950
|
+
if (import_typescript4.default.isUnionTypeNode(alias.type)) {
|
|
951
|
+
return alias.name.text === typeName;
|
|
952
|
+
}
|
|
953
|
+
}
|
|
954
|
+
return false;
|
|
955
|
+
}));
|
|
956
|
+
if (isEnumType) {
|
|
957
|
+
return factory.createTypeReferenceNode(
|
|
958
|
+
factory.createQualifiedName(
|
|
959
|
+
factory.createIdentifier("Enum"),
|
|
960
|
+
typeNode.typeName
|
|
961
|
+
),
|
|
962
|
+
typeNode.typeArguments?.map(wrapWithSchemeIfComponent)
|
|
963
|
+
);
|
|
964
|
+
}
|
|
865
965
|
if (schemeTypeNames.has(typeName)) {
|
|
866
966
|
return factory.createTypeReferenceNode(
|
|
867
967
|
factory.createQualifiedName(
|
|
@@ -882,6 +982,40 @@ async function generateApi(spec, {
|
|
|
882
982
|
return factory.createArrayTypeNode(wrapWithSchemeIfComponent(typeNode.elementType));
|
|
883
983
|
}
|
|
884
984
|
if (import_typescript4.default.isUnionTypeNode(typeNode)) {
|
|
985
|
+
const unionTypes = typeNode.types;
|
|
986
|
+
if (unionTypes.length > 0 && unionTypes.every(
|
|
987
|
+
(type) => import_typescript4.default.isLiteralTypeNode(type) && (import_typescript4.default.isStringLiteral(type.literal) || import_typescript4.default.isNumericLiteral(type.literal))
|
|
988
|
+
)) {
|
|
989
|
+
const enumValues = unionTypes.map((type) => {
|
|
990
|
+
if (import_typescript4.default.isLiteralTypeNode(type)) {
|
|
991
|
+
if (import_typescript4.default.isStringLiteral(type.literal)) {
|
|
992
|
+
return type.literal.text;
|
|
993
|
+
} else if (import_typescript4.default.isNumericLiteral(type.literal)) {
|
|
994
|
+
return type.literal.text;
|
|
995
|
+
}
|
|
996
|
+
}
|
|
997
|
+
return null;
|
|
998
|
+
}).filter(Boolean);
|
|
999
|
+
const matchingEnum = apiGen.aliases.find((alias) => {
|
|
1000
|
+
if (import_typescript4.default.isTypeAliasDeclaration(alias) && import_typescript4.default.isUnionTypeNode(alias.type)) {
|
|
1001
|
+
const aliasValues = alias.type.types.map((type) => {
|
|
1002
|
+
if (import_typescript4.default.isLiteralTypeNode(type)) {
|
|
1003
|
+
if (import_typescript4.default.isStringLiteral(type.literal)) {
|
|
1004
|
+
return type.literal.text;
|
|
1005
|
+
} else if (import_typescript4.default.isNumericLiteral(type.literal)) {
|
|
1006
|
+
return type.literal.text;
|
|
1007
|
+
}
|
|
1008
|
+
}
|
|
1009
|
+
return null;
|
|
1010
|
+
}).filter(Boolean);
|
|
1011
|
+
return aliasValues.length === enumValues.length && aliasValues.every((val) => enumValues.includes(val));
|
|
1012
|
+
}
|
|
1013
|
+
return false;
|
|
1014
|
+
});
|
|
1015
|
+
if (matchingEnum && import_typescript4.default.isTypeAliasDeclaration(matchingEnum)) {
|
|
1016
|
+
return typeNode;
|
|
1017
|
+
}
|
|
1018
|
+
}
|
|
885
1019
|
return factory.createUnionTypeNode(typeNode.types.map(wrapWithSchemeIfComponent));
|
|
886
1020
|
}
|
|
887
1021
|
if (import_typescript4.default.isTypeLiteralNode(typeNode)) {
|
|
@@ -1018,12 +1152,36 @@ function parseConfig(fullConfig) {
|
|
|
1018
1152
|
}, {});
|
|
1019
1153
|
Object.entries(groupedPaths).forEach(([groupName, paths2]) => {
|
|
1020
1154
|
const finalOutputPath = outputPath.replace("$1", groupName);
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1155
|
+
if (filterEndpoint) {
|
|
1156
|
+
const pathBasedFilter = (operationName, operationDefinition) => {
|
|
1157
|
+
const path4 = operationDefinition.path;
|
|
1158
|
+
const pathGroupName = getGroupNameFromPath(path4, pattern);
|
|
1159
|
+
if (pathGroupName !== groupName) {
|
|
1160
|
+
return false;
|
|
1161
|
+
}
|
|
1162
|
+
const endpointFilter = filterEndpoint(groupName);
|
|
1163
|
+
if (endpointFilter instanceof RegExp) {
|
|
1164
|
+
return endpointFilter.test(operationName);
|
|
1165
|
+
}
|
|
1166
|
+
return true;
|
|
1167
|
+
};
|
|
1168
|
+
outFiles.push({
|
|
1169
|
+
...commonConfig,
|
|
1170
|
+
outputFile: finalOutputPath,
|
|
1171
|
+
filterEndpoints: pathBasedFilter
|
|
1172
|
+
});
|
|
1173
|
+
} else {
|
|
1174
|
+
const pathBasedFilter = (operationName, operationDefinition) => {
|
|
1175
|
+
const path4 = operationDefinition.path;
|
|
1176
|
+
const pathGroupName = getGroupNameFromPath(path4, pattern);
|
|
1177
|
+
return pathGroupName === groupName;
|
|
1178
|
+
};
|
|
1179
|
+
outFiles.push({
|
|
1180
|
+
...commonConfig,
|
|
1181
|
+
outputFile: finalOutputPath,
|
|
1182
|
+
filterEndpoints: pathBasedFilter
|
|
1183
|
+
});
|
|
1184
|
+
}
|
|
1027
1185
|
});
|
|
1028
1186
|
} else {
|
|
1029
1187
|
outFiles.push(fullConfig);
|