@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.mjs
CHANGED
|
@@ -305,14 +305,14 @@ function removeUndefined(t) {
|
|
|
305
305
|
|
|
306
306
|
// src/generators/react-hooks.ts
|
|
307
307
|
var createBinding = ({
|
|
308
|
-
operationDefinition: { verb, path: path4 },
|
|
308
|
+
operationDefinition: { verb, path: path4, operation },
|
|
309
309
|
overrides,
|
|
310
310
|
isLazy = false
|
|
311
311
|
}) => factory.createBindingElement(
|
|
312
312
|
void 0,
|
|
313
313
|
void 0,
|
|
314
314
|
factory.createIdentifier(
|
|
315
|
-
`use${isLazy ? "Lazy" : ""}${capitalize(getOperationName(verb, path4,
|
|
315
|
+
`use${isLazy ? "Lazy" : ""}${capitalize(getOperationName(verb, path4, operation.operationId))}${isQuery(verb, overrides) ? "Query" : "Mutation"}`
|
|
316
316
|
),
|
|
317
317
|
void 0
|
|
318
318
|
);
|
|
@@ -366,8 +366,8 @@ function defaultIsDataResponse(code, includeDefault) {
|
|
|
366
366
|
const parsedCode = Number(code);
|
|
367
367
|
return !Number.isNaN(parsedCode) && parsedCode >= 200 && parsedCode < 300;
|
|
368
368
|
}
|
|
369
|
-
function getOperationName2({ verb, path: path4 }) {
|
|
370
|
-
return _getOperationName(verb, path4,
|
|
369
|
+
function getOperationName2({ verb, path: path4, operation }) {
|
|
370
|
+
return _getOperationName(verb, path4, operation.operationId);
|
|
371
371
|
}
|
|
372
372
|
function getTags({ verb, pathItem }) {
|
|
373
373
|
return verb ? pathItem[verb]?.tags || [] : [];
|
|
@@ -441,153 +441,41 @@ async function generateApi(spec, {
|
|
|
441
441
|
useEnumType,
|
|
442
442
|
mergeReadWriteOnly
|
|
443
443
|
});
|
|
444
|
-
const schemeTypeNames = /* @__PURE__ */ new Set();
|
|
445
|
-
function addSchemeTypeName(name) {
|
|
446
|
-
schemeTypeNames.add(name);
|
|
447
|
-
schemeTypeNames.add(camelCase(name));
|
|
448
|
-
schemeTypeNames.add(capitalize(camelCase(name)));
|
|
449
|
-
}
|
|
450
444
|
if (sharedTypesFile) {
|
|
451
|
-
const resultFile2 = ts4.createSourceFile(
|
|
452
|
-
"sharedTypes.ts",
|
|
453
|
-
"",
|
|
454
|
-
ts4.ScriptTarget.Latest,
|
|
455
|
-
/*setParentNodes*/
|
|
456
|
-
false,
|
|
457
|
-
ts4.ScriptKind.TS
|
|
458
|
-
);
|
|
459
|
-
const printer2 = ts4.createPrinter({ newLine: ts4.NewLineKind.LineFeed });
|
|
460
|
-
const allTypeDefinitions = [];
|
|
461
|
-
const definedTypeNames = /* @__PURE__ */ new Set();
|
|
462
445
|
const components = v3Doc.components;
|
|
463
446
|
if (components) {
|
|
464
|
-
const
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
447
|
+
const resultFile2 = ts4.createSourceFile(
|
|
448
|
+
"sharedTypes.ts",
|
|
449
|
+
"",
|
|
450
|
+
ts4.ScriptTarget.Latest,
|
|
451
|
+
/*setParentNodes*/
|
|
452
|
+
false,
|
|
453
|
+
ts4.ScriptKind.TS
|
|
454
|
+
);
|
|
455
|
+
const printer2 = ts4.createPrinter({ newLine: ts4.NewLineKind.LineFeed });
|
|
456
|
+
const typeDefinitions = Object.entries(components).flatMap(([_, componentDefs]) => {
|
|
457
|
+
return Object.entries(componentDefs).map(([name, def]) => {
|
|
458
|
+
const typeNode = apiGen.getTypeFromSchema(def);
|
|
470
459
|
return factory.createTypeAliasDeclaration(
|
|
471
460
|
[factory.createModifier(ts4.SyntaxKind.ExportKeyword)],
|
|
472
|
-
factory.createIdentifier(
|
|
461
|
+
factory.createIdentifier(name),
|
|
473
462
|
void 0,
|
|
474
463
|
typeNode
|
|
475
464
|
);
|
|
476
465
|
});
|
|
477
|
-
return factory.createModuleDeclaration(
|
|
478
|
-
[factory.createModifier(ts4.SyntaxKind.ExportKeyword)],
|
|
479
|
-
factory.createIdentifier("Scheme"),
|
|
480
|
-
factory.createModuleBlock(typeEntries),
|
|
481
|
-
ts4.NodeFlags.Namespace
|
|
482
|
-
);
|
|
483
466
|
});
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
[factory.createModifier(ts4.SyntaxKind.ExportKeyword)],
|
|
493
|
-
enumDecl.name,
|
|
494
|
-
enumDecl.members
|
|
495
|
-
);
|
|
496
|
-
} else if (ts4.isTypeAliasDeclaration(enumDecl)) {
|
|
497
|
-
return factory.createTypeAliasDeclaration(
|
|
498
|
-
[factory.createModifier(ts4.SyntaxKind.ExportKeyword)],
|
|
499
|
-
enumDecl.name,
|
|
500
|
-
enumDecl.typeParameters,
|
|
501
|
-
enumDecl.type
|
|
502
|
-
);
|
|
503
|
-
}
|
|
504
|
-
return enumDecl;
|
|
505
|
-
});
|
|
506
|
-
const unionTypeEnums = apiGen.aliases.filter((alias) => {
|
|
507
|
-
if (ts4.isTypeAliasDeclaration(alias) && alias.type) {
|
|
508
|
-
return ts4.isUnionTypeNode(alias.type);
|
|
509
|
-
}
|
|
510
|
-
return false;
|
|
511
|
-
}).map((alias) => {
|
|
512
|
-
if (ts4.isTypeAliasDeclaration(alias)) {
|
|
513
|
-
return factory.createTypeAliasDeclaration(
|
|
514
|
-
[factory.createModifier(ts4.SyntaxKind.ExportKeyword)],
|
|
515
|
-
alias.name,
|
|
516
|
-
alias.typeParameters,
|
|
517
|
-
alias.type
|
|
518
|
-
);
|
|
519
|
-
}
|
|
520
|
-
return alias;
|
|
521
|
-
});
|
|
522
|
-
const allEnumEntries = [...enumEntries, ...unionTypeEnums];
|
|
523
|
-
if (allEnumEntries.length > 0) {
|
|
524
|
-
allTypeDefinitions.push(
|
|
525
|
-
factory.createModuleDeclaration(
|
|
526
|
-
[factory.createModifier(ts4.SyntaxKind.ExportKeyword)],
|
|
527
|
-
factory.createIdentifier("Enum"),
|
|
528
|
-
factory.createModuleBlock(allEnumEntries),
|
|
529
|
-
ts4.NodeFlags.Namespace
|
|
530
|
-
)
|
|
467
|
+
const output = printer2.printNode(
|
|
468
|
+
ts4.EmitHint.Unspecified,
|
|
469
|
+
factory.createSourceFile(
|
|
470
|
+
typeDefinitions,
|
|
471
|
+
factory.createToken(ts4.SyntaxKind.EndOfFileToken),
|
|
472
|
+
ts4.NodeFlags.None
|
|
473
|
+
),
|
|
474
|
+
resultFile2
|
|
531
475
|
);
|
|
476
|
+
const fs2 = await import("node:fs/promises");
|
|
477
|
+
await fs2.writeFile(sharedTypesFile, output, "utf-8");
|
|
532
478
|
}
|
|
533
|
-
if (apiGen.aliases.length > 0) {
|
|
534
|
-
const aliasEntries = apiGen.aliases.filter((alias) => {
|
|
535
|
-
if (ts4.isTypeAliasDeclaration(alias)) {
|
|
536
|
-
const isDefinedInComponents = definedTypeNames.has(alias.name.text);
|
|
537
|
-
const isUnionTypeEnum = ts4.isUnionTypeNode(alias.type);
|
|
538
|
-
return !isDefinedInComponents && !isUnionTypeEnum;
|
|
539
|
-
}
|
|
540
|
-
return false;
|
|
541
|
-
}).map((alias) => {
|
|
542
|
-
if (ts4.isTypeAliasDeclaration(alias)) {
|
|
543
|
-
return factory.createTypeAliasDeclaration(
|
|
544
|
-
[factory.createModifier(ts4.SyntaxKind.ExportKeyword)],
|
|
545
|
-
alias.name,
|
|
546
|
-
alias.typeParameters,
|
|
547
|
-
alias.type
|
|
548
|
-
);
|
|
549
|
-
}
|
|
550
|
-
return alias;
|
|
551
|
-
});
|
|
552
|
-
if (aliasEntries.length > 0) {
|
|
553
|
-
const existingSchemeIndex = allTypeDefinitions.findIndex(
|
|
554
|
-
(def) => ts4.isModuleDeclaration(def) && ts4.isIdentifier(def.name) && def.name.text === "Scheme"
|
|
555
|
-
);
|
|
556
|
-
if (existingSchemeIndex >= 0) {
|
|
557
|
-
const existingScheme = allTypeDefinitions[existingSchemeIndex];
|
|
558
|
-
const mergedMembers = [...existingScheme.body.statements, ...aliasEntries];
|
|
559
|
-
allTypeDefinitions[existingSchemeIndex] = factory.createModuleDeclaration(
|
|
560
|
-
[factory.createModifier(ts4.SyntaxKind.ExportKeyword)],
|
|
561
|
-
factory.createIdentifier("Scheme"),
|
|
562
|
-
factory.createModuleBlock(mergedMembers),
|
|
563
|
-
ts4.NodeFlags.Namespace
|
|
564
|
-
);
|
|
565
|
-
} else {
|
|
566
|
-
allTypeDefinitions.push(
|
|
567
|
-
factory.createModuleDeclaration(
|
|
568
|
-
[factory.createModifier(ts4.SyntaxKind.ExportKeyword)],
|
|
569
|
-
factory.createIdentifier("Scheme"),
|
|
570
|
-
factory.createModuleBlock(aliasEntries),
|
|
571
|
-
ts4.NodeFlags.Namespace
|
|
572
|
-
)
|
|
573
|
-
);
|
|
574
|
-
}
|
|
575
|
-
}
|
|
576
|
-
}
|
|
577
|
-
const fs2 = await import("node:fs/promises");
|
|
578
|
-
const path4 = await import("node:path");
|
|
579
|
-
const sharedTypesDir = path4.dirname(sharedTypesFile);
|
|
580
|
-
await fs2.mkdir(sharedTypesDir, { recursive: true });
|
|
581
|
-
const output = printer2.printNode(
|
|
582
|
-
ts4.EmitHint.Unspecified,
|
|
583
|
-
factory.createSourceFile(
|
|
584
|
-
allTypeDefinitions,
|
|
585
|
-
factory.createToken(ts4.SyntaxKind.EndOfFileToken),
|
|
586
|
-
ts4.NodeFlags.None
|
|
587
|
-
),
|
|
588
|
-
resultFile2
|
|
589
|
-
);
|
|
590
|
-
await fs2.writeFile(sharedTypesFile, output, "utf-8");
|
|
591
479
|
}
|
|
592
480
|
if (apiGen.spec.components?.schemas) {
|
|
593
481
|
apiGen.preprocessComponents(apiGen.spec.components.schemas);
|
|
@@ -618,13 +506,16 @@ async function generateApi(spec, {
|
|
|
618
506
|
apiFile = apiFile.replace(/\\/g, "/");
|
|
619
507
|
if (!apiFile.startsWith(".")) apiFile = `./${apiFile}`;
|
|
620
508
|
}
|
|
509
|
+
if (sharedTypesFile && sharedTypesFile.startsWith(".")) {
|
|
510
|
+
sharedTypesFile = path2.relative(path2.dirname(outputFile), sharedTypesFile);
|
|
511
|
+
sharedTypesFile = sharedTypesFile.replace(/\\/g, "/");
|
|
512
|
+
if (!sharedTypesFile.startsWith(".")) sharedTypesFile = `./${sharedTypesFile}`;
|
|
513
|
+
}
|
|
621
514
|
}
|
|
622
515
|
apiFile = apiFile.replace(/\.[jt]sx?$/, "");
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
return rel;
|
|
627
|
-
})() : "./shared-types";
|
|
516
|
+
if (sharedTypesFile) {
|
|
517
|
+
sharedTypesFile = sharedTypesFile.replace(/\.[jt]sx?$/, "");
|
|
518
|
+
}
|
|
628
519
|
return printer.printNode(
|
|
629
520
|
ts4.EmitHint.Unspecified,
|
|
630
521
|
factory.createSourceFile(
|
|
@@ -632,10 +523,16 @@ async function generateApi(spec, {
|
|
|
632
523
|
generateImportNode(apiFile, { [apiImport]: "api" }),
|
|
633
524
|
generateImportNode("@acrool/react-fetcher", { IRestFulEndpointsQueryReturn: "IRestFulEndpointsQueryReturn" }),
|
|
634
525
|
...sharedTypesFile ? [
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
526
|
+
factory.createImportDeclaration(
|
|
527
|
+
void 0,
|
|
528
|
+
factory.createImportClause(
|
|
529
|
+
false,
|
|
530
|
+
void 0,
|
|
531
|
+
factory.createNamespaceImport(factory.createIdentifier("SharedTypes"))
|
|
532
|
+
),
|
|
533
|
+
factory.createStringLiteral(sharedTypesFile),
|
|
534
|
+
void 0
|
|
535
|
+
)
|
|
639
536
|
] : [],
|
|
640
537
|
...tag ? [generateTagTypes({ addTagTypes: extractAllTagTypes({ operationDefinitions }) })] : [],
|
|
641
538
|
generateCreateApiCall({
|
|
@@ -644,8 +541,7 @@ async function generateApi(spec, {
|
|
|
644
541
|
operationDefinitions.map(
|
|
645
542
|
(operationDefinition) => generateEndpoint({
|
|
646
543
|
operationDefinition,
|
|
647
|
-
overrides: getOverrides(operationDefinition, endpointOverrides)
|
|
648
|
-
sharedTypesFile: !!sharedTypesFile
|
|
544
|
+
overrides: getOverrides(operationDefinition, endpointOverrides)
|
|
649
545
|
})
|
|
650
546
|
),
|
|
651
547
|
true
|
|
@@ -684,8 +580,7 @@ async function generateApi(spec, {
|
|
|
684
580
|
}
|
|
685
581
|
function generateEndpoint({
|
|
686
582
|
operationDefinition,
|
|
687
|
-
overrides
|
|
688
|
-
sharedTypesFile: sharedTypesFile2
|
|
583
|
+
overrides
|
|
689
584
|
}) {
|
|
690
585
|
const {
|
|
691
586
|
verb,
|
|
@@ -694,30 +589,60 @@ async function generateApi(spec, {
|
|
|
694
589
|
operation,
|
|
695
590
|
operation: { responses, requestBody }
|
|
696
591
|
} = operationDefinition;
|
|
697
|
-
const operationName = getOperationName2({ verb, path: path4 });
|
|
592
|
+
const operationName = getOperationName2({ verb, path: path4, operation });
|
|
698
593
|
const tags = tag ? getTags({ verb, pathItem }) : [];
|
|
699
594
|
const isQuery2 = isQuery(verb, overrides);
|
|
700
595
|
const returnsJson = apiGen.getResponseType(responses) === "json";
|
|
701
596
|
let ResponseType = factory.createKeywordTypeNode(ts4.SyntaxKind.UnknownKeyword);
|
|
597
|
+
function replaceReferences(schema) {
|
|
598
|
+
if (!schema) return factory.createKeywordTypeNode(ts4.SyntaxKind.UnknownKeyword);
|
|
599
|
+
const refName = getReferenceName(schema);
|
|
600
|
+
if (refName && sharedTypesFile) {
|
|
601
|
+
return factory.createTypeReferenceNode(
|
|
602
|
+
factory.createQualifiedName(
|
|
603
|
+
factory.createIdentifier("SharedTypes"),
|
|
604
|
+
factory.createIdentifier(refName)
|
|
605
|
+
),
|
|
606
|
+
void 0
|
|
607
|
+
);
|
|
608
|
+
}
|
|
609
|
+
if (schema.type === "object" && schema.properties) {
|
|
610
|
+
const members = Object.entries(schema.properties).map(([key, value]) => {
|
|
611
|
+
return factory.createPropertySignature(
|
|
612
|
+
void 0,
|
|
613
|
+
factory.createIdentifier(key),
|
|
614
|
+
schema.required?.includes(key) ? void 0 : factory.createToken(ts4.SyntaxKind.QuestionToken),
|
|
615
|
+
replaceReferences(value)
|
|
616
|
+
);
|
|
617
|
+
});
|
|
618
|
+
return factory.createTypeLiteralNode(members);
|
|
619
|
+
}
|
|
620
|
+
if (schema.type === "array" && schema.items) {
|
|
621
|
+
return factory.createArrayTypeNode(replaceReferences(schema.items));
|
|
622
|
+
}
|
|
623
|
+
return apiGen.getTypeFromSchema(schema);
|
|
624
|
+
}
|
|
702
625
|
if (returnsJson) {
|
|
703
626
|
const returnTypes = Object.entries(responses || {}).map(
|
|
704
|
-
([code, response]) =>
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
627
|
+
([code, response]) => {
|
|
628
|
+
const resolvedResponse = apiGen.resolve(response);
|
|
629
|
+
if (!resolvedResponse.content?.["application/json"]?.schema) {
|
|
630
|
+
return [code, resolvedResponse, factory.createKeywordTypeNode(ts4.SyntaxKind.UndefinedKeyword)];
|
|
631
|
+
}
|
|
632
|
+
const schema = resolvedResponse.content["application/json"].schema;
|
|
633
|
+
const type = replaceReferences(schema);
|
|
634
|
+
return [code, resolvedResponse, type];
|
|
635
|
+
}
|
|
711
636
|
).filter(
|
|
712
637
|
([status, response]) => isDataResponse(status, includeDefault, apiGen.resolve(response), responses || {})
|
|
713
|
-
).filter(([_1, _2, type]) => type !== keywordType.void).map(
|
|
714
|
-
|
|
715
|
-
type,
|
|
638
|
+
).filter(([_1, _2, type]) => type !== keywordType.void).map(
|
|
639
|
+
([code, response, type]) => ts4.addSyntheticLeadingComment(
|
|
640
|
+
{ ...type },
|
|
716
641
|
ts4.SyntaxKind.MultiLineCommentTrivia,
|
|
717
642
|
`* status ${code} ${response.description} `,
|
|
718
643
|
false
|
|
719
|
-
)
|
|
720
|
-
|
|
644
|
+
)
|
|
645
|
+
);
|
|
721
646
|
if (returnTypes.length > 0) {
|
|
722
647
|
ResponseType = factory.createUnionTypeNode(returnTypes);
|
|
723
648
|
}
|
|
@@ -752,21 +677,10 @@ async function generateApi(spec, {
|
|
|
752
677
|
}
|
|
753
678
|
return name;
|
|
754
679
|
}
|
|
755
|
-
for (const param of parameters) {
|
|
756
|
-
const name = generateName(param.name, param.in);
|
|
757
|
-
queryArg[name] = {
|
|
758
|
-
origin: "param",
|
|
759
|
-
name,
|
|
760
|
-
originalName: param.name,
|
|
761
|
-
type: wrapWithSchemeIfComponent(apiGen.getTypeFromSchema(isReference(param) ? param : param.schema, void 0, "writeOnly")),
|
|
762
|
-
required: param.required,
|
|
763
|
-
param
|
|
764
|
-
};
|
|
765
|
-
}
|
|
766
680
|
if (requestBody) {
|
|
767
681
|
const body = apiGen.resolve(requestBody);
|
|
768
682
|
const schema = apiGen.getSchemaFromContent(body.content);
|
|
769
|
-
const type =
|
|
683
|
+
const type = replaceReferences(schema);
|
|
770
684
|
const schemaName = camelCase(
|
|
771
685
|
type.name || getReferenceName(schema) || typeof schema === "object" && "title" in schema && schema.title || "body"
|
|
772
686
|
);
|
|
@@ -775,11 +689,24 @@ async function generateApi(spec, {
|
|
|
775
689
|
origin: "body",
|
|
776
690
|
name,
|
|
777
691
|
originalName: schemaName,
|
|
778
|
-
type
|
|
692
|
+
type,
|
|
779
693
|
required: true,
|
|
780
694
|
body
|
|
781
695
|
};
|
|
782
696
|
}
|
|
697
|
+
for (const param of parameters) {
|
|
698
|
+
const name = generateName(param.name, param.in);
|
|
699
|
+
const paramSchema = isReference(param) ? param : param.schema;
|
|
700
|
+
const type = replaceReferences(paramSchema);
|
|
701
|
+
queryArg[name] = {
|
|
702
|
+
origin: "param",
|
|
703
|
+
name,
|
|
704
|
+
originalName: param.name,
|
|
705
|
+
type,
|
|
706
|
+
required: param.required,
|
|
707
|
+
param
|
|
708
|
+
};
|
|
709
|
+
}
|
|
783
710
|
const propertyName = (name) => {
|
|
784
711
|
if (typeof name === "string") {
|
|
785
712
|
return isValidIdentifier(name) ? factory.createIdentifier(name) : factory.createStringLiteral(name);
|
|
@@ -916,105 +843,6 @@ async function generateApi(spec, {
|
|
|
916
843
|
function generateMutationEndpointProps({}) {
|
|
917
844
|
return {};
|
|
918
845
|
}
|
|
919
|
-
function wrapWithSchemeIfComponent(typeNode) {
|
|
920
|
-
if (ts4.isTypeReferenceNode(typeNode) && ts4.isIdentifier(typeNode.typeName)) {
|
|
921
|
-
const typeName = typeNode.typeName.text;
|
|
922
|
-
const isEnumType = useEnumType && (apiGen.enumAliases.some((enumDecl) => {
|
|
923
|
-
if (ts4.isEnumDeclaration(enumDecl) || ts4.isTypeAliasDeclaration(enumDecl)) {
|
|
924
|
-
return enumDecl.name.text === typeName;
|
|
925
|
-
}
|
|
926
|
-
return false;
|
|
927
|
-
}) || apiGen.aliases.some((alias) => {
|
|
928
|
-
if (ts4.isTypeAliasDeclaration(alias) && alias.type) {
|
|
929
|
-
if (ts4.isUnionTypeNode(alias.type)) {
|
|
930
|
-
return alias.name.text === typeName;
|
|
931
|
-
}
|
|
932
|
-
}
|
|
933
|
-
return false;
|
|
934
|
-
}));
|
|
935
|
-
if (isEnumType) {
|
|
936
|
-
return factory.createTypeReferenceNode(
|
|
937
|
-
factory.createQualifiedName(
|
|
938
|
-
factory.createIdentifier("Enum"),
|
|
939
|
-
typeNode.typeName
|
|
940
|
-
),
|
|
941
|
-
typeNode.typeArguments?.map(wrapWithSchemeIfComponent)
|
|
942
|
-
);
|
|
943
|
-
}
|
|
944
|
-
if (schemeTypeNames.has(typeName)) {
|
|
945
|
-
return factory.createTypeReferenceNode(
|
|
946
|
-
factory.createQualifiedName(
|
|
947
|
-
factory.createIdentifier("Scheme"),
|
|
948
|
-
typeNode.typeName
|
|
949
|
-
),
|
|
950
|
-
typeNode.typeArguments?.map(wrapWithSchemeIfComponent)
|
|
951
|
-
);
|
|
952
|
-
}
|
|
953
|
-
if (typeNode.typeArguments) {
|
|
954
|
-
return factory.createTypeReferenceNode(
|
|
955
|
-
typeNode.typeName,
|
|
956
|
-
typeNode.typeArguments.map(wrapWithSchemeIfComponent)
|
|
957
|
-
);
|
|
958
|
-
}
|
|
959
|
-
}
|
|
960
|
-
if (ts4.isArrayTypeNode(typeNode)) {
|
|
961
|
-
return factory.createArrayTypeNode(wrapWithSchemeIfComponent(typeNode.elementType));
|
|
962
|
-
}
|
|
963
|
-
if (ts4.isUnionTypeNode(typeNode)) {
|
|
964
|
-
const unionTypes = typeNode.types;
|
|
965
|
-
if (unionTypes.length > 0 && unionTypes.every(
|
|
966
|
-
(type) => ts4.isLiteralTypeNode(type) && (ts4.isStringLiteral(type.literal) || ts4.isNumericLiteral(type.literal))
|
|
967
|
-
)) {
|
|
968
|
-
const enumValues = unionTypes.map((type) => {
|
|
969
|
-
if (ts4.isLiteralTypeNode(type)) {
|
|
970
|
-
if (ts4.isStringLiteral(type.literal)) {
|
|
971
|
-
return type.literal.text;
|
|
972
|
-
} else if (ts4.isNumericLiteral(type.literal)) {
|
|
973
|
-
return type.literal.text;
|
|
974
|
-
}
|
|
975
|
-
}
|
|
976
|
-
return null;
|
|
977
|
-
}).filter(Boolean);
|
|
978
|
-
const matchingEnum = apiGen.aliases.find((alias) => {
|
|
979
|
-
if (ts4.isTypeAliasDeclaration(alias) && ts4.isUnionTypeNode(alias.type)) {
|
|
980
|
-
const aliasValues = alias.type.types.map((type) => {
|
|
981
|
-
if (ts4.isLiteralTypeNode(type)) {
|
|
982
|
-
if (ts4.isStringLiteral(type.literal)) {
|
|
983
|
-
return type.literal.text;
|
|
984
|
-
} else if (ts4.isNumericLiteral(type.literal)) {
|
|
985
|
-
return type.literal.text;
|
|
986
|
-
}
|
|
987
|
-
}
|
|
988
|
-
return null;
|
|
989
|
-
}).filter(Boolean);
|
|
990
|
-
return aliasValues.length === enumValues.length && aliasValues.every((val) => enumValues.includes(val));
|
|
991
|
-
}
|
|
992
|
-
return false;
|
|
993
|
-
});
|
|
994
|
-
if (matchingEnum && ts4.isTypeAliasDeclaration(matchingEnum)) {
|
|
995
|
-
return typeNode;
|
|
996
|
-
}
|
|
997
|
-
}
|
|
998
|
-
return factory.createUnionTypeNode(typeNode.types.map(wrapWithSchemeIfComponent));
|
|
999
|
-
}
|
|
1000
|
-
if (ts4.isTypeLiteralNode(typeNode)) {
|
|
1001
|
-
return factory.createTypeLiteralNode(
|
|
1002
|
-
typeNode.members.map((member) => {
|
|
1003
|
-
if (ts4.isPropertySignature(member) && member.type) {
|
|
1004
|
-
return factory.updatePropertySignature(
|
|
1005
|
-
member,
|
|
1006
|
-
member.modifiers,
|
|
1007
|
-
member.name,
|
|
1008
|
-
member.questionToken,
|
|
1009
|
-
wrapWithSchemeIfComponent(member.type)
|
|
1010
|
-
);
|
|
1011
|
-
}
|
|
1012
|
-
return member;
|
|
1013
|
-
})
|
|
1014
|
-
);
|
|
1015
|
-
}
|
|
1016
|
-
return typeNode;
|
|
1017
|
-
}
|
|
1018
846
|
}
|
|
1019
847
|
function generatePathExpression(path4, pathParameters, rootObject, isFlatArg, encodePathParams) {
|
|
1020
848
|
const expressions = [];
|
|
@@ -1042,55 +870,7 @@ function generatePathExpression(path4, pathParameters, rootObject, isFlatArg, en
|
|
|
1042
870
|
}
|
|
1043
871
|
|
|
1044
872
|
// src/index.ts
|
|
1045
|
-
import camelCase2 from "lodash.camelcase";
|
|
1046
873
|
var require2 = createRequire(__filename);
|
|
1047
|
-
async function ensureDirectoryExists(filePath) {
|
|
1048
|
-
const dirname = path3.dirname(filePath);
|
|
1049
|
-
if (!fs.existsSync(dirname)) {
|
|
1050
|
-
await fs.promises.mkdir(dirname, { recursive: true });
|
|
1051
|
-
}
|
|
1052
|
-
}
|
|
1053
|
-
function fileExists(filePath) {
|
|
1054
|
-
try {
|
|
1055
|
-
return fs.statSync(filePath).isFile();
|
|
1056
|
-
} catch {
|
|
1057
|
-
return false;
|
|
1058
|
-
}
|
|
1059
|
-
}
|
|
1060
|
-
function getApiNameFromDir(dirPath) {
|
|
1061
|
-
const dirName = path3.basename(dirPath);
|
|
1062
|
-
return `${dirName}Api`;
|
|
1063
|
-
}
|
|
1064
|
-
async function ensureBaseFilesExist(outputDir) {
|
|
1065
|
-
const enhanceEndpointsPath = path3.join(outputDir, "enhanceEndpoints.ts");
|
|
1066
|
-
const indexPath = path3.join(outputDir, "index.ts");
|
|
1067
|
-
const apiName = getApiNameFromDir(outputDir);
|
|
1068
|
-
if (!fileExists(enhanceEndpointsPath)) {
|
|
1069
|
-
const enhanceEndpointsContent = `import api from './query.generated';
|
|
1070
|
-
|
|
1071
|
-
const enhancedApi = api.enhanceEndpoints({
|
|
1072
|
-
endpoints: {
|
|
1073
|
-
},
|
|
1074
|
-
});
|
|
1075
|
-
|
|
1076
|
-
export default enhancedApi;
|
|
1077
|
-
`;
|
|
1078
|
-
await fs.promises.writeFile(enhanceEndpointsPath, enhanceEndpointsContent, "utf-8");
|
|
1079
|
-
}
|
|
1080
|
-
if (!fileExists(indexPath)) {
|
|
1081
|
-
const indexContent = `export * from './query.generated';
|
|
1082
|
-
export {default as ${apiName}} from './enhanceEndpoints';
|
|
1083
|
-
`;
|
|
1084
|
-
await fs.promises.writeFile(indexPath, indexContent, "utf-8");
|
|
1085
|
-
}
|
|
1086
|
-
}
|
|
1087
|
-
function getGroupNameFromPath(path4, pattern) {
|
|
1088
|
-
const match = path4.match(pattern);
|
|
1089
|
-
if (match && match[1]) {
|
|
1090
|
-
return camelCase2(match[1]);
|
|
1091
|
-
}
|
|
1092
|
-
return "common";
|
|
1093
|
-
}
|
|
1094
874
|
async function generateEndpoints(options) {
|
|
1095
875
|
const schemaLocation = options.schemaFile;
|
|
1096
876
|
const schemaAbsPath = isValidUrl(options.schemaFile) ? options.schemaFile : path3.resolve(process.cwd(), schemaLocation);
|
|
@@ -1099,12 +879,8 @@ async function generateEndpoints(options) {
|
|
|
1099
879
|
});
|
|
1100
880
|
const { outputFile, prettierConfigFile } = options;
|
|
1101
881
|
if (outputFile) {
|
|
1102
|
-
const outputPath = path3.resolve(process.cwd(), outputFile);
|
|
1103
|
-
await ensureDirectoryExists(outputPath);
|
|
1104
|
-
const outputDir = path3.dirname(outputPath);
|
|
1105
|
-
await ensureBaseFilesExist(outputDir);
|
|
1106
882
|
fs.writeFileSync(
|
|
1107
|
-
|
|
883
|
+
path3.resolve(process.cwd(), outputFile),
|
|
1108
884
|
await prettify(outputFile, sourceCode, prettierConfigFile)
|
|
1109
885
|
);
|
|
1110
886
|
} else {
|
|
@@ -1115,53 +891,13 @@ function parseConfig(fullConfig) {
|
|
|
1115
891
|
const outFiles = [];
|
|
1116
892
|
if ("outputFiles" in fullConfig) {
|
|
1117
893
|
const { outputFiles, ...commonConfig } = fullConfig;
|
|
1118
|
-
const
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
const groupName = getGroupNameFromPath(path4, pattern);
|
|
1126
|
-
if (!acc[groupName]) {
|
|
1127
|
-
acc[groupName] = [];
|
|
1128
|
-
}
|
|
1129
|
-
acc[groupName].push(path4);
|
|
1130
|
-
return acc;
|
|
1131
|
-
}, {});
|
|
1132
|
-
Object.entries(groupedPaths).forEach(([groupName, paths2]) => {
|
|
1133
|
-
const finalOutputPath = outputPath.replace("$1", groupName);
|
|
1134
|
-
if (filterEndpoint) {
|
|
1135
|
-
const pathBasedFilter = (operationName, operationDefinition) => {
|
|
1136
|
-
const path4 = operationDefinition.path;
|
|
1137
|
-
const pathGroupName = getGroupNameFromPath(path4, pattern);
|
|
1138
|
-
if (pathGroupName !== groupName) {
|
|
1139
|
-
return false;
|
|
1140
|
-
}
|
|
1141
|
-
const endpointFilter = filterEndpoint(groupName);
|
|
1142
|
-
if (endpointFilter instanceof RegExp) {
|
|
1143
|
-
return endpointFilter.test(operationName);
|
|
1144
|
-
}
|
|
1145
|
-
return true;
|
|
1146
|
-
};
|
|
1147
|
-
outFiles.push({
|
|
1148
|
-
...commonConfig,
|
|
1149
|
-
outputFile: finalOutputPath,
|
|
1150
|
-
filterEndpoints: pathBasedFilter
|
|
1151
|
-
});
|
|
1152
|
-
} else {
|
|
1153
|
-
const pathBasedFilter = (operationName, operationDefinition) => {
|
|
1154
|
-
const path4 = operationDefinition.path;
|
|
1155
|
-
const pathGroupName = getGroupNameFromPath(path4, pattern);
|
|
1156
|
-
return pathGroupName === groupName;
|
|
1157
|
-
};
|
|
1158
|
-
outFiles.push({
|
|
1159
|
-
...commonConfig,
|
|
1160
|
-
outputFile: finalOutputPath,
|
|
1161
|
-
filterEndpoints: pathBasedFilter
|
|
1162
|
-
});
|
|
1163
|
-
}
|
|
1164
|
-
});
|
|
894
|
+
for (const [outputFile, specificConfig] of Object.entries(outputFiles)) {
|
|
895
|
+
outFiles.push({
|
|
896
|
+
...commonConfig,
|
|
897
|
+
...specificConfig,
|
|
898
|
+
outputFile
|
|
899
|
+
});
|
|
900
|
+
}
|
|
1165
901
|
} else {
|
|
1166
902
|
outFiles.push(fullConfig);
|
|
1167
903
|
}
|