mobx-tanstack-query-api 0.41.0 → 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 +36 -18
- package/cli.cjs.map +1 -1
- package/cli.d.ts +4 -3
- package/cli.js +36 -18
- package/cli.js.map +1 -1
- package/package.json +1 -1
package/cli.d.ts
CHANGED
|
@@ -206,8 +206,9 @@ interface GenerateQueryApiParams {
|
|
|
206
206
|
* - **string**: expression inserted as-is → `contract: <expr> ? <contractVar> : undefined`. E.g. `"process.env.NODE_ENV === \"development\""`.
|
|
207
207
|
* - **function** (contractName, routeInfo) => boolean: at codegen time, if true → `contract: <contractVar>`, if false → `contract: undefined`.
|
|
208
208
|
*
|
|
209
|
-
* Optional `suffix`: suffix for generated Zod
|
|
210
|
-
*
|
|
209
|
+
* Optional `suffix`: suffix for generated shared Zod schema variables.
|
|
210
|
+
* Shared schema names are based on data contract names in camelCase, e.g. `DispatchReceiptDC` -> `dispatchReceiptDc`.
|
|
211
|
+
* Default: `""`.
|
|
211
212
|
*/
|
|
212
213
|
zodContracts?: boolean | {
|
|
213
214
|
validate: MaybeFn<RuntimeExpressionOrBoolean | {
|
|
@@ -229,7 +230,7 @@ interface GenerateQueryApiParams {
|
|
|
229
230
|
contractName: string,
|
|
230
231
|
routeInfo: ZodContractsRouteInfo
|
|
231
232
|
]>;
|
|
232
|
-
/** Suffix for
|
|
233
|
+
/** Suffix for generated shared Zod schema variables. Default: "". */
|
|
233
234
|
suffix?: string;
|
|
234
235
|
};
|
|
235
236
|
}
|
package/cli.js
CHANGED
|
@@ -216,10 +216,15 @@ const createShortModelType = (shortModelType) => {
|
|
|
216
216
|
description: shortModelType.description || ""
|
|
217
217
|
};
|
|
218
218
|
};
|
|
219
|
-
const
|
|
219
|
+
const DEFAULT_DATA_CONTRACT_TYPE_SUFFIX = "DC";
|
|
220
|
+
const DEFAULT_ZOD_CONTRACT_SUFFIX = "";
|
|
221
|
+
const DEFAULT_ENDPOINT_ZOD_CONTRACT_SUFFIX = "Contract";
|
|
220
222
|
function getZodContractSuffix(zodContracts) {
|
|
221
223
|
return (typeof zodContracts === "object" && zodContracts !== null && typeof zodContracts.suffix === "string" ? zodContracts.suffix : void 0) ?? DEFAULT_ZOD_CONTRACT_SUFFIX;
|
|
222
224
|
}
|
|
225
|
+
function getEndpointZodContractSuffix(zodContracts) {
|
|
226
|
+
return (typeof zodContracts === "object" && zodContracts !== null && typeof zodContracts.suffix === "string" ? zodContracts.suffix : void 0) ?? DEFAULT_ENDPOINT_ZOD_CONTRACT_SUFFIX;
|
|
227
|
+
}
|
|
223
228
|
const REF_PREFIX = "#/components/schemas/";
|
|
224
229
|
const REF_PREFIX_PARAMS = "#/components/parameters/";
|
|
225
230
|
function parseRef(ref) {
|
|
@@ -248,7 +253,7 @@ function getResponseSchemaKeyFromOperation(rawOperation) {
|
|
|
248
253
|
if (typeof ref !== "string") return null;
|
|
249
254
|
return parseRef(ref);
|
|
250
255
|
}
|
|
251
|
-
function typeNameToSchemaKey(typeName, typeSuffix =
|
|
256
|
+
function typeNameToSchemaKey(typeName, typeSuffix = DEFAULT_DATA_CONTRACT_TYPE_SUFFIX) {
|
|
252
257
|
const t = typeName.trim();
|
|
253
258
|
if (typeSuffix && t.endsWith(typeSuffix))
|
|
254
259
|
return t.slice(0, -typeSuffix.length);
|
|
@@ -375,9 +380,16 @@ function collectRefs(schema, schemas, out) {
|
|
|
375
380
|
collectRefs(schema.additionalProperties, schemas, out);
|
|
376
381
|
}
|
|
377
382
|
}
|
|
378
|
-
function
|
|
383
|
+
function dataContractTypeNameToZodVarName(typeName, utils, contractSuffix = DEFAULT_ZOD_CONTRACT_SUFFIX) {
|
|
379
384
|
const _ = utils._;
|
|
380
|
-
return `${_.camelCase(
|
|
385
|
+
return `${_.camelCase(typeName)}${contractSuffix}`;
|
|
386
|
+
}
|
|
387
|
+
function schemaKeyToContractVarName(key, utils, contractSuffix = DEFAULT_ZOD_CONTRACT_SUFFIX, typeSuffix = DEFAULT_DATA_CONTRACT_TYPE_SUFFIX) {
|
|
388
|
+
return dataContractTypeNameToZodVarName(
|
|
389
|
+
`${key}${typeSuffix}`,
|
|
390
|
+
utils,
|
|
391
|
+
contractSuffix
|
|
392
|
+
);
|
|
381
393
|
}
|
|
382
394
|
function resolveQueryParameters(operation, componentsParameters) {
|
|
383
395
|
const list = [];
|
|
@@ -445,9 +457,10 @@ function buildCentralZodContractsFile(params) {
|
|
|
445
457
|
const {
|
|
446
458
|
componentsSchemas,
|
|
447
459
|
utils,
|
|
448
|
-
contractSuffix = DEFAULT_ZOD_CONTRACT_SUFFIX
|
|
460
|
+
contractSuffix = DEFAULT_ZOD_CONTRACT_SUFFIX,
|
|
461
|
+
typeSuffix = DEFAULT_DATA_CONTRACT_TYPE_SUFFIX
|
|
449
462
|
} = params;
|
|
450
|
-
const schemaKeyToContractVarNameFn = (key) => schemaKeyToContractVarName(key, utils, contractSuffix);
|
|
463
|
+
const schemaKeyToContractVarNameFn = (key) => schemaKeyToContractVarName(key, utils, contractSuffix, typeSuffix);
|
|
451
464
|
const lines = generateAuxiliarySchemas(
|
|
452
465
|
Object.keys(componentsSchemas),
|
|
453
466
|
componentsSchemas,
|
|
@@ -473,7 +486,7 @@ function typeToZodSchemaFallback(typeStr) {
|
|
|
473
486
|
if (t === "unknown") return "z.unknown()";
|
|
474
487
|
return "z.any()";
|
|
475
488
|
}
|
|
476
|
-
function typeToZodSchemaWithSchema(typeStr, schemas, utils, typeSuffix, contractSuffix = DEFAULT_ZOD_CONTRACT_SUFFIX) {
|
|
489
|
+
function typeToZodSchemaWithSchema(typeStr, schemas, utils, typeSuffix = DEFAULT_DATA_CONTRACT_TYPE_SUFFIX, contractSuffix = DEFAULT_ZOD_CONTRACT_SUFFIX) {
|
|
477
490
|
const t = typeStr.trim();
|
|
478
491
|
if (!schemas || Object.keys(schemas).length === 0) {
|
|
479
492
|
return { expr: typeToZodSchemaFallback(t), refs: [] };
|
|
@@ -486,7 +499,7 @@ function typeToZodSchemaWithSchema(typeStr, schemas, utils, typeSuffix, contract
|
|
|
486
499
|
}
|
|
487
500
|
const refs = /* @__PURE__ */ new Set();
|
|
488
501
|
collectRefs(schema, schemas, refs);
|
|
489
|
-
const schemaKeyToContractVarNameFn = (key) => schemaKeyToContractVarName(key, utils, contractSuffix);
|
|
502
|
+
const schemaKeyToContractVarNameFn = (key) => schemaKeyToContractVarName(key, utils, contractSuffix, typeSuffix);
|
|
490
503
|
const expr = schemaToZodExpr(
|
|
491
504
|
schema,
|
|
492
505
|
schemas,
|
|
@@ -495,12 +508,12 @@ function typeToZodSchemaWithSchema(typeStr, schemas, utils, typeSuffix, contract
|
|
|
495
508
|
);
|
|
496
509
|
return { expr, refs: [...refs] };
|
|
497
510
|
}
|
|
498
|
-
function schemaKeyToZod(schemaKey, schemas, utils, contractSuffix = DEFAULT_ZOD_CONTRACT_SUFFIX) {
|
|
511
|
+
function schemaKeyToZod(schemaKey, schemas, utils, typeSuffix = DEFAULT_DATA_CONTRACT_TYPE_SUFFIX, contractSuffix = DEFAULT_ZOD_CONTRACT_SUFFIX) {
|
|
499
512
|
const schema = schemas[schemaKey];
|
|
500
513
|
if (!schema) return { expr: "z.any()", refs: [] };
|
|
501
514
|
const refs = /* @__PURE__ */ new Set();
|
|
502
515
|
collectRefs(schema, schemas, refs);
|
|
503
|
-
const schemaKeyToContractVarNameFn = (key) => schemaKeyToContractVarName(key, utils, contractSuffix);
|
|
516
|
+
const schemaKeyToContractVarNameFn = (key) => schemaKeyToContractVarName(key, utils, contractSuffix, typeSuffix);
|
|
504
517
|
const expr = schemaToZodExpr(
|
|
505
518
|
schema,
|
|
506
519
|
schemas,
|
|
@@ -517,7 +530,7 @@ function buildEndpointZodContractsCode(params) {
|
|
|
517
530
|
utils,
|
|
518
531
|
contractSuffix = DEFAULT_ZOD_CONTRACT_SUFFIX,
|
|
519
532
|
componentsSchemas = null,
|
|
520
|
-
typeSuffix =
|
|
533
|
+
typeSuffix = DEFAULT_DATA_CONTRACT_TYPE_SUFFIX,
|
|
521
534
|
responseSchemaKey,
|
|
522
535
|
useExternalZodSchemas = false,
|
|
523
536
|
openApiOperation = null,
|
|
@@ -528,7 +541,7 @@ function buildEndpointZodContractsCode(params) {
|
|
|
528
541
|
const allAuxiliaryKeys = /* @__PURE__ */ new Set();
|
|
529
542
|
const paramParts = [];
|
|
530
543
|
const resolvedQueryParams = openApiOperation && (openApiComponentsParameters || openApiOperation.parameters?.length) ? resolveQueryParameters(openApiOperation, openApiComponentsParameters) : [];
|
|
531
|
-
const schemaKeyToContractVarNameFn = (key) => schemaKeyToContractVarName(key, utils, contractSuffix);
|
|
544
|
+
const schemaKeyToContractVarNameFn = (key) => schemaKeyToContractVarName(key, utils, contractSuffix, typeSuffix);
|
|
532
545
|
for (const p of inputParams) {
|
|
533
546
|
let expr;
|
|
534
547
|
let refKeys = [];
|
|
@@ -557,6 +570,7 @@ function buildEndpointZodContractsCode(params) {
|
|
|
557
570
|
responseSchemaKey,
|
|
558
571
|
componentsSchemas,
|
|
559
572
|
utils,
|
|
573
|
+
typeSuffix,
|
|
560
574
|
contractSuffix
|
|
561
575
|
) : typeToZodSchemaWithSchema(
|
|
562
576
|
responseDataTypeName,
|
|
@@ -751,7 +765,8 @@ const newEndpointTmpl = ({
|
|
|
751
765
|
}) => {
|
|
752
766
|
const zodContractsIsObject = typeof zodContracts === "object" && zodContracts !== null;
|
|
753
767
|
const hasZodContracts = zodContracts === true || zodContractsIsObject;
|
|
754
|
-
const
|
|
768
|
+
const sharedContractSuffix = getZodContractSuffix(zodContracts);
|
|
769
|
+
const endpointContractSuffix = getEndpointZodContractSuffix(zodContracts);
|
|
755
770
|
const { _ } = utils;
|
|
756
771
|
const positiveResponseTypes = route.raw.responsesTypes?.filter(
|
|
757
772
|
(it) => +it.status >= 200 && +it.status < 300 && (!it.typeData || filterTypes(it.typeData))
|
|
@@ -865,7 +880,7 @@ const newEndpointTmpl = ({
|
|
|
865
880
|
});
|
|
866
881
|
const isAllowedInputType = filterTypes(requestInputTypeDc);
|
|
867
882
|
const defaultOkResponseType = positiveResponseTypes?.[0]?.type ?? "unknown";
|
|
868
|
-
const contractVarName = hasZodContracts ? `${_.camelCase(route.routeName.usage)}${
|
|
883
|
+
const contractVarName = hasZodContracts ? `${_.camelCase(route.routeName.usage)}${endpointContractSuffix}` : null;
|
|
869
884
|
const routeInfoForContracts = contractVarName != null ? {
|
|
870
885
|
operationId: raw.operationId ?? "",
|
|
871
886
|
path: path2,
|
|
@@ -908,7 +923,10 @@ const newEndpointTmpl = ({
|
|
|
908
923
|
(m) => m.name === defaultOkResponseType
|
|
909
924
|
);
|
|
910
925
|
if (aliasType?.typeIdentifier === "type" && typeof aliasType.content === "string" && /^[A-Za-z0-9_]+$/.test(aliasType.content.trim())) {
|
|
911
|
-
const resolved = typeNameToSchemaKey(
|
|
926
|
+
const resolved = typeNameToSchemaKey(
|
|
927
|
+
aliasType.content.trim(),
|
|
928
|
+
DEFAULT_DATA_CONTRACT_TYPE_SUFFIX
|
|
929
|
+
);
|
|
912
930
|
if (resolved in componentsSchemas) responseSchemaKey = resolved;
|
|
913
931
|
}
|
|
914
932
|
}
|
|
@@ -926,10 +944,10 @@ const newEndpointTmpl = ({
|
|
|
926
944
|
contractVarName,
|
|
927
945
|
utils,
|
|
928
946
|
componentsSchemas: componentsSchemas ?? void 0,
|
|
929
|
-
typeSuffix:
|
|
947
|
+
typeSuffix: DEFAULT_DATA_CONTRACT_TYPE_SUFFIX,
|
|
930
948
|
responseSchemaKey: responseSchemaKey ?? void 0,
|
|
931
949
|
useExternalZodSchemas: Boolean(relativePathZodSchemas),
|
|
932
|
-
contractSuffix,
|
|
950
|
+
contractSuffix: sharedContractSuffix,
|
|
933
951
|
openApiOperation: operationFromSpec ?? void 0,
|
|
934
952
|
openApiComponentsParameters: swaggerSchema?.components?.parameters ?? void 0,
|
|
935
953
|
queryParamName: queryName
|
|
@@ -1506,7 +1524,7 @@ const generateApi = async (params) => {
|
|
|
1506
1524
|
cleanOutput: params.cleanOutput ?? true,
|
|
1507
1525
|
modular: true,
|
|
1508
1526
|
patch: true,
|
|
1509
|
-
typeSuffix:
|
|
1527
|
+
typeSuffix: DEFAULT_DATA_CONTRACT_TYPE_SUFFIX,
|
|
1510
1528
|
disableStrictSSL: false,
|
|
1511
1529
|
singleHttpClient: true,
|
|
1512
1530
|
extractRequestBody: true,
|