mobx-tanstack-query-api 0.38.0 → 0.38.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.cjs +95 -20
- package/cli.cjs.map +1 -1
- package/cli.js +95 -20
- package/cli.js.map +1 -1
- package/package.json +1 -1
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
|
-
|
|
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) {
|
|
@@ -376,7 +429,8 @@ function generateAuxiliarySchemas(schemaKeys, schemas, schemaKeyToVarNameFn, vis
|
|
|
376
429
|
const expr = schemaToZodExpr(
|
|
377
430
|
schema,
|
|
378
431
|
schemas,
|
|
379
|
-
schemaKeyToVarNameFn
|
|
432
|
+
schemaKeyToVarNameFn,
|
|
433
|
+
visited
|
|
380
434
|
);
|
|
381
435
|
lines.push(`export const ${varName} = ${expr};`);
|
|
382
436
|
}
|
|
@@ -427,7 +481,8 @@ function typeToZodSchemaWithSchema(typeStr, schemas, utils, typeSuffix) {
|
|
|
427
481
|
const expr = schemaToZodExpr(
|
|
428
482
|
schema,
|
|
429
483
|
schemas,
|
|
430
|
-
schemaKeyToVarNameFn
|
|
484
|
+
schemaKeyToVarNameFn,
|
|
485
|
+
/* @__PURE__ */ new Set()
|
|
431
486
|
);
|
|
432
487
|
return { expr, refs: [...refs] };
|
|
433
488
|
}
|
|
@@ -440,7 +495,8 @@ function schemaKeyToZod(schemaKey, schemas, utils) {
|
|
|
440
495
|
const expr = schemaToZodExpr(
|
|
441
496
|
schema,
|
|
442
497
|
schemas,
|
|
443
|
-
schemaKeyToVarNameFn
|
|
498
|
+
schemaKeyToVarNameFn,
|
|
499
|
+
/* @__PURE__ */ new Set()
|
|
444
500
|
);
|
|
445
501
|
return { expr, refs: [...refs] };
|
|
446
502
|
}
|
|
@@ -454,20 +510,37 @@ function buildEndpointZodContractsCode(params) {
|
|
|
454
510
|
componentsSchemas = null,
|
|
455
511
|
typeSuffix = "DC",
|
|
456
512
|
responseSchemaKey,
|
|
457
|
-
useExternalZodSchemas = false
|
|
513
|
+
useExternalZodSchemas = false,
|
|
514
|
+
openApiOperation = null,
|
|
515
|
+
openApiComponentsParameters = null,
|
|
516
|
+
queryParamName = "query"
|
|
458
517
|
} = params;
|
|
459
518
|
const _ = utils._;
|
|
460
519
|
const paramsSchemaName = `${_.camelCase(routeNameUsage)}ParamsSchema`;
|
|
461
520
|
const dataSchemaName = `${_.camelCase(routeNameUsage)}DataSchema`;
|
|
462
521
|
const allAuxiliaryKeys = /* @__PURE__ */ new Set();
|
|
463
522
|
const paramParts = [];
|
|
523
|
+
const resolvedQueryParams = openApiOperation && (openApiComponentsParameters || openApiOperation.parameters?.length) ? resolveQueryParameters(openApiOperation, openApiComponentsParameters) : [];
|
|
524
|
+
const schemaKeyToVarNameFn = (key) => schemaKeyToVarName(key, utils);
|
|
464
525
|
for (const p of inputParams) {
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
526
|
+
let expr;
|
|
527
|
+
let refKeys = [];
|
|
528
|
+
if (p.name === queryParamName && resolvedQueryParams.length > 0 && componentsSchemas) {
|
|
529
|
+
expr = queryParamsToZodObject(
|
|
530
|
+
resolvedQueryParams,
|
|
531
|
+
componentsSchemas,
|
|
532
|
+
schemaKeyToVarNameFn
|
|
533
|
+
);
|
|
534
|
+
} else {
|
|
535
|
+
const result = typeToZodSchemaWithSchema(
|
|
536
|
+
p.type,
|
|
537
|
+
componentsSchemas,
|
|
538
|
+
utils,
|
|
539
|
+
typeSuffix
|
|
540
|
+
);
|
|
541
|
+
expr = result.expr;
|
|
542
|
+
refKeys = result.refs;
|
|
543
|
+
}
|
|
471
544
|
for (const k of refKeys) allAuxiliaryKeys.add(k);
|
|
472
545
|
const schemaWithOptional = p.optional ? `${expr}.optional()` : expr;
|
|
473
546
|
paramParts.push(`${p.name}: ${schemaWithOptional}`);
|
|
@@ -478,7 +551,6 @@ function buildEndpointZodContractsCode(params) {
|
|
|
478
551
|
utils,
|
|
479
552
|
typeSuffix
|
|
480
553
|
);
|
|
481
|
-
const schemaKeyToVarNameFn = (key) => schemaKeyToVarName(key, utils);
|
|
482
554
|
const useDataSchemaFromCentral = useExternalZodSchemas && responseSchemaKey && componentsSchemas && responseSchemaKey in componentsSchemas;
|
|
483
555
|
if (useDataSchemaFromCentral) {
|
|
484
556
|
allAuxiliaryKeys.add(responseSchemaKey);
|
|
@@ -832,7 +904,10 @@ const newEndpointTmpl = ({
|
|
|
832
904
|
componentsSchemas: componentsSchemas ?? void 0,
|
|
833
905
|
typeSuffix: "DC",
|
|
834
906
|
responseSchemaKey: responseSchemaKey ?? void 0,
|
|
835
|
-
useExternalZodSchemas: Boolean(relativePathZodSchemas)
|
|
907
|
+
useExternalZodSchemas: Boolean(relativePathZodSchemas),
|
|
908
|
+
openApiOperation: operationFromSpec ?? void 0,
|
|
909
|
+
openApiComponentsParameters: swaggerSchema?.components?.parameters ?? void 0,
|
|
910
|
+
queryParamName: queryName
|
|
836
911
|
}) : null;
|
|
837
912
|
const contractsLine = contractsVarName != null ? `contracts: ${contractsVarName},` : "";
|
|
838
913
|
const validateContractsLine = (() => {
|