mobx-tanstack-query-api 0.35.1 → 0.36.1

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/cli.js CHANGED
@@ -382,17 +382,17 @@ const allEndpointPerFileTmpl = async (params) => {
382
382
  } = params;
383
383
  const { _ } = utils;
384
384
  const dataContractNamesInThisFile = [];
385
+ const dataContactNames = new Set(
386
+ Object.keys(
387
+ configuration.config.swaggerSchema?.components?.schemas
388
+ ).map((schemaName) => utils.formatModelName(schemaName))
389
+ );
385
390
  const newEndpointTemplates = routes.map((route) => {
386
391
  const newEndpointTemplateData = newEndpointTmpl({
387
392
  ...params,
388
393
  route
389
394
  });
390
395
  const { reservedDataContractNames } = newEndpointTemplateData;
391
- const dataContactNames = new Set(
392
- Object.keys(
393
- configuration.config.swaggerSchema?.components?.schemas
394
- ).map((schemaName) => utils.formatModelName(schemaName))
395
- );
396
396
  reservedDataContractNames.forEach((reservedDataContractName) => {
397
397
  if (!dataContactNames.has(reservedDataContractName)) {
398
398
  dataContractNamesInThisFile.push(reservedDataContractName);
@@ -435,14 +435,14 @@ const allEndpointPerFileTmpl = async (params) => {
435
435
  }
436
436
  )
437
437
  );
438
+ const endpointTemplatesContent = endpointTemplates.filter(Boolean).join("\n\n");
438
439
  if (metaInfo) {
439
440
  extraImportLines.push(
440
441
  `import { ${[groupName && "Group", metaInfo?.namespace && "namespace", "Tag"].filter(Boolean).join(",")} } from "${groupName ? "../" : "./"}meta-info";`
441
442
  );
442
443
  }
443
- return {
444
- reservedDataContractNames: dataContractNamesInThisFile,
445
- content: await formatTSContent(`${LINTERS_IGNORE}
444
+ const dataContractImportToken = "/*__DATA_CONTRACT_IMPORTS__*/";
445
+ const contentWithImportToken = await formatTSContent(`${LINTERS_IGNORE}
446
446
  import {
447
447
  RequestParams,
448
448
  HttpResponse,
@@ -452,32 +452,40 @@ const allEndpointPerFileTmpl = async (params) => {
452
452
  import { ${importFileParams.httpClient.exportName} } from "${importFileParams.httpClient.path}";
453
453
  import { ${importFileParams.queryClient.exportName} } from "${importFileParams.queryClient.path}";
454
454
  ${extraImportLines.join("\n")}
455
-
456
- ${configuration.modelTypes.length > 0 ? `
457
- import { ${configuration.modelTypes.map((it) => it.name).filter(
458
- (it) => !dataContractNamesInThisFile.includes(it)
459
- )} } from "${relativePathDataContracts}";
460
- ` : ""}
455
+ ${dataContractImportToken}
461
456
 
462
457
  ${(await Promise.all(
463
- dataContractNamesInThisFile.map(async (dataContractName) => {
464
- const modelType = configuration.modelTypes.find(
465
- (modelType2) => modelType2.name === dataContractName
466
- );
467
- if (!modelType) {
468
- return "";
469
- }
470
- const contractType = await dataContractTmpl({
471
- ...params,
472
- contract: modelType,
473
- addExportKeyword: true
474
- });
475
- return contractType;
476
- })
477
- )).filter(Boolean).join("\n\n")}
458
+ dataContractNamesInThisFile.map(async (dataContractName) => {
459
+ const modelType = configuration.modelTypes.find(
460
+ (modelType2) => modelType2.name === dataContractName
461
+ );
462
+ if (!modelType) {
463
+ return "";
464
+ }
465
+ const contractType = await dataContractTmpl({
466
+ ...params,
467
+ contract: modelType,
468
+ addExportKeyword: true
469
+ });
470
+ return contractType;
471
+ })
472
+ )).filter(Boolean).join("\n\n")}
478
473
 
479
- ${endpointTemplates.filter(Boolean).join("\n\n")}
480
- `)
474
+ ${endpointTemplatesContent}
475
+ `);
476
+ const escapeRegExp = (value) => value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
477
+ const usedDataContractNames = configuration.modelTypes.map((modelType) => modelType.name).filter(
478
+ (modelTypeName) => !dataContractNamesInThisFile.includes(modelTypeName) && dataContactNames.has(modelTypeName) && new RegExp(`\\b${escapeRegExp(modelTypeName)}\\b`).test(
479
+ contentWithImportToken
480
+ )
481
+ );
482
+ const dataContractImportLine = usedDataContractNames.length > 0 ? `import { ${usedDataContractNames.join(", ")} } from "${relativePathDataContracts}";` : "";
483
+ return {
484
+ reservedDataContractNames: dataContractNamesInThisFile,
485
+ content: contentWithImportToken.replace(
486
+ dataContractImportToken,
487
+ dataContractImportLine
488
+ )
481
489
  };
482
490
  };
483
491
  const allExportsTmpl = async ({
@@ -562,9 +570,8 @@ const endpointPerFileTmpl = async (params) => {
562
570
  `import { ${requestInfoMeta.typeName} } from "${requestInfoMeta.typeNameImportPath}";`
563
571
  );
564
572
  }
565
- return {
566
- reservedDataContractNames: dataContractNamesInThisFile,
567
- content: await formatTSContent(`${LINTERS_IGNORE}
573
+ const dataContractImportToken = "/*__DATA_CONTRACT_IMPORTS__*/";
574
+ const contentWithImportToken = await formatTSContent(`${LINTERS_IGNORE}
568
575
  import {
569
576
  RequestParams,
570
577
  HttpResponse,
@@ -574,47 +581,55 @@ const endpointPerFileTmpl = async (params) => {
574
581
  import { ${importFileParams.httpClient.exportName} } from "${importFileParams.httpClient.path}";
575
582
  import { ${importFileParams.queryClient.exportName} } from "${importFileParams.queryClient.path}";
576
583
  ${extraImportLines.join("\n")}
577
-
578
- ${configuration.modelTypes.length > 0 ? `
579
- import { ${configuration.modelTypes.map((it) => it.name).filter(
580
- (it) => !dataContractNamesInThisFile.includes(it)
581
- )} } from "${relativePathDataContracts}";
582
- ` : ""}
584
+ ${dataContractImportToken}
583
585
 
584
586
  ${(await Promise.all(
585
- dataContractNamesInThisFile.map(async (dataContractName) => {
586
- const modelType = configuration.modelTypes.find(
587
- (modelType2) => modelType2.name === dataContractName
588
- );
589
- if (!modelType) {
590
- return "";
591
- }
592
- const contractType = await dataContractTmpl({
593
- ...params,
594
- contract: modelType,
595
- addExportKeyword: true
596
- });
597
- return contractType;
598
- })
599
- )).filter(Boolean).join("\n\n")}
587
+ dataContractNamesInThisFile.map(async (dataContractName) => {
588
+ const modelType = configuration.modelTypes.find(
589
+ (modelType2) => modelType2.name === dataContractName
590
+ );
591
+ if (!modelType) {
592
+ return "";
593
+ }
594
+ const contractType = await dataContractTmpl({
595
+ ...params,
596
+ contract: modelType,
597
+ addExportKeyword: true
598
+ });
599
+ return contractType;
600
+ })
601
+ )).filter(Boolean).join("\n\n")}
600
602
 
601
603
  ${(await Promise.all(
602
- localModelTypes.map(async (modelType) => {
603
- const contractType = await dataContractTmpl({
604
- ...params,
605
- contract: modelType,
606
- addExportKeyword: true
607
- });
608
- return contractType;
609
- })
610
- )).filter(Boolean).join("\n\n")}
604
+ localModelTypes.map(async (modelType) => {
605
+ const contractType = await dataContractTmpl({
606
+ ...params,
607
+ contract: modelType,
608
+ addExportKeyword: true
609
+ });
610
+ return contractType;
611
+ })
612
+ )).filter(Boolean).join("\n\n")}
611
613
 
612
614
  ${endpointJSDocTmpl({
613
- ...params,
614
- route
615
- })}
615
+ ...params,
616
+ route
617
+ })}
616
618
  export const ${_.camelCase(route.routeName.usage)} = ${requestInfoInstanceContent}
