mobx-tanstack-query-api 0.40.1 → 0.41.0
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 +121 -104
- package/cli.cjs.map +1 -1
- package/cli.d.ts +33 -18
- package/cli.js +121 -104
- 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.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ParsedRoute, ModelType, RawRouteInfo, GenerateApiConfiguration, GenerateApiParams } from 'swagger-typescript-api';
|
|
2
|
-
import { AnyObject, PartialKeys, Maybe, KeyOfByValue, Defined } from 'yummies/types';
|
|
2
|
+
import { AnyObject, PartialKeys, Maybe, MaybeFn, KeyOfByValue, Defined } from 'yummies/types';
|
|
3
3
|
import { LoDashStatic } from '/home/runner/work/mobx-tanstack-query-api/mobx-tanstack-query-api/node_modules/.pnpm/@types+lodash@4.17.24/node_modules/@types/lodash/index.d.ts';
|
|
4
4
|
|
|
5
5
|
type AnyFilterOptionFn = (...args: any[]) => boolean;
|
|
@@ -27,6 +27,7 @@ interface ImportFileParams {
|
|
|
27
27
|
|
|
28
28
|
type TypeInfo = PartialKeys<ModelType, 'rawContent'>;
|
|
29
29
|
|
|
30
|
+
type RuntimeExpressionOrBoolean = string | boolean;
|
|
30
31
|
interface GenerateQueryApiParams {
|
|
31
32
|
/**
|
|
32
33
|
* [**Documentation**](https://js2me.github.io/mobx-tanstack-query-api/codegen/config/#output)
|
|
@@ -186,15 +187,15 @@ interface GenerateQueryApiParams {
|
|
|
186
187
|
*/
|
|
187
188
|
noBarrelFiles?: boolean;
|
|
188
189
|
/**
|
|
189
|
-
* Generate Zod contracts (params + data schemas) for each endpoint and add `
|
|
190
|
-
* When truthy, can also enable validation via `
|
|
190
|
+
* Generate Zod contracts (params + data schemas) for each endpoint and add `contract` to the endpoint config.
|
|
191
|
+
* When truthy, can also enable validation via `validateContract` in the endpoint config.
|
|
191
192
|
* Requires `zod` to be installed.
|
|
192
193
|
*
|
|
193
|
-
* - `true`: generate contracts and set `
|
|
194
|
+
* - `true`: generate contracts and set `validateContract: true` (validate params + data).
|
|
194
195
|
* - `false`: no contracts, no validation.
|
|
195
|
-
* - `{ validate: boolean }`: set `
|
|
196
|
-
* - `{ validate: string }`: set `
|
|
197
|
-
* - `{ validate: { params?: boolean | string; data?: boolean | string } }`: set `
|
|
196
|
+
* - `{ validate: boolean }`: set `validateContract` to that boolean.
|
|
197
|
+
* - `{ validate: string }`: set `validateContract` to the expression (inserted as-is). E.g. `"process.env.NODE_ENV === 'development'"`.
|
|
198
|
+
* - `{ validate: { params?: boolean | string; data?: boolean | string } }`: set `validateContract` to an object; each value is literal or expression (string inserted as-is).
|
|
198
199
|
*
|
|
199
200
|
* When using an object form, optional `throw` controls `throwContracts` (throw on validation errors vs warn):
|
|
200
201
|
* - `{ throw: boolean }`: set `throwContracts` to that boolean.
|
|
@@ -202,20 +203,34 @@ interface GenerateQueryApiParams {
|
|
|
202
203
|
* - `{ throw: { params?: boolean | string; data?: boolean | string } }`: set `throwContracts` to an object; each value is literal or expression (string inserted as-is).
|
|
203
204
|
*
|
|
204
205
|
* Optional `appendRule`: either a string (runtime) or a function (codegen-time).
|
|
205
|
-
* - **string**: expression inserted as-is → `
|
|
206
|
-
* - **function** (contractName, routeInfo) => boolean: at codegen time, if true → `
|
|
206
|
+
* - **string**: expression inserted as-is → `contract: <expr> ? <contractVar> : undefined`. E.g. `"process.env.NODE_ENV === \"development\""`.
|
|
207
|
+
* - **function** (contractName, routeInfo) => boolean: at codegen time, if true → `contract: <contractVar>`, if false → `contract: undefined`.
|
|
208
|
+
*
|
|
209
|
+
* Optional `suffix`: suffix for generated Zod contract variables.
|
|
210
|
+
* Default: `"Contract"`.
|
|
207
211
|
*/
|
|
208
212
|
zodContracts?: boolean | {
|
|
209
|
-
validate:
|
|
210
|
-
params?:
|
|
211
|
-
data?:
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
213
|
+
validate: MaybeFn<RuntimeExpressionOrBoolean | {
|
|
214
|
+
params?: RuntimeExpressionOrBoolean;
|
|
215
|
+
data?: RuntimeExpressionOrBoolean;
|
|
216
|
+
}, [
|
|
217
|
+
contractName: string,
|
|
218
|
+
routeInfo: ZodContractsRouteInfo
|
|
219
|
+
]>;
|
|
220
|
+
throw?: MaybeFn<RuntimeExpressionOrBoolean | {
|
|
221
|
+
params?: RuntimeExpressionOrBoolean;
|
|
222
|
+
data?: RuntimeExpressionOrBoolean;
|
|
223
|
+
}, [
|
|
224
|
+
contractName: string,
|
|
225
|
+
routeInfo: ZodContractsRouteInfo
|
|
226
|
+
]>;
|
|
217
227
|
/** String: runtime condition. Function: codegen-time filter for (contractName, routeInfo). */
|
|
218
|
-
appendRule?:
|
|
228
|
+
appendRule?: MaybeFn<RuntimeExpressionOrBoolean, [
|
|
229
|
+
contractName: string,
|
|
230
|
+
routeInfo: ZodContractsRouteInfo
|
|
231
|
+
]>;
|
|
232
|
+
/** Suffix for all generated Zod contract variables. Default: "Contract". */
|
|
233
|
+
suffix?: string;
|
|
219
234
|
};
|
|
220
235
|
}
|
|
221
236
|
/** Route info passed to zodContracts.appendRule at codegen time. */
|
package/cli.js
CHANGED
|
@@ -216,6 +216,10 @@ const createShortModelType = (shortModelType) => {
|
|
|
216
216
|
description: shortModelType.description || ""
|
|
217
217
|
};
|
|
218
218
|
};
|
|
219
|
+
const DEFAULT_ZOD_CONTRACT_SUFFIX = "Contract";
|
|
220
|
+
function getZodContractSuffix(zodContracts) {
|
|
221
|
+
return (typeof zodContracts === "object" && zodContracts !== null && typeof zodContracts.suffix === "string" ? zodContracts.suffix : void 0) ?? DEFAULT_ZOD_CONTRACT_SUFFIX;
|
|
222
|
+
}
|
|
219
223
|
const REF_PREFIX = "#/components/schemas/";
|
|
220
224
|
const REF_PREFIX_PARAMS = "#/components/parameters/";
|
|
221
225
|
function parseRef(ref) {
|
|
@@ -267,14 +271,14 @@ function schemaToNumber(schema) {
|
|
|
267
271
|
if (schema.maximum != null) n += `.max(${schema.maximum})`;
|
|
268
272
|
return n;
|
|
269
273
|
}
|
|
270
|
-
function schemaToArray(schema, schemas,
|
|
271
|
-
const items = schema.items ? schemaToZodExpr(schema.items, schemas,
|
|
274
|
+
function schemaToArray(schema, schemas, schemaKeyToVarName, visited) {
|
|
275
|
+
const items = schema.items ? schemaToZodExpr(schema.items, schemas, schemaKeyToVarName, visited) : "z.any()";
|
|
272
276
|
let a = `z.array(${items})`;
|
|
273
277
|
if (schema.minItems != null) a += `.min(${schema.minItems})`;
|
|
274
278
|
if (schema.maxItems != null) a += `.max(${schema.maxItems})`;
|
|
275
279
|
return a;
|
|
276
280
|
}
|
|
277
|
-
function schemaToObject(schema, schemas,
|
|
281
|
+
function schemaToObject(schema, schemas, schemaKeyToVarName, visited) {
|
|
278
282
|
if (schema.properties && Object.keys(schema.properties).length > 0) {
|
|
279
283
|
const required = new Set(schema.required ?? []);
|
|
280
284
|
const entries = Object.entries(schema.properties).map(
|
|
@@ -282,7 +286,7 @@ function schemaToObject(schema, schemas, schemaKeyToVarName2, visited) {
|
|
|
282
286
|
const expr = schemaToZodExpr(
|
|
283
287
|
propSchema,
|
|
284
288
|
schemas,
|
|
285
|
-
|
|
289
|
+
schemaKeyToVarName,
|
|
286
290
|
visited
|
|
287
291
|
);
|
|
288
292
|
const optional = !required.has(propName);
|
|
@@ -300,26 +304,26 @@ ${entries.join(",\n")}
|
|
|
300
304
|
const value = schemaToZodExpr(
|
|
301
305
|
schema.additionalProperties,
|
|
302
306
|
schemas,
|
|
303
|
-
|
|
307
|
+
schemaKeyToVarName,
|
|
304
308
|
visited
|
|
305
309
|
);
|
|
306
310
|
return `z.record(z.string(), ${value})`;
|
|
307
311
|
}
|
|
308
312
|
return "z.record(z.string(), z.any())";
|
|
309
313
|
}
|
|
310
|
-
function schemaToZodExpr(schema, schemas,
|
|
314
|
+
function schemaToZodExpr(schema, schemas, schemaKeyToVarName, visited) {
|
|
311
315
|
if (!schema) return "z.any()";
|
|
312
316
|
if (schema.$ref) {
|
|
313
317
|
const key = parseRef(schema.$ref);
|
|
314
318
|
if (key && key in schemas) {
|
|
315
319
|
const isCycle = visited.has(key);
|
|
316
|
-
return isCycle ? `z.lazy((): z.ZodTypeAny => ${
|
|
320
|
+
return isCycle ? `z.lazy((): z.ZodTypeAny => ${schemaKeyToVarName(key)})` : `z.lazy(() => ${schemaKeyToVarName(key)})`;
|
|
317
321
|
}
|
|
318
322
|
return "z.any()";
|
|
319
323
|
}
|
|
320
324
|
if (schema.allOf && schema.allOf.length > 0) {
|
|
321
325
|
const parts = schema.allOf.map(
|
|
322
|
-
(part) => schemaToZodExpr(part, schemas,
|
|
326
|
+
(part) => schemaToZodExpr(part, schemas, schemaKeyToVarName, visited)
|
|
323
327
|
);
|
|
324
328
|
const base2 = parts.length === 1 ? parts[0] : parts.reduce((acc, p) => `z.intersection(${acc}, ${p})`);
|
|
325
329
|
return schema.nullable === true ? `${base2}.nullable()` : base2;
|
|
@@ -338,10 +342,10 @@ function schemaToZodExpr(schema, schemas, schemaKeyToVarName2, visited) {
|
|
|
338
342
|
base = "z.boolean()";
|
|
339
343
|
break;
|
|
340
344
|
case "array":
|
|
341
|
-
base = schemaToArray(schema, schemas,
|
|
345
|
+
base = schemaToArray(schema, schemas, schemaKeyToVarName, visited);
|
|
342
346
|
break;
|
|
343
347
|
case "object":
|
|
344
|
-
base = schemaToObject(schema, schemas,
|
|
348
|
+
base = schemaToObject(schema, schemas, schemaKeyToVarName, visited);
|
|
345
349
|
break;
|
|
346
350
|
default:
|
|
347
351
|
base = "z.any()";
|
|
@@ -371,9 +375,9 @@ function collectRefs(schema, schemas, out) {
|
|
|
371
375
|
collectRefs(schema.additionalProperties, schemas, out);
|
|
372
376
|
}
|
|
373
377
|
}
|
|
374
|
-
function
|
|
378
|
+
function schemaKeyToContractVarName(key, utils, contractSuffix = DEFAULT_ZOD_CONTRACT_SUFFIX) {
|
|
375
379
|
const _ = utils._;
|
|
376
|
-
return `${_.camelCase(key)}
|
|
380
|
+
return `${_.camelCase(key)}${contractSuffix}`;
|
|
377
381
|
}
|
|
378
382
|
function resolveQueryParameters(operation, componentsParameters) {
|
|
379
383
|
const list = [];
|
|
@@ -402,13 +406,13 @@ function resolveQueryParameters(operation, componentsParameters) {
|
|
|
402
406
|
}
|
|
403
407
|
return list;
|
|
404
408
|
}
|
|
405
|
-
function queryParamsToZodObject(queryParams, schemas,
|
|
409
|
+
function queryParamsToZodObject(queryParams, schemas, schemaKeyToContractVarName2) {
|
|
406
410
|
if (queryParams.length === 0) return "z.object({})";
|
|
407
411
|
const entries = queryParams.map(({ name, required, schema }) => {
|
|
408
412
|
const expr = schemaToZodExpr(
|
|
409
413
|
schema,
|
|
410
414
|
schemas,
|
|
411
|
-
|
|
415
|
+
schemaKeyToContractVarName2,
|
|
412
416
|
/* @__PURE__ */ new Set()
|
|
413
417
|
);
|
|
414
418
|
const field = required ? expr : `${expr}.optional()`;
|
|
@@ -418,32 +422,36 @@ function queryParamsToZodObject(queryParams, schemas, schemaKeyToVarName2) {
|
|
|
418
422
|
${entries.join(",\n")}
|
|
419
423
|
})`;
|
|
420
424
|
}
|
|
421
|
-
function generateAuxiliarySchemas(schemaKeys, schemas,
|
|
425
|
+
function generateAuxiliarySchemas(schemaKeys, schemas, schemaKeyToContractVarNameFn, visited) {
|
|
422
426
|
const lines = [];
|
|
423
427
|
for (const key of schemaKeys) {
|
|
424
428
|
if (visited.has(key)) continue;
|
|
425
429
|
visited.add(key);
|
|
426
430
|
const schema = schemas[key];
|
|
427
431
|
if (!schema) continue;
|
|
428
|
-
const varName =
|
|
432
|
+
const varName = schemaKeyToContractVarNameFn(key);
|
|
429
433
|
const cyclePath = /* @__PURE__ */ new Set([key]);
|
|
430
434
|
const expr = schemaToZodExpr(
|
|
431
435
|
schema,
|
|
432
436
|
schemas,
|
|
433
|
-
|
|
437
|
+
schemaKeyToContractVarNameFn,
|
|
434
438
|
cyclePath
|
|
435
439
|
);
|
|
436
440
|
lines.push(`export const ${varName} = ${expr};`);
|
|
437
441
|
}
|
|
438
442
|
return lines;
|
|
439
443
|
}
|
|
440
|
-
function
|
|
441
|
-
const {
|
|
442
|
-
|
|
444
|
+
function buildCentralZodContractsFile(params) {
|
|
445
|
+
const {
|
|
446
|
+
componentsSchemas,
|
|
447
|
+
utils,
|
|
448
|
+
contractSuffix = DEFAULT_ZOD_CONTRACT_SUFFIX
|
|
449
|
+
} = params;
|
|
450
|
+
const schemaKeyToContractVarNameFn = (key) => schemaKeyToContractVarName(key, utils, contractSuffix);
|
|
443
451
|
const lines = generateAuxiliarySchemas(
|
|
444
452
|
Object.keys(componentsSchemas),
|
|
445
453
|
componentsSchemas,
|
|
446
|
-
|
|
454
|
+
schemaKeyToContractVarNameFn,
|
|
447
455
|
/* @__PURE__ */ new Set()
|
|
448
456
|
);
|
|
449
457
|
return `import * as z from "zod";
|
|
@@ -465,7 +473,7 @@ function typeToZodSchemaFallback(typeStr) {
|
|
|
465
473
|
if (t === "unknown") return "z.unknown()";
|
|
466
474
|
return "z.any()";
|
|
467
475
|
}
|
|
468
|
-
function typeToZodSchemaWithSchema(typeStr, schemas, utils, typeSuffix) {
|
|
476
|
+
function typeToZodSchemaWithSchema(typeStr, schemas, utils, typeSuffix, contractSuffix = DEFAULT_ZOD_CONTRACT_SUFFIX) {
|
|
469
477
|
const t = typeStr.trim();
|
|
470
478
|
if (!schemas || Object.keys(schemas).length === 0) {
|
|
471
479
|
return { expr: typeToZodSchemaFallback(t), refs: [] };
|
|
@@ -478,36 +486,36 @@ function typeToZodSchemaWithSchema(typeStr, schemas, utils, typeSuffix) {
|
|
|
478
486
|
}
|
|
479
487
|
const refs = /* @__PURE__ */ new Set();
|
|
480
488
|
collectRefs(schema, schemas, refs);
|
|
481
|
-
const
|
|
489
|
+
const schemaKeyToContractVarNameFn = (key) => schemaKeyToContractVarName(key, utils, contractSuffix);
|
|
482
490
|
const expr = schemaToZodExpr(
|
|
483
491
|
schema,
|
|
484
492
|
schemas,
|
|
485
|
-
|
|
493
|
+
schemaKeyToContractVarNameFn,
|
|
486
494
|
/* @__PURE__ */ new Set()
|
|
487
495
|
);
|
|
488
496
|
return { expr, refs: [...refs] };
|
|
489
497
|
}
|
|
490
|
-
function schemaKeyToZod(schemaKey, schemas, utils) {
|
|
498
|
+
function schemaKeyToZod(schemaKey, schemas, utils, contractSuffix = DEFAULT_ZOD_CONTRACT_SUFFIX) {
|
|
491
499
|
const schema = schemas[schemaKey];
|
|
492
500
|
if (!schema) return { expr: "z.any()", refs: [] };
|
|
493
501
|
const refs = /* @__PURE__ */ new Set();
|
|
494
502
|
collectRefs(schema, schemas, refs);
|
|
495
|
-
const
|
|
503
|
+
const schemaKeyToContractVarNameFn = (key) => schemaKeyToContractVarName(key, utils, contractSuffix);
|
|
496
504
|
const expr = schemaToZodExpr(
|
|
497
505
|
schema,
|
|
498
506
|
schemas,
|
|
499
|
-
|
|
507
|
+
schemaKeyToContractVarNameFn,
|
|
500
508
|
/* @__PURE__ */ new Set()
|
|
501
509
|
);
|
|
502
510
|
return { expr, refs: [...refs] };
|
|
503
511
|
}
|
|
504
512
|
function buildEndpointZodContractsCode(params) {
|
|
505
513
|
const {
|
|
506
|
-
routeNameUsage,
|
|
507
514
|
inputParams,
|
|
508
515
|
responseDataTypeName,
|
|
509
|
-
|
|
516
|
+
contractVarName,
|
|
510
517
|
utils,
|
|
518
|
+
contractSuffix = DEFAULT_ZOD_CONTRACT_SUFFIX,
|
|
511
519
|
componentsSchemas = null,
|
|
512
520
|
typeSuffix = "DC",
|
|
513
521
|
responseSchemaKey,
|
|
@@ -516,13 +524,11 @@ function buildEndpointZodContractsCode(params) {
|
|
|
516
524
|
openApiComponentsParameters = null,
|
|
517
525
|
queryParamName = "query"
|
|
518
526
|
} = params;
|
|
519
|
-
|
|
520
|
-
const paramsSchemaName = `${_.camelCase(routeNameUsage)}ParamsSchema`;
|
|
521
|
-
const dataSchemaName = `${_.camelCase(routeNameUsage)}DataSchema`;
|
|
527
|
+
utils._;
|
|
522
528
|
const allAuxiliaryKeys = /* @__PURE__ */ new Set();
|
|
523
529
|
const paramParts = [];
|
|
524
530
|
const resolvedQueryParams = openApiOperation && (openApiComponentsParameters || openApiOperation.parameters?.length) ? resolveQueryParameters(openApiOperation, openApiComponentsParameters) : [];
|
|
525
|
-
const
|
|
531
|
+
const schemaKeyToContractVarNameFn = (key) => schemaKeyToContractVarName(key, utils, contractSuffix);
|
|
526
532
|
for (const p of inputParams) {
|
|
527
533
|
let expr;
|
|
528
534
|
let refKeys = [];
|
|
@@ -530,14 +536,15 @@ function buildEndpointZodContractsCode(params) {
|
|
|
530
536
|
expr = queryParamsToZodObject(
|
|
531
537
|
resolvedQueryParams,
|
|
532
538
|
componentsSchemas,
|
|
533
|
-
|
|
539
|
+
schemaKeyToContractVarNameFn
|
|
534
540
|
);
|
|
535
541
|
} else {
|
|
536
542
|
const result = typeToZodSchemaWithSchema(
|
|
537
543
|
p.type,
|
|
538
544
|
componentsSchemas,
|
|
539
545
|
utils,
|
|
540
|
-
typeSuffix
|
|
546
|
+
typeSuffix,
|
|
547
|
+
contractSuffix
|
|
541
548
|
);
|
|
542
549
|
expr = result.expr;
|
|
543
550
|
refKeys = result.refs;
|
|
@@ -546,11 +553,17 @@ function buildEndpointZodContractsCode(params) {
|
|
|
546
553
|
const schemaWithOptional = p.optional ? `${expr}.optional()` : expr;
|
|
547
554
|
paramParts.push(`${p.name}: ${schemaWithOptional}`);
|
|
548
555
|
}
|
|
549
|
-
const responseResult = responseSchemaKey && componentsSchemas && responseSchemaKey in componentsSchemas ? schemaKeyToZod(
|
|
556
|
+
const responseResult = responseSchemaKey && componentsSchemas && responseSchemaKey in componentsSchemas ? schemaKeyToZod(
|
|
557
|
+
responseSchemaKey,
|
|
558
|
+
componentsSchemas,
|
|
559
|
+
utils,
|
|
560
|
+
contractSuffix
|
|
561
|
+
) : typeToZodSchemaWithSchema(
|
|
550
562
|
responseDataTypeName,
|
|
551
563
|
componentsSchemas,
|
|
552
564
|
utils,
|
|
553
|
-
typeSuffix
|
|
565
|
+
typeSuffix,
|
|
566
|
+
contractSuffix
|
|
554
567
|
);
|
|
555
568
|
const useDataSchemaFromCentral = useExternalZodSchemas && responseSchemaKey && componentsSchemas && responseSchemaKey in componentsSchemas;
|
|
556
569
|
if (useDataSchemaFromCentral) {
|
|
@@ -558,31 +571,27 @@ function buildEndpointZodContractsCode(params) {
|
|
|
558
571
|
} else {
|
|
559
572
|
for (const k of responseResult.refs) allAuxiliaryKeys.add(k);
|
|
560
573
|
}
|
|
561
|
-
const
|
|
574
|
+
const zodContractImportNames = useExternalZodSchemas && allAuxiliaryKeys.size > 0 ? [...allAuxiliaryKeys].map(schemaKeyToContractVarNameFn) : [];
|
|
562
575
|
const allAuxiliary = useExternalZodSchemas ? [] : generateAuxiliarySchemas(
|
|
563
576
|
[...allAuxiliaryKeys],
|
|
564
577
|
componentsSchemas ?? {},
|
|
565
|
-
|
|
578
|
+
schemaKeyToContractVarNameFn,
|
|
566
579
|
/* @__PURE__ */ new Set()
|
|
567
580
|
);
|
|
568
|
-
const paramsFields = paramParts.join(",\n
|
|
569
|
-
const
|
|
570
|
-
|
|
571
|
-
})
|
|
572
|
-
const
|
|
573
|
-
const contractsCode = `export const ${
|
|
574
|
-
params: ${
|
|
575
|
-
data: ${
|
|
581
|
+
const paramsFields = paramParts.join(",\n ");
|
|
582
|
+
const paramsSchemaExpr = `z.object({
|
|
583
|
+
${paramsFields},
|
|
584
|
+
})`;
|
|
585
|
+
const dataSchemaExpr = useDataSchemaFromCentral ? schemaKeyToContractVarNameFn(responseSchemaKey) : responseResult.expr;
|
|
586
|
+
const contractsCode = `export const ${contractVarName} = {
|
|
587
|
+
params: ${paramsSchemaExpr},
|
|
588
|
+
data: ${dataSchemaExpr},
|
|
576
589
|
};`;
|
|
577
590
|
const auxiliaryBlock = allAuxiliary.length > 0 ? `${allAuxiliary.join("\n\n")}
|
|
578
591
|
|
|
579
592
|
` : "";
|
|
580
|
-
const content = `${auxiliaryBlock}${
|
|
581
|
-
|
|
582
|
-
${dataSchemaCode}
|
|
583
|
-
|
|
584
|
-
${contractsCode}`;
|
|
585
|
-
return { content, zodSchemaImportNames };
|
|
593
|
+
const content = `${auxiliaryBlock}${contractsCode}`;
|
|
594
|
+
return { content, zodContractImportNames };
|
|
586
595
|
}
|
|
587
596
|
const formatGroupNameEnumKey = (groupName, { _ }) => _.upperFirst(_.camelCase(groupName));
|
|
588
597
|
const formatTagNameEnumKey = (tagName, utils) => formatGroupNameEnumKey(tagName, utils);
|
|
@@ -742,10 +751,7 @@ const newEndpointTmpl = ({
|
|
|
742
751
|
}) => {
|
|
743
752
|
const zodContractsIsObject = typeof zodContracts === "object" && zodContracts !== null;
|
|
744
753
|
const hasZodContracts = zodContracts === true || zodContractsIsObject;
|
|
745
|
-
const
|
|
746
|
-
const throwOpt = zodContractsIsObject ? zodContracts.throw : void 0;
|
|
747
|
-
const validateOptObj = validateOpt != null && typeof validateOpt === "object" && !Array.isArray(validateOpt) ? validateOpt : null;
|
|
748
|
-
const throwOptObj = throwOpt != null && typeof throwOpt === "object" && !Array.isArray(throwOpt) ? throwOpt : null;
|
|
754
|
+
const contractSuffix = getZodContractSuffix(zodContracts);
|
|
749
755
|
const { _ } = utils;
|
|
750
756
|
const positiveResponseTypes = route.raw.responsesTypes?.filter(
|
|
751
757
|
(it) => +it.status >= 200 && +it.status < 300 && (!it.typeData || filterTypes(it.typeData))
|
|
@@ -859,7 +865,24 @@ const newEndpointTmpl = ({
|
|
|
859
865
|
});
|
|
860
866
|
const isAllowedInputType = filterTypes(requestInputTypeDc);
|
|
861
867
|
const defaultOkResponseType = positiveResponseTypes?.[0]?.type ?? "unknown";
|
|
862
|
-
const
|
|
868
|
+
const contractVarName = hasZodContracts ? `${_.camelCase(route.routeName.usage)}${contractSuffix}` : null;
|
|
869
|
+
const routeInfoForContracts = contractVarName != null ? {
|
|
870
|
+
operationId: raw.operationId ?? "",
|
|
871
|
+
path: path2,
|
|
872
|
+
method,
|
|
873
|
+
contractName: contractVarName
|
|
874
|
+
} : null;
|
|
875
|
+
const resolveZodContractsMaybeFn = (value) => {
|
|
876
|
+
if (typeof value === "function" && routeInfoForContracts != null) {
|
|
877
|
+
return value(routeInfoForContracts.contractName, routeInfoForContracts);
|
|
878
|
+
}
|
|
879
|
+
return value;
|
|
880
|
+
};
|
|
881
|
+
const validateOpt = zodContractsIsObject ? resolveZodContractsMaybeFn(zodContracts.validate) : zodContracts === true ? true : void 0;
|
|
882
|
+
const throwOpt = zodContractsIsObject ? resolveZodContractsMaybeFn(zodContracts.throw) : void 0;
|
|
883
|
+
const isRuntimeContractsRuleObject = (value) => value != null && typeof value === "object" && !Array.isArray(value);
|
|
884
|
+
const validateOptObj = isRuntimeContractsRuleObject(validateOpt) ? validateOpt : null;
|
|
885
|
+
const throwOptObj = isRuntimeContractsRuleObject(throwOpt) ? throwOpt : null;
|
|
863
886
|
const swaggerSchema = configuration.config?.swaggerSchema ?? configuration?.swaggerSchema;
|
|
864
887
|
const componentsSchemas = swaggerSchema?.components?.schemas;
|
|
865
888
|
let operationFromSpec = null;
|
|
@@ -896,46 +919,36 @@ const newEndpointTmpl = ({
|
|
|
896
919
|
if (candidate in componentsSchemas) responseSchemaKey = candidate;
|
|
897
920
|
}
|
|
898
921
|
}
|
|
899
|
-
const contractsCode = hasZodContracts &&
|
|
922
|
+
const contractsCode = hasZodContracts && contractVarName ? buildEndpointZodContractsCode({
|
|
900
923
|
routeNameUsage: route.routeName.usage,
|
|
901
924
|
inputParams,
|
|
902
925
|
responseDataTypeName: defaultOkResponseType,
|
|
903
|
-
|
|
926
|
+
contractVarName,
|
|
904
927
|
utils,
|
|
905
928
|
componentsSchemas: componentsSchemas ?? void 0,
|
|
906
929
|
typeSuffix: "DC",
|
|
907
930
|
responseSchemaKey: responseSchemaKey ?? void 0,
|
|
908
931
|
useExternalZodSchemas: Boolean(relativePathZodSchemas),
|
|
932
|
+
contractSuffix,
|
|
909
933
|
openApiOperation: operationFromSpec ?? void 0,
|
|
910
934
|
openApiComponentsParameters: swaggerSchema?.components?.parameters ?? void 0,
|
|
911
935
|
queryParamName: queryName
|
|
912
936
|
}) : null;
|
|
913
|
-
const appendRuleOpt = zodContractsIsObject && zodContracts.appendRule != null ? zodContracts.appendRule : null;
|
|
914
|
-
const
|
|
915
|
-
|
|
916
|
-
path: path2,
|
|
917
|
-
method,
|
|
918
|
-
contractName: contractsVarName
|
|
919
|
-
} : null;
|
|
920
|
-
const contractsLine = (() => {
|
|
921
|
-
if (contractsVarName == null) return "";
|
|
937
|
+
const appendRuleOpt = zodContractsIsObject && zodContracts.appendRule != null ? resolveZodContractsMaybeFn(zodContracts.appendRule) : null;
|
|
938
|
+
const contractLine = (() => {
|
|
939
|
+
if (contractVarName == null) return "";
|
|
922
940
|
if (typeof appendRuleOpt === "string")
|
|
923
|
-
return `
|
|
924
|
-
if (
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
routeInfoForAppend
|
|
928
|
-
);
|
|
929
|
-
return include ? `contracts: ${contractsVarName},` : "contracts: undefined,";
|
|
930
|
-
}
|
|
931
|
-
return `contracts: ${contractsVarName},`;
|
|
941
|
+
return `contract: ${appendRuleOpt} ? ${contractVarName} : undefined,`;
|
|
942
|
+
if (appendRuleOpt === false) return "contract: undefined,";
|
|
943
|
+
if (appendRuleOpt === true) return `contract: ${contractVarName},`;
|
|
944
|
+
return `contract: ${contractVarName},`;
|
|
932
945
|
})();
|
|
933
|
-
const
|
|
946
|
+
const validateContractLine = (() => {
|
|
934
947
|
if (validateOpt === void 0) return "";
|
|
935
948
|
if (typeof validateOpt === "string")
|
|
936
|
-
return `
|
|
949
|
+
return `validateContract: ${validateOpt},`;
|
|
937
950
|
if (typeof validateOpt === "boolean")
|
|
938
|
-
return `
|
|
951
|
+
return `validateContract: ${validateOpt},`;
|
|
939
952
|
if (validateOptObj !== null) {
|
|
940
953
|
const parts = [];
|
|
941
954
|
if (validateOptObj.params !== void 0)
|
|
@@ -946,7 +959,7 @@ const newEndpointTmpl = ({
|
|
|
946
959
|
parts.push(
|
|
947
960
|
`data: ${typeof validateOptObj.data === "string" ? validateOptObj.data : validateOptObj.data}`
|
|
948
961
|
);
|
|
949
|
-
return parts.length > 0 ? `
|
|
962
|
+
return parts.length > 0 ? `validateContract: { ${parts.join(", ")} },` : "";
|
|
950
963
|
}
|
|
951
964
|
return "";
|
|
952
965
|
})();
|
|
@@ -968,7 +981,7 @@ const newEndpointTmpl = ({
|
|
|
968
981
|
reservedDataContractNames,
|
|
969
982
|
localModelTypes: isAllowedInputType ? [requestInputTypeDc] : [],
|
|
970
983
|
contractsCode: contractsCode ?? void 0,
|
|
971
|
-
|
|
984
|
+
contractVarName: contractVarName ?? void 0,
|
|
972
985
|
content: `
|
|
973
986
|
new ${importFileParams.endpoint.exportName}<
|
|
974
987
|
${getHttpRequestGenerics()},
|
|
@@ -1001,8 +1014,8 @@ new ${importFileParams.endpoint.exportName}<
|
|
|
1001
1014
|
${groupName ? `group: ${metaInfo ? `Group.${formatGroupNameEnumKey(groupName, utils)}` : `"${groupName}"`},` : ""}
|
|
1002
1015
|
${metaInfo?.namespace ? `namespace,` : ""}
|
|
1003
1016
|
meta: ${requestInfoMeta?.tmplData ?? "{} as any"},
|
|
1004
|
-
${
|
|
1005
|
-
${
|
|
1017
|
+
${contractLine}
|
|
1018
|
+
${validateContractLine}
|
|
1006
1019
|
${throwContractsLine}
|
|
1007
1020
|
},
|
|
1008
1021
|
${importFileParams.queryClient.exportName},
|
|
@@ -1052,17 +1065,17 @@ const allEndpointPerFileTmpl = async (params) => {
|
|
|
1052
1065
|
const hasAnyZodContracts = newEndpointTemplates.some(
|
|
1053
1066
|
(t) => t.contractsCode != null
|
|
1054
1067
|
);
|
|
1055
|
-
const
|
|
1068
|
+
const allZodContractImportNames = /* @__PURE__ */ new Set();
|
|
1056
1069
|
newEndpointTemplates.forEach((t) => {
|
|
1057
1070
|
const c = t.contractsCode;
|
|
1058
|
-
if (c != null && typeof c === "object" && c.
|
|
1059
|
-
for (const n of c.
|
|
1060
|
-
|
|
1071
|
+
if (c != null && typeof c === "object" && c.zodContractImportNames?.length) {
|
|
1072
|
+
for (const n of c.zodContractImportNames) {
|
|
1073
|
+
allZodContractImportNames.add(n);
|
|
1061
1074
|
}
|
|
1062
1075
|
}
|
|
1063
1076
|
});
|
|
1064
1077
|
const zodImportLine = hasAnyZodContracts ? 'import * as z from "zod";' : "";
|
|
1065
|
-
const zodSchemasImportLine =
|
|
1078
|
+
const zodSchemasImportLine = allZodContractImportNames.size && relativePathZodSchemas ? `import { ${[...allZodContractImportNames].sort().join(", ")} } from "${relativePathZodSchemas}";` : "";
|
|
1066
1079
|
const endpointTemplates = await Promise.all(
|
|
1067
1080
|
newEndpointTemplates.map(
|
|
1068
1081
|
async ({
|
|
@@ -1166,7 +1179,7 @@ const allExportsTmpl = async ({
|
|
|
1166
1179
|
}) => {
|
|
1167
1180
|
return await formatTSContent(`${LINTERS_IGNORE}
|
|
1168
1181
|
export * from './data-contracts';
|
|
1169
|
-
${exportSchemas ? " export * from './
|
|
1182
|
+
${exportSchemas ? " export * from './contracts';\n " : ""}${collectedExportFiles.map((fileName) => `export * from './${fileName}';`).join("\n")}
|
|
1170
1183
|
${metaInfo ? 'export * from "./meta-info";' : ""}
|
|
1171
1184
|
`);
|
|
1172
1185
|
};
|
|
@@ -1248,7 +1261,7 @@ const endpointPerFileTmpl = async (params) => {
|
|
|
1248
1261
|
const dataContractImportToken = "/*__DATA_CONTRACT_IMPORTS__*/";
|
|
1249
1262
|
const contractsResult = contractsCode != null && typeof contractsCode === "object" ? contractsCode : null;
|
|
1250
1263
|
const zodImportLine = contractsResult != null ? 'import * as z from "zod";' : "";
|
|
1251
|
-
const zodSchemasImportLine = contractsResult?.
|
|
1264
|
+
const zodSchemasImportLine = contractsResult?.zodContractImportNames?.length && relativePathZodSchemas ? `import { ${contractsResult.zodContractImportNames.join(", ")} } from "${relativePathZodSchemas}";` : "";
|
|
1252
1265
|
const contractsBlock = contractsResult != null ? `
|
|
1253
1266
|
|
|
1254
1267
|
${contractsResult.content}
|
|
@@ -1658,7 +1671,8 @@ const generateApi = async (params) => {
|
|
|
1658
1671
|
};
|
|
1659
1672
|
const reservedDataContractNamesMap = /* @__PURE__ */ new Map();
|
|
1660
1673
|
const componentsSchemasForZod = generated.configuration.config?.swaggerSchema?.components?.schemas ?? generated.configuration.swaggerSchema?.components?.schemas;
|
|
1661
|
-
const
|
|
1674
|
+
const zodContractSuffix = getZodContractSuffix(params.zodContracts);
|
|
1675
|
+
const hasZodContractsFile = (params.zodContracts === true || typeof params.zodContracts === "object" && params.zodContracts != null) && componentsSchemasForZod && typeof componentsSchemasForZod === "object" && Object.keys(componentsSchemasForZod).length > 0;
|
|
1662
1676
|
const collectedExportFilesFromIndexFile = [];
|
|
1663
1677
|
const groupsMap = /* @__PURE__ */ new Map();
|
|
1664
1678
|
const nonEmptyGroups = /* @__PURE__ */ new Set();
|
|
@@ -1681,7 +1695,7 @@ const generateApi = async (params) => {
|
|
|
1681
1695
|
groupNames: [],
|
|
1682
1696
|
namespace
|
|
1683
1697
|
},
|
|
1684
|
-
relativePathZodSchemas:
|
|
1698
|
+
relativePathZodSchemas: hasZodContractsFile ? "../contracts" : null
|
|
1685
1699
|
});
|
|
1686
1700
|
if (Array.isArray(route.raw.tags)) {
|
|
1687
1701
|
route.raw.tags.forEach((tag) => {
|
|
@@ -1726,7 +1740,7 @@ const generateApi = async (params) => {
|
|
|
1726
1740
|
namespace,
|
|
1727
1741
|
groupNames: []
|
|
1728
1742
|
},
|
|
1729
|
-
relativePathZodSchemas:
|
|
1743
|
+
relativePathZodSchemas: hasZodContractsFile ? "./contracts" : null
|
|
1730
1744
|
});
|
|
1731
1745
|
reservedDataContractNames.forEach((name) => {
|
|
1732
1746
|
reservedDataContractNamesMap.set(
|
|
@@ -1799,6 +1813,7 @@ const generateApi = async (params) => {
|
|
|
1799
1813
|
...baseTmplParams,
|
|
1800
1814
|
route,
|
|
1801
1815
|
relativePathDataContracts: "../../data-contracts",
|
|
1816
|
+
relativePathZodSchemas: hasZodContractsFile ? "../../contracts" : null,
|
|
1802
1817
|
groupName,
|
|
1803
1818
|
metaInfo: params.noMetaInfo ? null : {
|
|
1804
1819
|
namespace,
|
|
@@ -1841,6 +1856,7 @@ const generateApi = async (params) => {
|
|
|
1841
1856
|
...baseTmplParams,
|
|
1842
1857
|
routes,
|
|
1843
1858
|
relativePathDataContracts: "../data-contracts",
|
|
1859
|
+
relativePathZodSchemas: hasZodContractsFile ? "../contracts" : null,
|
|
1844
1860
|
groupName,
|
|
1845
1861
|
metaInfo: params.noMetaInfo ? null : {
|
|
1846
1862
|
namespace,
|
|
@@ -1922,20 +1938,21 @@ export * as ${exportGroupName} from './endpoints';
|
|
|
1922
1938
|
withPrefix: false,
|
|
1923
1939
|
content: dataContractsContent
|
|
1924
1940
|
});
|
|
1925
|
-
if (
|
|
1926
|
-
const
|
|
1941
|
+
if (hasZodContractsFile && componentsSchemasForZod) {
|
|
1942
|
+
const contractsTsContent = buildCentralZodContractsFile({
|
|
1927
1943
|
componentsSchemas: componentsSchemasForZod,
|
|
1928
|
-
utils
|
|
1944
|
+
utils,
|
|
1945
|
+
contractSuffix: zodContractSuffix
|
|
1929
1946
|
});
|
|
1930
|
-
const
|
|
1947
|
+
const formattedContractsContent = await generated.formatTSContent(
|
|
1931
1948
|
`${LINTERS_IGNORE}
|
|
1932
|
-
${
|
|
1949
|
+
${contractsTsContent}`
|
|
1933
1950
|
);
|
|
1934
1951
|
codegenFs.createFile({
|
|
1935
1952
|
path: paths.outputDir,
|
|
1936
|
-
fileName: "
|
|
1953
|
+
fileName: "contracts.ts",
|
|
1937
1954
|
withPrefix: false,
|
|
1938
|
-
content:
|
|
1955
|
+
content: formattedContractsContent
|
|
1939
1956
|
});
|
|
1940
1957
|
}
|
|
1941
1958
|
if (metaInfo) {
|
|
@@ -1958,7 +1975,7 @@ ${schemasTsContent}`
|
|
|
1958
1975
|
...baseTmplParams,
|
|
1959
1976
|
collectedExportFiles: collectedExportFilesFromIndexFile,
|
|
1960
1977
|
metaInfo,
|
|
1961
|
-
exportSchemas:
|
|
1978
|
+
exportSchemas: hasZodContractsFile
|
|
1962
1979
|
})
|
|
1963
1980
|
});
|
|
1964
1981
|
if (shouldGenerateBarrelFiles) {
|
|
@@ -1981,7 +1998,7 @@ export * as ${namespace} from './__exports';
|
|
|
1981
1998
|
...baseTmplParams,
|
|
1982
1999
|
collectedExportFiles: collectedExportFilesFromIndexFile,
|
|
1983
2000
|
metaInfo,
|
|
1984
|
-
exportSchemas:
|
|
2001
|
+
exportSchemas: hasZodContractsFile
|
|
1985
2002
|
})
|
|
1986
2003
|
});
|
|
1987
2004
|
}
|