@acrool/rtk-query-codegen-openapi 0.0.2-test.8 → 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 +116 -246
- package/lib/index.js.map +1 -1
- package/lib/index.mjs +116 -246
- package/lib/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/generate.ts +128 -162
- 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,61 +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
466
|
const components = v3Doc.components;
|
|
483
467
|
if (components) {
|
|
484
|
-
const
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
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);
|
|
489
480
|
return factory.createTypeAliasDeclaration(
|
|
490
481
|
[factory.createModifier(import_typescript4.default.SyntaxKind.ExportKeyword)],
|
|
491
|
-
factory.createIdentifier(
|
|
482
|
+
factory.createIdentifier(name),
|
|
492
483
|
void 0,
|
|
493
484
|
typeNode
|
|
494
485
|
);
|
|
495
486
|
});
|
|
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
|
-
);
|
|
502
487
|
});
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
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
|
|
496
|
+
);
|
|
497
|
+
const fs2 = await import("node:fs/promises");
|
|
498
|
+
await fs2.writeFile(sharedTypesFile, output, "utf-8");
|
|
507
499
|
}
|
|
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");
|
|
520
500
|
}
|
|
521
501
|
if (apiGen.spec.components?.schemas) {
|
|
522
502
|
apiGen.preprocessComponents(apiGen.spec.components.schemas);
|
|
@@ -547,20 +527,34 @@ async function generateApi(spec, {
|
|
|
547
527
|
apiFile = apiFile.replace(/\\/g, "/");
|
|
548
528
|
if (!apiFile.startsWith(".")) apiFile = `./${apiFile}`;
|
|
549
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
|
+
}
|
|
550
535
|
}
|
|
551
536
|
apiFile = apiFile.replace(/\.[jt]sx?$/, "");
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
return rel;
|
|
556
|
-
})() : "./shared-types";
|
|
537
|
+
if (sharedTypesFile) {
|
|
538
|
+
sharedTypesFile = sharedTypesFile.replace(/\.[jt]sx?$/, "");
|
|
539
|
+
}
|
|
557
540
|
return printer.printNode(
|
|
558
541
|
import_typescript4.default.EmitHint.Unspecified,
|
|
559
542
|
factory.createSourceFile(
|
|
560
543
|
[
|
|
561
544
|
generateImportNode(apiFile, { [apiImport]: "api" }),
|
|
562
545
|
generateImportNode("@acrool/react-fetcher", { IRestFulEndpointsQueryReturn: "IRestFulEndpointsQueryReturn" }),
|
|
563
|
-
...sharedTypesFile ? [
|
|
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
|
+
] : [],
|
|
564
558
|
...tag ? [generateTagTypes({ addTagTypes: extractAllTagTypes({ operationDefinitions }) })] : [],
|
|
565
559
|
generateCreateApiCall({
|
|
566
560
|
tag,
|
|
@@ -568,8 +562,7 @@ async function generateApi(spec, {
|
|
|
568
562
|
operationDefinitions.map(
|
|
569
563
|
(operationDefinition) => generateEndpoint({
|
|
570
564
|
operationDefinition,
|
|
571
|
-
overrides: getOverrides(operationDefinition, endpointOverrides)
|
|
572
|
-
sharedTypesFile: !!sharedTypesFile
|
|
565
|
+
overrides: getOverrides(operationDefinition, endpointOverrides)
|
|
573
566
|
})
|
|
574
567
|
),
|
|
575
568
|
true
|
|
@@ -608,8 +601,7 @@ async function generateApi(spec, {
|
|
|
608
601
|
}
|
|
609
602
|
function generateEndpoint({
|
|
610
603
|
operationDefinition,
|
|
611
|
-
overrides
|
|
612
|
-
sharedTypesFile: sharedTypesFile2
|
|
604
|
+
overrides
|
|
613
605
|
}) {
|
|
614
606
|
const {
|
|
615
607
|
verb,
|
|
@@ -618,49 +610,60 @@ async function generateApi(spec, {
|
|
|
618
610
|
operation,
|
|
619
611
|
operation: { responses, requestBody }
|
|
620
612
|
} = operationDefinition;
|
|
621
|
-
const operationName = getOperationName2({ verb, path: path4 });
|
|
613
|
+
const operationName = getOperationName2({ verb, path: path4, operation });
|
|
622
614
|
const tags = tag ? getTags({ verb, pathItem }) : [];
|
|
623
615
|
const isQuery2 = isQuery(verb, overrides);
|
|
624
616
|
const returnsJson = apiGen.getResponseType(responses) === "json";
|
|
625
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
|
+
}
|
|
626
646
|
if (returnsJson) {
|
|
627
647
|
const returnTypes = Object.entries(responses || {}).map(
|
|
628
|
-
([code, response]) =>
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
apiGen.getTypeFromResponse(response, "readOnly") || factory.createKeywordTypeNode(import_typescript4.default.SyntaxKind.UndefinedKeyword)
|
|
633
|
-
)
|
|
634
|
-
]
|
|
635
|
-
).filter(
|
|
636
|
-
([status, response]) => isDataResponse(status, includeDefault, apiGen.resolve(response), responses || {})
|
|
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
|
-
}
|
|
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)];
|
|
655
652
|
}
|
|
653
|
+
const schema = resolvedResponse.content["application/json"].schema;
|
|
654
|
+
const type = replaceReferences(schema);
|
|
655
|
+
return [code, resolvedResponse, type];
|
|
656
656
|
}
|
|
657
|
-
|
|
658
|
-
|
|
657
|
+
).filter(
|
|
658
|
+
([status, response]) => isDataResponse(status, includeDefault, apiGen.resolve(response), responses || {})
|
|
659
|
+
).filter(([_1, _2, type]) => type !== import_generate3.keywordType.void).map(
|
|
660
|
+
([code, response, type]) => import_typescript4.default.addSyntheticLeadingComment(
|
|
661
|
+
{ ...type },
|
|
659
662
|
import_typescript4.default.SyntaxKind.MultiLineCommentTrivia,
|
|
660
663
|
`* status ${code} ${response.description} `,
|
|
661
664
|
false
|
|
662
|
-
)
|
|
663
|
-
|
|
665
|
+
)
|
|
666
|
+
);
|
|
664
667
|
if (returnTypes.length > 0) {
|
|
665
668
|
ResponseType = factory.createUnionTypeNode(returnTypes);
|
|
666
669
|
}
|
|
@@ -695,21 +698,10 @@ async function generateApi(spec, {
|
|
|
695
698
|
}
|
|
696
699
|
return name;
|
|
697
700
|
}
|
|
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
|
-
}
|
|
709
701
|
if (requestBody) {
|
|
710
702
|
const body = apiGen.resolve(requestBody);
|
|
711
703
|
const schema = apiGen.getSchemaFromContent(body.content);
|
|
712
|
-
const type =
|
|
704
|
+
const type = replaceReferences(schema);
|
|
713
705
|
const schemaName = (0, import_lodash.default)(
|
|
714
706
|
type.name || (0, import_generate3.getReferenceName)(schema) || typeof schema === "object" && "title" in schema && schema.title || "body"
|
|
715
707
|
);
|
|
@@ -718,11 +710,24 @@ async function generateApi(spec, {
|
|
|
718
710
|
origin: "body",
|
|
719
711
|
name,
|
|
720
712
|
originalName: schemaName,
|
|
721
|
-
type
|
|
713
|
+
type,
|
|
722
714
|
required: true,
|
|
723
715
|
body
|
|
724
716
|
};
|
|
725
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
|
+
}
|
|
726
731
|
const propertyName = (name) => {
|
|
727
732
|
if (typeof name === "string") {
|
|
728
733
|
return (0, import_generate3.isValidIdentifier)(name) ? factory.createIdentifier(name) : factory.createStringLiteral(name);
|
|
@@ -859,49 +864,6 @@ async function generateApi(spec, {
|
|
|
859
864
|
function generateMutationEndpointProps({}) {
|
|
860
865
|
return {};
|
|
861
866
|
}
|
|
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
|
-
}
|
|
905
867
|
}
|
|
906
868
|
function generatePathExpression(path4, pathParameters, rootObject, isFlatArg, encodePathParams) {
|
|
907
869
|
const expressions = [];
|
|
@@ -929,55 +891,7 @@ function generatePathExpression(path4, pathParameters, rootObject, isFlatArg, en
|
|
|
929
891
|
}
|
|
930
892
|
|
|
931
893
|
// src/index.ts
|
|
932
|
-
var import_lodash2 = __toESM(require("lodash.camelcase"));
|
|
933
894
|
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
|
-
}
|
|
981
895
|
async function generateEndpoints(options) {
|
|
982
896
|
const schemaLocation = options.schemaFile;
|
|
983
897
|
const schemaAbsPath = isValidUrl(options.schemaFile) ? options.schemaFile : import_node_path3.default.resolve(process.cwd(), schemaLocation);
|
|
@@ -986,12 +900,8 @@ async function generateEndpoints(options) {
|
|
|
986
900
|
});
|
|
987
901
|
const { outputFile, prettierConfigFile } = options;
|
|
988
902
|
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);
|
|
993
903
|
import_node_fs.default.writeFileSync(
|
|
994
|
-
|
|
904
|
+
import_node_path3.default.resolve(process.cwd(), outputFile),
|
|
995
905
|
await prettify(outputFile, sourceCode, prettierConfigFile)
|
|
996
906
|
);
|
|
997
907
|
} else {
|
|
@@ -1002,53 +912,13 @@ function parseConfig(fullConfig) {
|
|
|
1002
912
|
const outFiles = [];
|
|
1003
913
|
if ("outputFiles" in fullConfig) {
|
|
1004
914
|
const { outputFiles, ...commonConfig } = fullConfig;
|
|
1005
|
-
const
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
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
|
-
if (filterEndpoint) {
|
|
1022
|
-
const pathBasedFilter = (operationName, operationDefinition) => {
|
|
1023
|
-
const path4 = operationDefinition.path;
|
|
1024
|
-
const pathGroupName = getGroupNameFromPath(path4, pattern);
|
|
1025
|
-
if (pathGroupName !== groupName) {
|
|
1026
|
-
return false;
|
|
1027
|
-
}
|
|
1028
|
-
const endpointFilter = filterEndpoint(groupName);
|
|
1029
|
-
if (endpointFilter instanceof RegExp) {
|
|
1030
|
-
return endpointFilter.test(operationName);
|
|
1031
|
-
}
|
|
1032
|
-
return true;
|
|
1033
|
-
};
|
|
1034
|
-
outFiles.push({
|
|
1035
|
-
...commonConfig,
|
|
1036
|
-
outputFile: finalOutputPath,
|
|
1037
|
-
filterEndpoints: pathBasedFilter
|
|
1038
|
-
});
|
|
1039
|
-
} else {
|
|
1040
|
-
const pathBasedFilter = (operationName, operationDefinition) => {
|
|
1041
|
-
const path4 = operationDefinition.path;
|
|
1042
|
-
const pathGroupName = getGroupNameFromPath(path4, pattern);
|
|
1043
|
-
return pathGroupName === groupName;
|
|
1044
|
-
};
|
|
1045
|
-
outFiles.push({
|
|
1046
|
-
...commonConfig,
|
|
1047
|
-
outputFile: finalOutputPath,
|
|
1048
|
-
filterEndpoints: pathBasedFilter
|
|
1049
|
-
});
|
|
1050
|
-
}
|
|
1051
|
-
});
|
|
915
|
+
for (const [outputFile, specificConfig] of Object.entries(outputFiles)) {
|
|
916
|
+
outFiles.push({
|
|
917
|
+
...commonConfig,
|
|
918
|
+
...specificConfig,
|
|
919
|
+
outputFile
|
|
920
|
+
});
|
|
921
|
+
}
|
|
1052
922
|
} else {
|
|
1053
923
|
outFiles.push(fullConfig);
|
|
1054
924
|
}
|