mobx-tanstack-query-api 0.38.2 → 0.39.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.d.ts CHANGED
@@ -149,7 +149,7 @@ interface GenerateQueryApiParams {
149
149
  * [**Documentation**](https://js2me.github.io/mobx-tanstack-query-api/codegen/config/#fetchschemarequestoptions)
150
150
  */
151
151
  fetchSchemaRequestOptions?: RequestInit;
152
- otherCodegenParams?: AnyObject;
152
+ otherCodegenParams?: Partial<GenerateApiConfiguration['config']>;
153
153
  /**
154
154
  * [**Documentation**](https://js2me.github.io/mobx-tanstack-query-api/codegen/config/#filterendpoints)
155
155
  */
@@ -199,6 +199,10 @@ interface GenerateQueryApiParams {
199
199
  * - `{ throw: boolean }`: set `throwContracts` to that boolean.
200
200
  * - `{ throw: string }`: set `throwContracts` to the expression (inserted as-is).
201
201
  * - `{ throw: { params?: boolean | string; data?: boolean | string } }`: set `throwContracts` to an object; each value is literal or expression (string inserted as-is).
202
+ *
203
+ * Optional `appendRule`: either a string (runtime) or a function (codegen-time).
204
+ * - **string**: expression inserted as-is → `contracts: <expr> ? <contractsVar> : undefined`. E.g. `"process.env.NODE_ENV === \"development\""`.
205
+ * - **function** (contractName, routeInfo) => boolean: at codegen time, if true → `contracts: <contractsVar>`, if false → `contracts: undefined`.
202
206
  */
203
207
  zodContracts?: boolean | {
204
208
  validate: boolean | string | {
@@ -209,8 +213,17 @@ interface GenerateQueryApiParams {
209
213
  params?: boolean | string;
210
214
  data?: boolean | string;
211
215
  };
216
+ /** String: runtime condition. Function: codegen-time filter for (contractName, routeInfo). */
217
+ appendRule?: string | ((contractName: string, routeInfo: ZodContractsRouteInfo) => boolean);
212
218
  };
213
219
  }
220
+ /** Route info passed to zodContracts.appendRule at codegen time. */
221
+ interface ZodContractsRouteInfo {
222
+ operationId: string;
223
+ path: string;
224
+ method: string;
225
+ contractName: string;
226
+ }
214
227
 
215
228
  type AllImportFileParams = Record<KeyOfByValue<Required<GenerateQueryApiParams>, 'builtin' | ImportFileParams>, ImportFileParams>;
216
229
 
@@ -235,4 +248,4 @@ declare const generateApi: (params: GenerateQueryApiParams | GenerateQueryApiPar
235
248
  declare const defineConfig: (...configs: Maybe<GenerateQueryApiParams | GenerateQueryApiParams[]>[]) => GenerateQueryApiParams[];
236
249
 
237
250
  export { defineConfig, generateApi };
238
- export type { AllImportFileParams, BaseTmplParams, CodegenDataUtils, EndpointData, GenerateQueryApiParams, ImportFileParams, MetaInfo };
251
+ export type { AllImportFileParams, BaseTmplParams, CodegenDataUtils, EndpointData, GenerateQueryApiParams, ImportFileParams, MetaInfo, ZodContractsRouteInfo };
package/cli.js CHANGED
@@ -910,7 +910,26 @@ const newEndpointTmpl = ({
910
910
  openApiComponentsParameters: swaggerSchema?.components?.parameters ?? void 0,
911
911
  queryParamName: queryName
912
912
  }) : null;
913
- const contractsLine = contractsVarName != null ? `contracts: ${contractsVarName},` : "";
913
+ const appendRuleOpt = zodContractsIsObject && zodContracts.appendRule != null ? zodContracts.appendRule : null;
914
+ const routeInfoForAppend = contractsVarName != null ? {
915
+ operationId: raw.operationId ?? "",
916
+ path: path2,
917
+ method,
918
+ contractName: contractsVarName
919
+ } : null;
920
+ const contractsLine = (() => {
921
+ if (contractsVarName == null) return "";
922
+ if (typeof appendRuleOpt === "string")
923
+ return `contracts: ${appendRuleOpt} ? ${contractsVarName} : undefined,`;
924
+ if (typeof appendRuleOpt === "function" && routeInfoForAppend) {
925
+ const include = appendRuleOpt(
926
+ routeInfoForAppend.contractName,
927
+ routeInfoForAppend
928
+ );
929
+ return include ? `contracts: ${contractsVarName},` : "contracts: undefined,";
930
+ }
931
+ return `contracts: ${contractsVarName},`;
932
+ })();
914
933
  const validateContractsLine = (() => {
915
934
  if (validateOpt === void 0) return "";
916
935
  if (typeof validateOpt === "string")
@@ -1488,12 +1507,25 @@ const generateApi = async (params) => {
1488
1507
  sortTypes: true,
1489
1508
  templates: paths.templates.toString(),
1490
1509
  primitiveTypeConstructs: (constructs) => {
1491
- return {
1510
+ const result = {
1492
1511
  ...constructs,
1493
1512
  object: () => `Record<string, any>`,
1494
- float: () => `number`,
1495
- ...params.otherCodegenParams?.primitiveTypeConstructs?.(constructs)
1513
+ float: () => `number`
1496
1514
  };
1515
+ if (params.otherCodegenParams?.primitiveTypeConstructs) {
1516
+ if (typeof params.otherCodegenParams?.primitiveTypeConstructs === "function") {
1517
+ Object.assign(
1518
+ result,
1519
+ params.otherCodegenParams.primitiveTypeConstructs(result)
1520
+ );
1521
+ } else {
1522
+ Object.assign(
1523
+ result,
1524
+ params.otherCodegenParams.primitiveTypeConstructs
1525
+ );
1526
+ }
1527
+ }
1528
+ return result;
1497
1529
  },
1498
1530
  requestOptions: params.fetchSchemaRequestOptions,
1499
1531
  ...params.otherCodegenParams
@@ -1507,6 +1539,34 @@ const generateApi = async (params) => {
1507
1539
  );
1508
1540
  return;
1509
1541
  }
1542
+ const prepareConfig = (config) => {
1543
+ config.routes.combined?.forEach((routeInfo) => {
1544
+ routeInfo.routes.sort(
1545
+ (routeA, routeB) => routeA.routeName.usage.localeCompare(routeB.routeName.usage)
1546
+ );
1547
+ });
1548
+ return params.otherCodegenParams?.hooks?.onPrepareConfig?.(config);
1549
+ };
1550
+ const formatRouteName = (routeInfo, usageRouteName) => {
1551
+ let formattedRouteName = usageRouteName;
1552
+ if (params.addPathSegmentToRouteName === true || typeof params.addPathSegmentToRouteName === "number") {
1553
+ const pathSegmentForSuffix = typeof params.addPathSegmentToRouteName === "number" ? params.addPathSegmentToRouteName : 0;
1554
+ const pathSegments = routeInfo.route.split("/").filter(Boolean);
1555
+ const { _: _2 } = codegenProcess.getRenderTemplateData().utils;
1556
+ formattedRouteName = _2.camelCase(
1557
+ `${pathSegments[pathSegmentForSuffix] || ""}_${formattedRouteName}`
1558
+ );
1559
+ }
1560
+ const endpointName = formattedRouteName;
1561
+ const resultRouteName = params?.formatEndpointName?.(endpointName, routeInfo) ?? swaggerTypescriptApiCodegenBaseParams?.hooks?.onFormatRouteName?.(
1562
+ routeInfo,
1563
+ endpointName
1564
+ ) ?? endpointName;
1565
+ return params.otherCodegenParams?.hooks?.onFormatRouteName?.(
1566
+ routeInfo,
1567
+ resultRouteName
1568
+ ) ?? resultRouteName;
1569
+ };
1510
1570
  const inputToCodegenInput = (input) => {
1511
1571
  const inputData = {};
1512
1572
  if (typeof input === "string") {
@@ -1523,32 +1583,11 @@ const generateApi = async (params) => {
1523
1583
  ...swaggerTypescriptApiCodegenBaseParams,
1524
1584
  ...inputToCodegenInput(params.mixinInput),
1525
1585
  hooks: {
1526
- onInit: (configuration) => {
1586
+ onInit: (configuration, codegenProcess2) => {
1527
1587
  mixinSwaggerSchema = cloneDeep(configuration.swaggerSchema);
1528
1588
  },
1529
- onPrepareConfig: (config) => {
1530
- config.routes.combined?.forEach((routeInfo) => {
1531
- routeInfo.routes.sort(
1532
- (routeA, routeB) => routeA.routeName.usage.localeCompare(routeB.routeName.usage)
1533
- );
1534
- });
1535
- },
1536
- onFormatRouteName: (routeInfo, usageRouteName) => {
1537
- let formattedRouteName = usageRouteName;
1538
- if (params.addPathSegmentToRouteName === true || typeof params.addPathSegmentToRouteName === "number") {
1539
- const pathSegmentForSuffix = typeof params.addPathSegmentToRouteName === "number" ? params.addPathSegmentToRouteName : 0;
1540
- const pathSegments = routeInfo.route.split("/").filter(Boolean);
1541
- const { _: _2 } = codegenProcess.getRenderTemplateData().utils;
1542
- formattedRouteName = _2.camelCase(
1543
- `${pathSegments[pathSegmentForSuffix] || ""}_${formattedRouteName}`
1544
- );
1545
- }
1546
- const endpointName = formattedRouteName;
1547
- return params?.formatEndpointName?.(endpointName, routeInfo) ?? swaggerTypescriptApiCodegenBaseParams?.hooks?.onFormatRouteName?.(
1548
- routeInfo,
1549
- endpointName
1550
- ) ?? endpointName;
1551
- }
1589
+ onPrepareConfig: prepareConfig,
1590
+ onFormatRouteName: formatRouteName
1552
1591
  }
1553
1592
  });
1554
1593
  }
@@ -1574,32 +1613,8 @@ const generateApi = async (params) => {
1574
1613
  codeGenProcessFromInit
1575
1614
  );
1576
1615
  },
1577
- onPrepareConfig: (config) => {
1578
- config.routes.combined?.forEach((routeInfo) => {
1579
- routeInfo.routes.sort(
1580
- (routeA, routeB) => routeA.routeName.usage.localeCompare(routeB.routeName.usage)
1581
- );
1582
- });
1583
- return swaggerTypescriptApiCodegenBaseParams?.hooks?.onPrepareConfig?.(
1584
- config
1585
- );
1586
- },
1587
- onFormatRouteName: (routeInfo, usageRouteName) => {
1588
- let formattedRouteName = usageRouteName;
1589
- if (params.addPathSegmentToRouteName === true || typeof params.addPathSegmentToRouteName === "number") {
1590
- const pathSegmentForSuffix = typeof params.addPathSegmentToRouteName === "number" ? params.addPathSegmentToRouteName : 0;
1591
- const pathSegments = routeInfo.route.split("/").filter(Boolean);
1592
- const { _: _2 } = codegenProcess.getRenderTemplateData().utils;
1593
- formattedRouteName = _2.camelCase(
1594
- `${pathSegments[pathSegmentForSuffix] || ""}_${formattedRouteName}`
1595
- );
1596
- }
1597
- const endpointName = formattedRouteName;
1598
- return params?.formatEndpointName?.(endpointName, routeInfo) ?? swaggerTypescriptApiCodegenBaseParams?.hooks?.onFormatRouteName?.(
1599
- routeInfo,
1600
- endpointName
1601
- ) ?? endpointName;
1602
- }
1616
+ onPrepareConfig: prepareConfig,
1617
+ onFormatRouteName: formatRouteName
1603
1618
  }
1604
1619
  });
1605
1620
  const utils = codegenProcess.getRenderTemplateData().utils;