@acrool/rtk-query-codegen-openapi 0.0.2-test.6 → 0.0.2-test.7
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 +219 -113
- package/lib/index.js.map +1 -1
- package/lib/index.mjs +219 -113
- package/lib/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/generate.ts +162 -128
- package/src/generators/react-hooks.ts +2 -2
- package/src/index.ts +120 -8
- package/src/types.ts +9 -1
package/lib/index.d.mts
CHANGED
|
@@ -130,13 +130,17 @@ type EndpointOverrides = {
|
|
|
130
130
|
type: 'mutation' | 'query';
|
|
131
131
|
parameterFilter: ParameterMatcher;
|
|
132
132
|
}>;
|
|
133
|
-
type
|
|
134
|
-
|
|
135
|
-
|
|
133
|
+
type OutputFilesConfig = {
|
|
134
|
+
[outputFile: string]: {
|
|
135
|
+
groupMatch: RegExp;
|
|
136
|
+
filterEndpoint: (groupName: string) => RegExp;
|
|
136
137
|
};
|
|
138
|
+
};
|
|
139
|
+
type ConfigFile = Id<Require<CommonOptions & OutputFileOptions, 'outputFile'>> | Id<Omit<CommonOptions, 'outputFile'> & {
|
|
140
|
+
outputFiles: OutputFilesConfig;
|
|
137
141
|
}>;
|
|
138
142
|
|
|
139
143
|
declare function generateEndpoints(options: GenerationOptions): Promise<string | void>;
|
|
140
144
|
declare function parseConfig(fullConfig: ConfigFile): (CommonOptions & OutputFileOptions)[];
|
|
141
145
|
|
|
142
|
-
export { type ConfigFile, generateEndpoints, parseConfig };
|
|
146
|
+
export { type ConfigFile, type OutputFilesConfig, generateEndpoints, parseConfig };
|
package/lib/index.d.ts
CHANGED
|
@@ -130,13 +130,17 @@ type EndpointOverrides = {
|
|
|
130
130
|
type: 'mutation' | 'query';
|
|
131
131
|
parameterFilter: ParameterMatcher;
|
|
132
132
|
}>;
|
|
133
|
-
type
|
|
134
|
-
|
|
135
|
-
|
|
133
|
+
type OutputFilesConfig = {
|
|
134
|
+
[outputFile: string]: {
|
|
135
|
+
groupMatch: RegExp;
|
|
136
|
+
filterEndpoint: (groupName: string) => RegExp;
|
|
136
137
|
};
|
|
138
|
+
};
|
|
139
|
+
type ConfigFile = Id<Require<CommonOptions & OutputFileOptions, 'outputFile'>> | Id<Omit<CommonOptions, 'outputFile'> & {
|
|
140
|
+
outputFiles: OutputFilesConfig;
|
|
137
141
|
}>;
|
|
138
142
|
|
|
139
143
|
declare function generateEndpoints(options: GenerationOptions): Promise<string | void>;
|
|
140
144
|
declare function parseConfig(fullConfig: ConfigFile): (CommonOptions & OutputFileOptions)[];
|
|
141
145
|
|
|
142
|
-
export { type ConfigFile, generateEndpoints, parseConfig };
|
|
146
|
+
export { type ConfigFile, type OutputFilesConfig, generateEndpoints, parseConfig };
|
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,61 @@ 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 = [];
|
|
466
482
|
const components = v3Doc.components;
|
|
467
483
|
if (components) {
|
|
468
|
-
const
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
false,
|
|
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);
|
|
484
|
+
const componentDefinitions = Object.entries(components).map(([componentType, componentDefs]) => {
|
|
485
|
+
const typeEntries = Object.entries(componentDefs).map(([name, def]) => {
|
|
486
|
+
addSchemeTypeName(name);
|
|
487
|
+
const typeName = capitalize((0, import_lodash.default)(name));
|
|
488
|
+
const typeNode = wrapWithSchemeIfComponent(apiGen.getTypeFromSchema(def));
|
|
480
489
|
return factory.createTypeAliasDeclaration(
|
|
481
490
|
[factory.createModifier(import_typescript4.default.SyntaxKind.ExportKeyword)],
|
|
482
|
-
factory.createIdentifier(
|
|
491
|
+
factory.createIdentifier(typeName),
|
|
483
492
|
void 0,
|
|
484
493
|
typeNode
|
|
485
494
|
);
|
|
486
495
|
});
|
|
496
|
+
return factory.createModuleDeclaration(
|
|
497
|
+
[factory.createModifier(import_typescript4.default.SyntaxKind.ExportKeyword)],
|
|
498
|
+
factory.createIdentifier("Scheme"),
|
|
499
|
+
factory.createModuleBlock(typeEntries),
|
|
500
|
+
import_typescript4.default.NodeFlags.Namespace
|
|
501
|
+
);
|
|
487
502
|
});
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
factory.createToken(import_typescript4.default.SyntaxKind.EndOfFileToken),
|
|
493
|
-
import_typescript4.default.NodeFlags.None
|
|
494
|
-
),
|
|
495
|
-
resultFile2
|
|
496
|
-
);
|
|
497
|
-
const fs2 = await import("node:fs/promises");
|
|
498
|
-
await fs2.writeFile(sharedTypesFile, output, "utf-8");
|
|
503
|
+
allTypeDefinitions.push(...componentDefinitions);
|
|
504
|
+
}
|
|
505
|
+
if (useEnumType) {
|
|
506
|
+
allTypeDefinitions.push(...apiGen.enumAliases);
|
|
499
507
|
}
|
|
508
|
+
allTypeDefinitions.push(...apiGen.aliases);
|
|
509
|
+
const output = printer2.printNode(
|
|
510
|
+
import_typescript4.default.EmitHint.Unspecified,
|
|
511
|
+
factory.createSourceFile(
|
|
512
|
+
allTypeDefinitions,
|
|
513
|
+
factory.createToken(import_typescript4.default.SyntaxKind.EndOfFileToken),
|
|
514
|
+
import_typescript4.default.NodeFlags.None
|
|
515
|
+
),
|
|
516
|
+
resultFile2
|
|
517
|
+
);
|
|
518
|
+
const fs2 = await import("node:fs/promises");
|
|
519
|
+
await fs2.writeFile(sharedTypesFile, output, "utf-8");
|
|
500
520
|
}
|
|
501
521
|
if (apiGen.spec.components?.schemas) {
|
|
502
522
|
apiGen.preprocessComponents(apiGen.spec.components.schemas);
|
|
@@ -527,34 +547,20 @@ async function generateApi(spec, {
|
|
|
527
547
|
apiFile = apiFile.replace(/\\/g, "/");
|
|
528
548
|
if (!apiFile.startsWith(".")) apiFile = `./${apiFile}`;
|
|
529
549
|
}
|
|
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
550
|
}
|
|
536
551
|
apiFile = apiFile.replace(/\.[jt]sx?$/, "");
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
552
|
+
const sharedTypesImportPath = sharedTypesFile && outputFile ? (() => {
|
|
553
|
+
let rel = import_node_path2.default.relative(import_node_path2.default.dirname(outputFile), sharedTypesFile).replace(/\\/g, "/").replace(/\.[jt]sx?$/, "");
|
|
554
|
+
if (!rel.startsWith(".")) rel = "./" + rel;
|
|
555
|
+
return rel;
|
|
556
|
+
})() : "./shared-types";
|
|
540
557
|
return printer.printNode(
|
|
541
558
|
import_typescript4.default.EmitHint.Unspecified,
|
|
542
559
|
factory.createSourceFile(
|
|
543
560
|
[
|
|
544
561
|
generateImportNode(apiFile, { [apiImport]: "api" }),
|
|
545
562
|
generateImportNode("@acrool/react-fetcher", { IRestFulEndpointsQueryReturn: "IRestFulEndpointsQueryReturn" }),
|
|
546
|
-
...sharedTypesFile ? [
|
|
547
|
-
factory.createImportDeclaration(
|
|
548
|
-
void 0,
|
|
549
|
-
factory.createImportClause(
|
|
550
|
-
false,
|
|
551
|
-
void 0,
|
|
552
|
-
factory.createNamespaceImport(factory.createIdentifier("SharedTypes"))
|
|
553
|
-
),
|
|
554
|
-
factory.createStringLiteral(sharedTypesFile),
|
|
555
|
-
void 0
|
|
556
|
-
)
|
|
557
|
-
] : [],
|
|
563
|
+
...sharedTypesFile ? [generateImportNode(sharedTypesImportPath, { Scheme: "Scheme" })] : [],
|
|
558
564
|
...tag ? [generateTagTypes({ addTagTypes: extractAllTagTypes({ operationDefinitions }) })] : [],
|
|
559
565
|
generateCreateApiCall({
|
|
560
566
|
tag,
|
|
@@ -562,7 +568,8 @@ async function generateApi(spec, {
|
|
|
562
568
|
operationDefinitions.map(
|
|
563
569
|
(operationDefinition) => generateEndpoint({
|
|
564
570
|
operationDefinition,
|
|
565
|
-
overrides: getOverrides(operationDefinition, endpointOverrides)
|
|
571
|
+
overrides: getOverrides(operationDefinition, endpointOverrides),
|
|
572
|
+
sharedTypesFile: !!sharedTypesFile
|
|
566
573
|
})
|
|
567
574
|
),
|
|
568
575
|
true
|
|
@@ -601,7 +608,8 @@ async function generateApi(spec, {
|
|
|
601
608
|
}
|
|
602
609
|
function generateEndpoint({
|
|
603
610
|
operationDefinition,
|
|
604
|
-
overrides
|
|
611
|
+
overrides,
|
|
612
|
+
sharedTypesFile: sharedTypesFile2
|
|
605
613
|
}) {
|
|
606
614
|
const {
|
|
607
615
|
verb,
|
|
@@ -610,60 +618,49 @@ async function generateApi(spec, {
|
|
|
610
618
|
operation,
|
|
611
619
|
operation: { responses, requestBody }
|
|
612
620
|
} = operationDefinition;
|
|
613
|
-
const operationName = getOperationName2({ verb, path: path4
|
|
621
|
+
const operationName = getOperationName2({ verb, path: path4 });
|
|
614
622
|
const tags = tag ? getTags({ verb, pathItem }) : [];
|
|
615
623
|
const isQuery2 = isQuery(verb, overrides);
|
|
616
624
|
const returnsJson = apiGen.getResponseType(responses) === "json";
|
|
617
625
|
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
626
|
if (returnsJson) {
|
|
647
627
|
const returnTypes = Object.entries(responses || {}).map(
|
|
648
|
-
([code, response]) =>
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
return [code, resolvedResponse, type];
|
|
656
|
-
}
|
|
628
|
+
([code, response]) => [
|
|
629
|
+
code,
|
|
630
|
+
apiGen.resolve(response),
|
|
631
|
+
wrapWithSchemeIfComponent(
|
|
632
|
+
apiGen.getTypeFromResponse(response, "readOnly") || factory.createKeywordTypeNode(import_typescript4.default.SyntaxKind.UndefinedKeyword)
|
|
633
|
+
)
|
|
634
|
+
]
|
|
657
635
|
).filter(
|
|
658
636
|
([status, response]) => isDataResponse(status, includeDefault, apiGen.resolve(response), responses || {})
|
|
659
|
-
).filter(([_1, _2, type]) => type !== import_generate3.keywordType.void).map(
|
|
660
|
-
(
|
|
661
|
-
|
|
637
|
+
).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
|
+
return import_typescript4.default.addSyntheticLeadingComment(
|
|
658
|
+
type,
|
|
662
659
|
import_typescript4.default.SyntaxKind.MultiLineCommentTrivia,
|
|
663
660
|
`* status ${code} ${response.description} `,
|
|
664
661
|
false
|
|
665
|
-
)
|
|
666
|
-
);
|
|
662
|
+
);
|
|
663
|
+
});
|
|
667
664
|
if (returnTypes.length > 0) {
|
|
668
665
|
ResponseType = factory.createUnionTypeNode(returnTypes);
|
|
669
666
|
}
|
|
@@ -698,10 +695,21 @@ async function generateApi(spec, {
|
|
|
698
695
|
}
|
|
699
696
|
return name;
|
|
700
697
|
}
|
|
698
|
+
for (const param of parameters) {
|
|
699
|
+
const name = generateName(param.name, param.in);
|
|
700
|
+
queryArg[name] = {
|
|
701
|
+
origin: "param",
|
|
702
|
+
name,
|
|
703
|
+
originalName: param.name,
|
|
704
|
+
type: wrapWithSchemeIfComponent(apiGen.getTypeFromSchema((0, import_generate3.isReference)(param) ? param : param.schema, void 0, "writeOnly")),
|
|
705
|
+
required: param.required,
|
|
706
|
+
param
|
|
707
|
+
};
|
|
708
|
+
}
|
|
701
709
|
if (requestBody) {
|
|
702
710
|
const body = apiGen.resolve(requestBody);
|
|
703
711
|
const schema = apiGen.getSchemaFromContent(body.content);
|
|
704
|
-
const type =
|
|
712
|
+
const type = wrapWithSchemeIfComponent(apiGen.getTypeFromSchema(schema));
|
|
705
713
|
const schemaName = (0, import_lodash.default)(
|
|
706
714
|
type.name || (0, import_generate3.getReferenceName)(schema) || typeof schema === "object" && "title" in schema && schema.title || "body"
|
|
707
715
|
);
|
|
@@ -710,24 +718,11 @@ async function generateApi(spec, {
|
|
|
710
718
|
origin: "body",
|
|
711
719
|
name,
|
|
712
720
|
originalName: schemaName,
|
|
713
|
-
type,
|
|
721
|
+
type: wrapWithSchemeIfComponent(apiGen.getTypeFromSchema(schema, void 0, "writeOnly")),
|
|
714
722
|
required: true,
|
|
715
723
|
body
|
|
716
724
|
};
|
|
717
725
|
}
|
|
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
726
|
const propertyName = (name) => {
|
|
732
727
|
if (typeof name === "string") {
|
|
733
728
|
return (0, import_generate3.isValidIdentifier)(name) ? factory.createIdentifier(name) : factory.createStringLiteral(name);
|
|
@@ -864,6 +859,49 @@ async function generateApi(spec, {
|
|
|
864
859
|
function generateMutationEndpointProps({}) {
|
|
865
860
|
return {};
|
|
866
861
|
}
|
|
862
|
+
function wrapWithSchemeIfComponent(typeNode) {
|
|
863
|
+
if (import_typescript4.default.isTypeReferenceNode(typeNode) && import_typescript4.default.isIdentifier(typeNode.typeName)) {
|
|
864
|
+
const typeName = typeNode.typeName.text;
|
|
865
|
+
if (schemeTypeNames.has(typeName)) {
|
|
866
|
+
return factory.createTypeReferenceNode(
|
|
867
|
+
factory.createQualifiedName(
|
|
868
|
+
factory.createIdentifier("Scheme"),
|
|
869
|
+
typeNode.typeName
|
|
870
|
+
),
|
|
871
|
+
typeNode.typeArguments?.map(wrapWithSchemeIfComponent)
|
|
872
|
+
);
|
|
873
|
+
}
|
|
874
|
+
if (typeNode.typeArguments) {
|
|
875
|
+
return factory.createTypeReferenceNode(
|
|
876
|
+
typeNode.typeName,
|
|
877
|
+
typeNode.typeArguments.map(wrapWithSchemeIfComponent)
|
|
878
|
+
);
|
|
879
|
+
}
|
|
880
|
+
}
|
|
881
|
+
if (import_typescript4.default.isArrayTypeNode(typeNode)) {
|
|
882
|
+
return factory.createArrayTypeNode(wrapWithSchemeIfComponent(typeNode.elementType));
|
|
883
|
+
}
|
|
884
|
+
if (import_typescript4.default.isUnionTypeNode(typeNode)) {
|
|
885
|
+
return factory.createUnionTypeNode(typeNode.types.map(wrapWithSchemeIfComponent));
|
|
886
|
+
}
|
|
887
|
+
if (import_typescript4.default.isTypeLiteralNode(typeNode)) {
|
|
888
|
+
return factory.createTypeLiteralNode(
|
|
889
|
+
typeNode.members.map((member) => {
|
|
890
|
+
if (import_typescript4.default.isPropertySignature(member) && member.type) {
|
|
891
|
+
return factory.updatePropertySignature(
|
|
892
|
+
member,
|
|
893
|
+
member.modifiers,
|
|
894
|
+
member.name,
|
|
895
|
+
member.questionToken,
|
|
896
|
+
wrapWithSchemeIfComponent(member.type)
|
|
897
|
+
);
|
|
898
|
+
}
|
|
899
|
+
return member;
|
|
900
|
+
})
|
|
901
|
+
);
|
|
902
|
+
}
|
|
903
|
+
return typeNode;
|
|
904
|
+
}
|
|
867
905
|
}
|
|
868
906
|
function generatePathExpression(path4, pathParameters, rootObject, isFlatArg, encodePathParams) {
|
|
869
907
|
const expressions = [];
|
|
@@ -891,7 +929,55 @@ function generatePathExpression(path4, pathParameters, rootObject, isFlatArg, en
|
|
|
891
929
|
}
|
|
892
930
|
|
|
893
931
|
// src/index.ts
|
|
932
|
+
var import_lodash2 = __toESM(require("lodash.camelcase"));
|
|
894
933
|
var require2 = (0, import_node_module.createRequire)(__filename);
|
|
934
|
+
async function ensureDirectoryExists(filePath) {
|
|
935
|
+
const dirname = import_node_path3.default.dirname(filePath);
|
|
936
|
+
if (!import_node_fs.default.existsSync(dirname)) {
|
|
937
|
+
await import_node_fs.default.promises.mkdir(dirname, { recursive: true });
|
|
938
|
+
}
|
|
939
|
+
}
|
|
940
|
+
function fileExists(filePath) {
|
|
941
|
+
try {
|
|
942
|
+
return import_node_fs.default.statSync(filePath).isFile();
|
|
943
|
+
} catch {
|
|
944
|
+
return false;
|
|
945
|
+
}
|
|
946
|
+
}
|
|
947
|
+
function getApiNameFromDir(dirPath) {
|
|
948
|
+
const dirName = import_node_path3.default.basename(dirPath);
|
|
949
|
+
return `${dirName}Api`;
|
|
950
|
+
}
|
|
951
|
+
async function ensureBaseFilesExist(outputDir) {
|
|
952
|
+
const enhanceEndpointsPath = import_node_path3.default.join(outputDir, "enhanceEndpoints.ts");
|
|
953
|
+
const indexPath = import_node_path3.default.join(outputDir, "index.ts");
|
|
954
|
+
const apiName = getApiNameFromDir(outputDir);
|
|
955
|
+
if (!fileExists(enhanceEndpointsPath)) {
|
|
956
|
+
const enhanceEndpointsContent = `import api from './query.generated';
|
|
957
|
+
|
|
958
|
+
const enhancedApi = api.enhanceEndpoints({
|
|
959
|
+
endpoints: {
|
|
960
|
+
},
|
|
961
|
+
});
|
|
962
|
+
|
|
963
|
+
export default enhancedApi;
|
|
964
|
+
`;
|
|
965
|
+
await import_node_fs.default.promises.writeFile(enhanceEndpointsPath, enhanceEndpointsContent, "utf-8");
|
|
966
|
+
}
|
|
967
|
+
if (!fileExists(indexPath)) {
|
|
968
|
+
const indexContent = `export * from './query.generated';
|
|
969
|
+
export {default as ${apiName}} from './enhanceEndpoints';
|
|
970
|
+
`;
|
|
971
|
+
await import_node_fs.default.promises.writeFile(indexPath, indexContent, "utf-8");
|
|
972
|
+
}
|
|
973
|
+
}
|
|
974
|
+
function getGroupNameFromPath(path4, pattern) {
|
|
975
|
+
const match = path4.match(pattern);
|
|
976
|
+
if (match && match[1]) {
|
|
977
|
+
return (0, import_lodash2.default)(match[1]);
|
|
978
|
+
}
|
|
979
|
+
return "common";
|
|
980
|
+
}
|
|
895
981
|
async function generateEndpoints(options) {
|
|
896
982
|
const schemaLocation = options.schemaFile;
|
|
897
983
|
const schemaAbsPath = isValidUrl(options.schemaFile) ? options.schemaFile : import_node_path3.default.resolve(process.cwd(), schemaLocation);
|
|
@@ -900,8 +986,12 @@ async function generateEndpoints(options) {
|
|
|
900
986
|
});
|
|
901
987
|
const { outputFile, prettierConfigFile } = options;
|
|
902
988
|
if (outputFile) {
|
|
989
|
+
const outputPath = import_node_path3.default.resolve(process.cwd(), outputFile);
|
|
990
|
+
await ensureDirectoryExists(outputPath);
|
|
991
|
+
const outputDir = import_node_path3.default.dirname(outputPath);
|
|
992
|
+
await ensureBaseFilesExist(outputDir);
|
|
903
993
|
import_node_fs.default.writeFileSync(
|
|
904
|
-
|
|
994
|
+
outputPath,
|
|
905
995
|
await prettify(outputFile, sourceCode, prettierConfigFile)
|
|
906
996
|
);
|
|
907
997
|
} else {
|
|
@@ -912,13 +1002,29 @@ function parseConfig(fullConfig) {
|
|
|
912
1002
|
const outFiles = [];
|
|
913
1003
|
if ("outputFiles" in fullConfig) {
|
|
914
1004
|
const { outputFiles, ...commonConfig } = fullConfig;
|
|
915
|
-
|
|
1005
|
+
const openApiDoc = JSON.parse(import_node_fs.default.readFileSync(fullConfig.schemaFile, "utf-8"));
|
|
1006
|
+
const paths = Object.keys(openApiDoc.paths);
|
|
1007
|
+
const [outputPath, config] = Object.entries(outputFiles)[0];
|
|
1008
|
+
const patterns = config.groupMatch;
|
|
1009
|
+
const filterEndpoint = config.filterEndpoint;
|
|
1010
|
+
const pattern = patterns;
|
|
1011
|
+
const groupedPaths = paths.reduce((acc, path4) => {
|
|
1012
|
+
const groupName = getGroupNameFromPath(path4, pattern);
|
|
1013
|
+
if (!acc[groupName]) {
|
|
1014
|
+
acc[groupName] = [];
|
|
1015
|
+
}
|
|
1016
|
+
acc[groupName].push(path4);
|
|
1017
|
+
return acc;
|
|
1018
|
+
}, {});
|
|
1019
|
+
Object.entries(groupedPaths).forEach(([groupName, paths2]) => {
|
|
1020
|
+
const finalOutputPath = outputPath.replace("$1", groupName);
|
|
1021
|
+
const filterEndpoints = filterEndpoint(groupName);
|
|
916
1022
|
outFiles.push({
|
|
917
1023
|
...commonConfig,
|
|
918
|
-
|
|
919
|
-
|
|
1024
|
+
outputFile: finalOutputPath,
|
|
1025
|
+
filterEndpoints: [filterEndpoints]
|
|
920
1026
|
});
|
|
921
|
-
}
|
|
1027
|
+
});
|
|
922
1028
|
} else {
|
|
923
1029
|
outFiles.push(fullConfig);
|
|
924
1030
|
}
|