@acrool/rtk-query-codegen-openapi 0.0.7 → 0.0.9
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/README.md +33 -0
- package/lib/index.d.mts +15 -5
- package/lib/index.d.ts +15 -5
- package/lib/index.js +185 -129
- package/lib/index.js.map +1 -1
- package/lib/index.mjs +185 -129
- package/lib/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/generate.ts +33 -8
- package/src/generators/react-hooks.ts +10 -4
- package/src/index.ts +76 -77
- package/src/types.ts +20 -5
- package/src/utils/downloadSchema.ts +33 -0
- package/src/utils/index.ts +1 -0
- package/src/utils/isQuery.ts +9 -1
package/lib/index.js
CHANGED
|
@@ -34,13 +34,13 @@ __export(src_exports, {
|
|
|
34
34
|
parseConfig: () => parseConfig
|
|
35
35
|
});
|
|
36
36
|
module.exports = __toCommonJS(src_exports);
|
|
37
|
-
var
|
|
37
|
+
var import_node_fs2 = __toESM(require("node:fs"));
|
|
38
38
|
var import_node_module = require("node:module");
|
|
39
|
-
var
|
|
39
|
+
var import_node_path4 = __toESM(require("node:path"));
|
|
40
40
|
|
|
41
41
|
// src/generate.ts
|
|
42
42
|
var import_lodash = __toESM(require("lodash.camelcase"));
|
|
43
|
-
var
|
|
43
|
+
var import_node_path3 = __toESM(require("node:path"));
|
|
44
44
|
var import_generate3 = __toESM(require("oazapfts/generate"));
|
|
45
45
|
var import_typescript4 = __toESM(require("typescript"));
|
|
46
46
|
|
|
@@ -223,16 +223,54 @@ function capitalize(str) {
|
|
|
223
223
|
return str.replace(str[0], str[0].toUpperCase());
|
|
224
224
|
}
|
|
225
225
|
|
|
226
|
+
// src/utils/downloadSchema.ts
|
|
227
|
+
var import_node_fs = __toESM(require("node:fs"));
|
|
228
|
+
var import_node_path = __toESM(require("node:path"));
|
|
229
|
+
|
|
230
|
+
// src/utils/isValidUrl.ts
|
|
231
|
+
function isValidUrl(string) {
|
|
232
|
+
try {
|
|
233
|
+
new URL(string);
|
|
234
|
+
} catch (_) {
|
|
235
|
+
return false;
|
|
236
|
+
}
|
|
237
|
+
return true;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
// src/utils/downloadSchema.ts
|
|
241
|
+
async function downloadSchemaFile(remoteFile, targetPath) {
|
|
242
|
+
if (!isValidUrl(remoteFile)) {
|
|
243
|
+
throw new Error(`remoteFile must be a valid URL: ${remoteFile}`);
|
|
244
|
+
}
|
|
245
|
+
try {
|
|
246
|
+
const dir = import_node_path.default.dirname(targetPath);
|
|
247
|
+
if (!import_node_fs.default.existsSync(dir)) {
|
|
248
|
+
await import_node_fs.default.promises.mkdir(dir, { recursive: true });
|
|
249
|
+
}
|
|
250
|
+
const response = await fetch(remoteFile);
|
|
251
|
+
if (!response.ok) {
|
|
252
|
+
throw new Error(`Failed to download schema from ${remoteFile}: ${response.statusText}`);
|
|
253
|
+
}
|
|
254
|
+
const content = await response.text();
|
|
255
|
+
await import_node_fs.default.promises.writeFile(targetPath, content, "utf-8");
|
|
256
|
+
console.log(`Schema downloaded from ${remoteFile} to ${targetPath}`);
|
|
257
|
+
return targetPath;
|
|
258
|
+
} catch (error) {
|
|
259
|
+
console.error(`Error downloading schema from ${remoteFile}:`, error);
|
|
260
|
+
throw error;
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
|
|
226
264
|
// src/types.ts
|
|
227
265
|
var operationKeys = ["get", "put", "post", "delete", "options", "head", "patch", "trace"];
|
|
228
266
|
|
|
229
267
|
// src/utils/getOperationDefinitions.ts
|
|
230
268
|
function getOperationDefinitions(v3Doc) {
|
|
231
269
|
return Object.entries(v3Doc.paths).flatMap(
|
|
232
|
-
([
|
|
270
|
+
([path5, pathItem]) => !pathItem ? [] : Object.entries(pathItem).filter(
|
|
233
271
|
(arg) => operationKeys.includes(arg[0])
|
|
234
272
|
).map(([verb, operation]) => ({
|
|
235
|
-
path:
|
|
273
|
+
path: path5,
|
|
236
274
|
verb,
|
|
237
275
|
pathItem,
|
|
238
276
|
operation
|
|
@@ -259,25 +297,18 @@ async function getV3Doc(spec, httpResolverOptions) {
|
|
|
259
297
|
}
|
|
260
298
|
|
|
261
299
|
// src/utils/isQuery.ts
|
|
262
|
-
function isQuery(verb, overrides) {
|
|
300
|
+
function isQuery(verb, path5, overrides, queryMatch) {
|
|
301
|
+
if (queryMatch) {
|
|
302
|
+
return queryMatch(verb, path5);
|
|
303
|
+
}
|
|
263
304
|
if (overrides?.type) {
|
|
264
305
|
return overrides.type === "query";
|
|
265
306
|
}
|
|
266
307
|
return verb === "get";
|
|
267
308
|
}
|
|
268
309
|
|
|
269
|
-
// src/utils/isValidUrl.ts
|
|
270
|
-
function isValidUrl(string) {
|
|
271
|
-
try {
|
|
272
|
-
new URL(string);
|
|
273
|
-
} catch (_) {
|
|
274
|
-
return false;
|
|
275
|
-
}
|
|
276
|
-
return true;
|
|
277
|
-
}
|
|
278
|
-
|
|
279
310
|
// src/utils/prettier.ts
|
|
280
|
-
var
|
|
311
|
+
var import_node_path2 = __toESM(require("node:path"));
|
|
281
312
|
var import_prettier = __toESM(require("prettier"));
|
|
282
313
|
var EXTENSION_TO_PARSER = {
|
|
283
314
|
ts: "typescript",
|
|
@@ -300,7 +331,7 @@ async function prettify(filePath, content, prettierConfigFile) {
|
|
|
300
331
|
let config = null;
|
|
301
332
|
let parser = "typescript";
|
|
302
333
|
if (filePath) {
|
|
303
|
-
const fileExtension =
|
|
334
|
+
const fileExtension = import_node_path2.default.extname(filePath).slice(1);
|
|
304
335
|
parser = EXTENSION_TO_PARSER[fileExtension];
|
|
305
336
|
config = await import_prettier.default.resolveConfig(process.cwd(), {
|
|
306
337
|
useCache: true,
|
|
@@ -326,24 +357,26 @@ function removeUndefined(t) {
|
|
|
326
357
|
|
|
327
358
|
// src/generators/react-hooks.ts
|
|
328
359
|
var createBinding = ({
|
|
329
|
-
operationDefinition: { verb, path:
|
|
360
|
+
operationDefinition: { verb, path: path5 },
|
|
330
361
|
overrides,
|
|
331
|
-
isLazy = false
|
|
362
|
+
isLazy = false,
|
|
363
|
+
queryMatch
|
|
332
364
|
}) => factory.createBindingElement(
|
|
333
365
|
void 0,
|
|
334
366
|
void 0,
|
|
335
367
|
factory.createIdentifier(
|
|
336
|
-
`use${isLazy ? "Lazy" : ""}${capitalize((0, import_generate.getOperationName)(verb,
|
|
368
|
+
`use${isLazy ? "Lazy" : ""}${capitalize((0, import_generate.getOperationName)(verb, path5, void 0))}${isQuery(verb, path5, overrides, queryMatch) ? "Query" : "Mutation"}`
|
|
337
369
|
),
|
|
338
370
|
void 0
|
|
339
371
|
);
|
|
340
|
-
var getReactHookName = ({ operationDefinition, endpointOverrides, config }) => {
|
|
372
|
+
var getReactHookName = ({ operationDefinition, endpointOverrides, config, queryMatch }) => {
|
|
341
373
|
const overrides = getOverrides(operationDefinition, endpointOverrides);
|
|
342
374
|
const baseParams = {
|
|
343
375
|
operationDefinition,
|
|
344
|
-
overrides
|
|
376
|
+
overrides,
|
|
377
|
+
queryMatch
|
|
345
378
|
};
|
|
346
|
-
const _isQuery = isQuery(operationDefinition.verb, overrides);
|
|
379
|
+
const _isQuery = isQuery(operationDefinition.verb, operationDefinition.path, overrides, queryMatch);
|
|
347
380
|
if (typeof config === "boolean") {
|
|
348
381
|
return createBinding(baseParams);
|
|
349
382
|
}
|
|
@@ -359,14 +392,15 @@ var generateReactHooks = ({
|
|
|
359
392
|
exportName,
|
|
360
393
|
operationDefinitions,
|
|
361
394
|
endpointOverrides,
|
|
362
|
-
config
|
|
395
|
+
config,
|
|
396
|
+
queryMatch
|
|
363
397
|
}) => factory.createVariableStatement(
|
|
364
398
|
[factory.createModifier(import_typescript3.default.SyntaxKind.ExportKeyword)],
|
|
365
399
|
factory.createVariableDeclarationList(
|
|
366
400
|
[
|
|
367
401
|
factory.createVariableDeclaration(
|
|
368
402
|
factory.createObjectBindingPattern(
|
|
369
|
-
operationDefinitions.map((operationDefinition) => getReactHookName({ operationDefinition, endpointOverrides, config })).flat()
|
|
403
|
+
operationDefinitions.map((operationDefinition) => getReactHookName({ operationDefinition, endpointOverrides, config, queryMatch })).flat()
|
|
370
404
|
),
|
|
371
405
|
void 0,
|
|
372
406
|
void 0,
|
|
@@ -387,8 +421,8 @@ function defaultIsDataResponse(code, includeDefault) {
|
|
|
387
421
|
const parsedCode = Number(code);
|
|
388
422
|
return !Number.isNaN(parsedCode) && parsedCode >= 200 && parsedCode < 300;
|
|
389
423
|
}
|
|
390
|
-
function getOperationName2({ verb, path:
|
|
391
|
-
return (0, import_generate3.getOperationName)(verb,
|
|
424
|
+
function getOperationName2({ verb, path: path5 }) {
|
|
425
|
+
return (0, import_generate3.getOperationName)(verb, path5, void 0);
|
|
392
426
|
}
|
|
393
427
|
function getTags({ verb, pathItem }) {
|
|
394
428
|
return verb ? pathItem[verb]?.tags || [] : [];
|
|
@@ -454,7 +488,9 @@ async function generateApi(spec, {
|
|
|
454
488
|
useEnumType = false,
|
|
455
489
|
mergeReadWriteOnly = false,
|
|
456
490
|
httpResolverOptions,
|
|
457
|
-
sharedTypesFile
|
|
491
|
+
sharedTypesFile,
|
|
492
|
+
queryMatch,
|
|
493
|
+
endpointsQueryReturnTypeFile = "./endpointsQueryReturnType"
|
|
458
494
|
}) {
|
|
459
495
|
const v3Doc = v3DocCache[spec] ??= await getV3Doc(spec, httpResolverOptions);
|
|
460
496
|
const apiGen = new import_generate3.default(v3Doc, {
|
|
@@ -596,10 +632,10 @@ async function generateApi(spec, {
|
|
|
596
632
|
}
|
|
597
633
|
}
|
|
598
634
|
}
|
|
599
|
-
const
|
|
600
|
-
const
|
|
601
|
-
const sharedTypesDir =
|
|
602
|
-
await
|
|
635
|
+
const fs3 = await import("node:fs/promises");
|
|
636
|
+
const path5 = await import("node:path");
|
|
637
|
+
const sharedTypesDir = path5.dirname(sharedTypesFile);
|
|
638
|
+
await fs3.mkdir(sharedTypesDir, { recursive: true });
|
|
603
639
|
const output = printer2.printNode(
|
|
604
640
|
import_typescript4.default.EmitHint.Unspecified,
|
|
605
641
|
factory.createSourceFile(
|
|
@@ -609,7 +645,7 @@ async function generateApi(spec, {
|
|
|
609
645
|
),
|
|
610
646
|
resultFile2
|
|
611
647
|
);
|
|
612
|
-
await
|
|
648
|
+
await fs3.writeFile(sharedTypesFile, output, "utf-8");
|
|
613
649
|
}
|
|
614
650
|
if (apiGen.spec.components?.schemas) {
|
|
615
651
|
apiGen.preprocessComponents(apiGen.spec.components.schemas);
|
|
@@ -634,25 +670,32 @@ async function generateApi(spec, {
|
|
|
634
670
|
return declaration;
|
|
635
671
|
}
|
|
636
672
|
if (outputFile) {
|
|
637
|
-
outputFile =
|
|
673
|
+
outputFile = import_node_path3.default.resolve(process.cwd(), outputFile);
|
|
638
674
|
if (apiFile.startsWith(".")) {
|
|
639
|
-
apiFile =
|
|
675
|
+
apiFile = import_node_path3.default.relative(import_node_path3.default.dirname(outputFile), apiFile);
|
|
640
676
|
apiFile = apiFile.replace(/\\/g, "/");
|
|
641
677
|
if (!apiFile.startsWith(".")) apiFile = `./${apiFile}`;
|
|
642
678
|
}
|
|
679
|
+
if (endpointsQueryReturnTypeFile.startsWith(".")) {
|
|
680
|
+
endpointsQueryReturnTypeFile = import_node_path3.default.relative(import_node_path3.default.dirname(outputFile), endpointsQueryReturnTypeFile);
|
|
681
|
+
endpointsQueryReturnTypeFile = endpointsQueryReturnTypeFile.replace(/\\/g, "/");
|
|
682
|
+
if (!endpointsQueryReturnTypeFile.startsWith(".")) endpointsQueryReturnTypeFile = `./${endpointsQueryReturnTypeFile}`;
|
|
683
|
+
}
|
|
643
684
|
}
|
|
644
685
|
apiFile = apiFile.replace(/\.[jt]sx?$/, "");
|
|
686
|
+
endpointsQueryReturnTypeFile = endpointsQueryReturnTypeFile.replace(/\.[jt]sx?$/, "");
|
|
645
687
|
const sharedTypesImportPath = sharedTypesFile && outputFile ? (() => {
|
|
646
|
-
let rel =
|
|
688
|
+
let rel = import_node_path3.default.relative(import_node_path3.default.dirname(outputFile), sharedTypesFile).replace(/\\/g, "/").replace(/\.[jt]sx?$/, "");
|
|
647
689
|
if (!rel.startsWith(".")) rel = "./" + rel;
|
|
648
690
|
return rel;
|
|
649
691
|
})() : "./shared-types";
|
|
650
|
-
|
|
692
|
+
const operationNames = [];
|
|
693
|
+
const sourceCode = printer.printNode(
|
|
651
694
|
import_typescript4.default.EmitHint.Unspecified,
|
|
652
695
|
factory.createSourceFile(
|
|
653
696
|
[
|
|
654
697
|
generateImportNode(apiFile, { [apiImport]: "api" }),
|
|
655
|
-
generateImportNode(
|
|
698
|
+
generateImportNode(endpointsQueryReturnTypeFile, { IRestFulEndpointsQueryReturn: "IRestFulEndpointsQueryReturn" }),
|
|
656
699
|
...sharedTypesFile ? [
|
|
657
700
|
generateImportNode(sharedTypesImportPath, {
|
|
658
701
|
Scheme: "Scheme",
|
|
@@ -663,13 +706,17 @@ async function generateApi(spec, {
|
|
|
663
706
|
generateCreateApiCall({
|
|
664
707
|
tag,
|
|
665
708
|
endpointDefinitions: factory.createObjectLiteralExpression(
|
|
666
|
-
operationDefinitions.map(
|
|
667
|
-
(operationDefinition
|
|
709
|
+
operationDefinitions.map((operationDefinition) => {
|
|
710
|
+
const operationName = getOperationName2({ verb: operationDefinition.verb, path: operationDefinition.path });
|
|
711
|
+
const finalOperationName = operationNameSuffix ? capitalize(operationName + operationNameSuffix) : operationName;
|
|
712
|
+
operationNames.push(finalOperationName);
|
|
713
|
+
return generateEndpoint({
|
|
668
714
|
operationDefinition,
|
|
669
715
|
overrides: getOverrides(operationDefinition, endpointOverrides),
|
|
670
|
-
sharedTypesFile: !!sharedTypesFile
|
|
671
|
-
|
|
672
|
-
|
|
716
|
+
sharedTypesFile: !!sharedTypesFile,
|
|
717
|
+
queryMatch
|
|
718
|
+
});
|
|
719
|
+
}),
|
|
673
720
|
true
|
|
674
721
|
)
|
|
675
722
|
}),
|
|
@@ -681,7 +728,8 @@ async function generateApi(spec, {
|
|
|
681
728
|
exportName: generatedApiName,
|
|
682
729
|
operationDefinitions,
|
|
683
730
|
endpointOverrides,
|
|
684
|
-
config: hooks
|
|
731
|
+
config: hooks,
|
|
732
|
+
queryMatch
|
|
685
733
|
})
|
|
686
734
|
] : []
|
|
687
735
|
],
|
|
@@ -690,6 +738,10 @@ async function generateApi(spec, {
|
|
|
690
738
|
),
|
|
691
739
|
resultFile
|
|
692
740
|
);
|
|
741
|
+
return {
|
|
742
|
+
sourceCode,
|
|
743
|
+
operationNames
|
|
744
|
+
};
|
|
693
745
|
function extractAllTagTypes({ operationDefinitions: operationDefinitions2 }) {
|
|
694
746
|
const allTagTypes = /* @__PURE__ */ new Set();
|
|
695
747
|
for (const operationDefinition of operationDefinitions2) {
|
|
@@ -703,18 +755,19 @@ async function generateApi(spec, {
|
|
|
703
755
|
function generateEndpoint({
|
|
704
756
|
operationDefinition,
|
|
705
757
|
overrides,
|
|
706
|
-
sharedTypesFile: sharedTypesFile2
|
|
758
|
+
sharedTypesFile: sharedTypesFile2,
|
|
759
|
+
queryMatch: queryMatch2
|
|
707
760
|
}) {
|
|
708
761
|
const {
|
|
709
762
|
verb,
|
|
710
|
-
path:
|
|
763
|
+
path: path5,
|
|
711
764
|
pathItem,
|
|
712
765
|
operation,
|
|
713
766
|
operation: { responses, requestBody }
|
|
714
767
|
} = operationDefinition;
|
|
715
|
-
const operationName = getOperationName2({ verb, path:
|
|
768
|
+
const operationName = getOperationName2({ verb, path: path5 });
|
|
716
769
|
const tags = tag ? getTags({ verb, pathItem }) : [];
|
|
717
|
-
const isQuery2 = isQuery(verb, overrides);
|
|
770
|
+
const isQuery2 = isQuery(verb, path5, overrides, queryMatch2);
|
|
718
771
|
const returnsJson = apiGen.getResponseType(responses) === "json";
|
|
719
772
|
let ResponseType = factory.createKeywordTypeNode(import_typescript4.default.SyntaxKind.UnknownKeyword);
|
|
720
773
|
if (returnsJson) {
|
|
@@ -863,7 +916,7 @@ async function generateApi(spec, {
|
|
|
863
916
|
encodePathParams: encodePathParams2,
|
|
864
917
|
encodeQueryParams: encodeQueryParams2
|
|
865
918
|
}) {
|
|
866
|
-
const { path:
|
|
919
|
+
const { path: path5, verb, operation } = operationDefinition;
|
|
867
920
|
const bodyParameter = Object.values(queryArg).find((def) => def.origin === "body");
|
|
868
921
|
const rootObject = factory.createIdentifier("queryArg");
|
|
869
922
|
const variablesObject = factory.createPropertyAccessExpression(rootObject, factory.createIdentifier("variables"));
|
|
@@ -910,7 +963,7 @@ async function generateApi(spec, {
|
|
|
910
963
|
[
|
|
911
964
|
factory.createPropertyAssignment(
|
|
912
965
|
factory.createIdentifier("url"),
|
|
913
|
-
generatePathExpression(
|
|
966
|
+
generatePathExpression(path5, pickParams("path"), variablesObject, isFlatArg, encodePathParams2)
|
|
914
967
|
),
|
|
915
968
|
isQuery2 && verb.toUpperCase() === "GET" ? void 0 : factory.createPropertyAssignment(
|
|
916
969
|
factory.createIdentifier("method"),
|
|
@@ -1043,12 +1096,12 @@ async function generateApi(spec, {
|
|
|
1043
1096
|
return typeNode;
|
|
1044
1097
|
}
|
|
1045
1098
|
}
|
|
1046
|
-
function generatePathExpression(
|
|
1099
|
+
function generatePathExpression(path5, pathParameters, rootObject, isFlatArg, encodePathParams) {
|
|
1047
1100
|
const expressions = [];
|
|
1048
|
-
const head =
|
|
1101
|
+
const head = path5.replace(/\{(.*?)}(.*?)(?=\{|$)/g, (_, expression, literal) => {
|
|
1049
1102
|
const param = pathParameters.find((p) => p.originalName === expression);
|
|
1050
1103
|
if (!param) {
|
|
1051
|
-
throw new Error(`path parameter ${expression} does not seem to be defined in '${
|
|
1104
|
+
throw new Error(`path parameter ${expression} does not seem to be defined in '${path5}'!`);
|
|
1052
1105
|
}
|
|
1053
1106
|
expressions.push([param.name, literal]);
|
|
1054
1107
|
return "";
|
|
@@ -1072,82 +1125,81 @@ function generatePathExpression(path4, pathParameters, rootObject, isFlatArg, en
|
|
|
1072
1125
|
var import_lodash2 = __toESM(require("lodash.camelcase"));
|
|
1073
1126
|
var require2 = (0, import_node_module.createRequire)(__filename);
|
|
1074
1127
|
async function ensureDirectoryExists(filePath) {
|
|
1075
|
-
const dirname =
|
|
1076
|
-
if (!
|
|
1077
|
-
await
|
|
1128
|
+
const dirname = import_node_path4.default.dirname(filePath);
|
|
1129
|
+
if (!import_node_fs2.default.existsSync(dirname)) {
|
|
1130
|
+
await import_node_fs2.default.promises.mkdir(dirname, { recursive: true });
|
|
1078
1131
|
}
|
|
1079
1132
|
}
|
|
1080
1133
|
function fileExists(filePath) {
|
|
1081
1134
|
try {
|
|
1082
|
-
return
|
|
1135
|
+
return import_node_fs2.default.statSync(filePath).isFile();
|
|
1083
1136
|
} catch {
|
|
1084
1137
|
return false;
|
|
1085
1138
|
}
|
|
1086
1139
|
}
|
|
1087
1140
|
function getApiNameFromDir(dirPath) {
|
|
1088
|
-
const dirName =
|
|
1141
|
+
const dirName = import_node_path4.default.basename(dirPath);
|
|
1089
1142
|
return `${dirName}Api`;
|
|
1090
1143
|
}
|
|
1091
|
-
async function ensureBaseFilesExist(outputDir) {
|
|
1092
|
-
const enhanceEndpointsPath =
|
|
1093
|
-
const indexPath =
|
|
1144
|
+
async function ensureBaseFilesExist(outputDir, operationNames) {
|
|
1145
|
+
const enhanceEndpointsPath = import_node_path4.default.join(outputDir, "enhanceEndpoints.ts");
|
|
1146
|
+
const indexPath = import_node_path4.default.join(outputDir, "index.ts");
|
|
1094
1147
|
const apiName = getApiNameFromDir(outputDir);
|
|
1095
1148
|
if (!fileExists(enhanceEndpointsPath)) {
|
|
1149
|
+
const operationNamesString = operationNames.map((name) => ` ${name}: {},`).join("\n");
|
|
1096
1150
|
const enhanceEndpointsContent = `import api from './query.generated';
|
|
1097
1151
|
|
|
1098
1152
|
const enhancedApi = api.enhanceEndpoints({
|
|
1099
1153
|
endpoints: {
|
|
1154
|
+
${operationNamesString}
|
|
1100
1155
|
},
|
|
1101
1156
|
});
|
|
1102
1157
|
|
|
1103
1158
|
export default enhancedApi;
|
|
1104
1159
|
`;
|
|
1105
|
-
await
|
|
1160
|
+
await import_node_fs2.default.promises.writeFile(enhanceEndpointsPath, enhanceEndpointsContent, "utf-8");
|
|
1106
1161
|
}
|
|
1107
1162
|
if (!fileExists(indexPath)) {
|
|
1108
1163
|
const indexContent = `export * from './query.generated';
|
|
1109
1164
|
export {default as ${apiName}} from './enhanceEndpoints';
|
|
1110
1165
|
`;
|
|
1111
|
-
await
|
|
1166
|
+
await import_node_fs2.default.promises.writeFile(indexPath, indexContent, "utf-8");
|
|
1112
1167
|
}
|
|
1113
1168
|
}
|
|
1114
|
-
function getGroupNameFromPath(path4, pattern) {
|
|
1115
|
-
const match = path4.match(pattern);
|
|
1116
|
-
if (match && match[1]) {
|
|
1117
|
-
return (0, import_lodash2.default)(match[1]);
|
|
1118
|
-
}
|
|
1119
|
-
return "common";
|
|
1120
|
-
}
|
|
1121
1169
|
async function generateEndpoints(options) {
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1170
|
+
let actualSchemaFile = options.schemaFile;
|
|
1171
|
+
if (options.remoteFile) {
|
|
1172
|
+
actualSchemaFile = await downloadSchemaFile(options.remoteFile, options.schemaFile);
|
|
1173
|
+
}
|
|
1174
|
+
const updatedOptions = {
|
|
1175
|
+
...options,
|
|
1176
|
+
schemaFile: actualSchemaFile
|
|
1177
|
+
};
|
|
1178
|
+
const schemaLocation = updatedOptions.schemaFile;
|
|
1179
|
+
const schemaAbsPath = import_node_path4.default.resolve(process.cwd(), schemaLocation);
|
|
1180
|
+
if ("outputFiles" in options) {
|
|
1181
|
+
const { outputFiles, ...commonConfig } = updatedOptions;
|
|
1182
|
+
const openApiDoc = await getV3Doc(actualSchemaFile, updatedOptions.httpResolverOptions);
|
|
1127
1183
|
const paths = Object.keys(openApiDoc.paths);
|
|
1128
|
-
const
|
|
1129
|
-
const
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
const groupedPaths = paths.reduce((acc, path4) => {
|
|
1134
|
-
const groupName = getGroupNameFromPath(path4, pattern);
|
|
1135
|
-
if (!acc[groupName]) {
|
|
1136
|
-
acc[groupName] = [];
|
|
1184
|
+
const { groupKeyMatch, outputDir, filterEndpoint, queryMatch } = outputFiles;
|
|
1185
|
+
const groupedPaths = paths.reduce((acc, path5) => {
|
|
1186
|
+
const groupKey = (0, import_lodash2.default)(groupKeyMatch(path5));
|
|
1187
|
+
if (!acc[groupKey]) {
|
|
1188
|
+
acc[groupKey] = [];
|
|
1137
1189
|
}
|
|
1138
|
-
acc[
|
|
1190
|
+
acc[groupKey].push(path5);
|
|
1139
1191
|
return acc;
|
|
1140
1192
|
}, {});
|
|
1141
|
-
for (const [
|
|
1142
|
-
const finalOutputPath =
|
|
1193
|
+
for (const [groupKey, paths2] of Object.entries(groupedPaths)) {
|
|
1194
|
+
const finalOutputPath = `${outputDir}/${groupKey}/query.generated.ts`;
|
|
1143
1195
|
if (filterEndpoint) {
|
|
1144
1196
|
const pathBasedFilter = (operationName, operationDefinition) => {
|
|
1145
|
-
const
|
|
1146
|
-
const
|
|
1147
|
-
if (
|
|
1197
|
+
const path5 = operationDefinition.path;
|
|
1198
|
+
const pathGroupKey = (0, import_lodash2.default)(groupKeyMatch(path5));
|
|
1199
|
+
if (pathGroupKey !== groupKey) {
|
|
1148
1200
|
return false;
|
|
1149
1201
|
}
|
|
1150
|
-
const endpointFilter = filterEndpoint(
|
|
1202
|
+
const endpointFilter = filterEndpoint(groupKey);
|
|
1151
1203
|
if (endpointFilter instanceof RegExp) {
|
|
1152
1204
|
return endpointFilter.test(operationName);
|
|
1153
1205
|
}
|
|
@@ -1156,45 +1208,49 @@ async function generateEndpoints(options) {
|
|
|
1156
1208
|
const groupOptions = {
|
|
1157
1209
|
...commonConfig,
|
|
1158
1210
|
outputFile: finalOutputPath,
|
|
1159
|
-
|
|
1211
|
+
sharedTypesFile: `${outputDir}/shared-types.ts`,
|
|
1212
|
+
filterEndpoints: pathBasedFilter,
|
|
1213
|
+
queryMatch
|
|
1160
1214
|
};
|
|
1161
1215
|
await generateSingleEndpoint(groupOptions);
|
|
1162
1216
|
} else {
|
|
1163
1217
|
const pathBasedFilter = (operationName, operationDefinition) => {
|
|
1164
|
-
const
|
|
1165
|
-
const
|
|
1166
|
-
return
|
|
1218
|
+
const path5 = operationDefinition.path;
|
|
1219
|
+
const pathGroupKey = (0, import_lodash2.default)(groupKeyMatch(path5));
|
|
1220
|
+
return pathGroupKey === groupKey;
|
|
1167
1221
|
};
|
|
1168
1222
|
const groupOptions = {
|
|
1169
1223
|
...commonConfig,
|
|
1170
1224
|
outputFile: finalOutputPath,
|
|
1171
|
-
|
|
1225
|
+
sharedTypesFile: `${outputDir}/shared-types.ts`,
|
|
1226
|
+
filterEndpoints: pathBasedFilter,
|
|
1227
|
+
queryMatch
|
|
1172
1228
|
};
|
|
1173
1229
|
await generateSingleEndpoint(groupOptions);
|
|
1174
1230
|
}
|
|
1175
1231
|
}
|
|
1176
1232
|
return;
|
|
1177
1233
|
}
|
|
1178
|
-
await generateSingleEndpoint(
|
|
1234
|
+
await generateSingleEndpoint(updatedOptions);
|
|
1179
1235
|
}
|
|
1180
1236
|
async function generateSingleEndpoint(options) {
|
|
1181
1237
|
const schemaLocation = options.schemaFile;
|
|
1182
|
-
const schemaAbsPath =
|
|
1183
|
-
const
|
|
1238
|
+
const schemaAbsPath = import_node_path4.default.resolve(process.cwd(), schemaLocation);
|
|
1239
|
+
const result = await enforceOazapftsTsVersion(async () => {
|
|
1184
1240
|
return generateApi(schemaAbsPath, options);
|
|
1185
1241
|
});
|
|
1186
1242
|
const { outputFile, prettierConfigFile } = options;
|
|
1187
1243
|
if (outputFile) {
|
|
1188
|
-
const outputPath =
|
|
1244
|
+
const outputPath = import_node_path4.default.resolve(process.cwd(), outputFile);
|
|
1189
1245
|
await ensureDirectoryExists(outputPath);
|
|
1190
|
-
const outputDir =
|
|
1191
|
-
await ensureBaseFilesExist(outputDir);
|
|
1192
|
-
|
|
1246
|
+
const outputDir = import_node_path4.default.dirname(outputPath);
|
|
1247
|
+
await ensureBaseFilesExist(outputDir, result.operationNames);
|
|
1248
|
+
import_node_fs2.default.writeFileSync(
|
|
1193
1249
|
outputPath,
|
|
1194
|
-
await prettify(outputFile, sourceCode, prettierConfigFile)
|
|
1250
|
+
await prettify(outputFile, result.sourceCode, prettierConfigFile)
|
|
1195
1251
|
);
|
|
1196
1252
|
} else {
|
|
1197
|
-
return await prettify(null, sourceCode, prettierConfigFile);
|
|
1253
|
+
return await prettify(null, result.sourceCode, prettierConfigFile);
|
|
1198
1254
|
}
|
|
1199
1255
|
}
|
|
1200
1256
|
function parseConfig(fullConfig) {
|
|
@@ -1202,36 +1258,32 @@ function parseConfig(fullConfig) {
|
|
|
1202
1258
|
if ("outputFiles" in fullConfig) {
|
|
1203
1259
|
const { outputFiles, ...commonConfig } = fullConfig;
|
|
1204
1260
|
let openApiDoc;
|
|
1205
|
-
if (
|
|
1261
|
+
if (fullConfig.remoteFile) {
|
|
1206
1262
|
outFiles.push(fullConfig);
|
|
1207
1263
|
return outFiles;
|
|
1208
1264
|
} else {
|
|
1209
|
-
openApiDoc = JSON.parse(
|
|
1265
|
+
openApiDoc = JSON.parse(import_node_fs2.default.readFileSync(fullConfig.schemaFile, "utf-8"));
|
|
1210
1266
|
}
|
|
1211
1267
|
const paths = Object.keys(openApiDoc.paths);
|
|
1212
|
-
const
|
|
1213
|
-
const
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
const groupedPaths = paths.reduce((acc, path4) => {
|
|
1218
|
-
const groupName = getGroupNameFromPath(path4, pattern);
|
|
1219
|
-
if (!acc[groupName]) {
|
|
1220
|
-
acc[groupName] = [];
|
|
1268
|
+
const { groupKeyMatch, outputDir, filterEndpoint, queryMatch } = outputFiles;
|
|
1269
|
+
const groupedPaths = paths.reduce((acc, path5) => {
|
|
1270
|
+
const groupKey = (0, import_lodash2.default)(groupKeyMatch(path5));
|
|
1271
|
+
if (!acc[groupKey]) {
|
|
1272
|
+
acc[groupKey] = [];
|
|
1221
1273
|
}
|
|
1222
|
-
acc[
|
|
1274
|
+
acc[groupKey].push(path5);
|
|
1223
1275
|
return acc;
|
|
1224
1276
|
}, {});
|
|
1225
|
-
Object.entries(groupedPaths).forEach(([
|
|
1226
|
-
const finalOutputPath =
|
|
1277
|
+
Object.entries(groupedPaths).forEach(([groupKey, paths2]) => {
|
|
1278
|
+
const finalOutputPath = `${outputDir}/${groupKey}/query.generated.ts`;
|
|
1227
1279
|
if (filterEndpoint) {
|
|
1228
1280
|
const pathBasedFilter = (operationName, operationDefinition) => {
|
|
1229
|
-
const
|
|
1230
|
-
const
|
|
1231
|
-
if (
|
|
1281
|
+
const path5 = operationDefinition.path;
|
|
1282
|
+
const pathGroupKey = (0, import_lodash2.default)(groupKeyMatch(path5));
|
|
1283
|
+
if (pathGroupKey !== groupKey) {
|
|
1232
1284
|
return false;
|
|
1233
1285
|
}
|
|
1234
|
-
const endpointFilter = filterEndpoint(
|
|
1286
|
+
const endpointFilter = filterEndpoint(groupKey);
|
|
1235
1287
|
if (endpointFilter instanceof RegExp) {
|
|
1236
1288
|
return endpointFilter.test(operationName);
|
|
1237
1289
|
}
|
|
@@ -1240,18 +1292,22 @@ function parseConfig(fullConfig) {
|
|
|
1240
1292
|
outFiles.push({
|
|
1241
1293
|
...commonConfig,
|
|
1242
1294
|
outputFile: finalOutputPath,
|
|
1243
|
-
|
|
1295
|
+
sharedTypesFile: `${outputDir}/shared-types.ts`,
|
|
1296
|
+
filterEndpoints: pathBasedFilter,
|
|
1297
|
+
queryMatch
|
|
1244
1298
|
});
|
|
1245
1299
|
} else {
|
|
1246
1300
|
const pathBasedFilter = (operationName, operationDefinition) => {
|
|
1247
|
-
const
|
|
1248
|
-
const
|
|
1249
|
-
return
|
|
1301
|
+
const path5 = operationDefinition.path;
|
|
1302
|
+
const pathGroupKey = (0, import_lodash2.default)(groupKeyMatch(path5));
|
|
1303
|
+
return pathGroupKey === groupKey;
|
|
1250
1304
|
};
|
|
1251
1305
|
outFiles.push({
|
|
1252
1306
|
...commonConfig,
|
|
1253
1307
|
outputFile: finalOutputPath,
|
|
1254
|
-
|
|
1308
|
+
sharedTypesFile: `${outputDir}/shared-types.ts`,
|
|
1309
|
+
filterEndpoints: pathBasedFilter,
|
|
1310
|
+
queryMatch
|
|
1255
1311
|
});
|
|
1256
1312
|
}
|
|
1257
1313
|
});
|