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