mobx-tanstack-query-api 0.40.1 → 0.41.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 +144 -109
- package/cli.cjs.map +1 -1
- package/cli.d.ts +34 -18
- package/cli.js +144 -109
- package/cli.js.map +1 -1
- package/index.cjs +6 -6
- package/index.cjs.map +1 -1
- package/index.d.ts +4 -4
- package/index.js +6 -6
- package/index.js.map +1 -1
- package/package.json +1 -1
package/cli.cjs
CHANGED
|
@@ -219,6 +219,15 @@ const createShortModelType = (shortModelType) => {
|
|
|
219
219
|
description: shortModelType.description || ""
|
|
220
220
|
};
|
|
221
221
|
};
|
|
222
|
+
const DEFAULT_DATA_CONTRACT_TYPE_SUFFIX = "DC";
|
|
223
|
+
const DEFAULT_ZOD_CONTRACT_SUFFIX = "";
|
|
224
|
+
const DEFAULT_ENDPOINT_ZOD_CONTRACT_SUFFIX = "Contract";
|
|
225
|
+
function getZodContractSuffix(zodContracts) {
|
|
226
|
+
return (typeof zodContracts === "object" && zodContracts !== null && typeof zodContracts.suffix === "string" ? zodContracts.suffix : void 0) ?? DEFAULT_ZOD_CONTRACT_SUFFIX;
|
|
227
|
+
}
|
|
228
|
+
function getEndpointZodContractSuffix(zodContracts) {
|
|
229
|
+
return (typeof zodContracts === "object" && zodContracts !== null && typeof zodContracts.suffix === "string" ? zodContracts.suffix : void 0) ?? DEFAULT_ENDPOINT_ZOD_CONTRACT_SUFFIX;
|
|
230
|
+
}
|
|
222
231
|
const REF_PREFIX = "#/components/schemas/";
|
|
223
232
|
const REF_PREFIX_PARAMS = "#/components/parameters/";
|
|
224
233
|
function parseRef(ref) {
|
|
@@ -247,7 +256,7 @@ function getResponseSchemaKeyFromOperation(rawOperation) {
|
|
|
247
256
|
if (typeof ref !== "string") return null;
|
|
248
257
|
return parseRef(ref);
|
|
249
258
|
}
|
|
250
|
-
function typeNameToSchemaKey(typeName, typeSuffix =
|
|
259
|
+
function typeNameToSchemaKey(typeName, typeSuffix = DEFAULT_DATA_CONTRACT_TYPE_SUFFIX) {
|
|
251
260
|
const t = typeName.trim();
|
|
252
261
|
if (typeSuffix && t.endsWith(typeSuffix))
|
|
253
262
|
return t.slice(0, -typeSuffix.length);
|
|
@@ -270,14 +279,14 @@ function schemaToNumber(schema) {
|
|
|
270
279
|
if (schema.maximum != null) n += `.max(${schema.maximum})`;
|
|
271
280
|
return n;
|
|
272
281
|
}
|
|
273
|
-
function schemaToArray(schema, schemas,
|
|
274
|
-
const items = schema.items ? schemaToZodExpr(schema.items, schemas,
|
|
282
|
+
function schemaToArray(schema, schemas, schemaKeyToVarName, visited) {
|
|
283
|
+
const items = schema.items ? schemaToZodExpr(schema.items, schemas, schemaKeyToVarName, visited) : "z.any()";
|
|
275
284
|
let a = `z.array(${items})`;
|
|
276
285
|
if (schema.minItems != null) a += `.min(${schema.minItems})`;
|
|
277
286
|
if (schema.maxItems != null) a += `.max(${schema.maxItems})`;
|
|
278
287
|
return a;
|
|
279
288
|
}
|
|
280
|
-
function schemaToObject(schema, schemas,
|
|
289
|
+
function schemaToObject(schema, schemas, schemaKeyToVarName, visited) {
|
|
281
290
|
if (schema.properties && Object.keys(schema.properties).length > 0) {
|
|
282
291
|
const required = new Set(schema.required ?? []);
|
|
283
292
|
const entries = Object.entries(schema.properties).map(
|
|
@@ -285,7 +294,7 @@ function schemaToObject(schema, schemas, schemaKeyToVarName2, visited) {
|
|
|
285
294
|
const expr = schemaToZodExpr(
|
|
286
295
|
propSchema,
|
|
287
296
|
schemas,
|
|
288
|
-
|
|
297
|
+
schemaKeyToVarName,
|
|
289
298
|
visited
|
|
290
299
|
);
|
|
291
300
|
const optional = !required.has(propName);
|
|
@@ -303,26 +312,26 @@ ${entries.join(",\n")}
|
|
|
303
312
|
const value = schemaToZodExpr(
|
|
304
313
|
schema.additionalProperties,
|
|
305
314
|
schemas,
|
|
306
|
-
|
|
315
|
+
schemaKeyToVarName,
|
|
307
316
|
visited
|
|
308
317
|
);
|
|
309
318
|
return `z.record(z.string(), ${value})`;
|
|
310
319
|
}
|
|
311
320
|
return "z.record(z.string(), z.any())";
|
|
312
321
|
}
|
|
313
|
-
function schemaToZodExpr(schema, schemas,
|
|
322
|
+
function schemaToZodExpr(schema, schemas, schemaKeyToVarName, visited) {
|
|
314
323
|
if (!schema) return "z.any()";
|
|
315
324
|
if (schema.$ref) {
|
|
316
325
|
const key = parseRef(schema.$ref);
|
|
317
326
|
if (key && key in schemas) {
|
|
318
327
|
const isCycle = visited.has(key);
|
|
319
|
-
return isCycle ? `z.lazy((): z.ZodTypeAny => ${
|
|
328
|
+
return isCycle ? `z.lazy((): z.ZodTypeAny => ${schemaKeyToVarName(key)})` : `z.lazy(() => ${schemaKeyToVarName(key)})`;
|
|
320
329
|
}
|
|
321
330
|
return "z.any()";
|
|
322
331
|
}
|
|
323
332
|
if (schema.allOf && schema.allOf.length > 0) {
|
|
324
333
|
const parts = schema.allOf.map(
|
|
325
|
-
(part) => schemaToZodExpr(part, schemas,
|
|
334
|
+
(part) => schemaToZodExpr(part, schemas, schemaKeyToVarName, visited)
|
|
326
335
|
);
|
|
327
336
|
const base2 = parts.length === 1 ? parts[0] : parts.reduce((acc, p) => `z.intersection(${acc}, ${p})`);
|
|
328
337
|
return schema.nullable === true ? `${base2}.nullable()` : base2;
|
|
@@ -341,10 +350,10 @@ function schemaToZodExpr(schema, schemas, schemaKeyToVarName2, visited) {
|
|
|
341
350
|
base = "z.boolean()";
|
|
342
351
|
break;
|
|
343
352
|
case "array":
|
|
344
|
-
base = schemaToArray(schema, schemas,
|
|
353
|
+
base = schemaToArray(schema, schemas, schemaKeyToVarName, visited);
|
|
345
354
|
break;
|
|
346
355
|
case "object":
|
|
347
|
-
base = schemaToObject(schema, schemas,
|
|
356
|
+
base = schemaToObject(schema, schemas, schemaKeyToVarName, visited);
|
|
348
357
|
break;
|
|
349
358
|
default:
|
|
350
359
|
base = "z.any()";
|
|
@@ -374,9 +383,16 @@ function collectRefs(schema, schemas, out) {
|
|
|
374
383
|
collectRefs(schema.additionalProperties, schemas, out);
|
|
375
384
|
}
|
|
376
385
|
}
|
|
377
|
-
function
|
|
386
|
+
function dataContractTypeNameToZodVarName(typeName, utils, contractSuffix = DEFAULT_ZOD_CONTRACT_SUFFIX) {
|
|
378
387
|
const _ = utils._;
|
|
379
|
-
return `${_.camelCase(
|
|
388
|
+
return `${_.camelCase(typeName)}${contractSuffix}`;
|
|
389
|
+
}
|
|
390
|
+
function schemaKeyToContractVarName(key, utils, contractSuffix = DEFAULT_ZOD_CONTRACT_SUFFIX, typeSuffix = DEFAULT_DATA_CONTRACT_TYPE_SUFFIX) {
|
|
391
|
+
return dataContractTypeNameToZodVarName(
|
|
392
|
+
`${key}${typeSuffix}`,
|
|
393
|
+
utils,
|
|
394
|
+
contractSuffix
|
|
395
|
+
);
|
|
380
396
|
}
|
|
381
397
|
function resolveQueryParameters(operation, componentsParameters) {
|
|
382
398
|
const list = [];
|
|
@@ -405,13 +421,13 @@ function resolveQueryParameters(operation, componentsParameters) {
|
|
|
405
421
|
}
|
|
406
422
|
return list;
|
|
407
423
|
}
|
|
408
|
-
function queryParamsToZodObject(queryParams, schemas,
|
|
424
|
+
function queryParamsToZodObject(queryParams, schemas, schemaKeyToContractVarName2) {
|
|
409
425
|
if (queryParams.length === 0) return "z.object({})";
|
|
410
426
|
const entries = queryParams.map(({ name, required, schema }) => {
|
|
411
427
|
const expr = schemaToZodExpr(
|
|
412
428
|
schema,
|
|
413
429
|
schemas,
|
|
414
|
-
|
|
430
|
+
schemaKeyToContractVarName2,
|
|
415
431
|
/* @__PURE__ */ new Set()
|
|
416
432
|
);
|
|
417
433
|
const field = required ? expr : `${expr}.optional()`;
|
|
@@ -421,32 +437,37 @@ function queryParamsToZodObject(queryParams, schemas, schemaKeyToVarName2) {
|
|
|
421
437
|
${entries.join(",\n")}
|
|
422
438
|
})`;
|
|
423
439
|
}
|
|
424
|
-
function generateAuxiliarySchemas(schemaKeys, schemas,
|
|
440
|
+
function generateAuxiliarySchemas(schemaKeys, schemas, schemaKeyToContractVarNameFn, visited) {
|
|
425
441
|
const lines = [];
|
|
426
442
|
for (const key of schemaKeys) {
|
|
427
443
|
if (visited.has(key)) continue;
|
|
428
444
|
visited.add(key);
|
|
429
445
|
const schema = schemas[key];
|
|
430
446
|
if (!schema) continue;
|
|
431
|
-
const varName =
|
|
447
|
+
const varName = schemaKeyToContractVarNameFn(key);
|
|
432
448
|
const cyclePath = /* @__PURE__ */ new Set([key]);
|
|
433
449
|
const expr = schemaToZodExpr(
|
|
434
450
|
schema,
|
|
435
451
|
schemas,
|
|
436
|
-
|
|
452
|
+
schemaKeyToContractVarNameFn,
|
|
437
453
|
cyclePath
|
|
438
454
|
);
|
|
439
455
|
lines.push(`export const ${varName} = ${expr};`);
|
|
440
456
|
}
|
|
441
457
|
return lines;
|
|
442
458
|
}
|
|
443
|
-
function
|
|
444
|
-
const {
|
|
445
|
-
|
|
459
|
+
function buildCentralZodContractsFile(params) {
|
|
460
|
+
const {
|
|
461
|
+
componentsSchemas,
|
|
462
|
+
utils,
|
|
463
|
+
contractSuffix = DEFAULT_ZOD_CONTRACT_SUFFIX,
|
|
464
|
+
typeSuffix = DEFAULT_DATA_CONTRACT_TYPE_SUFFIX
|
|
465
|
+
} = params;
|
|
466
|
+
const schemaKeyToContractVarNameFn = (key) => schemaKeyToContractVarName(key, utils, contractSuffix, typeSuffix);
|
|
446
467
|
const lines = generateAuxiliarySchemas(
|
|
447
468
|
Object.keys(componentsSchemas),
|
|
448
469
|
componentsSchemas,
|
|
449
|
-
|
|
470
|
+
schemaKeyToContractVarNameFn,
|
|
450
471
|
/* @__PURE__ */ new Set()
|
|
451
472
|
);
|
|
452
473
|
return `import * as z from "zod";
|
|
@@ -468,7 +489,7 @@ function typeToZodSchemaFallback(typeStr) {
|
|
|
468
489
|
if (t === "unknown") return "z.unknown()";
|
|
469
490
|
return "z.any()";
|
|
470
491
|
}
|
|
471
|
-
function typeToZodSchemaWithSchema(typeStr, schemas, utils, typeSuffix) {
|
|
492
|
+
function typeToZodSchemaWithSchema(typeStr, schemas, utils, typeSuffix = DEFAULT_DATA_CONTRACT_TYPE_SUFFIX, contractSuffix = DEFAULT_ZOD_CONTRACT_SUFFIX) {
|
|
472
493
|
const t = typeStr.trim();
|
|
473
494
|
if (!schemas || Object.keys(schemas).length === 0) {
|
|
474
495
|
return { expr: typeToZodSchemaFallback(t), refs: [] };
|
|
@@ -481,51 +502,49 @@ function typeToZodSchemaWithSchema(typeStr, schemas, utils, typeSuffix) {
|
|
|
481
502
|
}
|
|
482
503
|
const refs = /* @__PURE__ */ new Set();
|
|
483
504
|
collectRefs(schema, schemas, refs);
|
|
484
|
-
const
|
|
505
|
+
const schemaKeyToContractVarNameFn = (key) => schemaKeyToContractVarName(key, utils, contractSuffix, typeSuffix);
|
|
485
506
|
const expr = schemaToZodExpr(
|
|
486
507
|
schema,
|
|
487
508
|
schemas,
|
|
488
|
-
|
|
509
|
+
schemaKeyToContractVarNameFn,
|
|
489
510
|
/* @__PURE__ */ new Set()
|
|
490
511
|
);
|
|
491
512
|
return { expr, refs: [...refs] };
|
|
492
513
|
}
|
|
493
|
-
function schemaKeyToZod(schemaKey, schemas, utils) {
|
|
514
|
+
function schemaKeyToZod(schemaKey, schemas, utils, typeSuffix = DEFAULT_DATA_CONTRACT_TYPE_SUFFIX, contractSuffix = DEFAULT_ZOD_CONTRACT_SUFFIX) {
|
|
494
515
|
const schema = schemas[schemaKey];
|
|
495
516
|
if (!schema) return { expr: "z.any()", refs: [] };
|
|
496
517
|
const refs = /* @__PURE__ */ new Set();
|
|
497
518
|
collectRefs(schema, schemas, refs);
|
|
498
|
-
const
|
|
519
|
+
const schemaKeyToContractVarNameFn = (key) => schemaKeyToContractVarName(key, utils, contractSuffix, typeSuffix);
|
|
499
520
|
const expr = schemaToZodExpr(
|
|
500
521
|
schema,
|
|
501
522
|
schemas,
|
|
502
|
-
|
|
523
|
+
schemaKeyToContractVarNameFn,
|
|
503
524
|
/* @__PURE__ */ new Set()
|
|
504
525
|
);
|
|
505
526
|
return { expr, refs: [...refs] };
|
|
506
527
|
}
|
|
507
528
|
function buildEndpointZodContractsCode(params) {
|
|
508
529
|
const {
|
|
509
|
-
routeNameUsage,
|
|
510
530
|
inputParams,
|
|
511
531
|
responseDataTypeName,
|
|
512
|
-
|
|
532
|
+
contractVarName,
|
|
513
533
|
utils,
|
|
534
|
+
contractSuffix = DEFAULT_ZOD_CONTRACT_SUFFIX,
|
|
514
535
|
componentsSchemas = null,
|
|
515
|
-
typeSuffix =
|
|
536
|
+
typeSuffix = DEFAULT_DATA_CONTRACT_TYPE_SUFFIX,
|
|
516
537
|
responseSchemaKey,
|
|
517
538
|
useExternalZodSchemas = false,
|
|
518
539
|
openApiOperation = null,
|
|
519
540
|
openApiComponentsParameters = null,
|
|
520
541
|
queryParamName = "query"
|
|
521
542
|
} = params;
|
|
522
|
-
|
|
523
|
-
const paramsSchemaName = `${_.camelCase(routeNameUsage)}ParamsSchema`;
|
|
524
|
-
const dataSchemaName = `${_.camelCase(routeNameUsage)}DataSchema`;
|
|
543
|
+
utils._;
|
|
525
544
|
const allAuxiliaryKeys = /* @__PURE__ */ new Set();
|
|
526
545
|
const paramParts = [];
|
|
527
546
|
const resolvedQueryParams = openApiOperation && (openApiComponentsParameters || openApiOperation.parameters?.length) ? resolveQueryParameters(openApiOperation, openApiComponentsParameters) : [];
|
|
528
|
-
const
|
|
547
|
+
const schemaKeyToContractVarNameFn = (key) => schemaKeyToContractVarName(key, utils, contractSuffix, typeSuffix);
|
|
529
548
|
for (const p of inputParams) {
|
|
530
549
|
let expr;
|
|
531
550
|
let refKeys = [];
|
|
@@ -533,14 +552,15 @@ function buildEndpointZodContractsCode(params) {
|
|
|
533
552
|
expr = queryParamsToZodObject(
|
|
534
553
|
resolvedQueryParams,
|
|
535
554
|
componentsSchemas,
|
|
536
|
-
|
|
555
|
+
schemaKeyToContractVarNameFn
|
|
537
556
|
);
|
|
538
557
|
} else {
|
|
539
558
|
const result = typeToZodSchemaWithSchema(
|
|
540
559
|
p.type,
|
|
541
560
|
componentsSchemas,
|
|
542
561
|
utils,
|
|
543
|
-
typeSuffix
|
|
562
|
+
typeSuffix,
|
|
563
|
+
contractSuffix
|
|
544
564
|
);
|
|
545
565
|
expr = result.expr;
|
|
546
566
|
refKeys = result.refs;
|
|
@@ -549,11 +569,18 @@ function buildEndpointZodContractsCode(params) {
|
|
|
549
569
|
const schemaWithOptional = p.optional ? `${expr}.optional()` : expr;
|
|
550
570
|
paramParts.push(`${p.name}: ${schemaWithOptional}`);
|
|
551
571
|
}
|
|
552
|
-
const responseResult = responseSchemaKey && componentsSchemas && responseSchemaKey in componentsSchemas ? schemaKeyToZod(
|
|
572
|
+
const responseResult = responseSchemaKey && componentsSchemas && responseSchemaKey in componentsSchemas ? schemaKeyToZod(
|
|
573
|
+
responseSchemaKey,
|
|
574
|
+
componentsSchemas,
|
|
575
|
+
utils,
|
|
576
|
+
typeSuffix,
|
|
577
|
+
contractSuffix
|
|
578
|
+
) : typeToZodSchemaWithSchema(
|
|
553
579
|
responseDataTypeName,
|
|
554
580
|
componentsSchemas,
|
|
555
581
|
utils,
|
|
556
|
-
typeSuffix
|
|
582
|
+
typeSuffix,
|
|
583
|
+
contractSuffix
|
|
557
584
|
);
|
|
558
585
|
const useDataSchemaFromCentral = useExternalZodSchemas && responseSchemaKey && componentsSchemas && responseSchemaKey in componentsSchemas;
|
|
559
586
|
if (useDataSchemaFromCentral) {
|
|
@@ -561,31 +588,27 @@ function buildEndpointZodContractsCode(params) {
|
|
|
561
588
|
} else {
|
|
562
589
|
for (const k of responseResult.refs) allAuxiliaryKeys.add(k);
|
|
563
590
|
}
|
|
564
|
-
const
|
|
591
|
+
const zodContractImportNames = useExternalZodSchemas && allAuxiliaryKeys.size > 0 ? [...allAuxiliaryKeys].map(schemaKeyToContractVarNameFn) : [];
|
|
565
592
|
const allAuxiliary = useExternalZodSchemas ? [] : generateAuxiliarySchemas(
|
|
566
593
|
[...allAuxiliaryKeys],
|
|
567
594
|
componentsSchemas ?? {},
|
|
568
|
-
|
|
595
|
+
schemaKeyToContractVarNameFn,
|
|
569
596
|
/* @__PURE__ */ new Set()
|
|
570
597
|
);
|
|
571
|
-
const paramsFields = paramParts.join(",\n
|
|
572
|
-
const
|
|
573
|
-
|
|
574
|
-
})
|
|
575
|
-
const
|
|
576
|
-
const contractsCode = `export const ${
|
|
577
|
-
params: ${
|
|
578
|
-
data: ${
|
|
598
|
+
const paramsFields = paramParts.join(",\n ");
|
|
599
|
+
const paramsSchemaExpr = `z.object({
|
|
600
|
+
${paramsFields},
|
|
601
|
+
})`;
|
|
602
|
+
const dataSchemaExpr = useDataSchemaFromCentral ? schemaKeyToContractVarNameFn(responseSchemaKey) : responseResult.expr;
|
|
603
|
+
const contractsCode = `export const ${contractVarName} = {
|
|
604
|
+
params: ${paramsSchemaExpr},
|
|
605
|
+
data: ${dataSchemaExpr},
|
|
579
606
|
};`;
|
|
580
607
|
const auxiliaryBlock = allAuxiliary.length > 0 ? `${allAuxiliary.join("\n\n")}
|
|
581
608
|
|
|
582
609
|
` : "";
|
|
583
|
-
const content = `${auxiliaryBlock}${
|
|
584
|
-
|
|
585
|
-
${dataSchemaCode}
|
|
586
|
-
|
|
587
|
-
${contractsCode}`;
|
|
588
|
-
return { content, zodSchemaImportNames };
|
|
610
|
+
const content = `${auxiliaryBlock}${contractsCode}`;
|
|
611
|
+
return { content, zodContractImportNames };
|
|
589
612
|
}
|
|
590
613
|
const formatGroupNameEnumKey = (groupName, { _ }) => _.upperFirst(_.camelCase(groupName));
|
|
591
614
|
const formatTagNameEnumKey = (tagName, utils) => formatGroupNameEnumKey(tagName, utils);
|
|
@@ -745,10 +768,8 @@ const newEndpointTmpl = ({
|
|
|
745
768
|
}) => {
|
|
746
769
|
const zodContractsIsObject = typeof zodContracts === "object" && zodContracts !== null;
|
|
747
770
|
const hasZodContracts = zodContracts === true || zodContractsIsObject;
|
|
748
|
-
const
|
|
749
|
-
const
|
|
750
|
-
const validateOptObj = validateOpt != null && typeof validateOpt === "object" && !Array.isArray(validateOpt) ? validateOpt : null;
|
|
751
|
-
const throwOptObj = throwOpt != null && typeof throwOpt === "object" && !Array.isArray(throwOpt) ? throwOpt : null;
|
|
771
|
+
const sharedContractSuffix = getZodContractSuffix(zodContracts);
|
|
772
|
+
const endpointContractSuffix = getEndpointZodContractSuffix(zodContracts);
|
|
752
773
|
const { _ } = utils;
|
|
753
774
|
const positiveResponseTypes = route.raw.responsesTypes?.filter(
|
|
754
775
|
(it) => +it.status >= 200 && +it.status < 300 && (!it.typeData || filterTypes(it.typeData))
|
|
@@ -862,7 +883,24 @@ const newEndpointTmpl = ({
|
|
|
862
883
|
});
|
|
863
884
|
const isAllowedInputType = filterTypes(requestInputTypeDc);
|
|
864
885
|
const defaultOkResponseType = positiveResponseTypes?.[0]?.type ?? "unknown";
|
|
865
|
-
const
|
|
886
|
+
const contractVarName = hasZodContracts ? `${_.camelCase(route.routeName.usage)}${endpointContractSuffix}` : null;
|
|
887
|
+
const routeInfoForContracts = contractVarName != null ? {
|
|
888
|
+
operationId: raw.operationId ?? "",
|
|
889
|
+
path: path2,
|
|
890
|
+
method,
|
|
891
|
+
contractName: contractVarName
|
|
892
|
+
} : null;
|
|
893
|
+
const resolveZodContractsMaybeFn = (value) => {
|
|
894
|
+
if (typeof value === "function" && routeInfoForContracts != null) {
|
|
895
|
+
return value(routeInfoForContracts.contractName, routeInfoForContracts);
|
|
896
|
+
}
|
|
897
|
+
return value;
|
|
898
|
+
};
|
|
899
|
+
const validateOpt = zodContractsIsObject ? resolveZodContractsMaybeFn(zodContracts.validate) : zodContracts === true ? true : void 0;
|
|
900
|
+
const throwOpt = zodContractsIsObject ? resolveZodContractsMaybeFn(zodContracts.throw) : void 0;
|
|
901
|
+
const isRuntimeContractsRuleObject = (value) => value != null && typeof value === "object" && !Array.isArray(value);
|
|
902
|
+
const validateOptObj = isRuntimeContractsRuleObject(validateOpt) ? validateOpt : null;
|
|
903
|
+
const throwOptObj = isRuntimeContractsRuleObject(throwOpt) ? throwOpt : null;
|
|
866
904
|
const swaggerSchema = configuration.config?.swaggerSchema ?? configuration?.swaggerSchema;
|
|
867
905
|
const componentsSchemas = swaggerSchema?.components?.schemas;
|
|
868
906
|
let operationFromSpec = null;
|
|
@@ -888,7 +926,10 @@ const newEndpointTmpl = ({
|
|
|
888
926
|
(m) => m.name === defaultOkResponseType
|
|
889
927
|
);
|
|
890
928
|
if (aliasType?.typeIdentifier === "type" && typeof aliasType.content === "string" && /^[A-Za-z0-9_]+$/.test(aliasType.content.trim())) {
|
|
891
|
-
const resolved = typeNameToSchemaKey(
|
|
929
|
+
const resolved = typeNameToSchemaKey(
|
|
930
|
+
aliasType.content.trim(),
|
|
931
|
+
DEFAULT_DATA_CONTRACT_TYPE_SUFFIX
|
|
932
|
+
);
|
|
892
933
|
if (resolved in componentsSchemas) responseSchemaKey = resolved;
|
|
893
934
|
}
|
|
894
935
|
}
|
|
@@ -899,46 +940,36 @@ const newEndpointTmpl = ({
|
|
|
899
940
|
if (candidate in componentsSchemas) responseSchemaKey = candidate;
|
|
900
941
|
}
|
|
901
942
|
}
|
|
902
|
-
const contractsCode = hasZodContracts &&
|
|
943
|
+
const contractsCode = hasZodContracts && contractVarName ? buildEndpointZodContractsCode({
|
|
903
944
|
routeNameUsage: route.routeName.usage,
|
|
904
945
|
inputParams,
|
|
905
946
|
responseDataTypeName: defaultOkResponseType,
|
|
906
|
-
|
|
947
|
+
contractVarName,
|
|
907
948
|
utils,
|
|
908
949
|
componentsSchemas: componentsSchemas ?? void 0,
|
|
909
|
-
typeSuffix:
|
|
950
|
+
typeSuffix: DEFAULT_DATA_CONTRACT_TYPE_SUFFIX,
|
|
910
951
|
responseSchemaKey: responseSchemaKey ?? void 0,
|
|
911
952
|
useExternalZodSchemas: Boolean(relativePathZodSchemas),
|
|
953
|
+
contractSuffix: sharedContractSuffix,
|
|
912
954
|
openApiOperation: operationFromSpec ?? void 0,
|
|
913
955
|
openApiComponentsParameters: swaggerSchema?.components?.parameters ?? void 0,
|
|
914
956
|
queryParamName: queryName
|
|
915
957
|
}) : null;
|
|
916
|
-
const appendRuleOpt = zodContractsIsObject && zodContracts.appendRule != null ? zodContracts.appendRule : null;
|
|
917
|
-
const
|
|
918
|
-
|
|
919
|
-
path: path2,
|
|
920
|
-
method,
|
|
921
|
-
contractName: contractsVarName
|
|
922
|
-
} : null;
|
|
923
|
-
const contractsLine = (() => {
|
|
924
|
-
if (contractsVarName == null) return "";
|
|
958
|
+
const appendRuleOpt = zodContractsIsObject && zodContracts.appendRule != null ? resolveZodContractsMaybeFn(zodContracts.appendRule) : null;
|
|
959
|
+
const contractLine = (() => {
|
|
960
|
+
if (contractVarName == null) return "";
|
|
925
961
|
if (typeof appendRuleOpt === "string")
|
|
926
|
-
return `
|
|
927
|
-
if (
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
routeInfoForAppend
|
|
931
|
-
);
|
|
932
|
-
return include ? `contracts: ${contractsVarName},` : "contracts: undefined,";
|
|
933
|
-
}
|
|
934
|
-
return `contracts: ${contractsVarName},`;
|
|
962
|
+
return `contract: ${appendRuleOpt} ? ${contractVarName} : undefined,`;
|
|
963
|
+
if (appendRuleOpt === false) return "contract: undefined,";
|
|
964
|
+
if (appendRuleOpt === true) return `contract: ${contractVarName},`;
|
|
965
|
+
return `contract: ${contractVarName},`;
|
|
935
966
|
})();
|
|
936
|
-
const
|
|
967
|
+
const validateContractLine = (() => {
|
|
937
968
|
if (validateOpt === void 0) return "";
|
|
938
969
|
if (typeof validateOpt === "string")
|
|
939
|
-
return `
|
|
970
|
+
return `validateContract: ${validateOpt},`;
|
|
940
971
|
if (typeof validateOpt === "boolean")
|
|
941
|
-
return `
|
|
972
|
+
return `validateContract: ${validateOpt},`;
|
|
942
973
|
if (validateOptObj !== null) {
|
|
943
974
|
const parts = [];
|
|
944
975
|
if (validateOptObj.params !== void 0)
|
|
@@ -949,7 +980,7 @@ const newEndpointTmpl = ({
|
|
|
949
980
|
parts.push(
|
|
950
981
|
`data: ${typeof validateOptObj.data === "string" ? validateOptObj.data : validateOptObj.data}`
|
|
951
982
|
);
|
|
952
|
-
return parts.length > 0 ? `
|
|
983
|
+
return parts.length > 0 ? `validateContract: { ${parts.join(", ")} },` : "";
|
|
953
984
|
}
|
|
954
985
|
return "";
|
|
955
986
|
})();
|
|
@@ -971,7 +1002,7 @@ const newEndpointTmpl = ({
|
|
|
971
1002
|
reservedDataContractNames,
|
|
972
1003
|
localModelTypes: isAllowedInputType ? [requestInputTypeDc] : [],
|
|
973
1004
|
contractsCode: contractsCode ?? void 0,
|
|
974
|
-
|
|
1005
|
+
contractVarName: contractVarName ?? void 0,
|
|
975
1006
|
content: `
|
|
976
1007
|
new ${importFileParams.endpoint.exportName}<
|
|
977
1008
|
${getHttpRequestGenerics()},
|
|
@@ -1004,8 +1035,8 @@ new ${importFileParams.endpoint.exportName}<
|
|
|
1004
1035
|
${groupName ? `group: ${metaInfo ? `Group.${formatGroupNameEnumKey(groupName, utils)}` : `"${groupName}"`},` : ""}
|
|
1005
1036
|
${metaInfo?.namespace ? `namespace,` : ""}
|
|
1006
1037
|
meta: ${requestInfoMeta?.tmplData ?? "{} as any"},
|
|
1007
|
-
${
|
|
1008
|
-
${
|
|
1038
|
+
${contractLine}
|
|
1039
|
+
${validateContractLine}
|
|
1009
1040
|
${throwContractsLine}
|
|
1010
1041
|
},
|
|
1011
1042
|
${importFileParams.queryClient.exportName},
|
|
@@ -1055,17 +1086,17 @@ const allEndpointPerFileTmpl = async (params) => {
|
|
|
1055
1086
|
const hasAnyZodContracts = newEndpointTemplates.some(
|
|
1056
1087
|
(t) => t.contractsCode != null
|
|
1057
1088
|
);
|
|
1058
|
-
const
|
|
1089
|
+
const allZodContractImportNames = /* @__PURE__ */ new Set();
|
|
1059
1090
|
newEndpointTemplates.forEach((t) => {
|
|
1060
1091
|
const c = t.contractsCode;
|
|
1061
|
-
if (c != null && typeof c === "object" && c.
|
|
1062
|
-
for (const n of c.
|
|
1063
|
-
|
|
1092
|
+
if (c != null && typeof c === "object" && c.zodContractImportNames?.length) {
|
|
1093
|
+
for (const n of c.zodContractImportNames) {
|
|
1094
|
+
allZodContractImportNames.add(n);
|
|
1064
1095
|
}
|
|
1065
1096
|
}
|
|
1066
1097
|
});
|
|
1067
1098
|
const zodImportLine = hasAnyZodContracts ? 'import * as z from "zod";' : "";
|
|
1068
|
-
const zodSchemasImportLine =
|
|
1099
|
+
const zodSchemasImportLine = allZodContractImportNames.size && relativePathZodSchemas ? `import { ${[...allZodContractImportNames].sort().join(", ")} } from "${relativePathZodSchemas}";` : "";
|
|
1069
1100
|
const endpointTemplates = await Promise.all(
|
|
1070
1101
|
newEndpointTemplates.map(
|
|
1071
1102
|
async ({
|
|
@@ -1169,7 +1200,7 @@ const allExportsTmpl = async ({
|
|
|
1169
1200
|
}) => {
|
|
1170
1201
|
return await formatTSContent(`${LINTERS_IGNORE}
|
|
1171
1202
|
export * from './data-contracts';
|
|
1172
|
-
${exportSchemas ? " export * from './
|
|
1203
|
+
${exportSchemas ? " export * from './contracts';\n " : ""}${collectedExportFiles.map((fileName) => `export * from './${fileName}';`).join("\n")}
|
|
1173
1204
|
${metaInfo ? 'export * from "./meta-info";' : ""}
|
|
1174
1205
|
`);
|
|
1175
1206
|
};
|
|
@@ -1251,7 +1282,7 @@ const endpointPerFileTmpl = async (params) => {
|
|
|
1251
1282
|
const dataContractImportToken = "/*__DATA_CONTRACT_IMPORTS__*/";
|
|
1252
1283
|
const contractsResult = contractsCode != null && typeof contractsCode === "object" ? contractsCode : null;
|
|
1253
1284
|
const zodImportLine = contractsResult != null ? 'import * as z from "zod";' : "";
|
|
1254
|
-
const zodSchemasImportLine = contractsResult?.
|
|
1285
|
+
const zodSchemasImportLine = contractsResult?.zodContractImportNames?.length && relativePathZodSchemas ? `import { ${contractsResult.zodContractImportNames.join(", ")} } from "${relativePathZodSchemas}";` : "";
|
|
1255
1286
|
const contractsBlock = contractsResult != null ? `
|
|
1256
1287
|
|
|
1257
1288
|
${contractsResult.content}
|
|
@@ -1496,7 +1527,7 @@ const generateApi = async (params) => {
|
|
|
1496
1527
|
cleanOutput: params.cleanOutput ?? true,
|
|
1497
1528
|
modular: true,
|
|
1498
1529
|
patch: true,
|
|
1499
|
-
typeSuffix:
|
|
1530
|
+
typeSuffix: DEFAULT_DATA_CONTRACT_TYPE_SUFFIX,
|
|
1500
1531
|
disableStrictSSL: false,
|
|
1501
1532
|
singleHttpClient: true,
|
|
1502
1533
|
extractRequestBody: true,
|
|
@@ -1661,7 +1692,8 @@ const generateApi = async (params) => {
|
|
|
1661
1692
|
};
|
|
1662
1693
|
const reservedDataContractNamesMap = /* @__PURE__ */ new Map();
|
|
1663
1694
|
const componentsSchemasForZod = generated.configuration.config?.swaggerSchema?.components?.schemas ?? generated.configuration.swaggerSchema?.components?.schemas;
|
|
1664
|
-
const
|
|
1695
|
+
const zodContractSuffix = getZodContractSuffix(params.zodContracts);
|
|
1696
|
+
const hasZodContractsFile = (params.zodContracts === true || typeof params.zodContracts === "object" && params.zodContracts != null) && componentsSchemasForZod && typeof componentsSchemasForZod === "object" && Object.keys(componentsSchemasForZod).length > 0;
|
|
1665
1697
|
const collectedExportFilesFromIndexFile = [];
|
|
1666
1698
|
const groupsMap = /* @__PURE__ */ new Map();
|
|
1667
1699
|
const nonEmptyGroups = /* @__PURE__ */ new Set();
|
|
@@ -1684,7 +1716,7 @@ const generateApi = async (params) => {
|
|
|
1684
1716
|
groupNames: [],
|
|
1685
1717
|
namespace
|
|
1686
1718
|
},
|
|
1687
|
-
relativePathZodSchemas:
|
|
1719
|
+
relativePathZodSchemas: hasZodContractsFile ? "../contracts" : null
|
|
1688
1720
|
});
|
|
1689
1721
|
if (Array.isArray(route.raw.tags)) {
|
|
1690
1722
|
route.raw.tags.forEach((tag) => {
|
|
@@ -1729,7 +1761,7 @@ const generateApi = async (params) => {
|
|
|
1729
1761
|
namespace,
|
|
1730
1762
|
groupNames: []
|
|
1731
1763
|
},
|
|
1732
|
-
relativePathZodSchemas:
|
|
1764
|
+
relativePathZodSchemas: hasZodContractsFile ? "./contracts" : null
|
|
1733
1765
|
});
|
|
1734
1766
|
reservedDataContractNames.forEach((name) => {
|
|
1735
1767
|
reservedDataContractNamesMap.set(
|
|
@@ -1802,6 +1834,7 @@ const generateApi = async (params) => {
|
|
|
1802
1834
|
...baseTmplParams,
|
|
1803
1835
|
route,
|
|
1804
1836
|
relativePathDataContracts: "../../data-contracts",
|
|
1837
|
+
relativePathZodSchemas: hasZodContractsFile ? "../../contracts" : null,
|
|
1805
1838
|
groupName,
|
|
1806
1839
|
metaInfo: params.noMetaInfo ? null : {
|
|
1807
1840
|
namespace,
|
|
@@ -1844,6 +1877,7 @@ const generateApi = async (params) => {
|
|
|
1844
1877
|
...baseTmplParams,
|
|
1845
1878
|
routes,
|
|
1846
1879
|
relativePathDataContracts: "../data-contracts",
|
|
1880
|
+
relativePathZodSchemas: hasZodContractsFile ? "../contracts" : null,
|
|
1847
1881
|
groupName,
|
|
1848
1882
|
metaInfo: params.noMetaInfo ? null : {
|
|
1849
1883
|
namespace,
|
|
@@ -1925,20 +1959,21 @@ export * as ${exportGroupName} from './endpoints';
|
|
|
1925
1959
|
withPrefix: false,
|
|
1926
1960
|
content: dataContractsContent
|
|
1927
1961
|
});
|
|
1928
|
-
if (
|
|
1929
|
-
const
|
|
1962
|
+
if (hasZodContractsFile && componentsSchemasForZod) {
|
|
1963
|
+
const contractsTsContent = buildCentralZodContractsFile({
|
|
1930
1964
|
componentsSchemas: componentsSchemasForZod,
|
|
1931
|
-
utils
|
|
1965
|
+
utils,
|
|
1966
|
+
contractSuffix: zodContractSuffix
|
|
1932
1967
|
});
|
|
1933
|
-
const
|
|
1968
|
+
const formattedContractsContent = await generated.formatTSContent(
|
|
1934
1969
|
`${LINTERS_IGNORE}
|
|
1935
|
-
${
|
|
1970
|
+
${contractsTsContent}`
|
|
1936
1971
|
);
|
|
1937
1972
|
codegenFs.createFile({
|
|
1938
1973
|
path: paths.outputDir,
|
|
1939
|
-
fileName: "
|
|
1974
|
+
fileName: "contracts.ts",
|
|
1940
1975
|
withPrefix: false,
|
|
1941
|
-
content:
|
|
1976
|
+
content: formattedContractsContent
|
|
1942
1977
|
});
|
|
1943
1978
|
}
|
|
1944
1979
|
if (metaInfo) {
|
|
@@ -1961,7 +1996,7 @@ ${schemasTsContent}`
|
|
|
1961
1996
|
...baseTmplParams,
|
|
1962
1997
|
collectedExportFiles: collectedExportFilesFromIndexFile,
|
|
1963
1998
|
metaInfo,
|
|
1964
|
-
exportSchemas:
|
|
1999
|
+
exportSchemas: hasZodContractsFile
|
|
1965
2000
|
})
|
|
1966
2001
|
});
|
|
1967
2002
|
if (shouldGenerateBarrelFiles) {
|
|
@@ -1984,7 +2019,7 @@ export * as ${namespace} from './__exports';
|
|
|
1984
2019
|
...baseTmplParams,
|
|
1985
2020
|
collectedExportFiles: collectedExportFilesFromIndexFile,
|
|
1986
2021
|
metaInfo,
|
|
1987
|
-
exportSchemas:
|
|
2022
|
+
exportSchemas: hasZodContractsFile
|
|
1988
2023
|
})
|
|
1989
2024
|
});
|
|
1990
2025
|
}
|