@acrool/rtk-query-codegen-openapi 0.0.2 → 0.0.6
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 +8 -4
- package/lib/index.d.ts +8 -4
- package/lib/index.js +442 -112
- package/lib/index.js.map +1 -1
- package/lib/index.mjs +442 -112
- package/lib/index.mjs.map +1 -1
- package/package.json +11 -2
- package/src/generate.ts +327 -127
- package/src/generators/react-hooks.ts +2 -2
- package/src/index.ts +250 -9
- package/src/types.ts +9 -1
package/lib/index.js
CHANGED
|
@@ -326,14 +326,14 @@ function removeUndefined(t) {
|
|
|
326
326
|
|
|
327
327
|
// src/generators/react-hooks.ts
|
|
328
328
|
var createBinding = ({
|
|
329
|
-
operationDefinition: { verb, path: path4
|
|
329
|
+
operationDefinition: { verb, path: path4 },
|
|
330
330
|
overrides,
|
|
331
331
|
isLazy = false
|
|
332
332
|
}) => factory.createBindingElement(
|
|
333
333
|
void 0,
|
|
334
334
|
void 0,
|
|
335
335
|
factory.createIdentifier(
|
|
336
|
-
`use${isLazy ? "Lazy" : ""}${capitalize((0, import_generate.getOperationName)(verb, path4,
|
|
336
|
+
`use${isLazy ? "Lazy" : ""}${capitalize((0, import_generate.getOperationName)(verb, path4, void 0))}${isQuery(verb, overrides) ? "Query" : "Mutation"}`
|
|
337
337
|
),
|
|
338
338
|
void 0
|
|
339
339
|
);
|
|
@@ -387,8 +387,8 @@ function defaultIsDataResponse(code, includeDefault) {
|
|
|
387
387
|
const parsedCode = Number(code);
|
|
388
388
|
return !Number.isNaN(parsedCode) && parsedCode >= 200 && parsedCode < 300;
|
|
389
389
|
}
|
|
390
|
-
function getOperationName2({ verb, path: path4
|
|
391
|
-
return (0, import_generate3.getOperationName)(verb, path4,
|
|
390
|
+
function getOperationName2({ verb, path: path4 }) {
|
|
391
|
+
return (0, import_generate3.getOperationName)(verb, path4, void 0);
|
|
392
392
|
}
|
|
393
393
|
function getTags({ verb, pathItem }) {
|
|
394
394
|
return verb ? pathItem[verb]?.tags || [] : [];
|
|
@@ -462,41 +462,153 @@ async function generateApi(spec, {
|
|
|
462
462
|
useEnumType,
|
|
463
463
|
mergeReadWriteOnly
|
|
464
464
|
});
|
|
465
|
+
const schemeTypeNames = /* @__PURE__ */ new Set();
|
|
466
|
+
function addSchemeTypeName(name) {
|
|
467
|
+
schemeTypeNames.add(name);
|
|
468
|
+
schemeTypeNames.add((0, import_lodash.default)(name));
|
|
469
|
+
schemeTypeNames.add(capitalize((0, import_lodash.default)(name)));
|
|
470
|
+
}
|
|
465
471
|
if (sharedTypesFile) {
|
|
472
|
+
const resultFile2 = import_typescript4.default.createSourceFile(
|
|
473
|
+
"sharedTypes.ts",
|
|
474
|
+
"",
|
|
475
|
+
import_typescript4.default.ScriptTarget.Latest,
|
|
476
|
+
/*setParentNodes*/
|
|
477
|
+
false,
|
|
478
|
+
import_typescript4.default.ScriptKind.TS
|
|
479
|
+
);
|
|
480
|
+
const printer2 = import_typescript4.default.createPrinter({ newLine: import_typescript4.default.NewLineKind.LineFeed });
|
|
481
|
+
const allTypeDefinitions = [];
|
|
482
|
+
const definedTypeNames = /* @__PURE__ */ new Set();
|
|
466
483
|
const components = v3Doc.components;
|
|
467
484
|
if (components) {
|
|
468
|
-
const
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
import_typescript4.default.ScriptKind.TS
|
|
475
|
-
);
|
|
476
|
-
const printer2 = import_typescript4.default.createPrinter({ newLine: import_typescript4.default.NewLineKind.LineFeed });
|
|
477
|
-
const typeDefinitions = Object.entries(components).flatMap(([_, componentDefs]) => {
|
|
478
|
-
return Object.entries(componentDefs).map(([name, def]) => {
|
|
479
|
-
const typeNode = apiGen.getTypeFromSchema(def);
|
|
485
|
+
const componentDefinitions = Object.entries(components).map(([componentType, componentDefs]) => {
|
|
486
|
+
const typeEntries = Object.entries(componentDefs).map(([name, def]) => {
|
|
487
|
+
addSchemeTypeName(name);
|
|
488
|
+
const typeName = capitalize((0, import_lodash.default)(name));
|
|
489
|
+
definedTypeNames.add(typeName);
|
|
490
|
+
const typeNode = wrapWithSchemeIfComponent(apiGen.getTypeFromSchema(def));
|
|
480
491
|
return factory.createTypeAliasDeclaration(
|
|
481
492
|
[factory.createModifier(import_typescript4.default.SyntaxKind.ExportKeyword)],
|
|
482
|
-
factory.createIdentifier(
|
|
493
|
+
factory.createIdentifier(typeName),
|
|
483
494
|
void 0,
|
|
484
495
|
typeNode
|
|
485
496
|
);
|
|
486
497
|
});
|
|
498
|
+
return factory.createModuleDeclaration(
|
|
499
|
+
[factory.createModifier(import_typescript4.default.SyntaxKind.ExportKeyword)],
|
|
500
|
+
factory.createIdentifier("Scheme"),
|
|
501
|
+
factory.createModuleBlock(typeEntries),
|
|
502
|
+
import_typescript4.default.NodeFlags.Namespace
|
|
503
|
+
);
|
|
487
504
|
});
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
505
|
+
allTypeDefinitions.push(...componentDefinitions);
|
|
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
|
+
)
|
|
496
552
|
);
|
|
497
|
-
const fs2 = await import("node:fs/promises");
|
|
498
|
-
await fs2.writeFile(sharedTypesFile, output, "utf-8");
|
|
499
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
|
+
}
|
|
597
|
+
}
|
|
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 });
|
|
602
|
+
const output = printer2.printNode(
|
|
603
|
+
import_typescript4.default.EmitHint.Unspecified,
|
|
604
|
+
factory.createSourceFile(
|
|
605
|
+
allTypeDefinitions,
|
|
606
|
+
factory.createToken(import_typescript4.default.SyntaxKind.EndOfFileToken),
|
|
607
|
+
import_typescript4.default.NodeFlags.None
|
|
608
|
+
),
|
|
609
|
+
resultFile2
|
|
610
|
+
);
|
|
611
|
+
await fs2.writeFile(sharedTypesFile, output, "utf-8");
|
|
500
612
|
}
|
|
501
613
|
if (apiGen.spec.components?.schemas) {
|
|
502
614
|
apiGen.preprocessComponents(apiGen.spec.components.schemas);
|
|
@@ -527,16 +639,13 @@ async function generateApi(spec, {
|
|
|
527
639
|
apiFile = apiFile.replace(/\\/g, "/");
|
|
528
640
|
if (!apiFile.startsWith(".")) apiFile = `./${apiFile}`;
|
|
529
641
|
}
|
|
530
|
-
if (sharedTypesFile && sharedTypesFile.startsWith(".")) {
|
|
531
|
-
sharedTypesFile = import_node_path2.default.relative(import_node_path2.default.dirname(outputFile), sharedTypesFile);
|
|
532
|
-
sharedTypesFile = sharedTypesFile.replace(/\\/g, "/");
|
|
533
|
-
if (!sharedTypesFile.startsWith(".")) sharedTypesFile = `./${sharedTypesFile}`;
|
|
534
|
-
}
|
|
535
642
|
}
|
|
536
643
|
apiFile = apiFile.replace(/\.[jt]sx?$/, "");
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
644
|
+
const sharedTypesImportPath = sharedTypesFile && outputFile ? (() => {
|
|
645
|
+
let rel = import_node_path2.default.relative(import_node_path2.default.dirname(outputFile), sharedTypesFile).replace(/\\/g, "/").replace(/\.[jt]sx?$/, "");
|
|
646
|
+
if (!rel.startsWith(".")) rel = "./" + rel;
|
|
647
|
+
return rel;
|
|
648
|
+
})() : "./shared-types";
|
|
540
649
|
return printer.printNode(
|
|
541
650
|
import_typescript4.default.EmitHint.Unspecified,
|
|
542
651
|
factory.createSourceFile(
|
|
@@ -544,16 +653,10 @@ async function generateApi(spec, {
|
|
|
544
653
|
generateImportNode(apiFile, { [apiImport]: "api" }),
|
|
545
654
|
generateImportNode("@acrool/react-fetcher", { IRestFulEndpointsQueryReturn: "IRestFulEndpointsQueryReturn" }),
|
|
546
655
|
...sharedTypesFile ? [
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
void 0,
|
|
552
|
-
factory.createNamespaceImport(factory.createIdentifier("SharedTypes"))
|
|
553
|
-
),
|
|
554
|
-
factory.createStringLiteral(sharedTypesFile),
|
|
555
|
-
void 0
|
|
556
|
-
)
|
|
656
|
+
generateImportNode(sharedTypesImportPath, {
|
|
657
|
+
Scheme: "Scheme",
|
|
658
|
+
...useEnumType ? { Enum: "Enum" } : {}
|
|
659
|
+
})
|
|
557
660
|
] : [],
|
|
558
661
|
...tag ? [generateTagTypes({ addTagTypes: extractAllTagTypes({ operationDefinitions }) })] : [],
|
|
559
662
|
generateCreateApiCall({
|
|
@@ -562,7 +665,8 @@ async function generateApi(spec, {
|
|
|
562
665
|
operationDefinitions.map(
|
|
563
666
|
(operationDefinition) => generateEndpoint({
|
|
564
667
|
operationDefinition,
|
|
565
|
-
overrides: getOverrides(operationDefinition, endpointOverrides)
|
|
668
|
+
overrides: getOverrides(operationDefinition, endpointOverrides),
|
|
669
|
+
sharedTypesFile: !!sharedTypesFile
|
|
566
670
|
})
|
|
567
671
|
),
|
|
568
672
|
true
|
|
@@ -601,7 +705,8 @@ async function generateApi(spec, {
|
|
|
601
705
|
}
|
|
602
706
|
function generateEndpoint({
|
|
603
707
|
operationDefinition,
|
|
604
|
-
overrides
|
|
708
|
+
overrides,
|
|
709
|
+
sharedTypesFile: sharedTypesFile2
|
|
605
710
|
}) {
|
|
606
711
|
const {
|
|
607
712
|
verb,
|
|
@@ -610,60 +715,30 @@ async function generateApi(spec, {
|
|
|
610
715
|
operation,
|
|
611
716
|
operation: { responses, requestBody }
|
|
612
717
|
} = operationDefinition;
|
|
613
|
-
const operationName = getOperationName2({ verb, path: path4
|
|
718
|
+
const operationName = getOperationName2({ verb, path: path4 });
|
|
614
719
|
const tags = tag ? getTags({ verb, pathItem }) : [];
|
|
615
720
|
const isQuery2 = isQuery(verb, overrides);
|
|
616
721
|
const returnsJson = apiGen.getResponseType(responses) === "json";
|
|
617
722
|
let ResponseType = factory.createKeywordTypeNode(import_typescript4.default.SyntaxKind.UnknownKeyword);
|
|
618
|
-
function replaceReferences(schema) {
|
|
619
|
-
if (!schema) return factory.createKeywordTypeNode(import_typescript4.default.SyntaxKind.UnknownKeyword);
|
|
620
|
-
const refName = (0, import_generate3.getReferenceName)(schema);
|
|
621
|
-
if (refName && sharedTypesFile) {
|
|
622
|
-
return factory.createTypeReferenceNode(
|
|
623
|
-
factory.createQualifiedName(
|
|
624
|
-
factory.createIdentifier("SharedTypes"),
|
|
625
|
-
factory.createIdentifier(refName)
|
|
626
|
-
),
|
|
627
|
-
void 0
|
|
628
|
-
);
|
|
629
|
-
}
|
|
630
|
-
if (schema.type === "object" && schema.properties) {
|
|
631
|
-
const members = Object.entries(schema.properties).map(([key, value]) => {
|
|
632
|
-
return factory.createPropertySignature(
|
|
633
|
-
void 0,
|
|
634
|
-
factory.createIdentifier(key),
|
|
635
|
-
schema.required?.includes(key) ? void 0 : factory.createToken(import_typescript4.default.SyntaxKind.QuestionToken),
|
|
636
|
-
replaceReferences(value)
|
|
637
|
-
);
|
|
638
|
-
});
|
|
639
|
-
return factory.createTypeLiteralNode(members);
|
|
640
|
-
}
|
|
641
|
-
if (schema.type === "array" && schema.items) {
|
|
642
|
-
return factory.createArrayTypeNode(replaceReferences(schema.items));
|
|
643
|
-
}
|
|
644
|
-
return apiGen.getTypeFromSchema(schema);
|
|
645
|
-
}
|
|
646
723
|
if (returnsJson) {
|
|
647
724
|
const returnTypes = Object.entries(responses || {}).map(
|
|
648
|
-
([code, response]) =>
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
return [code, resolvedResponse, type];
|
|
656
|
-
}
|
|
725
|
+
([code, response]) => [
|
|
726
|
+
code,
|
|
727
|
+
apiGen.resolve(response),
|
|
728
|
+
wrapWithSchemeIfComponent(
|
|
729
|
+
apiGen.getTypeFromResponse(response, "readOnly") || factory.createKeywordTypeNode(import_typescript4.default.SyntaxKind.UndefinedKeyword)
|
|
730
|
+
)
|
|
731
|
+
]
|
|
657
732
|
).filter(
|
|
658
733
|
([status, response]) => isDataResponse(status, includeDefault, apiGen.resolve(response), responses || {})
|
|
659
|
-
).filter(([_1, _2, type]) => type !== import_generate3.keywordType.void).map(
|
|
660
|
-
|
|
661
|
-
|
|
734
|
+
).filter(([_1, _2, type]) => type !== import_generate3.keywordType.void).map(([code, response, type]) => {
|
|
735
|
+
return import_typescript4.default.addSyntheticLeadingComment(
|
|
736
|
+
type,
|
|
662
737
|
import_typescript4.default.SyntaxKind.MultiLineCommentTrivia,
|
|
663
738
|
`* status ${code} ${response.description} `,
|
|
664
739
|
false
|
|
665
|
-
)
|
|
666
|
-
);
|
|
740
|
+
);
|
|
741
|
+
});
|
|
667
742
|
if (returnTypes.length > 0) {
|
|
668
743
|
ResponseType = factory.createUnionTypeNode(returnTypes);
|
|
669
744
|
}
|
|
@@ -698,10 +773,21 @@ async function generateApi(spec, {
|
|
|
698
773
|
}
|
|
699
774
|
return name;
|
|
700
775
|
}
|
|
776
|
+
for (const param of parameters) {
|
|
777
|
+
const name = generateName(param.name, param.in);
|
|
778
|
+
queryArg[name] = {
|
|
779
|
+
origin: "param",
|
|
780
|
+
name,
|
|
781
|
+
originalName: param.name,
|
|
782
|
+
type: wrapWithSchemeIfComponent(apiGen.getTypeFromSchema((0, import_generate3.isReference)(param) ? param : param.schema, void 0, "writeOnly")),
|
|
783
|
+
required: param.required,
|
|
784
|
+
param
|
|
785
|
+
};
|
|
786
|
+
}
|
|
701
787
|
if (requestBody) {
|
|
702
788
|
const body = apiGen.resolve(requestBody);
|
|
703
789
|
const schema = apiGen.getSchemaFromContent(body.content);
|
|
704
|
-
const type =
|
|
790
|
+
const type = wrapWithSchemeIfComponent(apiGen.getTypeFromSchema(schema));
|
|
705
791
|
const schemaName = (0, import_lodash.default)(
|
|
706
792
|
type.name || (0, import_generate3.getReferenceName)(schema) || typeof schema === "object" && "title" in schema && schema.title || "body"
|
|
707
793
|
);
|
|
@@ -710,24 +796,11 @@ async function generateApi(spec, {
|
|
|
710
796
|
origin: "body",
|
|
711
797
|
name,
|
|
712
798
|
originalName: schemaName,
|
|
713
|
-
type,
|
|
799
|
+
type: wrapWithSchemeIfComponent(apiGen.getTypeFromSchema(schema, void 0, "writeOnly")),
|
|
714
800
|
required: true,
|
|
715
801
|
body
|
|
716
802
|
};
|
|
717
803
|
}
|
|
718
|
-
for (const param of parameters) {
|
|
719
|
-
const name = generateName(param.name, param.in);
|
|
720
|
-
const paramSchema = (0, import_generate3.isReference)(param) ? param : param.schema;
|
|
721
|
-
const type = replaceReferences(paramSchema);
|
|
722
|
-
queryArg[name] = {
|
|
723
|
-
origin: "param",
|
|
724
|
-
name,
|
|
725
|
-
originalName: param.name,
|
|
726
|
-
type,
|
|
727
|
-
required: param.required,
|
|
728
|
-
param
|
|
729
|
-
};
|
|
730
|
-
}
|
|
731
804
|
const propertyName = (name) => {
|
|
732
805
|
if (typeof name === "string") {
|
|
733
806
|
return (0, import_generate3.isValidIdentifier)(name) ? factory.createIdentifier(name) : factory.createStringLiteral(name);
|
|
@@ -864,6 +937,105 @@ async function generateApi(spec, {
|
|
|
864
937
|
function generateMutationEndpointProps({}) {
|
|
865
938
|
return {};
|
|
866
939
|
}
|
|
940
|
+
function wrapWithSchemeIfComponent(typeNode) {
|
|
941
|
+
if (import_typescript4.default.isTypeReferenceNode(typeNode) && import_typescript4.default.isIdentifier(typeNode.typeName)) {
|
|
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
|
+
}
|
|
965
|
+
if (schemeTypeNames.has(typeName)) {
|
|
966
|
+
return factory.createTypeReferenceNode(
|
|
967
|
+
factory.createQualifiedName(
|
|
968
|
+
factory.createIdentifier("Scheme"),
|
|
969
|
+
typeNode.typeName
|
|
970
|
+
),
|
|
971
|
+
typeNode.typeArguments?.map(wrapWithSchemeIfComponent)
|
|
972
|
+
);
|
|
973
|
+
}
|
|
974
|
+
if (typeNode.typeArguments) {
|
|
975
|
+
return factory.createTypeReferenceNode(
|
|
976
|
+
typeNode.typeName,
|
|
977
|
+
typeNode.typeArguments.map(wrapWithSchemeIfComponent)
|
|
978
|
+
);
|
|
979
|
+
}
|
|
980
|
+
}
|
|
981
|
+
if (import_typescript4.default.isArrayTypeNode(typeNode)) {
|
|
982
|
+
return factory.createArrayTypeNode(wrapWithSchemeIfComponent(typeNode.elementType));
|
|
983
|
+
}
|
|
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
|
+
}
|
|
1019
|
+
return factory.createUnionTypeNode(typeNode.types.map(wrapWithSchemeIfComponent));
|
|
1020
|
+
}
|
|
1021
|
+
if (import_typescript4.default.isTypeLiteralNode(typeNode)) {
|
|
1022
|
+
return factory.createTypeLiteralNode(
|
|
1023
|
+
typeNode.members.map((member) => {
|
|
1024
|
+
if (import_typescript4.default.isPropertySignature(member) && member.type) {
|
|
1025
|
+
return factory.updatePropertySignature(
|
|
1026
|
+
member,
|
|
1027
|
+
member.modifiers,
|
|
1028
|
+
member.name,
|
|
1029
|
+
member.questionToken,
|
|
1030
|
+
wrapWithSchemeIfComponent(member.type)
|
|
1031
|
+
);
|
|
1032
|
+
}
|
|
1033
|
+
return member;
|
|
1034
|
+
})
|
|
1035
|
+
);
|
|
1036
|
+
}
|
|
1037
|
+
return typeNode;
|
|
1038
|
+
}
|
|
867
1039
|
}
|
|
868
1040
|
function generatePathExpression(path4, pathParameters, rootObject, isFlatArg, encodePathParams) {
|
|
869
1041
|
const expressions = [];
|
|
@@ -891,8 +1063,115 @@ function generatePathExpression(path4, pathParameters, rootObject, isFlatArg, en
|
|
|
891
1063
|
}
|
|
892
1064
|
|
|
893
1065
|
// src/index.ts
|
|
1066
|
+
var import_lodash2 = __toESM(require("lodash.camelcase"));
|
|
894
1067
|
var require2 = (0, import_node_module.createRequire)(__filename);
|
|
1068
|
+
async function ensureDirectoryExists(filePath) {
|
|
1069
|
+
const dirname = import_node_path3.default.dirname(filePath);
|
|
1070
|
+
if (!import_node_fs.default.existsSync(dirname)) {
|
|
1071
|
+
await import_node_fs.default.promises.mkdir(dirname, { recursive: true });
|
|
1072
|
+
}
|
|
1073
|
+
}
|
|
1074
|
+
function fileExists(filePath) {
|
|
1075
|
+
try {
|
|
1076
|
+
return import_node_fs.default.statSync(filePath).isFile();
|
|
1077
|
+
} catch {
|
|
1078
|
+
return false;
|
|
1079
|
+
}
|
|
1080
|
+
}
|
|
1081
|
+
function getApiNameFromDir(dirPath) {
|
|
1082
|
+
const dirName = import_node_path3.default.basename(dirPath);
|
|
1083
|
+
return `${dirName}Api`;
|
|
1084
|
+
}
|
|
1085
|
+
async function ensureBaseFilesExist(outputDir) {
|
|
1086
|
+
const enhanceEndpointsPath = import_node_path3.default.join(outputDir, "enhanceEndpoints.ts");
|
|
1087
|
+
const indexPath = import_node_path3.default.join(outputDir, "index.ts");
|
|
1088
|
+
const apiName = getApiNameFromDir(outputDir);
|
|
1089
|
+
if (!fileExists(enhanceEndpointsPath)) {
|
|
1090
|
+
const enhanceEndpointsContent = `import api from './query.generated';
|
|
1091
|
+
|
|
1092
|
+
const enhancedApi = api.enhanceEndpoints({
|
|
1093
|
+
endpoints: {
|
|
1094
|
+
},
|
|
1095
|
+
});
|
|
1096
|
+
|
|
1097
|
+
export default enhancedApi;
|
|
1098
|
+
`;
|
|
1099
|
+
await import_node_fs.default.promises.writeFile(enhanceEndpointsPath, enhanceEndpointsContent, "utf-8");
|
|
1100
|
+
}
|
|
1101
|
+
if (!fileExists(indexPath)) {
|
|
1102
|
+
const indexContent = `export * from './query.generated';
|
|
1103
|
+
export {default as ${apiName}} from './enhanceEndpoints';
|
|
1104
|
+
`;
|
|
1105
|
+
await import_node_fs.default.promises.writeFile(indexPath, indexContent, "utf-8");
|
|
1106
|
+
}
|
|
1107
|
+
}
|
|
1108
|
+
function getGroupNameFromPath(path4, pattern) {
|
|
1109
|
+
const match = path4.match(pattern);
|
|
1110
|
+
if (match && match[1]) {
|
|
1111
|
+
return (0, import_lodash2.default)(match[1]);
|
|
1112
|
+
}
|
|
1113
|
+
return "common";
|
|
1114
|
+
}
|
|
895
1115
|
async function generateEndpoints(options) {
|
|
1116
|
+
const schemaLocation = options.schemaFile;
|
|
1117
|
+
const schemaAbsPath = isValidUrl(options.schemaFile) ? options.schemaFile : import_node_path3.default.resolve(process.cwd(), schemaLocation);
|
|
1118
|
+
if (isValidUrl(options.schemaFile) && "outputFiles" in options) {
|
|
1119
|
+
const { outputFiles, ...commonConfig } = options;
|
|
1120
|
+
const openApiDoc = await getV3Doc(options.schemaFile, options.httpResolverOptions);
|
|
1121
|
+
const paths = Object.keys(openApiDoc.paths);
|
|
1122
|
+
const outputFilesEntries = Object.entries(outputFiles);
|
|
1123
|
+
const [outputPath, config] = outputFilesEntries[0];
|
|
1124
|
+
const patterns = config.groupMatch;
|
|
1125
|
+
const filterEndpoint = config.filterEndpoint;
|
|
1126
|
+
const pattern = patterns;
|
|
1127
|
+
const groupedPaths = paths.reduce((acc, path4) => {
|
|
1128
|
+
const groupName = getGroupNameFromPath(path4, pattern);
|
|
1129
|
+
if (!acc[groupName]) {
|
|
1130
|
+
acc[groupName] = [];
|
|
1131
|
+
}
|
|
1132
|
+
acc[groupName].push(path4);
|
|
1133
|
+
return acc;
|
|
1134
|
+
}, {});
|
|
1135
|
+
for (const [groupName, paths2] of Object.entries(groupedPaths)) {
|
|
1136
|
+
const finalOutputPath = outputPath.replace("$1", groupName);
|
|
1137
|
+
if (filterEndpoint) {
|
|
1138
|
+
const pathBasedFilter = (operationName, operationDefinition) => {
|
|
1139
|
+
const path4 = operationDefinition.path;
|
|
1140
|
+
const pathGroupName = getGroupNameFromPath(path4, pattern);
|
|
1141
|
+
if (pathGroupName !== groupName) {
|
|
1142
|
+
return false;
|
|
1143
|
+
}
|
|
1144
|
+
const endpointFilter = filterEndpoint(groupName);
|
|
1145
|
+
if (endpointFilter instanceof RegExp) {
|
|
1146
|
+
return endpointFilter.test(operationName);
|
|
1147
|
+
}
|
|
1148
|
+
return true;
|
|
1149
|
+
};
|
|
1150
|
+
const groupOptions = {
|
|
1151
|
+
...commonConfig,
|
|
1152
|
+
outputFile: finalOutputPath,
|
|
1153
|
+
filterEndpoints: pathBasedFilter
|
|
1154
|
+
};
|
|
1155
|
+
await generateSingleEndpoint(groupOptions);
|
|
1156
|
+
} else {
|
|
1157
|
+
const pathBasedFilter = (operationName, operationDefinition) => {
|
|
1158
|
+
const path4 = operationDefinition.path;
|
|
1159
|
+
const pathGroupName = getGroupNameFromPath(path4, pattern);
|
|
1160
|
+
return pathGroupName === groupName;
|
|
1161
|
+
};
|
|
1162
|
+
const groupOptions = {
|
|
1163
|
+
...commonConfig,
|
|
1164
|
+
outputFile: finalOutputPath,
|
|
1165
|
+
filterEndpoints: pathBasedFilter
|
|
1166
|
+
};
|
|
1167
|
+
await generateSingleEndpoint(groupOptions);
|
|
1168
|
+
}
|
|
1169
|
+
}
|
|
1170
|
+
return;
|
|
1171
|
+
}
|
|
1172
|
+
await generateSingleEndpoint(options);
|
|
1173
|
+
}
|
|
1174
|
+
async function generateSingleEndpoint(options) {
|
|
896
1175
|
const schemaLocation = options.schemaFile;
|
|
897
1176
|
const schemaAbsPath = isValidUrl(options.schemaFile) ? options.schemaFile : import_node_path3.default.resolve(process.cwd(), schemaLocation);
|
|
898
1177
|
const sourceCode = await enforceOazapftsTsVersion(async () => {
|
|
@@ -900,8 +1179,12 @@ async function generateEndpoints(options) {
|
|
|
900
1179
|
});
|
|
901
1180
|
const { outputFile, prettierConfigFile } = options;
|
|
902
1181
|
if (outputFile) {
|
|
1182
|
+
const outputPath = import_node_path3.default.resolve(process.cwd(), outputFile);
|
|
1183
|
+
await ensureDirectoryExists(outputPath);
|
|
1184
|
+
const outputDir = import_node_path3.default.dirname(outputPath);
|
|
1185
|
+
await ensureBaseFilesExist(outputDir);
|
|
903
1186
|
import_node_fs.default.writeFileSync(
|
|
904
|
-
|
|
1187
|
+
outputPath,
|
|
905
1188
|
await prettify(outputFile, sourceCode, prettierConfigFile)
|
|
906
1189
|
);
|
|
907
1190
|
} else {
|
|
@@ -912,13 +1195,60 @@ function parseConfig(fullConfig) {
|
|
|
912
1195
|
const outFiles = [];
|
|
913
1196
|
if ("outputFiles" in fullConfig) {
|
|
914
1197
|
const { outputFiles, ...commonConfig } = fullConfig;
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
1198
|
+
let openApiDoc;
|
|
1199
|
+
if (isValidUrl(fullConfig.schemaFile)) {
|
|
1200
|
+
outFiles.push(fullConfig);
|
|
1201
|
+
return outFiles;
|
|
1202
|
+
} else {
|
|
1203
|
+
openApiDoc = JSON.parse(import_node_fs.default.readFileSync(fullConfig.schemaFile, "utf-8"));
|
|
921
1204
|
}
|
|
1205
|
+
const paths = Object.keys(openApiDoc.paths);
|
|
1206
|
+
const outputFilesEntries = Object.entries(outputFiles);
|
|
1207
|
+
const [outputPath, config] = outputFilesEntries[0];
|
|
1208
|
+
const patterns = config.groupMatch;
|
|
1209
|
+
const filterEndpoint = config.filterEndpoint;
|
|
1210
|
+
const pattern = patterns;
|
|
1211
|
+
const groupedPaths = paths.reduce((acc, path4) => {
|
|
1212
|
+
const groupName = getGroupNameFromPath(path4, pattern);
|
|
1213
|
+
if (!acc[groupName]) {
|
|
1214
|
+
acc[groupName] = [];
|
|
1215
|
+
}
|
|
1216
|
+
acc[groupName].push(path4);
|
|
1217
|
+
return acc;
|
|
1218
|
+
}, {});
|
|
1219
|
+
Object.entries(groupedPaths).forEach(([groupName, paths2]) => {
|
|
1220
|
+
const finalOutputPath = outputPath.replace("$1", groupName);
|
|
1221
|
+
if (filterEndpoint) {
|
|
1222
|
+
const pathBasedFilter = (operationName, operationDefinition) => {
|
|
1223
|
+
const path4 = operationDefinition.path;
|
|
1224
|
+
const pathGroupName = getGroupNameFromPath(path4, pattern);
|
|
1225
|
+
if (pathGroupName !== groupName) {
|
|
1226
|
+
return false;
|
|
1227
|
+
}
|
|
1228
|
+
const endpointFilter = filterEndpoint(groupName);
|
|
1229
|
+
if (endpointFilter instanceof RegExp) {
|
|
1230
|
+
return endpointFilter.test(operationName);
|
|
1231
|
+
}
|
|
1232
|
+
return true;
|
|
1233
|
+
};
|
|
1234
|
+
outFiles.push({
|
|
1235
|
+
...commonConfig,
|
|
1236
|
+
outputFile: finalOutputPath,
|
|
1237
|
+
filterEndpoints: pathBasedFilter
|
|
1238
|
+
});
|
|
1239
|
+
} else {
|
|
1240
|
+
const pathBasedFilter = (operationName, operationDefinition) => {
|
|
1241
|
+
const path4 = operationDefinition.path;
|
|
1242
|
+
const pathGroupName = getGroupNameFromPath(path4, pattern);
|
|
1243
|
+
return pathGroupName === groupName;
|
|
1244
|
+
};
|
|
1245
|
+
outFiles.push({
|
|
1246
|
+
...commonConfig,
|
|
1247
|
+
outputFile: finalOutputPath,
|
|
1248
|
+
filterEndpoints: pathBasedFilter
|
|
1249
|
+
});
|
|
1250
|
+
}
|
|
1251
|
+
});
|
|
922
1252
|
} else {
|
|
923
1253
|
outFiles.push(fullConfig);
|
|
924
1254
|
}
|