mobx-tanstack-query-api 0.40.0 → 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 +122 -112
- package/cli.cjs.map +1 -1
- package/cli.d.ts +33 -18
- package/cli.js +122 -112
- 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
|
@@ -4,7 +4,6 @@ import { cloneDeep } from "lodash-es";
|
|
|
4
4
|
import { generateApi as generateApi$1 } from "swagger-typescript-api";
|
|
5
5
|
import { splitTextByLines } from "yummies/text";
|
|
6
6
|
import { Project, SyntaxKind, Node } from "ts-morph";
|
|
7
|
-
const fs = {};
|
|
8
7
|
const LINTERS_IGNORE = `/* eslint-disable */
|
|
9
8
|
/* tslint:disable */`;
|
|
10
9
|
const buildGenerics = (contract) => {
|
|
@@ -217,6 +216,10 @@ const createShortModelType = (shortModelType) => {
|
|
|
217
216
|
description: shortModelType.description || ""
|
|
218
217
|
};
|
|
219
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
|
+
}
|
|
220
223
|
const REF_PREFIX = "#/components/schemas/";
|
|
221
224
|
const REF_PREFIX_PARAMS = "#/components/parameters/";
|
|
222
225
|
function parseRef(ref) {
|
|
@@ -268,14 +271,14 @@ function schemaToNumber(schema) {
|
|
|
268
271
|
if (schema.maximum != null) n += `.max(${schema.maximum})`;
|
|
269
272
|
return n;
|
|
270
273
|
}
|
|
271
|
-
function schemaToArray(schema, schemas,
|
|
272
|
-
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()";
|
|
273
276
|
let a = `z.array(${items})`;
|
|
274
277
|
if (schema.minItems != null) a += `.min(${schema.minItems})`;
|
|
275
278
|
if (schema.maxItems != null) a += `.max(${schema.maxItems})`;
|
|
276
279
|
return a;
|
|
277
280
|
}
|
|
278
|
-
function schemaToObject(schema, schemas,
|
|
281
|
+
function schemaToObject(schema, schemas, schemaKeyToVarName, visited) {
|
|
279
282
|
if (schema.properties && Object.keys(schema.properties).length > 0) {
|
|
280
283
|
const required = new Set(schema.required ?? []);
|
|
281
284
|
const entries = Object.entries(schema.properties).map(
|
|
@@ -283,7 +286,7 @@ function schemaToObject(schema, schemas, schemaKeyToVarName2, visited) {
|
|
|
283
286
|
const expr = schemaToZodExpr(
|
|
284
287
|
propSchema,
|
|
285
288
|
schemas,
|
|
286
|
-
|
|
289
|
+
schemaKeyToVarName,
|
|
287
290
|
visited
|
|
288
291
|
);
|
|
289
292
|
const optional = !required.has(propName);
|
|
@@ -301,26 +304,26 @@ ${entries.join(",\n")}
|
|
|
301
304
|
const value = schemaToZodExpr(
|
|
302
305
|
schema.additionalProperties,
|
|
303
306
|
schemas,
|
|
304
|
-
|
|
307
|
+
schemaKeyToVarName,
|
|
305
308
|
visited
|
|
306
309
|
);
|
|
307
310
|
return `z.record(z.string(), ${value})`;
|
|
308
311
|
}
|
|
309
312
|
return "z.record(z.string(), z.any())";
|
|
310
313
|
}
|
|
311
|
-
function schemaToZodExpr(schema, schemas,
|
|
314
|
+
function schemaToZodExpr(schema, schemas, schemaKeyToVarName, visited) {
|
|
312
315
|
if (!schema) return "z.any()";
|
|
313
316
|
if (schema.$ref) {
|
|
314
317
|
const key = parseRef(schema.$ref);
|
|
315
318
|
if (key && key in schemas) {
|
|
316
319
|
const isCycle = visited.has(key);
|
|
317
|
-
return isCycle ? `z.lazy((): z.ZodTypeAny => ${
|
|
320
|
+
return isCycle ? `z.lazy((): z.ZodTypeAny => ${schemaKeyToVarName(key)})` : `z.lazy(() => ${schemaKeyToVarName(key)})`;
|
|
318
321
|
}
|
|
319
322
|
return "z.any()";
|
|
320
323
|
}
|
|
321
324
|
if (schema.allOf && schema.allOf.length > 0) {
|
|
322
325
|
const parts = schema.allOf.map(
|
|
323
|
-
(part) => schemaToZodExpr(part, schemas,
|
|
326
|
+
(part) => schemaToZodExpr(part, schemas, schemaKeyToVarName, visited)
|
|
324
327
|
);
|
|
325
328
|
const base2 = parts.length === 1 ? parts[0] : parts.reduce((acc, p) => `z.intersection(${acc}, ${p})`);
|
|
326
329
|
return schema.nullable === true ? `${base2}.nullable()` : base2;
|
|
@@ -339,10 +342,10 @@ function schemaToZodExpr(schema, schemas, schemaKeyToVarName2, visited) {
|
|
|
339
342
|
base = "z.boolean()";
|
|
340
343
|
break;
|
|
341
344
|
case "array":
|
|
342
|
-
base = schemaToArray(schema, schemas,
|
|
345
|
+
base = schemaToArray(schema, schemas, schemaKeyToVarName, visited);
|
|
343
346
|
break;
|
|
344
347
|
case "object":
|
|
345
|
-
base = schemaToObject(schema, schemas,
|
|
348
|
+
base = schemaToObject(schema, schemas, schemaKeyToVarName, visited);
|
|
346
349
|
break;
|
|
347
350
|
default:
|
|
348
351
|
base = "z.any()";
|
|
@@ -372,9 +375,9 @@ function collectRefs(schema, schemas, out) {
|
|
|
372
375
|
collectRefs(schema.additionalProperties, schemas, out);
|
|
373
376
|
}
|
|
374
377
|
}
|
|
375
|
-
function
|
|
378
|
+
function schemaKeyToContractVarName(key, utils, contractSuffix = DEFAULT_ZOD_CONTRACT_SUFFIX) {
|
|
376
379
|
const _ = utils._;
|
|
377
|
-
return `${_.camelCase(key)}
|
|
380
|
+
return `${_.camelCase(key)}${contractSuffix}`;
|
|
378
381
|
}
|
|
379
382
|
function resolveQueryParameters(operation, componentsParameters) {
|
|
380
383
|
const list = [];
|
|
@@ -403,13 +406,13 @@ function resolveQueryParameters(operation, componentsParameters) {
|
|
|
403
406
|
}
|
|
404
407
|
return list;
|
|
405
408
|
}
|
|
406
|
-
function queryParamsToZodObject(queryParams, schemas,
|
|
409
|
+
function queryParamsToZodObject(queryParams, schemas, schemaKeyToContractVarName2) {
|
|
407
410
|
if (queryParams.length === 0) return "z.object({})";
|
|
408
411
|
const entries = queryParams.map(({ name, required, schema }) => {
|
|
409
412
|
const expr = schemaToZodExpr(
|
|
410
413
|
schema,
|
|
411
414
|
schemas,
|
|
412
|
-
|
|
415
|
+
schemaKeyToContractVarName2,
|
|
413
416
|
/* @__PURE__ */ new Set()
|
|
414
417
|
);
|
|
415
418
|
const field = required ? expr : `${expr}.optional()`;
|
|
@@ -419,32 +422,36 @@ function queryParamsToZodObject(queryParams, schemas, schemaKeyToVarName2) {
|
|
|
419
422
|
${entries.join(",\n")}
|
|
420
423
|
})`;
|
|
421
424
|
}
|
|
422
|
-
function generateAuxiliarySchemas(schemaKeys, schemas,
|
|
425
|
+
function generateAuxiliarySchemas(schemaKeys, schemas, schemaKeyToContractVarNameFn, visited) {
|
|
423
426
|
const lines = [];
|
|
424
427
|
for (const key of schemaKeys) {
|
|
425
428
|
if (visited.has(key)) continue;
|
|
426
429
|
visited.add(key);
|
|
427
430
|
const schema = schemas[key];
|
|
428
431
|
if (!schema) continue;
|
|
429
|
-
const varName =
|
|
432
|
+
const varName = schemaKeyToContractVarNameFn(key);
|
|
430
433
|
const cyclePath = /* @__PURE__ */ new Set([key]);
|
|
431
434
|
const expr = schemaToZodExpr(
|
|
432
435
|
schema,
|
|
433
436
|
schemas,
|
|
434
|
-
|
|
437
|
+
schemaKeyToContractVarNameFn,
|
|
435
438
|
cyclePath
|
|
436
439
|
);
|
|
437
440
|
lines.push(`export const ${varName} = ${expr};`);
|
|
438
441
|
}
|
|
439
442
|
return lines;
|
|
440
443
|
}
|
|
441
|
-
function
|
|
442
|
-
const {
|
|
443
|
-
|
|
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);
|
|
444
451
|
const lines = generateAuxiliarySchemas(
|
|
445
452
|
Object.keys(componentsSchemas),
|
|
446
453
|
componentsSchemas,
|
|
447
|
-
|
|
454
|
+
schemaKeyToContractVarNameFn,
|
|
448
455
|
/* @__PURE__ */ new Set()
|
|
449
456
|
);
|
|
450
457
|
return `import * as z from "zod";
|
|
@@ -466,7 +473,7 @@ function typeToZodSchemaFallback(typeStr) {
|
|
|
466
473
|
if (t === "unknown") return "z.unknown()";
|
|
467
474
|
return "z.any()";
|
|
468
475
|
}
|
|
469
|
-
function typeToZodSchemaWithSchema(typeStr, schemas, utils, typeSuffix) {
|
|
476
|
+
function typeToZodSchemaWithSchema(typeStr, schemas, utils, typeSuffix, contractSuffix = DEFAULT_ZOD_CONTRACT_SUFFIX) {
|
|
470
477
|
const t = typeStr.trim();
|
|
471
478
|
if (!schemas || Object.keys(schemas).length === 0) {
|
|
472
479
|
return { expr: typeToZodSchemaFallback(t), refs: [] };
|
|
@@ -479,36 +486,36 @@ function typeToZodSchemaWithSchema(typeStr, schemas, utils, typeSuffix) {
|
|
|
479
486
|
}
|
|
480
487
|
const refs = /* @__PURE__ */ new Set();
|
|
481
488
|
collectRefs(schema, schemas, refs);
|
|
482
|
-
const
|
|
489
|
+
const schemaKeyToContractVarNameFn = (key) => schemaKeyToContractVarName(key, utils, contractSuffix);
|
|
483
490
|
const expr = schemaToZodExpr(
|
|
484
491
|
schema,
|
|
485
492
|
schemas,
|
|
486
|
-
|
|
493
|
+
schemaKeyToContractVarNameFn,
|
|
487
494
|
/* @__PURE__ */ new Set()
|
|
488
495
|
);
|
|
489
496
|
return { expr, refs: [...refs] };
|
|
490
497
|
}
|
|
491
|
-
function schemaKeyToZod(schemaKey, schemas, utils) {
|
|
498
|
+
function schemaKeyToZod(schemaKey, schemas, utils, contractSuffix = DEFAULT_ZOD_CONTRACT_SUFFIX) {
|
|
492
499
|
const schema = schemas[schemaKey];
|
|
493
500
|
if (!schema) return { expr: "z.any()", refs: [] };
|
|
494
501
|
const refs = /* @__PURE__ */ new Set();
|
|
495
502
|
collectRefs(schema, schemas, refs);
|
|
496
|
-
const
|
|
503
|
+
const schemaKeyToContractVarNameFn = (key) => schemaKeyToContractVarName(key, utils, contractSuffix);
|
|
497
504
|
const expr = schemaToZodExpr(
|
|
498
505
|
schema,
|
|
499
506
|
schemas,
|
|
500
|
-
|
|
507
|
+
schemaKeyToContractVarNameFn,
|
|
501
508
|
/* @__PURE__ */ new Set()
|
|
502
509
|
);
|
|
503
510
|
return { expr, refs: [...refs] };
|
|
504
511
|
}
|
|
505
512
|
function buildEndpointZodContractsCode(params) {
|
|
506
513
|
const {
|
|
507
|
-
routeNameUsage,
|
|
508
514
|
inputParams,
|
|
509
515
|
responseDataTypeName,
|
|
510
|
-
|
|
516
|
+
contractVarName,
|
|
511
517
|
utils,
|
|
518
|
+
contractSuffix = DEFAULT_ZOD_CONTRACT_SUFFIX,
|
|
512
519
|
componentsSchemas = null,
|
|
513
520
|
typeSuffix = "DC",
|
|
514
521
|
responseSchemaKey,
|
|
@@ -517,13 +524,11 @@ function buildEndpointZodContractsCode(params) {
|
|
|
517
524
|
openApiComponentsParameters = null,
|
|
518
525
|
queryParamName = "query"
|
|
519
526
|
} = params;
|
|
520
|
-
|
|
521
|
-
const paramsSchemaName = `${_.camelCase(routeNameUsage)}ParamsSchema`;
|
|
522
|
-
const dataSchemaName = `${_.camelCase(routeNameUsage)}DataSchema`;
|
|
527
|
+
utils._;
|
|
523
528
|
const allAuxiliaryKeys = /* @__PURE__ */ new Set();
|
|
524
529
|
const paramParts = [];
|
|
525
530
|
const resolvedQueryParams = openApiOperation && (openApiComponentsParameters || openApiOperation.parameters?.length) ? resolveQueryParameters(openApiOperation, openApiComponentsParameters) : [];
|
|
526
|
-
const
|
|
531
|
+
const schemaKeyToContractVarNameFn = (key) => schemaKeyToContractVarName(key, utils, contractSuffix);
|
|
527
532
|
for (const p of inputParams) {
|
|
528
533
|
let expr;
|
|
529
534
|
let refKeys = [];
|
|
@@ -531,14 +536,15 @@ function buildEndpointZodContractsCode(params) {
|
|
|
531
536
|
expr = queryParamsToZodObject(
|
|
532
537
|
resolvedQueryParams,
|
|
533
538
|
componentsSchemas,
|
|
534
|
-
|
|
539
|
+
schemaKeyToContractVarNameFn
|
|
535
540
|
);
|
|
536
541
|
} else {
|
|
537
542
|
const result = typeToZodSchemaWithSchema(
|
|
538
543
|
p.type,
|
|
539
544
|
componentsSchemas,
|
|
540
545
|
utils,
|
|
541
|
-
typeSuffix
|
|
546
|
+
typeSuffix,
|
|
547
|
+
contractSuffix
|
|
542
548
|
);
|
|
543
549
|
expr = result.expr;
|
|
544
550
|
refKeys = result.refs;
|
|
@@ -547,11 +553,17 @@ function buildEndpointZodContractsCode(params) {
|
|
|
547
553
|
const schemaWithOptional = p.optional ? `${expr}.optional()` : expr;
|
|
548
554
|
paramParts.push(`${p.name}: ${schemaWithOptional}`);
|
|
549
555
|
}
|
|
550
|
-
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(
|
|
551
562
|
responseDataTypeName,
|
|
552
563
|
componentsSchemas,
|
|
553
564
|
utils,
|
|
554
|
-
typeSuffix
|
|
565
|
+
typeSuffix,
|
|
566
|
+
contractSuffix
|
|
555
567
|
);
|
|
556
568
|
const useDataSchemaFromCentral = useExternalZodSchemas && responseSchemaKey && componentsSchemas && responseSchemaKey in componentsSchemas;
|
|
557
569
|
if (useDataSchemaFromCentral) {
|
|
@@ -559,31 +571,27 @@ function buildEndpointZodContractsCode(params) {
|
|
|
559
571
|
} else {
|
|
560
572
|
for (const k of responseResult.refs) allAuxiliaryKeys.add(k);
|
|
561
573
|
}
|
|
562
|
-
const
|
|
574
|
+
const zodContractImportNames = useExternalZodSchemas && allAuxiliaryKeys.size > 0 ? [...allAuxiliaryKeys].map(schemaKeyToContractVarNameFn) : [];
|
|
563
575
|
const allAuxiliary = useExternalZodSchemas ? [] : generateAuxiliarySchemas(
|
|
564
576
|
[...allAuxiliaryKeys],
|
|
565
577
|
componentsSchemas ?? {},
|
|
566
|
-
|
|
578
|
+
schemaKeyToContractVarNameFn,
|
|
567
579
|
/* @__PURE__ */ new Set()
|
|
568
580
|
);
|
|
569
|
-
const paramsFields = paramParts.join(",\n
|
|
570
|
-
const
|
|
571
|
-
|
|
572
|
-
})
|
|
573
|
-
const
|
|
574
|
-
const contractsCode = `export const ${
|
|
575
|
-
params: ${
|
|
576
|
-
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},
|
|
577
589
|
};`;
|
|
578
590
|
const auxiliaryBlock = allAuxiliary.length > 0 ? `${allAuxiliary.join("\n\n")}
|
|
579
591
|
|
|
580
592
|
` : "";
|
|
581
|
-
const content = `${auxiliaryBlock}${
|
|
582
|
-
|
|
583
|
-
${dataSchemaCode}
|
|
584
|
-
|
|
585
|
-
${contractsCode}`;
|
|
586
|
-
return { content, zodSchemaImportNames };
|
|
593
|
+
const content = `${auxiliaryBlock}${contractsCode}`;
|
|
594
|
+
return { content, zodContractImportNames };
|
|
587
595
|
}
|
|
588
596
|
const formatGroupNameEnumKey = (groupName, { _ }) => _.upperFirst(_.camelCase(groupName));
|
|
589
597
|
const formatTagNameEnumKey = (tagName, utils) => formatGroupNameEnumKey(tagName, utils);
|
|
@@ -743,10 +751,7 @@ const newEndpointTmpl = ({
|
|
|
743
751
|
}) => {
|
|
744
752
|
const zodContractsIsObject = typeof zodContracts === "object" && zodContracts !== null;
|
|
745
753
|
const hasZodContracts = zodContracts === true || zodContractsIsObject;
|
|
746
|
-
const
|
|
747
|
-
const throwOpt = zodContractsIsObject ? zodContracts.throw : void 0;
|
|
748
|
-
const validateOptObj = validateOpt != null && typeof validateOpt === "object" && !Array.isArray(validateOpt) ? validateOpt : null;
|
|
749
|
-
const throwOptObj = throwOpt != null && typeof throwOpt === "object" && !Array.isArray(throwOpt) ? throwOpt : null;
|
|
754
|
+
const contractSuffix = getZodContractSuffix(zodContracts);
|
|
750
755
|
const { _ } = utils;
|
|
751
756
|
const positiveResponseTypes = route.raw.responsesTypes?.filter(
|
|
752
757
|
(it) => +it.status >= 200 && +it.status < 300 && (!it.typeData || filterTypes(it.typeData))
|
|
@@ -860,7 +865,24 @@ const newEndpointTmpl = ({
|
|
|
860
865
|
});
|
|
861
866
|
const isAllowedInputType = filterTypes(requestInputTypeDc);
|
|
862
867
|
const defaultOkResponseType = positiveResponseTypes?.[0]?.type ?? "unknown";
|
|
863
|
-
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;
|
|
864
886
|
const swaggerSchema = configuration.config?.swaggerSchema ?? configuration?.swaggerSchema;
|
|
865
887
|
const componentsSchemas = swaggerSchema?.components?.schemas;
|
|
866
888
|
let operationFromSpec = null;
|
|
@@ -897,46 +919,36 @@ const newEndpointTmpl = ({
|
|
|
897
919
|
if (candidate in componentsSchemas) responseSchemaKey = candidate;
|
|
898
920
|
}
|
|
899
921
|
}
|
|
900
|
-
const contractsCode = hasZodContracts &&
|
|
922
|
+
const contractsCode = hasZodContracts && contractVarName ? buildEndpointZodContractsCode({
|
|
901
923
|
routeNameUsage: route.routeName.usage,
|
|
902
924
|
inputParams,
|
|
903
925
|
responseDataTypeName: defaultOkResponseType,
|
|
904
|
-
|
|
926
|
+
contractVarName,
|
|
905
927
|
utils,
|
|
906
928
|
componentsSchemas: componentsSchemas ?? void 0,
|
|
907
929
|
typeSuffix: "DC",
|
|
908
930
|
responseSchemaKey: responseSchemaKey ?? void 0,
|
|
909
931
|
useExternalZodSchemas: Boolean(relativePathZodSchemas),
|
|
932
|
+
contractSuffix,
|
|
910
933
|
openApiOperation: operationFromSpec ?? void 0,
|
|
911
934
|
openApiComponentsParameters: swaggerSchema?.components?.parameters ?? void 0,
|
|
912
935
|
queryParamName: queryName
|
|
913
936
|
}) : null;
|
|
914
|
-
const appendRuleOpt = zodContractsIsObject && zodContracts.appendRule != null ? zodContracts.appendRule : null;
|
|
915
|
-
const
|
|
916
|
-
|
|
917
|
-
path: path2,
|
|
918
|
-
method,
|
|
919
|
-
contractName: contractsVarName
|
|
920
|
-
} : null;
|
|
921
|
-
const contractsLine = (() => {
|
|
922
|
-
if (contractsVarName == null) return "";
|
|
937
|
+
const appendRuleOpt = zodContractsIsObject && zodContracts.appendRule != null ? resolveZodContractsMaybeFn(zodContracts.appendRule) : null;
|
|
938
|
+
const contractLine = (() => {
|
|
939
|
+
if (contractVarName == null) return "";
|
|
923
940
|
if (typeof appendRuleOpt === "string")
|
|
924
|
-
return `
|
|
925
|
-
if (
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
routeInfoForAppend
|
|
929
|
-
);
|
|
930
|
-
return include ? `contracts: ${contractsVarName},` : "contracts: undefined,";
|
|
931
|
-
}
|
|
932
|
-
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},`;
|
|
933
945
|
})();
|
|
934
|
-
const
|
|
946
|
+
const validateContractLine = (() => {
|
|
935
947
|
if (validateOpt === void 0) return "";
|
|
936
948
|
if (typeof validateOpt === "string")
|
|
937
|
-
return `
|
|
949
|
+
return `validateContract: ${validateOpt},`;
|
|
938
950
|
if (typeof validateOpt === "boolean")
|
|
939
|
-
return `
|
|
951
|
+
return `validateContract: ${validateOpt},`;
|
|
940
952
|
if (validateOptObj !== null) {
|
|
941
953
|
const parts = [];
|
|
942
954
|
if (validateOptObj.params !== void 0)
|
|
@@ -947,7 +959,7 @@ const newEndpointTmpl = ({
|
|
|
947
959
|
parts.push(
|
|
948
960
|
`data: ${typeof validateOptObj.data === "string" ? validateOptObj.data : validateOptObj.data}`
|
|
949
961
|
);
|
|
950
|
-
return parts.length > 0 ? `
|
|
962
|
+
return parts.length > 0 ? `validateContract: { ${parts.join(", ")} },` : "";
|
|
951
963
|
}
|
|
952
964
|
return "";
|
|
953
965
|
})();
|
|
@@ -969,7 +981,7 @@ const newEndpointTmpl = ({
|
|
|
969
981
|
reservedDataContractNames,
|
|
970
982
|
localModelTypes: isAllowedInputType ? [requestInputTypeDc] : [],
|
|
971
983
|
contractsCode: contractsCode ?? void 0,
|
|
972
|
-
|
|
984
|
+
contractVarName: contractVarName ?? void 0,
|
|
973
985
|
content: `
|
|
974
986
|
new ${importFileParams.endpoint.exportName}<
|
|
975
987
|
${getHttpRequestGenerics()},
|
|
@@ -1002,8 +1014,8 @@ new ${importFileParams.endpoint.exportName}<
|
|
|
1002
1014
|
${groupName ? `group: ${metaInfo ? `Group.${formatGroupNameEnumKey(groupName, utils)}` : `"${groupName}"`},` : ""}
|
|
1003
1015
|
${metaInfo?.namespace ? `namespace,` : ""}
|
|
1004
1016
|
meta: ${requestInfoMeta?.tmplData ?? "{} as any"},
|
|
1005
|
-
${
|
|
1006
|
-
${
|
|
1017
|
+
${contractLine}
|
|
1018
|
+
${validateContractLine}
|
|
1007
1019
|
${throwContractsLine}
|
|
1008
1020
|
},
|
|
1009
1021
|
${importFileParams.queryClient.exportName},
|
|
@@ -1053,17 +1065,17 @@ const allEndpointPerFileTmpl = async (params) => {
|
|
|
1053
1065
|
const hasAnyZodContracts = newEndpointTemplates.some(
|
|
1054
1066
|
(t) => t.contractsCode != null
|
|
1055
1067
|
);
|
|
1056
|
-
const
|
|
1068
|
+
const allZodContractImportNames = /* @__PURE__ */ new Set();
|
|
1057
1069
|
newEndpointTemplates.forEach((t) => {
|
|
1058
1070
|
const c = t.contractsCode;
|
|
1059
|
-
if (c != null && typeof c === "object" && c.
|
|
1060
|
-
for (const n of c.
|
|
1061
|
-
|
|
1071
|
+
if (c != null && typeof c === "object" && c.zodContractImportNames?.length) {
|
|
1072
|
+
for (const n of c.zodContractImportNames) {
|
|
1073
|
+
allZodContractImportNames.add(n);
|
|
1062
1074
|
}
|
|
1063
1075
|
}
|
|
1064
1076
|
});
|
|
1065
1077
|
const zodImportLine = hasAnyZodContracts ? 'import * as z from "zod";' : "";
|
|
1066
|
-
const zodSchemasImportLine =
|
|
1078
|
+
const zodSchemasImportLine = allZodContractImportNames.size && relativePathZodSchemas ? `import { ${[...allZodContractImportNames].sort().join(", ")} } from "${relativePathZodSchemas}";` : "";
|
|
1067
1079
|
const endpointTemplates = await Promise.all(
|
|
1068
1080
|
newEndpointTemplates.map(
|
|
1069
1081
|
async ({
|
|
@@ -1167,7 +1179,7 @@ const allExportsTmpl = async ({
|
|
|
1167
1179
|
}) => {
|
|
1168
1180
|
return await formatTSContent(`${LINTERS_IGNORE}
|
|
1169
1181
|
export * from './data-contracts';
|
|
1170
|
-
${exportSchemas ? " export * from './
|
|
1182
|
+
${exportSchemas ? " export * from './contracts';\n " : ""}${collectedExportFiles.map((fileName) => `export * from './${fileName}';`).join("\n")}
|
|
1171
1183
|
${metaInfo ? 'export * from "./meta-info";' : ""}
|
|
1172
1184
|
`);
|
|
1173
1185
|
};
|
|
@@ -1249,7 +1261,7 @@ const endpointPerFileTmpl = async (params) => {
|
|
|
1249
1261
|
const dataContractImportToken = "/*__DATA_CONTRACT_IMPORTS__*/";
|
|
1250
1262
|
const contractsResult = contractsCode != null && typeof contractsCode === "object" ? contractsCode : null;
|
|
1251
1263
|
const zodImportLine = contractsResult != null ? 'import * as z from "zod";' : "";
|
|
1252
|
-
const zodSchemasImportLine = contractsResult?.
|
|
1264
|
+
const zodSchemasImportLine = contractsResult?.zodContractImportNames?.length && relativePathZodSchemas ? `import { ${contractsResult.zodContractImportNames.join(", ")} } from "${relativePathZodSchemas}";` : "";
|
|
1253
1265
|
const contractsBlock = contractsResult != null ? `
|
|
1254
1266
|
|
|
1255
1267
|
${contractsResult.content}
|
|
@@ -1632,7 +1644,6 @@ const generateApi = async (params) => {
|
|
|
1632
1644
|
}
|
|
1633
1645
|
const codegenFs = codegenProcess.fileSystem;
|
|
1634
1646
|
await Promise.resolve(codegenFs.cleanDir(paths.outputDir));
|
|
1635
|
-
await fs.mkdir(paths.outputDir, { recursive: true });
|
|
1636
1647
|
await Promise.resolve(codegenFs.createDir(paths.outputDir));
|
|
1637
1648
|
const filterTypes = unpackFilterOption(
|
|
1638
1649
|
params.filterTypes,
|
|
@@ -1660,7 +1671,8 @@ const generateApi = async (params) => {
|
|
|
1660
1671
|
};
|
|
1661
1672
|
const reservedDataContractNamesMap = /* @__PURE__ */ new Map();
|
|
1662
1673
|
const componentsSchemasForZod = generated.configuration.config?.swaggerSchema?.components?.schemas ?? generated.configuration.swaggerSchema?.components?.schemas;
|
|
1663
|
-
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;
|
|
1664
1676
|
const collectedExportFilesFromIndexFile = [];
|
|
1665
1677
|
const groupsMap = /* @__PURE__ */ new Map();
|
|
1666
1678
|
const nonEmptyGroups = /* @__PURE__ */ new Set();
|
|
@@ -1668,12 +1680,7 @@ const generateApi = async (params) => {
|
|
|
1668
1680
|
if (params.groupBy == null) {
|
|
1669
1681
|
collectedExportFilesFromIndexFile.push("endpoints");
|
|
1670
1682
|
if (outputType === "one-endpoint-per-file") {
|
|
1671
|
-
|
|
1672
|
-
recursive: true
|
|
1673
|
-
});
|
|
1674
|
-
await Promise.resolve(
|
|
1675
|
-
codegenFs.createDir(path.resolve(params.output, "endpoints"))
|
|
1676
|
-
);
|
|
1683
|
+
codegenFs.createDir(path.resolve(params.output, "endpoints"));
|
|
1677
1684
|
const fileNamesWithRequestInfo = [];
|
|
1678
1685
|
for await (const route of allRoutes) {
|
|
1679
1686
|
const {
|
|
@@ -1688,7 +1695,7 @@ const generateApi = async (params) => {
|
|
|
1688
1695
|
groupNames: [],
|
|
1689
1696
|
namespace
|
|
1690
1697
|
},
|
|
1691
|
-
relativePathZodSchemas:
|
|
1698
|
+
relativePathZodSchemas: hasZodContractsFile ? "../contracts" : null
|
|
1692
1699
|
});
|
|
1693
1700
|
if (Array.isArray(route.raw.tags)) {
|
|
1694
1701
|
route.raw.tags.forEach((tag) => {
|
|
@@ -1733,7 +1740,7 @@ const generateApi = async (params) => {
|
|
|
1733
1740
|
namespace,
|
|
1734
1741
|
groupNames: []
|
|
1735
1742
|
},
|
|
1736
|
-
relativePathZodSchemas:
|
|
1743
|
+
relativePathZodSchemas: hasZodContractsFile ? "./contracts" : null
|
|
1737
1744
|
});
|
|
1738
1745
|
reservedDataContractNames.forEach((name) => {
|
|
1739
1746
|
reservedDataContractNamesMap.set(
|
|
@@ -1806,6 +1813,7 @@ const generateApi = async (params) => {
|
|
|
1806
1813
|
...baseTmplParams,
|
|
1807
1814
|
route,
|
|
1808
1815
|
relativePathDataContracts: "../../data-contracts",
|
|
1816
|
+
relativePathZodSchemas: hasZodContractsFile ? "../../contracts" : null,
|
|
1809
1817
|
groupName,
|
|
1810
1818
|
metaInfo: params.noMetaInfo ? null : {
|
|
1811
1819
|
namespace,
|
|
@@ -1848,6 +1856,7 @@ const generateApi = async (params) => {
|
|
|
1848
1856
|
...baseTmplParams,
|
|
1849
1857
|
routes,
|
|
1850
1858
|
relativePathDataContracts: "../data-contracts",
|
|
1859
|
+
relativePathZodSchemas: hasZodContractsFile ? "../contracts" : null,
|
|
1851
1860
|
groupName,
|
|
1852
1861
|
metaInfo: params.noMetaInfo ? null : {
|
|
1853
1862
|
namespace,
|
|
@@ -1929,20 +1938,21 @@ export * as ${exportGroupName} from './endpoints';
|
|
|
1929
1938
|
withPrefix: false,
|
|
1930
1939
|
content: dataContractsContent
|
|
1931
1940
|
});
|
|
1932
|
-
if (
|
|
1933
|
-
const
|
|
1941
|
+
if (hasZodContractsFile && componentsSchemasForZod) {
|
|
1942
|
+
const contractsTsContent = buildCentralZodContractsFile({
|
|
1934
1943
|
componentsSchemas: componentsSchemasForZod,
|
|
1935
|
-
utils
|
|
1944
|
+
utils,
|
|
1945
|
+
contractSuffix: zodContractSuffix
|
|
1936
1946
|
});
|
|
1937
|
-
const
|
|
1947
|
+
const formattedContractsContent = await generated.formatTSContent(
|
|
1938
1948
|
`${LINTERS_IGNORE}
|
|
1939
|
-
${
|
|
1949
|
+
${contractsTsContent}`
|
|
1940
1950
|
);
|
|
1941
1951
|
codegenFs.createFile({
|
|
1942
1952
|
path: paths.outputDir,
|
|
1943
|
-
fileName: "
|
|
1953
|
+
fileName: "contracts.ts",
|
|
1944
1954
|
withPrefix: false,
|
|
1945
|
-
content:
|
|
1955
|
+
content: formattedContractsContent
|
|
1946
1956
|
});
|
|
1947
1957
|
}
|
|
1948
1958
|
if (metaInfo) {
|
|
@@ -1965,7 +1975,7 @@ ${schemasTsContent}`
|
|
|
1965
1975
|
...baseTmplParams,
|
|
1966
1976
|
collectedExportFiles: collectedExportFilesFromIndexFile,
|
|
1967
1977
|
metaInfo,
|
|
1968
|
-
exportSchemas:
|
|
1978
|
+
exportSchemas: hasZodContractsFile
|
|
1969
1979
|
})
|
|
1970
1980
|
});
|
|
1971
1981
|
if (shouldGenerateBarrelFiles) {
|
|
@@ -1988,7 +1998,7 @@ export * as ${namespace} from './__exports';
|
|
|
1988
1998
|
...baseTmplParams,
|
|
1989
1999
|
collectedExportFiles: collectedExportFilesFromIndexFile,
|
|
1990
2000
|
metaInfo,
|
|
1991
|
-
exportSchemas:
|
|
2001
|
+
exportSchemas: hasZodContractsFile
|
|
1992
2002
|
})
|
|
1993
2003
|
});
|
|
1994
2004
|
}
|