617
- `)
619
+ `);
620
+ const escapeRegExp = (value) => value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
621
+ const usedDataContractNames = configuration.modelTypes.map((modelType) => modelType.name).filter(
622
+ (modelTypeName) => !dataContractNamesInThisFile.includes(modelTypeName) && dataContactNames.has(modelTypeName) && new RegExp(`\\b${escapeRegExp(modelTypeName)}\\b`).test(
623
+ contentWithImportToken
624
+ )
625
+ );
626
+ const dataContractImportLine = usedDataContractNames.length > 0 ? `import { ${usedDataContractNames.join(", ")} } from "${relativePathDataContracts}";` : "";
627
+ return {
628
+ reservedDataContractNames: dataContractNamesInThisFile,
629
+ content: contentWithImportToken.replace(
630
+ dataContractImportToken,
631
+ dataContractImportLine
632
+ )
618
633
  };
619
634
  };
620
635
  const indexTsForEndpointPerFileTmpl = async ({
@@ -728,15 +743,11 @@ const removeUnusedTypesItteration = async ({
728
743
  }
729
744
  }
730
745
  let removedCount = 0;
731
- const isNeedToRemoveType = unpackFilterOption(
732
- keepTypes,
733
- (name) => name,
734
- false
735
- );
746
+ const isKeepType = unpackFilterOption(keepTypes, (name) => name, false);
736
747
  for (const [name, declarations] of candidateTypes) {
737
748
  if (usedTypes.has(name)) continue;
738
749
  for (const decl of declarations) {
739
- if ("remove" in decl && isNeedToRemoveType(name)) {
750
+ if ("remove" in decl && !isKeepType(name)) {
740
751
  decl.remove();
741
752
  removedCount++;
742
753
  }
@@ -927,6 +938,7 @@ const generateApi = async (params) => {
927
938
  });
928
939
  const utils = codegenProcess.getRenderTemplateData().utils;
929
940
  const { _ } = utils;
941
+ const outputType = params.outputType ?? "one-endpoint-per-file";
930
942
  const shouldGenerateBarrelFiles = !params.noBarrelFiles;
931
943
  let namespace = null;
932
944
  if (params.namespace) {
@@ -937,8 +949,8 @@ const generateApi = async (params) => {
937
949
  }
938
950
  }
939
951
  const codegenFs = codegenProcess.fileSystem;
940
- codegenFs.cleanDir(params.output);
941
- codegenFs.createDir(params.output);
952
+ codegenFs.cleanDir(paths.outputDir);
953
+ codegenFs.createDir(paths.outputDir);
942
954
  const filterTypes = unpackFilterOption(
943
955
  params.filterTypes,
944
956
  (modelType) => modelType.name
@@ -970,7 +982,7 @@ const generateApi = async (params) => {
970
982
  const tagsSet = /* @__PURE__ */ new Set();
971
983
  if (params.groupBy == null) {
972
984
  collectedExportFilesFromIndexFile.push("endpoints");
973
- if (params.outputType === "one-endpoint-per-file") {
985
+ if (outputType === "one-endpoint-per-file") {
974
986
  codegenFs.createDir(path.resolve(params.output, "endpoints"));
975
987
  const fileNamesWithRequestInfo = [];
976
988
  for await (const route of allRoutes) {
@@ -1050,7 +1062,7 @@ const generateApi = async (params) => {
1050
1062
  const fileName = "endpoints.ts";
1051
1063
  collectedExportFilesFromIndexFile.push("endpoints");
1052
1064
  codegenFs.createFile({
1053
- path: params.output,
1065
+ path: paths.outputDir,
1054
1066
  fileName,
1055
1067
  withPrefix: false,
1056
1068
  content: requestInfoPerFileContent
@@ -1092,7 +1104,7 @@ const generateApi = async (params) => {
1092
1104
  );
1093
1105
  codegenFs.createDir(groupDirectory);
1094
1106
  let hasFilteredRoutes = false;
1095
- if (params.outputType === "one-endpoint-per-file") {
1107
+ if (outputType === "one-endpoint-per-file") {
1096
1108
  codegenFs.createDir(path.resolve(groupDirectory, "endpoints"));
1097
1109
  for await (const route of routes) {
1098
1110
  const {
@@ -1189,7 +1201,7 @@ export * as ${exportGroupName} from './endpoints';
1189
1201
  `
1190
1202
  });
1191
1203
  }
1192
- if (shouldGenerateBarrelFiles && params.outputType === "one-endpoint-per-file") {
1204
+ if (shouldGenerateBarrelFiles && outputType === "one-endpoint-per-file") {
1193
1205
  codegenFs.createFile({
1194
1206
  path: path.resolve(groupDirectory, "endpoints"),
1195
1207
  fileName: "index.ts",
@@ -1272,8 +1284,8 @@ export * as ${namespace} from './__exports';
1272
1284
  }
1273
1285
  }
1274
1286
  if (params.removeUnusedTypes) {
1275
- removeUnusedTypes({
1276
- directory: params.output,
1287
+ await removeUnusedTypes({
1288
+ directory: paths.outputDir,
1277
1289
  keepTypes: params.removeUnusedTypes === true ? void 0 : params.removeUnusedTypes.keepTypes
1278
1290
  });
1279
1291
  }