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.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) {
|
|
@@ -379,7 +432,8 @@ function generateAuxiliarySchemas(schemaKeys, schemas, schemaKeyToVarNameFn, vis
|
|
|
379
432
|
const expr = schemaToZodExpr(
|
|
380
433
|
schema,
|
|
381
434
|
schemas,
|
|
382
|
-
schemaKeyToVarNameFn
|
|
435
|
+
schemaKeyToVarNameFn,
|
|
436
|
+
visited
|
|
383
437
|
);
|
|
384
438
|
lines.push(`export const ${varName} = ${expr};`);
|
|
385
439
|
}
|
|
@@ -430,7 +484,8 @@ function typeToZodSchemaWithSchema(typeStr, schemas, utils, typeSuffix) {
|
|
|
430
484
|
const expr = schemaToZodExpr(
|
|
431
485
|
schema,
|
|
432
486
|
schemas,
|
|
433
|
-
schemaKeyToVarNameFn
|
|
487
|
+
schemaKeyToVarNameFn,
|
|
488
|
+
/* @__PURE__ */ new Set()
|
|
434
489
|
);
|
|
435
490
|
return { expr, refs: [...refs] };
|
|
436
491
|
}
|
|
@@ -443,7 +498,8 @@ function schemaKeyToZod(schemaKey, schemas, utils) {
|
|
|
443
498
|
const expr = schemaToZodExpr(
|
|
444
499
|
schema,
|
|
445
500
|
schemas,
|
|
446
|
-
schemaKeyToVarNameFn
|
|
501
|
+
schemaKeyToVarNameFn,
|
|
502
|
+
/* @__PURE__ */ new Set()
|
|
447
503
|
);
|
|
448
504
|
return { expr, refs: [...refs] };
|
|
449
505
|
}
|
|
@@ -457,20 +513,37 @@ function buildEndpointZodContractsCode(params) {
|
|
|
457
513
|
componentsSchemas = null,
|
|
458
514
|
typeSuffix = "DC",
|
|
459
515
|
responseSchemaKey,
|
|
460
|
-
useExternalZodSchemas = false
|
|
516
|
+
useExternalZodSchemas = false,
|
|
517
|
+
openApiOperation = null,
|
|
518
|
+
openApiComponentsParameters = null,
|
|
519
|
+
queryParamName = "query"
|
|
461
520
|
} = params;
|
|
462
521
|
const _ = utils._;
|
|
463
522
|
const paramsSchemaName = `${_.camelCase(routeNameUsage)}ParamsSchema`;
|
|
464
523
|
const dataSchemaName = `${_.camelCase(routeNameUsage)}DataSchema`;
|
|
465
524
|
const allAuxiliaryKeys = /* @__PURE__ */ new Set();
|
|
466
525
|
const paramParts = [];
|
|
526
|
+
const resolvedQueryParams = openApiOperation && (openApiComponentsParameters || openApiOperation.parameters?.length) ? resolveQueryParameters(openApiOperation, openApiComponentsParameters) : [];
|
|
527
|
+
const schemaKeyToVarNameFn = (key) => schemaKeyToVarName(key, utils);
|
|
467
528
|
for (const p of inputParams) {
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
529
|
+
let expr;
|
|
530
|
+
let refKeys = [];
|
|
531
|
+
if (p.name === queryParamName && resolvedQueryParams.length > 0 && componentsSchemas) {
|
|
532
|
+
expr = queryParamsToZodObject(
|
|
533
|
+
resolvedQueryParams,
|
|
534
|
+
componentsSchemas,
|
|
535
|
+
schemaKeyToVarNameFn
|
|
536
|
+
);
|
|
537
|
+
} else {
|
|
538
|
+
const result = typeToZodSchemaWithSchema(
|
|
539
|
+
p.type,
|
|
540
|
+
componentsSchemas,
|
|
541
|
+
utils,
|
|
542
|
+
typeSuffix
|
|
543
|
+
);
|
|
544
|
+
expr = result.expr;
|
|
545
|
+
refKeys = result.refs;
|
|
546
|
+
}
|
|
474
547
|
for (const k of refKeys) allAuxiliaryKeys.add(k);
|
|
475
548
|
const schemaWithOptional = p.optional ? `${expr}.optional()` : expr;
|
|
476
549
|
paramParts.push(`${p.name}: ${schemaWithOptional}`);
|
|
@@ -481,7 +554,6 @@ function buildEndpointZodContractsCode(params) {
|
|
|
481
554
|
utils,
|
|
482
555
|
typeSuffix
|
|
483
556
|
);
|
|
484
|
-
const schemaKeyToVarNameFn = (key) => schemaKeyToVarName(key, utils);
|
|
485
557
|
const useDataSchemaFromCentral = useExternalZodSchemas && responseSchemaKey && componentsSchemas && responseSchemaKey in componentsSchemas;
|
|
486
558
|
if (useDataSchemaFromCentral) {
|
|
487
559
|
allAuxiliaryKeys.add(responseSchemaKey);
|
|
@@ -835,7 +907,10 @@ const newEndpointTmpl = ({
|
|
|
835
907
|
componentsSchemas: componentsSchemas ?? void 0,
|
|
836
908
|
typeSuffix: "DC",
|
|
837
909
|
responseSchemaKey: responseSchemaKey ?? void 0,
|
|
838
|
-
useExternalZodSchemas: Boolean(relativePathZodSchemas)
|
|
910
|
+
useExternalZodSchemas: Boolean(relativePathZodSchemas),
|
|
911
|
+
openApiOperation: operationFromSpec ?? void 0,
|
|
912
|
+
openApiComponentsParameters: swaggerSchema?.components?.parameters ?? void 0,
|
|
913
|
+
queryParamName: queryName
|
|
839
914
|
}) : null;
|
|
840
915
|
const contractsLine = contractsVarName != null ? `contracts: ${contractsVarName},` : "";
|
|
841
916
|
const validateContractsLine = (() => {
|