@acrool/rtk-query-codegen-openapi 0.0.2-test.9 → 0.0.2
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 +4 -8
- package/lib/index.d.ts +4 -8
- package/lib/index.js +113 -377
- package/lib/index.js.map +1 -1
- package/lib/index.mjs +113 -377
- package/lib/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/generate.ts +127 -327
- package/src/generators/react-hooks.ts +2 -2
- package/src/index.ts +9 -152
- package/src/types.ts +1 -9
package/lib/index.d.mts
CHANGED
|
@@ -130,17 +130,13 @@ type EndpointOverrides = {
|
|
|
130
130
|
type: 'mutation' | 'query';
|
|
131
131
|
parameterFilter: ParameterMatcher;
|
|
132
132
|
}>;
|
|
133
|
-
type OutputFilesConfig = {
|
|
134
|
-
[outputFile: string]: {
|
|
135
|
-
groupMatch: RegExp;
|
|
136
|
-
filterEndpoint?: (groupName: string) => RegExp;
|
|
137
|
-
};
|
|
138
|
-
};
|
|
139
133
|
type ConfigFile = Id<Require<CommonOptions & OutputFileOptions, 'outputFile'>> | Id<Omit<CommonOptions, 'outputFile'> & {
|
|
140
|
-
outputFiles:
|
|
134
|
+
outputFiles: {
|
|
135
|
+
[outputFile: string]: Omit<OutputFileOptions, 'outputFile'>;
|
|
136
|
+
};
|
|
141
137
|
}>;
|
|
142
138
|
|
|
143
139
|
declare function generateEndpoints(options: GenerationOptions): Promise<string | void>;
|
|
144
140
|
declare function parseConfig(fullConfig: ConfigFile): (CommonOptions & OutputFileOptions)[];
|
|
145
141
|
|
|
146
|
-
export { type ConfigFile,
|
|
142
|
+
export { type ConfigFile, generateEndpoints, parseConfig };
|
package/lib/index.d.ts
CHANGED
|
@@ -130,17 +130,13 @@ type EndpointOverrides = {
|
|
|
130
130
|
type: 'mutation' | 'query';
|
|
131
131
|
parameterFilter: ParameterMatcher;
|
|
132
132
|
}>;
|
|
133
|
-
type OutputFilesConfig = {
|
|
134
|
-
[outputFile: string]: {
|
|
135
|
-
groupMatch: RegExp;
|
|
136
|
-
filterEndpoint?: (groupName: string) => RegExp;
|
|
137
|
-
};
|
|
138
|
-
};
|
|
139
133
|
type ConfigFile = Id<Require<CommonOptions & OutputFileOptions, 'outputFile'>> | Id<Omit<CommonOptions, 'outputFile'> & {
|
|
140
|
-
outputFiles:
|
|
134
|
+
outputFiles: {
|
|
135
|
+
[outputFile: string]: Omit<OutputFileOptions, 'outputFile'>;
|
|
136
|
+
};
|
|
141
137
|
}>;
|
|
142
138
|
|
|
143
139
|
declare function generateEndpoints(options: GenerationOptions): Promise<string | void>;
|
|
144
140
|
declare function parseConfig(fullConfig: ConfigFile): (CommonOptions & OutputFileOptions)[];
|
|
145
141
|
|
|
146
|
-
export { type ConfigFile,
|
|
142
|
+
export { type ConfigFile, 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, operation },
|
|
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, operation.operationId))}${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, operation }) {
|
|
391
|
+
return (0, import_generate3.getOperationName)(verb, path4, operation.operationId);
|
|
392
392
|
}
|
|
393
393
|
function getTags({ verb, pathItem }) {
|
|
394
394
|
return verb ? pathItem[verb]?.tags || [] : [];
|
|
@@ -462,153 +462,41 @@ 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
|
-
}
|
|
471
465
|
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();
|
|
483
466
|
const components = v3Doc.components;
|
|
484
467
|
if (components) {
|
|
485
|
-
const
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
468
|
+
const resultFile2 = import_typescript4.default.createSourceFile(
|
|
469
|
+
"sharedTypes.ts",
|
|
470
|
+
"",
|
|
471
|
+
import_typescript4.default.ScriptTarget.Latest,
|
|
472
|
+
/*setParentNodes*/
|
|
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);
|
|
491
480
|
return factory.createTypeAliasDeclaration(
|
|
492
481
|
[factory.createModifier(import_typescript4.default.SyntaxKind.ExportKeyword)],
|
|
493
|
-
factory.createIdentifier(
|
|
482
|
+
factory.createIdentifier(name),
|
|
494
483
|
void 0,
|
|
495
484
|
typeNode
|
|
496
485
|
);
|
|
497
486
|
});
|
|
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
|
-
);
|
|
504
487
|
});
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
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
|
-
)
|
|
488
|
+
const output = printer2.printNode(
|
|
489
|
+
import_typescript4.default.EmitHint.Unspecified,
|
|
490
|
+
factory.createSourceFile(
|
|
491
|
+
typeDefinitions,
|
|
492
|
+
factory.createToken(import_typescript4.default.SyntaxKind.EndOfFileToken),
|
|
493
|
+
import_typescript4.default.NodeFlags.None
|
|
494
|
+
),
|
|
495
|
+
resultFile2
|
|
552
496
|
);
|
|
497
|
+
const fs2 = await import("node:fs/promises");
|
|
498
|
+
await fs2.writeFile(sharedTypesFile, output, "utf-8");
|
|
553
499
|
}
|
|
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");
|
|
612
500
|
}
|
|
613
501
|
if (apiGen.spec.components?.schemas) {
|
|
614
502
|
apiGen.preprocessComponents(apiGen.spec.components.schemas);
|
|
@@ -639,13 +527,16 @@ async function generateApi(spec, {
|
|
|
639
527
|
apiFile = apiFile.replace(/\\/g, "/");
|
|
640
528
|
if (!apiFile.startsWith(".")) apiFile = `./${apiFile}`;
|
|
641
529
|
}
|
|
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
|
+
}
|
|
642
535
|
}
|
|
643
536
|
apiFile = apiFile.replace(/\.[jt]sx?$/, "");
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
return rel;
|
|
648
|
-
})() : "./shared-types";
|
|
537
|
+
if (sharedTypesFile) {
|
|
538
|
+
sharedTypesFile = sharedTypesFile.replace(/\.[jt]sx?$/, "");
|
|
539
|
+
}
|
|
649
540
|
return printer.printNode(
|
|
650
541
|
import_typescript4.default.EmitHint.Unspecified,
|
|
651
542
|
factory.createSourceFile(
|
|
@@ -653,10 +544,16 @@ async function generateApi(spec, {
|
|
|
653
544
|
generateImportNode(apiFile, { [apiImport]: "api" }),
|
|
654
545
|
generateImportNode("@acrool/react-fetcher", { IRestFulEndpointsQueryReturn: "IRestFulEndpointsQueryReturn" }),
|
|
655
546
|
...sharedTypesFile ? [
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
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
|
+
)
|
|
660
557
|
] : [],
|
|
661
558
|
...tag ? [generateTagTypes({ addTagTypes: extractAllTagTypes({ operationDefinitions }) })] : [],
|
|
662
559
|
generateCreateApiCall({
|
|
@@ -665,8 +562,7 @@ async function generateApi(spec, {
|
|
|
665
562
|
operationDefinitions.map(
|
|
666
563
|
(operationDefinition) => generateEndpoint({
|
|
667
564
|
operationDefinition,
|
|
668
|
-
overrides: getOverrides(operationDefinition, endpointOverrides)
|
|
669
|
-
sharedTypesFile: !!sharedTypesFile
|
|
565
|
+
overrides: getOverrides(operationDefinition, endpointOverrides)
|
|
670
566
|
})
|
|
671
567
|
),
|
|
672
568
|
true
|
|
@@ -705,8 +601,7 @@ async function generateApi(spec, {
|
|
|
705
601
|
}
|
|
706
602
|
function generateEndpoint({
|
|
707
603
|
operationDefinition,
|
|
708
|
-
overrides
|
|
709
|
-
sharedTypesFile: sharedTypesFile2
|
|
604
|
+
overrides
|
|
710
605
|
}) {
|
|
711
606
|
const {
|
|
712
607
|
verb,
|
|
@@ -715,30 +610,60 @@ async function generateApi(spec, {
|
|
|
715
610
|
operation,
|
|
716
611
|
operation: { responses, requestBody }
|
|
717
612
|
} = operationDefinition;
|
|
718
|
-
const operationName = getOperationName2({ verb, path: path4 });
|
|
613
|
+
const operationName = getOperationName2({ verb, path: path4, operation });
|
|
719
614
|
const tags = tag ? getTags({ verb, pathItem }) : [];
|
|
720
615
|
const isQuery2 = isQuery(verb, overrides);
|
|
721
616
|
const returnsJson = apiGen.getResponseType(responses) === "json";
|
|
722
617
|
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
|
+
}
|
|
723
646
|
if (returnsJson) {
|
|
724
647
|
const returnTypes = Object.entries(responses || {}).map(
|
|
725
|
-
([code, response]) =>
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
648
|
+
([code, response]) => {
|
|
649
|
+
const resolvedResponse = apiGen.resolve(response);
|
|
650
|
+
if (!resolvedResponse.content?.["application/json"]?.schema) {
|
|
651
|
+
return [code, resolvedResponse, factory.createKeywordTypeNode(import_typescript4.default.SyntaxKind.UndefinedKeyword)];
|
|
652
|
+
}
|
|
653
|
+
const schema = resolvedResponse.content["application/json"].schema;
|
|
654
|
+
const type = replaceReferences(schema);
|
|
655
|
+
return [code, resolvedResponse, type];
|
|
656
|
+
}
|
|
732
657
|
).filter(
|
|
733
658
|
([status, response]) => isDataResponse(status, includeDefault, apiGen.resolve(response), responses || {})
|
|
734
|
-
).filter(([_1, _2, type]) => type !== import_generate3.keywordType.void).map(
|
|
735
|
-
|
|
736
|
-
type,
|
|
659
|
+
).filter(([_1, _2, type]) => type !== import_generate3.keywordType.void).map(
|
|
660
|
+
([code, response, type]) => import_typescript4.default.addSyntheticLeadingComment(
|
|
661
|
+
{ ...type },
|
|
737
662
|
import_typescript4.default.SyntaxKind.MultiLineCommentTrivia,
|
|
738
663
|
`* status ${code} ${response.description} `,
|
|
739
664
|
false
|
|
740
|
-
)
|
|
741
|
-
|
|
665
|
+
)
|
|
666
|
+
);
|
|
742
667
|
if (returnTypes.length > 0) {
|
|
743
668
|
ResponseType = factory.createUnionTypeNode(returnTypes);
|
|
744
669
|
}
|
|
@@ -773,21 +698,10 @@ async function generateApi(spec, {
|
|
|
773
698
|
}
|
|
774
699
|
return name;
|
|
775
700
|
}
|
|
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
|
-
}
|
|
787
701
|
if (requestBody) {
|
|
788
702
|
const body = apiGen.resolve(requestBody);
|
|
789
703
|
const schema = apiGen.getSchemaFromContent(body.content);
|
|
790
|
-
const type =
|
|
704
|
+
const type = replaceReferences(schema);
|
|
791
705
|
const schemaName = (0, import_lodash.default)(
|
|
792
706
|
type.name || (0, import_generate3.getReferenceName)(schema) || typeof schema === "object" && "title" in schema && schema.title || "body"
|
|
793
707
|
);
|
|
@@ -796,11 +710,24 @@ async function generateApi(spec, {
|
|
|
796
710
|
origin: "body",
|
|
797
711
|
name,
|
|
798
712
|
originalName: schemaName,
|
|
799
|
-
type
|
|
713
|
+
type,
|
|
800
714
|
required: true,
|
|
801
715
|
body
|
|
802
716
|
};
|
|
803
717
|
}
|
|
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
|
+
}
|
|
804
731
|
const propertyName = (name) => {
|
|
805
732
|
if (typeof name === "string") {
|
|
806
733
|
return (0, import_generate3.isValidIdentifier)(name) ? factory.createIdentifier(name) : factory.createStringLiteral(name);
|
|
@@ -937,105 +864,6 @@ async function generateApi(spec, {
|
|
|
937
864
|
function generateMutationEndpointProps({}) {
|
|
938
865
|
return {};
|
|
939
866
|
}
|
|
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
|
-
}
|
|
1039
867
|
}
|
|
1040
868
|
function generatePathExpression(path4, pathParameters, rootObject, isFlatArg, encodePathParams) {
|
|
1041
869
|
const expressions = [];
|
|
@@ -1063,55 +891,7 @@ function generatePathExpression(path4, pathParameters, rootObject, isFlatArg, en
|
|
|
1063
891
|
}
|
|
1064
892
|
|
|
1065
893
|
// src/index.ts
|
|
1066
|
-
var import_lodash2 = __toESM(require("lodash.camelcase"));
|
|
1067
894
|
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
|
-
}
|
|
1115
895
|
async function generateEndpoints(options) {
|
|
1116
896
|
const schemaLocation = options.schemaFile;
|
|
1117
897
|
const schemaAbsPath = isValidUrl(options.schemaFile) ? options.schemaFile : import_node_path3.default.resolve(process.cwd(), schemaLocation);
|
|
@@ -1120,12 +900,8 @@ async function generateEndpoints(options) {
|
|
|
1120
900
|
});
|
|
1121
901
|
const { outputFile, prettierConfigFile } = options;
|
|
1122
902
|
if (outputFile) {
|
|
1123
|
-
const outputPath = import_node_path3.default.resolve(process.cwd(), outputFile);
|
|
1124
|
-
await ensureDirectoryExists(outputPath);
|
|
1125
|
-
const outputDir = import_node_path3.default.dirname(outputPath);
|
|
1126
|
-
await ensureBaseFilesExist(outputDir);
|
|
1127
903
|
import_node_fs.default.writeFileSync(
|
|
1128
|
-
|
|
904
|
+
import_node_path3.default.resolve(process.cwd(), outputFile),
|
|
1129
905
|
await prettify(outputFile, sourceCode, prettierConfigFile)
|
|
1130
906
|
);
|
|
1131
907
|
} else {
|
|
@@ -1136,53 +912,13 @@ function parseConfig(fullConfig) {
|
|
|
1136
912
|
const outFiles = [];
|
|
1137
913
|
if ("outputFiles" in fullConfig) {
|
|
1138
914
|
const { outputFiles, ...commonConfig } = fullConfig;
|
|
1139
|
-
const
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
const groupName = getGroupNameFromPath(path4, pattern);
|
|
1147
|
-
if (!acc[groupName]) {
|
|
1148
|
-
acc[groupName] = [];
|
|
1149
|
-
}
|
|
1150
|
-
acc[groupName].push(path4);
|
|
1151
|
-
return acc;
|
|
1152
|
-
}, {});
|
|
1153
|
-
Object.entries(groupedPaths).forEach(([groupName, paths2]) => {
|
|
1154
|
-
const finalOutputPath = outputPath.replace("$1", groupName);
|
|
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
|
-
}
|
|
1185
|
-
});
|
|
915
|
+
for (const [outputFile, specificConfig] of Object.entries(outputFiles)) {
|
|
916
|
+
outFiles.push({
|
|
917
|
+
...commonConfig,
|
|
918
|
+
...specificConfig,
|
|
919
|
+
outputFile
|
|
920
|
+
});
|
|
921
|
+
}
|
|
1186
922
|
} else {
|
|
1187
923
|
outFiles.push(fullConfig);
|
|
1188
924
|
}
|