mobx-tanstack-query-api 0.38.0 → 0.38.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/cli.js CHANGED
@@ -217,10 +217,16 @@ const createShortModelType = (shortModelType) => {
217
217
  };
218
218
  };
219
219
  const REF_PREFIX = "#/components/schemas/";
220
+ const REF_PREFIX_PARAMS = "#/components/parameters/";
220
221
  function parseRef(ref) {
221
222
  if (typeof ref !== "string" || !ref.startsWith(REF_PREFIX)) return null;
222
223
  return ref.slice(REF_PREFIX.length);
223
224
  }
225
+ function parseParamRef(ref) {
226
+ if (typeof ref !== "string" || !ref.startsWith(REF_PREFIX_PARAMS))
227
+ return null;
228
+ return ref.slice(REF_PREFIX_PARAMS.length);
229
+ }
224
230
  function getResponseSchemaKeyFromOperation(rawOperation) {
225
231
  const op = rawOperation;
226
232
  const responses = op?.responses;
@@ -262,7 +268,7 @@ function schemaToNumber(schema) {
262
268
  return n;
263
269
  }
264
270
  function schemaToArray(schema, schemas, schemaKeyToVarName2, visited) {
265
- const items = schema.items ? schemaToZodExpr(schema.items, schemas, schemaKeyToVarName2) : "z.any()";
271
+ const items = schema.items ? schemaToZodExpr(schema.items, schemas, schemaKeyToVarName2, visited) : "z.any()";
266
272
  let a = `z.array(${items})`;
267
273
  if (schema.minItems != null) a += `.min(${schema.minItems})`;
268
274
  if (schema.maxItems != null) a += `.max(${schema.maxItems})`;
@@ -276,7 +282,8 @@ function schemaToObject(schema, schemas, schemaKeyToVarName2, visited) {
276
282
  const expr = schemaToZodExpr(
277
283
  propSchema,
278
284
  schemas,
279
- schemaKeyToVarName2
285
+ schemaKeyToVarName2,
286
+ visited
280
287
  );
281
288
  const optional = !required.has(propName);
282
289
  const field = optional ? `${expr}.optional()` : expr;
@@ -293,7 +300,8 @@ ${entries.join(",\n")}
293
300
  const value = schemaToZodExpr(
294
301
  schema.additionalProperties,
295
302
  schemas,
296
- schemaKeyToVarName2
303
+ schemaKeyToVarName2,
304
+ visited
297
305
  );
298
306
  return `z.record(z.string(), ${value})`;
299
307
  }
@@ -303,13 +311,15 @@ function schemaToZodExpr(schema, schemas, schemaKeyToVarName2, visited) {
303
311
  if (!schema) return "z.any()";
304
312
  if (schema.$ref) {
305
313
  const key = parseRef(schema.$ref);
306
- if (key && key in schemas)
307
- return `z.lazy(() => ${schemaKeyToVarName2(key)})`;
314
+ if (key && key in schemas) {
315
+ const isCycle = visited.has(key);
316
+ return isCycle ? `z.lazy((): z.ZodTypeAny => ${schemaKeyToVarName2(key)})` : `z.lazy(() => ${schemaKeyToVarName2(key)})`;
317
+ }
308
318
  return "z.any()";
309
319
  }
310
320
  if (schema.allOf && schema.allOf.length > 0) {
311
321
  const parts = schema.allOf.map(
312
- (part) => schemaToZodExpr(part, schemas, schemaKeyToVarName2)
322
+ (part) => schemaToZodExpr(part, schemas, schemaKeyToVarName2, visited)
313
323
  );
314
324
  const base2 = parts.length === 1 ? parts[0] : parts.reduce((acc, p) => `z.intersection(${acc}, ${p})`);
315
325
  return schema.nullable === true ? `${base2}.nullable()` : base2;
@@ -328,10 +338,10 @@ function schemaToZodExpr(schema, schemas, schemaKeyToVarName2, visited) {
328
338
  base = "z.boolean()";
329
339
  break;
330
340
  case "array":
331
- base = schemaToArray(schema, schemas, schemaKeyToVarName2);
341
+ base = schemaToArray(schema, schemas, schemaKeyToVarName2, visited);
332
342
  break;
333
343
  case "object":
334
- base = schemaToObject(schema, schemas, schemaKeyToVarName2);
344
+ base = schemaToObject(schema, schemas, schemaKeyToVarName2, visited);
335
345
  break;
336
346
  default:
337
347
  base = "z.any()";
@@ -365,6 +375,49 @@ function schemaKeyToVarName(key, utils) {
365
375
  const _ = utils._;
366
376
  return `${_.camelCase(key)}Schema`;
367
377
  }
378
+ function resolveQueryParameters(operation, componentsParameters) {
379
+ const list = [];
380
+ const params = operation?.parameters;
381
+ if (!Array.isArray(params) || !params.length) return list;
382
+ for (const p of params) {
383
+ let param = p;
384
+ if (p.$ref && componentsParameters) {
385
+ const key = parseParamRef(p.$ref);
386
+ if (key && key in componentsParameters)
387
+ param = componentsParameters[key];
388
+ }
389
+ if (param.in !== "query") continue;
390
+ const name = param.name;
391
+ if (!name) continue;
392
+ const schema = param.schema ?? {
393
+ type: param.type ?? "string",
394
+ format: param.format,
395
+ items: param.items
396
+ };
397
+ list.push({
398
+ name,
399
+ required: param.required === true,
400
+ schema
401
+ });
402
+ }
403
+ return list;
404
+ }
405
+ function queryParamsToZodObject(queryParams, schemas, schemaKeyToVarName2) {
406
+ if (queryParams.length === 0) return "z.object({})";
407
+ const entries = queryParams.map(({ name, required, schema }) => {
408
+ const expr = schemaToZodExpr(
409
+ schema,
410
+ schemas,
411
+ schemaKeyToVarName2,
412
+ /* @__PURE__ */ new Set()
413
+ );
414
+ const field = required ? expr : `${expr}.optional()`;
415
+ return ` ${JSON.stringify(name)}: ${field}`;
416
+ });
417
+ return `z.object({
418
+ ${entries.join(",\n")}
419
+ })`;
420
+ }
368
421
  function generateAuxiliarySchemas(schemaKeys, schemas, schemaKeyToVarNameFn, visited) {
369
422
  const lines = [];
370
423
  for (const key of schemaKeys) {
@@ -373,10 +426,12 @@ function generateAuxiliarySchemas(schemaKeys, schemas, schemaKeyToVarNameFn, vis
373
426
  const schema = schemas[key];
374
427
  if (!schema) continue;
375
428
  const varName = schemaKeyToVarNameFn(key);
429
+ const cyclePath = /* @__PURE__ */ new Set([key]);
376
430
  const expr = schemaToZodExpr(
377
431
  schema,
378
432
  schemas,
379
- schemaKeyToVarNameFn
433
+ schemaKeyToVarNameFn,
434
+ cyclePath
380
435
  );
381
436
  lines.push(`export const ${varName} = ${expr};`);
382
437
  }
@@ -427,7 +482,8 @@ function typeToZodSchemaWithSchema(typeStr, schemas, utils, typeSuffix) {
427
482
  const expr = schemaToZodExpr(
428
483
  schema,
429
484
  schemas,
430
- schemaKeyToVarNameFn
485
+ schemaKeyToVarNameFn,
486
+ /* @__PURE__ */ new Set()
431
487
  );
432
488
  return { expr, refs: [...refs] };
433
489
  }
@@ -440,7 +496,8 @@ function schemaKeyToZod(schemaKey, schemas, utils) {
440
496
  const expr = schemaToZodExpr(
441
497
  schema,
442
498
  schemas,
443
- schemaKeyToVarNameFn
499
+ schemaKeyToVarNameFn,
500
+ /* @__PURE__ */ new Set()
444
501
  );
445
502
  return { expr, refs: [...refs] };
446
503
  }
@@ -454,20 +511,37 @@ function buildEndpointZodContractsCode(params) {
454
511
  componentsSchemas = null,
455
512
  typeSuffix = "DC",
456
513
  responseSchemaKey,
457
- useExternalZodSchemas = false
514
+ useExternalZodSchemas = false,
515
+ openApiOperation = null,
516
+ openApiComponentsParameters = null,
517
+ queryParamName = "query"
458
518
  } = params;
459
519
  const _ = utils._;
460
520
  const paramsSchemaName = `${_.camelCase(routeNameUsage)}ParamsSchema`;
461
521
  const dataSchemaName = `${_.camelCase(routeNameUsage)}DataSchema`;
462
522
  const allAuxiliaryKeys = /* @__PURE__ */ new Set();
463
523
  const paramParts = [];
524
+ const resolvedQueryParams = openApiOperation && (openApiComponentsParameters || openApiOperation.parameters?.length) ? resolveQueryParameters(openApiOperation, openApiComponentsParameters) : [];
525
+ const schemaKeyToVarNameFn = (key) => schemaKeyToVarName(key, utils);
464
526
  for (const p of inputParams) {
465
- const { expr, refs: refKeys } = typeToZodSchemaWithSchema(
466
- p.type,
467
- componentsSchemas,
468
- utils,
469
- typeSuffix
470
- );
527
+ let expr;
528
+ let refKeys = [];
529
+ if (p.name === queryParamName && resolvedQueryParams.length > 0 && componentsSchemas) {
530
+ expr = queryParamsToZodObject(
531
+ resolvedQueryParams,
532
+ componentsSchemas,
533
+ schemaKeyToVarNameFn
534
+ );
535
+ } else {
536
+ const result = typeToZodSchemaWithSchema(
537
+ p.type,
538
+ componentsSchemas,
539
+ utils,
540
+ typeSuffix
541
+ );
542
+ expr = result.expr;
543
+ refKeys = result.refs;
544
+ }
471
545
  for (const k of refKeys) allAuxiliaryKeys.add(k);
472
546
  const schemaWithOptional = p.optional ? `${expr}.optional()` : expr;
473
547
  paramParts.push(`${p.name}: ${schemaWithOptional}`);
@@ -478,7 +552,6 @@ function buildEndpointZodContractsCode(params) {
478
552
  utils,
479
553
  typeSuffix
480
554
  );
481
- const schemaKeyToVarNameFn = (key) => schemaKeyToVarName(key, utils);
482
555
  const useDataSchemaFromCentral = useExternalZodSchemas && responseSchemaKey && componentsSchemas && responseSchemaKey in componentsSchemas;
483
556
  if (useDataSchemaFromCentral) {
484
557
  allAuxiliaryKeys.add(responseSchemaKey);
@@ -832,7 +905,10 @@ const newEndpointTmpl = ({
832
905
  componentsSchemas: componentsSchemas ?? void 0,
833
906
  typeSuffix: "DC",
834
907
  responseSchemaKey: responseSchemaKey ?? void 0,
835
- useExternalZodSchemas: Boolean(relativePathZodSchemas)
908
+ useExternalZodSchemas: Boolean(relativePathZodSchemas),
909
+ openApiOperation: operationFromSpec ?? void 0,
910
+ openApiComponentsParameters: swaggerSchema?.components?.parameters ?? void 0,
911
+ queryParamName: queryName
836
912
  }) : null;
837
913
  const contractsLine = contractsVarName != null ? `contracts: ${contractsVarName},` : "";
838
914
  const validateContractsLine = (() => {