@tahanabavi/typefetch 1.2.2 → 1.4.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/README.md +214 -134
- package/dist/index.d.mts +270 -90
- package/dist/index.d.ts +270 -90
- package/dist/index.js +710 -665
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +709 -666
- package/dist/index.mjs.map +1 -1
- package/package.json +24 -4
package/dist/index.mjs
CHANGED
|
@@ -1,47 +1,8 @@
|
|
|
1
1
|
var __defProp = Object.defineProperty;
|
|
2
|
-
var __defProps = Object.defineProperties;
|
|
3
|
-
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
4
|
-
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
7
|
-
var __pow = Math.pow;
|
|
8
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
9
|
-
var __spreadValues = (a, b) => {
|
|
10
|
-
for (var prop in b || (b = {}))
|
|
11
|
-
if (__hasOwnProp.call(b, prop))
|
|
12
|
-
__defNormalProp(a, prop, b[prop]);
|
|
13
|
-
if (__getOwnPropSymbols)
|
|
14
|
-
for (var prop of __getOwnPropSymbols(b)) {
|
|
15
|
-
if (__propIsEnum.call(b, prop))
|
|
16
|
-
__defNormalProp(a, prop, b[prop]);
|
|
17
|
-
}
|
|
18
|
-
return a;
|
|
19
|
-
};
|
|
20
|
-
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
21
2
|
var __export = (target, all) => {
|
|
22
3
|
for (var name in all)
|
|
23
4
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
24
5
|
};
|
|
25
|
-
var __async = (__this, __arguments, generator) => {
|
|
26
|
-
return new Promise((resolve, reject) => {
|
|
27
|
-
var fulfilled = (value) => {
|
|
28
|
-
try {
|
|
29
|
-
step(generator.next(value));
|
|
30
|
-
} catch (e) {
|
|
31
|
-
reject(e);
|
|
32
|
-
}
|
|
33
|
-
};
|
|
34
|
-
var rejected = (value) => {
|
|
35
|
-
try {
|
|
36
|
-
step(generator.throw(value));
|
|
37
|
-
} catch (e) {
|
|
38
|
-
reject(e);
|
|
39
|
-
}
|
|
40
|
-
};
|
|
41
|
-
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
42
|
-
step((generator = generator.apply(__this, __arguments)).next());
|
|
43
|
-
});
|
|
44
|
-
};
|
|
45
6
|
|
|
46
7
|
// node_modules/zod/v3/external.js
|
|
47
8
|
var external_exports = {};
|
|
@@ -218,7 +179,11 @@ var util;
|
|
|
218
179
|
var objectUtil;
|
|
219
180
|
(function(objectUtil2) {
|
|
220
181
|
objectUtil2.mergeShapes = (first, second) => {
|
|
221
|
-
return
|
|
182
|
+
return {
|
|
183
|
+
...first,
|
|
184
|
+
...second
|
|
185
|
+
// second overwrites first
|
|
186
|
+
};
|
|
222
187
|
};
|
|
223
188
|
})(objectUtil || (objectUtil = {}));
|
|
224
189
|
var ZodParsedType = util.arrayToEnum([
|
|
@@ -519,24 +484,27 @@ function getErrorMap() {
|
|
|
519
484
|
var makeIssue = (params) => {
|
|
520
485
|
const { data, path, errorMaps, issueData } = params;
|
|
521
486
|
const fullPath = [...path, ...issueData.path || []];
|
|
522
|
-
const fullIssue =
|
|
487
|
+
const fullIssue = {
|
|
488
|
+
...issueData,
|
|
523
489
|
path: fullPath
|
|
524
|
-
}
|
|
490
|
+
};
|
|
525
491
|
if (issueData.message !== void 0) {
|
|
526
|
-
return
|
|
492
|
+
return {
|
|
493
|
+
...issueData,
|
|
527
494
|
path: fullPath,
|
|
528
495
|
message: issueData.message
|
|
529
|
-
}
|
|
496
|
+
};
|
|
530
497
|
}
|
|
531
498
|
let errorMessage = "";
|
|
532
499
|
const maps = errorMaps.filter((m) => !!m).slice().reverse();
|
|
533
500
|
for (const map of maps) {
|
|
534
501
|
errorMessage = map(fullIssue, { data, defaultError: errorMessage }).message;
|
|
535
502
|
}
|
|
536
|
-
return
|
|
503
|
+
return {
|
|
504
|
+
...issueData,
|
|
537
505
|
path: fullPath,
|
|
538
506
|
message: errorMessage
|
|
539
|
-
}
|
|
507
|
+
};
|
|
540
508
|
};
|
|
541
509
|
var EMPTY_PATH = [];
|
|
542
510
|
function addIssueToContext(ctx, issueData) {
|
|
@@ -581,19 +549,17 @@ var ParseStatus = class _ParseStatus {
|
|
|
581
549
|
}
|
|
582
550
|
return { status: status.value, value: arrayValue };
|
|
583
551
|
}
|
|
584
|
-
static mergeObjectAsync(status, pairs) {
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
return _ParseStatus.mergeObjectSync(status, syncPairs);
|
|
596
|
-
});
|
|
552
|
+
static async mergeObjectAsync(status, pairs) {
|
|
553
|
+
const syncPairs = [];
|
|
554
|
+
for (const pair of pairs) {
|
|
555
|
+
const key = await pair.key;
|
|
556
|
+
const value = await pair.value;
|
|
557
|
+
syncPairs.push({
|
|
558
|
+
key,
|
|
559
|
+
value
|
|
560
|
+
});
|
|
561
|
+
}
|
|
562
|
+
return _ParseStatus.mergeObjectSync(status, syncPairs);
|
|
597
563
|
}
|
|
598
564
|
static mergeObjectSync(status, pairs) {
|
|
599
565
|
const finalObject = {};
|
|
@@ -628,7 +594,7 @@ var isAsync = (x) => typeof Promise !== "undefined" && x instanceof Promise;
|
|
|
628
594
|
var errorUtil;
|
|
629
595
|
(function(errorUtil2) {
|
|
630
596
|
errorUtil2.errToObj = (message) => typeof message === "string" ? { message } : message || {};
|
|
631
|
-
errorUtil2.toString = (message) => typeof message === "string" ? message : message
|
|
597
|
+
errorUtil2.toString = (message) => typeof message === "string" ? message : message?.message;
|
|
632
598
|
})(errorUtil || (errorUtil = {}));
|
|
633
599
|
|
|
634
600
|
// node_modules/zod/v3/types.js
|
|
@@ -680,17 +646,16 @@ function processCreateParams(params) {
|
|
|
680
646
|
if (errorMap2)
|
|
681
647
|
return { errorMap: errorMap2, description };
|
|
682
648
|
const customMap = (iss, ctx) => {
|
|
683
|
-
var _a, _b;
|
|
684
649
|
const { message } = params;
|
|
685
650
|
if (iss.code === "invalid_enum_value") {
|
|
686
|
-
return { message: message
|
|
651
|
+
return { message: message ?? ctx.defaultError };
|
|
687
652
|
}
|
|
688
653
|
if (typeof ctx.data === "undefined") {
|
|
689
|
-
return { message:
|
|
654
|
+
return { message: message ?? required_error ?? ctx.defaultError };
|
|
690
655
|
}
|
|
691
656
|
if (iss.code !== "invalid_type")
|
|
692
657
|
return { message: ctx.defaultError };
|
|
693
|
-
return { message:
|
|
658
|
+
return { message: message ?? invalid_type_error ?? ctx.defaultError };
|
|
694
659
|
};
|
|
695
660
|
return { errorMap: customMap, description };
|
|
696
661
|
}
|
|
@@ -742,14 +707,13 @@ var ZodType = class {
|
|
|
742
707
|
throw result.error;
|
|
743
708
|
}
|
|
744
709
|
safeParse(data, params) {
|
|
745
|
-
var _a;
|
|
746
710
|
const ctx = {
|
|
747
711
|
common: {
|
|
748
712
|
issues: [],
|
|
749
|
-
async:
|
|
750
|
-
contextualErrorMap: params
|
|
713
|
+
async: params?.async ?? false,
|
|
714
|
+
contextualErrorMap: params?.errorMap
|
|
751
715
|
},
|
|
752
|
-
path:
|
|
716
|
+
path: params?.path || [],
|
|
753
717
|
schemaErrorMap: this._def.errorMap,
|
|
754
718
|
parent: null,
|
|
755
719
|
data,
|
|
@@ -759,7 +723,6 @@ var ZodType = class {
|
|
|
759
723
|
return handleResult(ctx, result);
|
|
760
724
|
}
|
|
761
725
|
"~validate"(data) {
|
|
762
|
-
var _a, _b;
|
|
763
726
|
const ctx = {
|
|
764
727
|
common: {
|
|
765
728
|
issues: [],
|
|
@@ -780,7 +743,7 @@ var ZodType = class {
|
|
|
780
743
|
issues: ctx.common.issues
|
|
781
744
|
};
|
|
782
745
|
} catch (err) {
|
|
783
|
-
if (
|
|
746
|
+
if (err?.message?.toLowerCase()?.includes("encountered")) {
|
|
784
747
|
this["~standard"].async = true;
|
|
785
748
|
}
|
|
786
749
|
ctx.common = {
|
|
@@ -795,32 +758,28 @@ var ZodType = class {
|
|
|
795
758
|
issues: ctx.common.issues
|
|
796
759
|
});
|
|
797
760
|
}
|
|
798
|
-
parseAsync(data, params) {
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
throw result.error;
|
|
804
|
-
});
|
|
761
|
+
async parseAsync(data, params) {
|
|
762
|
+
const result = await this.safeParseAsync(data, params);
|
|
763
|
+
if (result.success)
|
|
764
|
+
return result.data;
|
|
765
|
+
throw result.error;
|
|
805
766
|
}
|
|
806
|
-
safeParseAsync(data, params) {
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
return handleResult(ctx, result);
|
|
823
|
-
});
|
|
767
|
+
async safeParseAsync(data, params) {
|
|
768
|
+
const ctx = {
|
|
769
|
+
common: {
|
|
770
|
+
issues: [],
|
|
771
|
+
contextualErrorMap: params?.errorMap,
|
|
772
|
+
async: true
|
|
773
|
+
},
|
|
774
|
+
path: params?.path || [],
|
|
775
|
+
schemaErrorMap: this._def.errorMap,
|
|
776
|
+
parent: null,
|
|
777
|
+
data,
|
|
778
|
+
parsedType: getParsedType(data)
|
|
779
|
+
};
|
|
780
|
+
const maybeAsyncResult = this._parse({ data, path: ctx.path, parent: ctx });
|
|
781
|
+
const result = await (isAsync(maybeAsyncResult) ? maybeAsyncResult : Promise.resolve(maybeAsyncResult));
|
|
782
|
+
return handleResult(ctx, result);
|
|
824
783
|
}
|
|
825
784
|
refine(check, message) {
|
|
826
785
|
const getIssueProperties = (val) => {
|
|
@@ -834,9 +793,10 @@ var ZodType = class {
|
|
|
834
793
|
};
|
|
835
794
|
return this._refinement((val, ctx) => {
|
|
836
795
|
const result = check(val);
|
|
837
|
-
const setError = () => ctx.addIssue(
|
|
838
|
-
code: ZodIssueCode.custom
|
|
839
|
-
|
|
796
|
+
const setError = () => ctx.addIssue({
|
|
797
|
+
code: ZodIssueCode.custom,
|
|
798
|
+
...getIssueProperties(val)
|
|
799
|
+
});
|
|
840
800
|
if (typeof Promise !== "undefined" && result instanceof Promise) {
|
|
841
801
|
return result.then((data) => {
|
|
842
802
|
if (!data) {
|
|
@@ -930,39 +890,44 @@ var ZodType = class {
|
|
|
930
890
|
return ZodIntersection.create(this, incoming, this._def);
|
|
931
891
|
}
|
|
932
892
|
transform(transform) {
|
|
933
|
-
return new ZodEffects(
|
|
893
|
+
return new ZodEffects({
|
|
894
|
+
...processCreateParams(this._def),
|
|
934
895
|
schema: this,
|
|
935
896
|
typeName: ZodFirstPartyTypeKind.ZodEffects,
|
|
936
897
|
effect: { type: "transform", transform }
|
|
937
|
-
})
|
|
898
|
+
});
|
|
938
899
|
}
|
|
939
900
|
default(def) {
|
|
940
901
|
const defaultValueFunc = typeof def === "function" ? def : () => def;
|
|
941
|
-
return new ZodDefault(
|
|
902
|
+
return new ZodDefault({
|
|
903
|
+
...processCreateParams(this._def),
|
|
942
904
|
innerType: this,
|
|
943
905
|
defaultValue: defaultValueFunc,
|
|
944
906
|
typeName: ZodFirstPartyTypeKind.ZodDefault
|
|
945
|
-
})
|
|
907
|
+
});
|
|
946
908
|
}
|
|
947
909
|
brand() {
|
|
948
|
-
return new ZodBranded(
|
|
910
|
+
return new ZodBranded({
|
|
949
911
|
typeName: ZodFirstPartyTypeKind.ZodBranded,
|
|
950
|
-
type: this
|
|
951
|
-
|
|
912
|
+
type: this,
|
|
913
|
+
...processCreateParams(this._def)
|
|
914
|
+
});
|
|
952
915
|
}
|
|
953
916
|
catch(def) {
|
|
954
917
|
const catchValueFunc = typeof def === "function" ? def : () => def;
|
|
955
|
-
return new ZodCatch(
|
|
918
|
+
return new ZodCatch({
|
|
919
|
+
...processCreateParams(this._def),
|
|
956
920
|
innerType: this,
|
|
957
921
|
catchValue: catchValueFunc,
|
|
958
922
|
typeName: ZodFirstPartyTypeKind.ZodCatch
|
|
959
|
-
})
|
|
923
|
+
});
|
|
960
924
|
}
|
|
961
925
|
describe(description) {
|
|
962
926
|
const This = this.constructor;
|
|
963
|
-
return new This(
|
|
927
|
+
return new This({
|
|
928
|
+
...this._def,
|
|
964
929
|
description
|
|
965
|
-
})
|
|
930
|
+
});
|
|
966
931
|
}
|
|
967
932
|
pipe(target) {
|
|
968
933
|
return ZodPipeline.create(this, target);
|
|
@@ -1037,14 +1002,14 @@ function isValidJWT(jwt, alg) {
|
|
|
1037
1002
|
const decoded = JSON.parse(atob(base64));
|
|
1038
1003
|
if (typeof decoded !== "object" || decoded === null)
|
|
1039
1004
|
return false;
|
|
1040
|
-
if ("typ" in decoded &&
|
|
1005
|
+
if ("typ" in decoded && decoded?.typ !== "JWT")
|
|
1041
1006
|
return false;
|
|
1042
1007
|
if (!decoded.alg)
|
|
1043
1008
|
return false;
|
|
1044
1009
|
if (alg && decoded.alg !== alg)
|
|
1045
1010
|
return false;
|
|
1046
1011
|
return true;
|
|
1047
|
-
} catch
|
|
1012
|
+
} catch {
|
|
1048
1013
|
return false;
|
|
1049
1014
|
}
|
|
1050
1015
|
}
|
|
@@ -1203,7 +1168,7 @@ var ZodString = class _ZodString extends ZodType {
|
|
|
1203
1168
|
} else if (check.kind === "url") {
|
|
1204
1169
|
try {
|
|
1205
1170
|
new URL(input.data);
|
|
1206
|
-
} catch
|
|
1171
|
+
} catch {
|
|
1207
1172
|
ctx = this._getOrReturnCtx(input, ctx);
|
|
1208
1173
|
addIssueToContext(ctx, {
|
|
1209
1174
|
validation: "url",
|
|
@@ -1360,59 +1325,61 @@ var ZodString = class _ZodString extends ZodType {
|
|
|
1360
1325
|
return { status: status.value, value: input.data };
|
|
1361
1326
|
}
|
|
1362
1327
|
_regex(regex, validation, message) {
|
|
1363
|
-
return this.refinement((data) => regex.test(data),
|
|
1328
|
+
return this.refinement((data) => regex.test(data), {
|
|
1364
1329
|
validation,
|
|
1365
|
-
code: ZodIssueCode.invalid_string
|
|
1366
|
-
|
|
1330
|
+
code: ZodIssueCode.invalid_string,
|
|
1331
|
+
...errorUtil.errToObj(message)
|
|
1332
|
+
});
|
|
1367
1333
|
}
|
|
1368
1334
|
_addCheck(check) {
|
|
1369
|
-
return new _ZodString(
|
|
1335
|
+
return new _ZodString({
|
|
1336
|
+
...this._def,
|
|
1370
1337
|
checks: [...this._def.checks, check]
|
|
1371
|
-
})
|
|
1338
|
+
});
|
|
1372
1339
|
}
|
|
1373
1340
|
email(message) {
|
|
1374
|
-
return this._addCheck(
|
|
1341
|
+
return this._addCheck({ kind: "email", ...errorUtil.errToObj(message) });
|
|
1375
1342
|
}
|
|
1376
1343
|
url(message) {
|
|
1377
|
-
return this._addCheck(
|
|
1344
|
+
return this._addCheck({ kind: "url", ...errorUtil.errToObj(message) });
|
|
1378
1345
|
}
|
|
1379
1346
|
emoji(message) {
|
|
1380
|
-
return this._addCheck(
|
|
1347
|
+
return this._addCheck({ kind: "emoji", ...errorUtil.errToObj(message) });
|
|
1381
1348
|
}
|
|
1382
1349
|
uuid(message) {
|
|
1383
|
-
return this._addCheck(
|
|
1350
|
+
return this._addCheck({ kind: "uuid", ...errorUtil.errToObj(message) });
|
|
1384
1351
|
}
|
|
1385
1352
|
nanoid(message) {
|
|
1386
|
-
return this._addCheck(
|
|
1353
|
+
return this._addCheck({ kind: "nanoid", ...errorUtil.errToObj(message) });
|
|
1387
1354
|
}
|
|
1388
1355
|
cuid(message) {
|
|
1389
|
-
return this._addCheck(
|
|
1356
|
+
return this._addCheck({ kind: "cuid", ...errorUtil.errToObj(message) });
|
|
1390
1357
|
}
|
|
1391
1358
|
cuid2(message) {
|
|
1392
|
-
return this._addCheck(
|
|
1359
|
+
return this._addCheck({ kind: "cuid2", ...errorUtil.errToObj(message) });
|
|
1393
1360
|
}
|
|
1394
1361
|
ulid(message) {
|
|
1395
|
-
return this._addCheck(
|
|
1362
|
+
return this._addCheck({ kind: "ulid", ...errorUtil.errToObj(message) });
|
|
1396
1363
|
}
|
|
1397
1364
|
base64(message) {
|
|
1398
|
-
return this._addCheck(
|
|
1365
|
+
return this._addCheck({ kind: "base64", ...errorUtil.errToObj(message) });
|
|
1399
1366
|
}
|
|
1400
1367
|
base64url(message) {
|
|
1401
|
-
return this._addCheck(
|
|
1402
|
-
kind: "base64url"
|
|
1403
|
-
|
|
1368
|
+
return this._addCheck({
|
|
1369
|
+
kind: "base64url",
|
|
1370
|
+
...errorUtil.errToObj(message)
|
|
1371
|
+
});
|
|
1404
1372
|
}
|
|
1405
1373
|
jwt(options) {
|
|
1406
|
-
return this._addCheck(
|
|
1374
|
+
return this._addCheck({ kind: "jwt", ...errorUtil.errToObj(options) });
|
|
1407
1375
|
}
|
|
1408
1376
|
ip(options) {
|
|
1409
|
-
return this._addCheck(
|
|
1377
|
+
return this._addCheck({ kind: "ip", ...errorUtil.errToObj(options) });
|
|
1410
1378
|
}
|
|
1411
1379
|
cidr(options) {
|
|
1412
|
-
return this._addCheck(
|
|
1380
|
+
return this._addCheck({ kind: "cidr", ...errorUtil.errToObj(options) });
|
|
1413
1381
|
}
|
|
1414
1382
|
datetime(options) {
|
|
1415
|
-
var _a, _b;
|
|
1416
1383
|
if (typeof options === "string") {
|
|
1417
1384
|
return this._addCheck({
|
|
1418
1385
|
kind: "datetime",
|
|
@@ -1422,12 +1389,13 @@ var ZodString = class _ZodString extends ZodType {
|
|
|
1422
1389
|
message: options
|
|
1423
1390
|
});
|
|
1424
1391
|
}
|
|
1425
|
-
return this._addCheck(
|
|
1392
|
+
return this._addCheck({
|
|
1426
1393
|
kind: "datetime",
|
|
1427
|
-
precision: typeof
|
|
1428
|
-
offset:
|
|
1429
|
-
local:
|
|
1430
|
-
|
|
1394
|
+
precision: typeof options?.precision === "undefined" ? null : options?.precision,
|
|
1395
|
+
offset: options?.offset ?? false,
|
|
1396
|
+
local: options?.local ?? false,
|
|
1397
|
+
...errorUtil.errToObj(options?.message)
|
|
1398
|
+
});
|
|
1431
1399
|
}
|
|
1432
1400
|
date(message) {
|
|
1433
1401
|
return this._addCheck({ kind: "date", message });
|
|
@@ -1440,56 +1408,64 @@ var ZodString = class _ZodString extends ZodType {
|
|
|
1440
1408
|
message: options
|
|
1441
1409
|
});
|
|
1442
1410
|
}
|
|
1443
|
-
return this._addCheck(
|
|
1411
|
+
return this._addCheck({
|
|
1444
1412
|
kind: "time",
|
|
1445
|
-
precision: typeof
|
|
1446
|
-
|
|
1413
|
+
precision: typeof options?.precision === "undefined" ? null : options?.precision,
|
|
1414
|
+
...errorUtil.errToObj(options?.message)
|
|
1415
|
+
});
|
|
1447
1416
|
}
|
|
1448
1417
|
duration(message) {
|
|
1449
|
-
return this._addCheck(
|
|
1418
|
+
return this._addCheck({ kind: "duration", ...errorUtil.errToObj(message) });
|
|
1450
1419
|
}
|
|
1451
1420
|
regex(regex, message) {
|
|
1452
|
-
return this._addCheck(
|
|
1421
|
+
return this._addCheck({
|
|
1453
1422
|
kind: "regex",
|
|
1454
|
-
regex
|
|
1455
|
-
|
|
1423
|
+
regex,
|
|
1424
|
+
...errorUtil.errToObj(message)
|
|
1425
|
+
});
|
|
1456
1426
|
}
|
|
1457
1427
|
includes(value, options) {
|
|
1458
|
-
return this._addCheck(
|
|
1428
|
+
return this._addCheck({
|
|
1459
1429
|
kind: "includes",
|
|
1460
1430
|
value,
|
|
1461
|
-
position: options
|
|
1462
|
-
|
|
1431
|
+
position: options?.position,
|
|
1432
|
+
...errorUtil.errToObj(options?.message)
|
|
1433
|
+
});
|
|
1463
1434
|
}
|
|
1464
1435
|
startsWith(value, message) {
|
|
1465
|
-
return this._addCheck(
|
|
1436
|
+
return this._addCheck({
|
|
1466
1437
|
kind: "startsWith",
|
|
1467
|
-
value
|
|
1468
|
-
|
|
1438
|
+
value,
|
|
1439
|
+
...errorUtil.errToObj(message)
|
|
1440
|
+
});
|
|
1469
1441
|
}
|
|
1470
1442
|
endsWith(value, message) {
|
|
1471
|
-
return this._addCheck(
|
|
1443
|
+
return this._addCheck({
|
|
1472
1444
|
kind: "endsWith",
|
|
1473
|
-
value
|
|
1474
|
-
|
|
1445
|
+
value,
|
|
1446
|
+
...errorUtil.errToObj(message)
|
|
1447
|
+
});
|
|
1475
1448
|
}
|
|
1476
1449
|
min(minLength, message) {
|
|
1477
|
-
return this._addCheck(
|
|
1450
|
+
return this._addCheck({
|
|
1478
1451
|
kind: "min",
|
|
1479
|
-
value: minLength
|
|
1480
|
-
|
|
1452
|
+
value: minLength,
|
|
1453
|
+
...errorUtil.errToObj(message)
|
|
1454
|
+
});
|
|
1481
1455
|
}
|
|
1482
1456
|
max(maxLength, message) {
|
|
1483
|
-
return this._addCheck(
|
|
1457
|
+
return this._addCheck({
|
|
1484
1458
|
kind: "max",
|
|
1485
|
-
value: maxLength
|
|
1486
|
-
|
|
1459
|
+
value: maxLength,
|
|
1460
|
+
...errorUtil.errToObj(message)
|
|
1461
|
+
});
|
|
1487
1462
|
}
|
|
1488
1463
|
length(len, message) {
|
|
1489
|
-
return this._addCheck(
|
|
1464
|
+
return this._addCheck({
|
|
1490
1465
|
kind: "length",
|
|
1491
|
-
value: len
|
|
1492
|
-
|
|
1466
|
+
value: len,
|
|
1467
|
+
...errorUtil.errToObj(message)
|
|
1468
|
+
});
|
|
1493
1469
|
}
|
|
1494
1470
|
/**
|
|
1495
1471
|
* Equivalent to `.min(1)`
|
|
@@ -1498,19 +1474,22 @@ var ZodString = class _ZodString extends ZodType {
|
|
|
1498
1474
|
return this.min(1, errorUtil.errToObj(message));
|
|
1499
1475
|
}
|
|
1500
1476
|
trim() {
|
|
1501
|
-
return new _ZodString(
|
|
1477
|
+
return new _ZodString({
|
|
1478
|
+
...this._def,
|
|
1502
1479
|
checks: [...this._def.checks, { kind: "trim" }]
|
|
1503
|
-
})
|
|
1480
|
+
});
|
|
1504
1481
|
}
|
|
1505
1482
|
toLowerCase() {
|
|
1506
|
-
return new _ZodString(
|
|
1483
|
+
return new _ZodString({
|
|
1484
|
+
...this._def,
|
|
1507
1485
|
checks: [...this._def.checks, { kind: "toLowerCase" }]
|
|
1508
|
-
})
|
|
1486
|
+
});
|
|
1509
1487
|
}
|
|
1510
1488
|
toUpperCase() {
|
|
1511
|
-
return new _ZodString(
|
|
1489
|
+
return new _ZodString({
|
|
1490
|
+
...this._def,
|
|
1512
1491
|
checks: [...this._def.checks, { kind: "toUpperCase" }]
|
|
1513
|
-
})
|
|
1492
|
+
});
|
|
1514
1493
|
}
|
|
1515
1494
|
get isDatetime() {
|
|
1516
1495
|
return !!this._def.checks.find((ch) => ch.kind === "datetime");
|
|
@@ -1582,12 +1561,12 @@ var ZodString = class _ZodString extends ZodType {
|
|
|
1582
1561
|
}
|
|
1583
1562
|
};
|
|
1584
1563
|
ZodString.create = (params) => {
|
|
1585
|
-
|
|
1586
|
-
return new ZodString(__spreadValues({
|
|
1564
|
+
return new ZodString({
|
|
1587
1565
|
checks: [],
|
|
1588
1566
|
typeName: ZodFirstPartyTypeKind.ZodString,
|
|
1589
|
-
coerce:
|
|
1590
|
-
|
|
1567
|
+
coerce: params?.coerce ?? false,
|
|
1568
|
+
...processCreateParams(params)
|
|
1569
|
+
});
|
|
1591
1570
|
};
|
|
1592
1571
|
function floatSafeRemainder(val, step) {
|
|
1593
1572
|
const valDecCount = (val.toString().split(".")[1] || "").length;
|
|
@@ -1595,7 +1574,7 @@ function floatSafeRemainder(val, step) {
|
|
|
1595
1574
|
const decCount = valDecCount > stepDecCount ? valDecCount : stepDecCount;
|
|
1596
1575
|
const valInt = Number.parseInt(val.toFixed(decCount).replace(".", ""));
|
|
1597
1576
|
const stepInt = Number.parseInt(step.toFixed(decCount).replace(".", ""));
|
|
1598
|
-
return valInt % stepInt /
|
|
1577
|
+
return valInt % stepInt / 10 ** decCount;
|
|
1599
1578
|
}
|
|
1600
1579
|
var ZodNumber = class _ZodNumber extends ZodType {
|
|
1601
1580
|
constructor() {
|
|
@@ -1698,7 +1677,8 @@ var ZodNumber = class _ZodNumber extends ZodType {
|
|
|
1698
1677
|
return this.setLimit("max", value, false, errorUtil.toString(message));
|
|
1699
1678
|
}
|
|
1700
1679
|
setLimit(kind, value, inclusive, message) {
|
|
1701
|
-
return new _ZodNumber(
|
|
1680
|
+
return new _ZodNumber({
|
|
1681
|
+
...this._def,
|
|
1702
1682
|
checks: [
|
|
1703
1683
|
...this._def.checks,
|
|
1704
1684
|
{
|
|
@@ -1708,12 +1688,13 @@ var ZodNumber = class _ZodNumber extends ZodType {
|
|
|
1708
1688
|
message: errorUtil.toString(message)
|
|
1709
1689
|
}
|
|
1710
1690
|
]
|
|
1711
|
-
})
|
|
1691
|
+
});
|
|
1712
1692
|
}
|
|
1713
1693
|
_addCheck(check) {
|
|
1714
|
-
return new _ZodNumber(
|
|
1694
|
+
return new _ZodNumber({
|
|
1695
|
+
...this._def,
|
|
1715
1696
|
checks: [...this._def.checks, check]
|
|
1716
|
-
})
|
|
1697
|
+
});
|
|
1717
1698
|
}
|
|
1718
1699
|
int(message) {
|
|
1719
1700
|
return this._addCheck({
|
|
@@ -1820,11 +1801,12 @@ var ZodNumber = class _ZodNumber extends ZodType {
|
|
|
1820
1801
|
}
|
|
1821
1802
|
};
|
|
1822
1803
|
ZodNumber.create = (params) => {
|
|
1823
|
-
return new ZodNumber(
|
|
1804
|
+
return new ZodNumber({
|
|
1824
1805
|
checks: [],
|
|
1825
1806
|
typeName: ZodFirstPartyTypeKind.ZodNumber,
|
|
1826
|
-
coerce:
|
|
1827
|
-
|
|
1807
|
+
coerce: params?.coerce || false,
|
|
1808
|
+
...processCreateParams(params)
|
|
1809
|
+
});
|
|
1828
1810
|
};
|
|
1829
1811
|
var ZodBigInt = class _ZodBigInt extends ZodType {
|
|
1830
1812
|
constructor() {
|
|
@@ -1836,7 +1818,7 @@ var ZodBigInt = class _ZodBigInt extends ZodType {
|
|
|
1836
1818
|
if (this._def.coerce) {
|
|
1837
1819
|
try {
|
|
1838
1820
|
input.data = BigInt(input.data);
|
|
1839
|
-
} catch
|
|
1821
|
+
} catch {
|
|
1840
1822
|
return this._getInvalidInput(input);
|
|
1841
1823
|
}
|
|
1842
1824
|
}
|
|
@@ -1911,7 +1893,8 @@ var ZodBigInt = class _ZodBigInt extends ZodType {
|
|
|
1911
1893
|
return this.setLimit("max", value, false, errorUtil.toString(message));
|
|
1912
1894
|
}
|
|
1913
1895
|
setLimit(kind, value, inclusive, message) {
|
|
1914
|
-
return new _ZodBigInt(
|
|
1896
|
+
return new _ZodBigInt({
|
|
1897
|
+
...this._def,
|
|
1915
1898
|
checks: [
|
|
1916
1899
|
...this._def.checks,
|
|
1917
1900
|
{
|
|
@@ -1921,12 +1904,13 @@ var ZodBigInt = class _ZodBigInt extends ZodType {
|
|
|
1921
1904
|
message: errorUtil.toString(message)
|
|
1922
1905
|
}
|
|
1923
1906
|
]
|
|
1924
|
-
})
|
|
1907
|
+
});
|
|
1925
1908
|
}
|
|
1926
1909
|
_addCheck(check) {
|
|
1927
|
-
return new _ZodBigInt(
|
|
1910
|
+
return new _ZodBigInt({
|
|
1911
|
+
...this._def,
|
|
1928
1912
|
checks: [...this._def.checks, check]
|
|
1929
|
-
})
|
|
1913
|
+
});
|
|
1930
1914
|
}
|
|
1931
1915
|
positive(message) {
|
|
1932
1916
|
return this._addCheck({
|
|
@@ -1989,12 +1973,12 @@ var ZodBigInt = class _ZodBigInt extends ZodType {
|
|
|
1989
1973
|
}
|
|
1990
1974
|
};
|
|
1991
1975
|
ZodBigInt.create = (params) => {
|
|
1992
|
-
|
|
1993
|
-
return new ZodBigInt(__spreadValues({
|
|
1976
|
+
return new ZodBigInt({
|
|
1994
1977
|
checks: [],
|
|
1995
1978
|
typeName: ZodFirstPartyTypeKind.ZodBigInt,
|
|
1996
|
-
coerce:
|
|
1997
|
-
|
|
1979
|
+
coerce: params?.coerce ?? false,
|
|
1980
|
+
...processCreateParams(params)
|
|
1981
|
+
});
|
|
1998
1982
|
};
|
|
1999
1983
|
var ZodBoolean = class extends ZodType {
|
|
2000
1984
|
_parse(input) {
|
|
@@ -2015,10 +1999,11 @@ var ZodBoolean = class extends ZodType {
|
|
|
2015
1999
|
}
|
|
2016
2000
|
};
|
|
2017
2001
|
ZodBoolean.create = (params) => {
|
|
2018
|
-
return new ZodBoolean(
|
|
2002
|
+
return new ZodBoolean({
|
|
2019
2003
|
typeName: ZodFirstPartyTypeKind.ZodBoolean,
|
|
2020
|
-
coerce:
|
|
2021
|
-
|
|
2004
|
+
coerce: params?.coerce || false,
|
|
2005
|
+
...processCreateParams(params)
|
|
2006
|
+
});
|
|
2022
2007
|
};
|
|
2023
2008
|
var ZodDate = class _ZodDate extends ZodType {
|
|
2024
2009
|
_parse(input) {
|
|
@@ -2081,9 +2066,10 @@ var ZodDate = class _ZodDate extends ZodType {
|
|
|
2081
2066
|
};
|
|
2082
2067
|
}
|
|
2083
2068
|
_addCheck(check) {
|
|
2084
|
-
return new _ZodDate(
|
|
2069
|
+
return new _ZodDate({
|
|
2070
|
+
...this._def,
|
|
2085
2071
|
checks: [...this._def.checks, check]
|
|
2086
|
-
})
|
|
2072
|
+
});
|
|
2087
2073
|
}
|
|
2088
2074
|
min(minDate, message) {
|
|
2089
2075
|
return this._addCheck({
|
|
@@ -2121,11 +2107,12 @@ var ZodDate = class _ZodDate extends ZodType {
|
|
|
2121
2107
|
}
|
|
2122
2108
|
};
|
|
2123
2109
|
ZodDate.create = (params) => {
|
|
2124
|
-
return new ZodDate(
|
|
2110
|
+
return new ZodDate({
|
|
2125
2111
|
checks: [],
|
|
2126
|
-
coerce:
|
|
2127
|
-
typeName: ZodFirstPartyTypeKind.ZodDate
|
|
2128
|
-
|
|
2112
|
+
coerce: params?.coerce || false,
|
|
2113
|
+
typeName: ZodFirstPartyTypeKind.ZodDate,
|
|
2114
|
+
...processCreateParams(params)
|
|
2115
|
+
});
|
|
2129
2116
|
};
|
|
2130
2117
|
var ZodSymbol = class extends ZodType {
|
|
2131
2118
|
_parse(input) {
|
|
@@ -2143,9 +2130,10 @@ var ZodSymbol = class extends ZodType {
|
|
|
2143
2130
|
}
|
|
2144
2131
|
};
|
|
2145
2132
|
ZodSymbol.create = (params) => {
|
|
2146
|
-
return new ZodSymbol(
|
|
2147
|
-
typeName: ZodFirstPartyTypeKind.ZodSymbol
|
|
2148
|
-
|
|
2133
|
+
return new ZodSymbol({
|
|
2134
|
+
typeName: ZodFirstPartyTypeKind.ZodSymbol,
|
|
2135
|
+
...processCreateParams(params)
|
|
2136
|
+
});
|
|
2149
2137
|
};
|
|
2150
2138
|
var ZodUndefined = class extends ZodType {
|
|
2151
2139
|
_parse(input) {
|
|
@@ -2163,9 +2151,10 @@ var ZodUndefined = class extends ZodType {
|
|
|
2163
2151
|
}
|
|
2164
2152
|
};
|
|
2165
2153
|
ZodUndefined.create = (params) => {
|
|
2166
|
-
return new ZodUndefined(
|
|
2167
|
-
typeName: ZodFirstPartyTypeKind.ZodUndefined
|
|
2168
|
-
|
|
2154
|
+
return new ZodUndefined({
|
|
2155
|
+
typeName: ZodFirstPartyTypeKind.ZodUndefined,
|
|
2156
|
+
...processCreateParams(params)
|
|
2157
|
+
});
|
|
2169
2158
|
};
|
|
2170
2159
|
var ZodNull = class extends ZodType {
|
|
2171
2160
|
_parse(input) {
|
|
@@ -2183,9 +2172,10 @@ var ZodNull = class extends ZodType {
|
|
|
2183
2172
|
}
|
|
2184
2173
|
};
|
|
2185
2174
|
ZodNull.create = (params) => {
|
|
2186
|
-
return new ZodNull(
|
|
2187
|
-
typeName: ZodFirstPartyTypeKind.ZodNull
|
|
2188
|
-
|
|
2175
|
+
return new ZodNull({
|
|
2176
|
+
typeName: ZodFirstPartyTypeKind.ZodNull,
|
|
2177
|
+
...processCreateParams(params)
|
|
2178
|
+
});
|
|
2189
2179
|
};
|
|
2190
2180
|
var ZodAny = class extends ZodType {
|
|
2191
2181
|
constructor() {
|
|
@@ -2197,9 +2187,10 @@ var ZodAny = class extends ZodType {
|
|
|
2197
2187
|
}
|
|
2198
2188
|
};
|
|
2199
2189
|
ZodAny.create = (params) => {
|
|
2200
|
-
return new ZodAny(
|
|
2201
|
-
typeName: ZodFirstPartyTypeKind.ZodAny
|
|
2202
|
-
|
|
2190
|
+
return new ZodAny({
|
|
2191
|
+
typeName: ZodFirstPartyTypeKind.ZodAny,
|
|
2192
|
+
...processCreateParams(params)
|
|
2193
|
+
});
|
|
2203
2194
|
};
|
|
2204
2195
|
var ZodUnknown = class extends ZodType {
|
|
2205
2196
|
constructor() {
|
|
@@ -2211,9 +2202,10 @@ var ZodUnknown = class extends ZodType {
|
|
|
2211
2202
|
}
|
|
2212
2203
|
};
|
|
2213
2204
|
ZodUnknown.create = (params) => {
|
|
2214
|
-
return new ZodUnknown(
|
|
2215
|
-
typeName: ZodFirstPartyTypeKind.ZodUnknown
|
|
2216
|
-
|
|
2205
|
+
return new ZodUnknown({
|
|
2206
|
+
typeName: ZodFirstPartyTypeKind.ZodUnknown,
|
|
2207
|
+
...processCreateParams(params)
|
|
2208
|
+
});
|
|
2217
2209
|
};
|
|
2218
2210
|
var ZodNever = class extends ZodType {
|
|
2219
2211
|
_parse(input) {
|
|
@@ -2227,9 +2219,10 @@ var ZodNever = class extends ZodType {
|
|
|
2227
2219
|
}
|
|
2228
2220
|
};
|
|
2229
2221
|
ZodNever.create = (params) => {
|
|
2230
|
-
return new ZodNever(
|
|
2231
|
-
typeName: ZodFirstPartyTypeKind.ZodNever
|
|
2232
|
-
|
|
2222
|
+
return new ZodNever({
|
|
2223
|
+
typeName: ZodFirstPartyTypeKind.ZodNever,
|
|
2224
|
+
...processCreateParams(params)
|
|
2225
|
+
});
|
|
2233
2226
|
};
|
|
2234
2227
|
var ZodVoid = class extends ZodType {
|
|
2235
2228
|
_parse(input) {
|
|
@@ -2247,9 +2240,10 @@ var ZodVoid = class extends ZodType {
|
|
|
2247
2240
|
}
|
|
2248
2241
|
};
|
|
2249
2242
|
ZodVoid.create = (params) => {
|
|
2250
|
-
return new ZodVoid(
|
|
2251
|
-
typeName: ZodFirstPartyTypeKind.ZodVoid
|
|
2252
|
-
|
|
2243
|
+
return new ZodVoid({
|
|
2244
|
+
typeName: ZodFirstPartyTypeKind.ZodVoid,
|
|
2245
|
+
...processCreateParams(params)
|
|
2246
|
+
});
|
|
2253
2247
|
};
|
|
2254
2248
|
var ZodArray = class _ZodArray extends ZodType {
|
|
2255
2249
|
_parse(input) {
|
|
@@ -2321,32 +2315,36 @@ var ZodArray = class _ZodArray extends ZodType {
|
|
|
2321
2315
|
return this._def.type;
|
|
2322
2316
|
}
|
|
2323
2317
|
min(minLength, message) {
|
|
2324
|
-
return new _ZodArray(
|
|
2318
|
+
return new _ZodArray({
|
|
2319
|
+
...this._def,
|
|
2325
2320
|
minLength: { value: minLength, message: errorUtil.toString(message) }
|
|
2326
|
-
})
|
|
2321
|
+
});
|
|
2327
2322
|
}
|
|
2328
2323
|
max(maxLength, message) {
|
|
2329
|
-
return new _ZodArray(
|
|
2324
|
+
return new _ZodArray({
|
|
2325
|
+
...this._def,
|
|
2330
2326
|
maxLength: { value: maxLength, message: errorUtil.toString(message) }
|
|
2331
|
-
})
|
|
2327
|
+
});
|
|
2332
2328
|
}
|
|
2333
2329
|
length(len, message) {
|
|
2334
|
-
return new _ZodArray(
|
|
2330
|
+
return new _ZodArray({
|
|
2331
|
+
...this._def,
|
|
2335
2332
|
exactLength: { value: len, message: errorUtil.toString(message) }
|
|
2336
|
-
})
|
|
2333
|
+
});
|
|
2337
2334
|
}
|
|
2338
2335
|
nonempty(message) {
|
|
2339
2336
|
return this.min(1, message);
|
|
2340
2337
|
}
|
|
2341
2338
|
};
|
|
2342
2339
|
ZodArray.create = (schema, params) => {
|
|
2343
|
-
return new ZodArray(
|
|
2340
|
+
return new ZodArray({
|
|
2344
2341
|
type: schema,
|
|
2345
2342
|
minLength: null,
|
|
2346
2343
|
maxLength: null,
|
|
2347
2344
|
exactLength: null,
|
|
2348
|
-
typeName: ZodFirstPartyTypeKind.ZodArray
|
|
2349
|
-
|
|
2345
|
+
typeName: ZodFirstPartyTypeKind.ZodArray,
|
|
2346
|
+
...processCreateParams(params)
|
|
2347
|
+
});
|
|
2350
2348
|
};
|
|
2351
2349
|
function deepPartialify(schema) {
|
|
2352
2350
|
if (schema instanceof ZodObject) {
|
|
@@ -2355,13 +2353,15 @@ function deepPartialify(schema) {
|
|
|
2355
2353
|
const fieldSchema = schema.shape[key];
|
|
2356
2354
|
newShape[key] = ZodOptional.create(deepPartialify(fieldSchema));
|
|
2357
2355
|
}
|
|
2358
|
-
return new ZodObject(
|
|
2356
|
+
return new ZodObject({
|
|
2357
|
+
...schema._def,
|
|
2359
2358
|
shape: () => newShape
|
|
2360
|
-
})
|
|
2359
|
+
});
|
|
2361
2360
|
} else if (schema instanceof ZodArray) {
|
|
2362
|
-
return new ZodArray(
|
|
2361
|
+
return new ZodArray({
|
|
2362
|
+
...schema._def,
|
|
2363
2363
|
type: deepPartialify(schema.element)
|
|
2364
|
-
})
|
|
2364
|
+
});
|
|
2365
2365
|
} else if (schema instanceof ZodOptional) {
|
|
2366
2366
|
return ZodOptional.create(deepPartialify(schema.unwrap()));
|
|
2367
2367
|
} else if (schema instanceof ZodNullable) {
|
|
@@ -2454,11 +2454,11 @@ var ZodObject = class _ZodObject extends ZodType {
|
|
|
2454
2454
|
}
|
|
2455
2455
|
}
|
|
2456
2456
|
if (ctx.common.async) {
|
|
2457
|
-
return Promise.resolve().then(() =>
|
|
2457
|
+
return Promise.resolve().then(async () => {
|
|
2458
2458
|
const syncPairs = [];
|
|
2459
2459
|
for (const pair of pairs) {
|
|
2460
|
-
const key =
|
|
2461
|
-
const value =
|
|
2460
|
+
const key = await pair.key;
|
|
2461
|
+
const value = await pair.value;
|
|
2462
2462
|
syncPairs.push({
|
|
2463
2463
|
key,
|
|
2464
2464
|
value,
|
|
@@ -2466,7 +2466,7 @@ var ZodObject = class _ZodObject extends ZodType {
|
|
|
2466
2466
|
});
|
|
2467
2467
|
}
|
|
2468
2468
|
return syncPairs;
|
|
2469
|
-
})
|
|
2469
|
+
}).then((syncPairs) => {
|
|
2470
2470
|
return ParseStatus.mergeObjectSync(status, syncPairs);
|
|
2471
2471
|
});
|
|
2472
2472
|
} else {
|
|
@@ -2478,31 +2478,34 @@ var ZodObject = class _ZodObject extends ZodType {
|
|
|
2478
2478
|
}
|
|
2479
2479
|
strict(message) {
|
|
2480
2480
|
errorUtil.errToObj;
|
|
2481
|
-
return new _ZodObject(
|
|
2482
|
-
|
|
2483
|
-
|
|
2484
|
-
|
|
2485
|
-
|
|
2486
|
-
|
|
2487
|
-
|
|
2481
|
+
return new _ZodObject({
|
|
2482
|
+
...this._def,
|
|
2483
|
+
unknownKeys: "strict",
|
|
2484
|
+
...message !== void 0 ? {
|
|
2485
|
+
errorMap: (issue, ctx) => {
|
|
2486
|
+
const defaultError = this._def.errorMap?.(issue, ctx).message ?? ctx.defaultError;
|
|
2487
|
+
if (issue.code === "unrecognized_keys")
|
|
2488
|
+
return {
|
|
2489
|
+
message: errorUtil.errToObj(message).message ?? defaultError
|
|
2490
|
+
};
|
|
2488
2491
|
return {
|
|
2489
|
-
message:
|
|
2492
|
+
message: defaultError
|
|
2490
2493
|
};
|
|
2491
|
-
|
|
2492
|
-
|
|
2493
|
-
|
|
2494
|
-
}
|
|
2495
|
-
} : {}));
|
|
2494
|
+
}
|
|
2495
|
+
} : {}
|
|
2496
|
+
});
|
|
2496
2497
|
}
|
|
2497
2498
|
strip() {
|
|
2498
|
-
return new _ZodObject(
|
|
2499
|
+
return new _ZodObject({
|
|
2500
|
+
...this._def,
|
|
2499
2501
|
unknownKeys: "strip"
|
|
2500
|
-
})
|
|
2502
|
+
});
|
|
2501
2503
|
}
|
|
2502
2504
|
passthrough() {
|
|
2503
|
-
return new _ZodObject(
|
|
2505
|
+
return new _ZodObject({
|
|
2506
|
+
...this._def,
|
|
2504
2507
|
unknownKeys: "passthrough"
|
|
2505
|
-
})
|
|
2508
|
+
});
|
|
2506
2509
|
}
|
|
2507
2510
|
// const AugmentFactory =
|
|
2508
2511
|
// <Def extends ZodObjectDef>(def: Def) =>
|
|
@@ -2522,9 +2525,13 @@ var ZodObject = class _ZodObject extends ZodType {
|
|
|
2522
2525
|
// }) as any;
|
|
2523
2526
|
// };
|
|
2524
2527
|
extend(augmentation) {
|
|
2525
|
-
return new _ZodObject(
|
|
2526
|
-
|
|
2527
|
-
|
|
2528
|
+
return new _ZodObject({
|
|
2529
|
+
...this._def,
|
|
2530
|
+
shape: () => ({
|
|
2531
|
+
...this._def.shape(),
|
|
2532
|
+
...augmentation
|
|
2533
|
+
})
|
|
2534
|
+
});
|
|
2528
2535
|
}
|
|
2529
2536
|
/**
|
|
2530
2537
|
* Prior to zod@1.0.12 there was a bug in the
|
|
@@ -2535,7 +2542,10 @@ var ZodObject = class _ZodObject extends ZodType {
|
|
|
2535
2542
|
const merged = new _ZodObject({
|
|
2536
2543
|
unknownKeys: merging._def.unknownKeys,
|
|
2537
2544
|
catchall: merging._def.catchall,
|
|
2538
|
-
shape: () =>
|
|
2545
|
+
shape: () => ({
|
|
2546
|
+
...this._def.shape(),
|
|
2547
|
+
...merging._def.shape()
|
|
2548
|
+
}),
|
|
2539
2549
|
typeName: ZodFirstPartyTypeKind.ZodObject
|
|
2540
2550
|
});
|
|
2541
2551
|
return merged;
|
|
@@ -2600,9 +2610,10 @@ var ZodObject = class _ZodObject extends ZodType {
|
|
|
2600
2610
|
// return merged;
|
|
2601
2611
|
// }
|
|
2602
2612
|
catchall(index) {
|
|
2603
|
-
return new _ZodObject(
|
|
2613
|
+
return new _ZodObject({
|
|
2614
|
+
...this._def,
|
|
2604
2615
|
catchall: index
|
|
2605
|
-
})
|
|
2616
|
+
});
|
|
2606
2617
|
}
|
|
2607
2618
|
pick(mask) {
|
|
2608
2619
|
const shape = {};
|
|
@@ -2611,9 +2622,10 @@ var ZodObject = class _ZodObject extends ZodType {
|
|
|
2611
2622
|
shape[key] = this.shape[key];
|
|
2612
2623
|
}
|
|
2613
2624
|
}
|
|
2614
|
-
return new _ZodObject(
|
|
2625
|
+
return new _ZodObject({
|
|
2626
|
+
...this._def,
|
|
2615
2627
|
shape: () => shape
|
|
2616
|
-
})
|
|
2628
|
+
});
|
|
2617
2629
|
}
|
|
2618
2630
|
omit(mask) {
|
|
2619
2631
|
const shape = {};
|
|
@@ -2622,9 +2634,10 @@ var ZodObject = class _ZodObject extends ZodType {
|
|
|
2622
2634
|
shape[key] = this.shape[key];
|
|
2623
2635
|
}
|
|
2624
2636
|
}
|
|
2625
|
-
return new _ZodObject(
|
|
2637
|
+
return new _ZodObject({
|
|
2638
|
+
...this._def,
|
|
2626
2639
|
shape: () => shape
|
|
2627
|
-
})
|
|
2640
|
+
});
|
|
2628
2641
|
}
|
|
2629
2642
|
/**
|
|
2630
2643
|
* @deprecated
|
|
@@ -2642,9 +2655,10 @@ var ZodObject = class _ZodObject extends ZodType {
|
|
|
2642
2655
|
newShape[key] = fieldSchema.optional();
|
|
2643
2656
|
}
|
|
2644
2657
|
}
|
|
2645
|
-
return new _ZodObject(
|
|
2658
|
+
return new _ZodObject({
|
|
2659
|
+
...this._def,
|
|
2646
2660
|
shape: () => newShape
|
|
2647
|
-
})
|
|
2661
|
+
});
|
|
2648
2662
|
}
|
|
2649
2663
|
required(mask) {
|
|
2650
2664
|
const newShape = {};
|
|
@@ -2660,37 +2674,41 @@ var ZodObject = class _ZodObject extends ZodType {
|
|
|
2660
2674
|
newShape[key] = newField;
|
|
2661
2675
|
}
|
|
2662
2676
|
}
|
|
2663
|
-
return new _ZodObject(
|
|
2677
|
+
return new _ZodObject({
|
|
2678
|
+
...this._def,
|
|
2664
2679
|
shape: () => newShape
|
|
2665
|
-
})
|
|
2680
|
+
});
|
|
2666
2681
|
}
|
|
2667
2682
|
keyof() {
|
|
2668
2683
|
return createZodEnum(util.objectKeys(this.shape));
|
|
2669
2684
|
}
|
|
2670
2685
|
};
|
|
2671
2686
|
ZodObject.create = (shape, params) => {
|
|
2672
|
-
return new ZodObject(
|
|
2687
|
+
return new ZodObject({
|
|
2673
2688
|
shape: () => shape,
|
|
2674
2689
|
unknownKeys: "strip",
|
|
2675
2690
|
catchall: ZodNever.create(),
|
|
2676
|
-
typeName: ZodFirstPartyTypeKind.ZodObject
|
|
2677
|
-
|
|
2691
|
+
typeName: ZodFirstPartyTypeKind.ZodObject,
|
|
2692
|
+
...processCreateParams(params)
|
|
2693
|
+
});
|
|
2678
2694
|
};
|
|
2679
2695
|
ZodObject.strictCreate = (shape, params) => {
|
|
2680
|
-
return new ZodObject(
|
|
2696
|
+
return new ZodObject({
|
|
2681
2697
|
shape: () => shape,
|
|
2682
2698
|
unknownKeys: "strict",
|
|
2683
2699
|
catchall: ZodNever.create(),
|
|
2684
|
-
typeName: ZodFirstPartyTypeKind.ZodObject
|
|
2685
|
-
|
|
2700
|
+
typeName: ZodFirstPartyTypeKind.ZodObject,
|
|
2701
|
+
...processCreateParams(params)
|
|
2702
|
+
});
|
|
2686
2703
|
};
|
|
2687
2704
|
ZodObject.lazycreate = (shape, params) => {
|
|
2688
|
-
return new ZodObject(
|
|
2705
|
+
return new ZodObject({
|
|
2689
2706
|
shape,
|
|
2690
2707
|
unknownKeys: "strip",
|
|
2691
2708
|
catchall: ZodNever.create(),
|
|
2692
|
-
typeName: ZodFirstPartyTypeKind.ZodObject
|
|
2693
|
-
|
|
2709
|
+
typeName: ZodFirstPartyTypeKind.ZodObject,
|
|
2710
|
+
...processCreateParams(params)
|
|
2711
|
+
});
|
|
2694
2712
|
};
|
|
2695
2713
|
var ZodUnion = class extends ZodType {
|
|
2696
2714
|
_parse(input) {
|
|
@@ -2716,32 +2734,36 @@ var ZodUnion = class extends ZodType {
|
|
|
2716
2734
|
return INVALID;
|
|
2717
2735
|
}
|
|
2718
2736
|
if (ctx.common.async) {
|
|
2719
|
-
return Promise.all(options.map((option) =>
|
|
2720
|
-
const childCtx =
|
|
2721
|
-
|
|
2737
|
+
return Promise.all(options.map(async (option) => {
|
|
2738
|
+
const childCtx = {
|
|
2739
|
+
...ctx,
|
|
2740
|
+
common: {
|
|
2741
|
+
...ctx.common,
|
|
2722
2742
|
issues: []
|
|
2723
|
-
}
|
|
2743
|
+
},
|
|
2724
2744
|
parent: null
|
|
2725
|
-
}
|
|
2745
|
+
};
|
|
2726
2746
|
return {
|
|
2727
|
-
result:
|
|
2747
|
+
result: await option._parseAsync({
|
|
2728
2748
|
data: ctx.data,
|
|
2729
2749
|
path: ctx.path,
|
|
2730
2750
|
parent: childCtx
|
|
2731
2751
|
}),
|
|
2732
2752
|
ctx: childCtx
|
|
2733
2753
|
};
|
|
2734
|
-
}))
|
|
2754
|
+
})).then(handleResults);
|
|
2735
2755
|
} else {
|
|
2736
2756
|
let dirty = void 0;
|
|
2737
2757
|
const issues = [];
|
|
2738
2758
|
for (const option of options) {
|
|
2739
|
-
const childCtx =
|
|
2740
|
-
|
|
2759
|
+
const childCtx = {
|
|
2760
|
+
...ctx,
|
|
2761
|
+
common: {
|
|
2762
|
+
...ctx.common,
|
|
2741
2763
|
issues: []
|
|
2742
|
-
}
|
|
2764
|
+
},
|
|
2743
2765
|
parent: null
|
|
2744
|
-
}
|
|
2766
|
+
};
|
|
2745
2767
|
const result = option._parseSync({
|
|
2746
2768
|
data: ctx.data,
|
|
2747
2769
|
path: ctx.path,
|
|
@@ -2773,10 +2795,11 @@ var ZodUnion = class extends ZodType {
|
|
|
2773
2795
|
}
|
|
2774
2796
|
};
|
|
2775
2797
|
ZodUnion.create = (types, params) => {
|
|
2776
|
-
return new ZodUnion(
|
|
2798
|
+
return new ZodUnion({
|
|
2777
2799
|
options: types,
|
|
2778
|
-
typeName: ZodFirstPartyTypeKind.ZodUnion
|
|
2779
|
-
|
|
2800
|
+
typeName: ZodFirstPartyTypeKind.ZodUnion,
|
|
2801
|
+
...processCreateParams(params)
|
|
2802
|
+
});
|
|
2780
2803
|
};
|
|
2781
2804
|
var getDiscriminator = (type) => {
|
|
2782
2805
|
if (type instanceof ZodLazy) {
|
|
@@ -2876,12 +2899,13 @@ var ZodDiscriminatedUnion = class _ZodDiscriminatedUnion extends ZodType {
|
|
|
2876
2899
|
optionsMap.set(value, type);
|
|
2877
2900
|
}
|
|
2878
2901
|
}
|
|
2879
|
-
return new _ZodDiscriminatedUnion(
|
|
2902
|
+
return new _ZodDiscriminatedUnion({
|
|
2880
2903
|
typeName: ZodFirstPartyTypeKind.ZodDiscriminatedUnion,
|
|
2881
2904
|
discriminator,
|
|
2882
2905
|
options,
|
|
2883
|
-
optionsMap
|
|
2884
|
-
|
|
2906
|
+
optionsMap,
|
|
2907
|
+
...processCreateParams(params)
|
|
2908
|
+
});
|
|
2885
2909
|
}
|
|
2886
2910
|
};
|
|
2887
2911
|
function mergeValues(a, b) {
|
|
@@ -2892,7 +2916,7 @@ function mergeValues(a, b) {
|
|
|
2892
2916
|
} else if (aType === ZodParsedType.object && bType === ZodParsedType.object) {
|
|
2893
2917
|
const bKeys = util.objectKeys(b);
|
|
2894
2918
|
const sharedKeys = util.objectKeys(a).filter((key) => bKeys.indexOf(key) !== -1);
|
|
2895
|
-
const newObj =
|
|
2919
|
+
const newObj = { ...a, ...b };
|
|
2896
2920
|
for (const key of sharedKeys) {
|
|
2897
2921
|
const sharedValue = mergeValues(a[key], b[key]);
|
|
2898
2922
|
if (!sharedValue.valid) {
|
|
@@ -2968,11 +2992,12 @@ var ZodIntersection = class extends ZodType {
|
|
|
2968
2992
|
}
|
|
2969
2993
|
};
|
|
2970
2994
|
ZodIntersection.create = (left, right, params) => {
|
|
2971
|
-
return new ZodIntersection(
|
|
2995
|
+
return new ZodIntersection({
|
|
2972
2996
|
left,
|
|
2973
2997
|
right,
|
|
2974
|
-
typeName: ZodFirstPartyTypeKind.ZodIntersection
|
|
2975
|
-
|
|
2998
|
+
typeName: ZodFirstPartyTypeKind.ZodIntersection,
|
|
2999
|
+
...processCreateParams(params)
|
|
3000
|
+
});
|
|
2976
3001
|
};
|
|
2977
3002
|
var ZodTuple = class _ZodTuple extends ZodType {
|
|
2978
3003
|
_parse(input) {
|
|
@@ -3024,20 +3049,22 @@ var ZodTuple = class _ZodTuple extends ZodType {
|
|
|
3024
3049
|
return this._def.items;
|
|
3025
3050
|
}
|
|
3026
3051
|
rest(rest) {
|
|
3027
|
-
return new _ZodTuple(
|
|
3052
|
+
return new _ZodTuple({
|
|
3053
|
+
...this._def,
|
|
3028
3054
|
rest
|
|
3029
|
-
})
|
|
3055
|
+
});
|
|
3030
3056
|
}
|
|
3031
3057
|
};
|
|
3032
3058
|
ZodTuple.create = (schemas, params) => {
|
|
3033
3059
|
if (!Array.isArray(schemas)) {
|
|
3034
3060
|
throw new Error("You must pass an array of schemas to z.tuple([ ... ])");
|
|
3035
3061
|
}
|
|
3036
|
-
return new ZodTuple(
|
|
3062
|
+
return new ZodTuple({
|
|
3037
3063
|
items: schemas,
|
|
3038
3064
|
typeName: ZodFirstPartyTypeKind.ZodTuple,
|
|
3039
|
-
rest: null
|
|
3040
|
-
|
|
3065
|
+
rest: null,
|
|
3066
|
+
...processCreateParams(params)
|
|
3067
|
+
});
|
|
3041
3068
|
};
|
|
3042
3069
|
var ZodRecord = class _ZodRecord extends ZodType {
|
|
3043
3070
|
get keySchema() {
|
|
@@ -3077,17 +3104,19 @@ var ZodRecord = class _ZodRecord extends ZodType {
|
|
|
3077
3104
|
}
|
|
3078
3105
|
static create(first, second, third) {
|
|
3079
3106
|
if (second instanceof ZodType) {
|
|
3080
|
-
return new _ZodRecord(
|
|
3107
|
+
return new _ZodRecord({
|
|
3081
3108
|
keyType: first,
|
|
3082
3109
|
valueType: second,
|
|
3083
|
-
typeName: ZodFirstPartyTypeKind.ZodRecord
|
|
3084
|
-
|
|
3110
|
+
typeName: ZodFirstPartyTypeKind.ZodRecord,
|
|
3111
|
+
...processCreateParams(third)
|
|
3112
|
+
});
|
|
3085
3113
|
}
|
|
3086
|
-
return new _ZodRecord(
|
|
3114
|
+
return new _ZodRecord({
|
|
3087
3115
|
keyType: ZodString.create(),
|
|
3088
3116
|
valueType: first,
|
|
3089
|
-
typeName: ZodFirstPartyTypeKind.ZodRecord
|
|
3090
|
-
|
|
3117
|
+
typeName: ZodFirstPartyTypeKind.ZodRecord,
|
|
3118
|
+
...processCreateParams(second)
|
|
3119
|
+
});
|
|
3091
3120
|
}
|
|
3092
3121
|
};
|
|
3093
3122
|
var ZodMap = class extends ZodType {
|
|
@@ -3117,10 +3146,10 @@ var ZodMap = class extends ZodType {
|
|
|
3117
3146
|
});
|
|
3118
3147
|
if (ctx.common.async) {
|
|
3119
3148
|
const finalMap = /* @__PURE__ */ new Map();
|
|
3120
|
-
return Promise.resolve().then(() =>
|
|
3149
|
+
return Promise.resolve().then(async () => {
|
|
3121
3150
|
for (const pair of pairs) {
|
|
3122
|
-
const key =
|
|
3123
|
-
const value =
|
|
3151
|
+
const key = await pair.key;
|
|
3152
|
+
const value = await pair.value;
|
|
3124
3153
|
if (key.status === "aborted" || value.status === "aborted") {
|
|
3125
3154
|
return INVALID;
|
|
3126
3155
|
}
|
|
@@ -3130,7 +3159,7 @@ var ZodMap = class extends ZodType {
|
|
|
3130
3159
|
finalMap.set(key.value, value.value);
|
|
3131
3160
|
}
|
|
3132
3161
|
return { status: status.value, value: finalMap };
|
|
3133
|
-
})
|
|
3162
|
+
});
|
|
3134
3163
|
} else {
|
|
3135
3164
|
const finalMap = /* @__PURE__ */ new Map();
|
|
3136
3165
|
for (const pair of pairs) {
|
|
@@ -3149,11 +3178,12 @@ var ZodMap = class extends ZodType {
|
|
|
3149
3178
|
}
|
|
3150
3179
|
};
|
|
3151
3180
|
ZodMap.create = (keyType, valueType, params) => {
|
|
3152
|
-
return new ZodMap(
|
|
3181
|
+
return new ZodMap({
|
|
3153
3182
|
valueType,
|
|
3154
3183
|
keyType,
|
|
3155
|
-
typeName: ZodFirstPartyTypeKind.ZodMap
|
|
3156
|
-
|
|
3184
|
+
typeName: ZodFirstPartyTypeKind.ZodMap,
|
|
3185
|
+
...processCreateParams(params)
|
|
3186
|
+
});
|
|
3157
3187
|
};
|
|
3158
3188
|
var ZodSet = class _ZodSet extends ZodType {
|
|
3159
3189
|
_parse(input) {
|
|
@@ -3213,14 +3243,16 @@ var ZodSet = class _ZodSet extends ZodType {
|
|
|
3213
3243
|
}
|
|
3214
3244
|
}
|
|
3215
3245
|
min(minSize, message) {
|
|
3216
|
-
return new _ZodSet(
|
|
3246
|
+
return new _ZodSet({
|
|
3247
|
+
...this._def,
|
|
3217
3248
|
minSize: { value: minSize, message: errorUtil.toString(message) }
|
|
3218
|
-
})
|
|
3249
|
+
});
|
|
3219
3250
|
}
|
|
3220
3251
|
max(maxSize, message) {
|
|
3221
|
-
return new _ZodSet(
|
|
3252
|
+
return new _ZodSet({
|
|
3253
|
+
...this._def,
|
|
3222
3254
|
maxSize: { value: maxSize, message: errorUtil.toString(message) }
|
|
3223
|
-
})
|
|
3255
|
+
});
|
|
3224
3256
|
}
|
|
3225
3257
|
size(size, message) {
|
|
3226
3258
|
return this.min(size, message).max(size, message);
|
|
@@ -3230,12 +3262,13 @@ var ZodSet = class _ZodSet extends ZodType {
|
|
|
3230
3262
|
}
|
|
3231
3263
|
};
|
|
3232
3264
|
ZodSet.create = (valueType, params) => {
|
|
3233
|
-
return new ZodSet(
|
|
3265
|
+
return new ZodSet({
|
|
3234
3266
|
valueType,
|
|
3235
3267
|
minSize: null,
|
|
3236
3268
|
maxSize: null,
|
|
3237
|
-
typeName: ZodFirstPartyTypeKind.ZodSet
|
|
3238
|
-
|
|
3269
|
+
typeName: ZodFirstPartyTypeKind.ZodSet,
|
|
3270
|
+
...processCreateParams(params)
|
|
3271
|
+
});
|
|
3239
3272
|
};
|
|
3240
3273
|
var ZodFunction = class _ZodFunction extends ZodType {
|
|
3241
3274
|
constructor() {
|
|
@@ -3278,20 +3311,18 @@ var ZodFunction = class _ZodFunction extends ZodType {
|
|
|
3278
3311
|
const fn = ctx.data;
|
|
3279
3312
|
if (this._def.returns instanceof ZodPromise) {
|
|
3280
3313
|
const me = this;
|
|
3281
|
-
return OK(function(...args) {
|
|
3282
|
-
|
|
3283
|
-
|
|
3284
|
-
|
|
3285
|
-
|
|
3286
|
-
|
|
3287
|
-
|
|
3288
|
-
|
|
3289
|
-
|
|
3290
|
-
|
|
3291
|
-
throw error;
|
|
3292
|
-
});
|
|
3293
|
-
return parsedReturns;
|
|
3314
|
+
return OK(async function(...args) {
|
|
3315
|
+
const error = new ZodError([]);
|
|
3316
|
+
const parsedArgs = await me._def.args.parseAsync(args, params).catch((e) => {
|
|
3317
|
+
error.addIssue(makeArgsIssue(args, e));
|
|
3318
|
+
throw error;
|
|
3319
|
+
});
|
|
3320
|
+
const result = await Reflect.apply(fn, this, parsedArgs);
|
|
3321
|
+
const parsedReturns = await me._def.returns._def.type.parseAsync(result, params).catch((e) => {
|
|
3322
|
+
error.addIssue(makeReturnsIssue(result, e));
|
|
3323
|
+
throw error;
|
|
3294
3324
|
});
|
|
3325
|
+
return parsedReturns;
|
|
3295
3326
|
});
|
|
3296
3327
|
} else {
|
|
3297
3328
|
const me = this;
|
|
@@ -3316,14 +3347,16 @@ var ZodFunction = class _ZodFunction extends ZodType {
|
|
|
3316
3347
|
return this._def.returns;
|
|
3317
3348
|
}
|
|
3318
3349
|
args(...items) {
|
|
3319
|
-
return new _ZodFunction(
|
|
3350
|
+
return new _ZodFunction({
|
|
3351
|
+
...this._def,
|
|
3320
3352
|
args: ZodTuple.create(items).rest(ZodUnknown.create())
|
|
3321
|
-
})
|
|
3353
|
+
});
|
|
3322
3354
|
}
|
|
3323
3355
|
returns(returnType) {
|
|
3324
|
-
return new _ZodFunction(
|
|
3356
|
+
return new _ZodFunction({
|
|
3357
|
+
...this._def,
|
|
3325
3358
|
returns: returnType
|
|
3326
|
-
})
|
|
3359
|
+
});
|
|
3327
3360
|
}
|
|
3328
3361
|
implement(func) {
|
|
3329
3362
|
const validatedFunc = this.parse(func);
|
|
@@ -3334,11 +3367,12 @@ var ZodFunction = class _ZodFunction extends ZodType {
|
|
|
3334
3367
|
return validatedFunc;
|
|
3335
3368
|
}
|
|
3336
3369
|
static create(args, returns, params) {
|
|
3337
|
-
return new _ZodFunction(
|
|
3370
|
+
return new _ZodFunction({
|
|
3338
3371
|
args: args ? args : ZodTuple.create([]).rest(ZodUnknown.create()),
|
|
3339
3372
|
returns: returns || ZodUnknown.create(),
|
|
3340
|
-
typeName: ZodFirstPartyTypeKind.ZodFunction
|
|
3341
|
-
|
|
3373
|
+
typeName: ZodFirstPartyTypeKind.ZodFunction,
|
|
3374
|
+
...processCreateParams(params)
|
|
3375
|
+
});
|
|
3342
3376
|
}
|
|
3343
3377
|
};
|
|
3344
3378
|
var ZodLazy = class extends ZodType {
|
|
@@ -3352,10 +3386,11 @@ var ZodLazy = class extends ZodType {
|
|
|
3352
3386
|
}
|
|
3353
3387
|
};
|
|
3354
3388
|
ZodLazy.create = (getter, params) => {
|
|
3355
|
-
return new ZodLazy(
|
|
3389
|
+
return new ZodLazy({
|
|
3356
3390
|
getter,
|
|
3357
|
-
typeName: ZodFirstPartyTypeKind.ZodLazy
|
|
3358
|
-
|
|
3391
|
+
typeName: ZodFirstPartyTypeKind.ZodLazy,
|
|
3392
|
+
...processCreateParams(params)
|
|
3393
|
+
});
|
|
3359
3394
|
};
|
|
3360
3395
|
var ZodLiteral = class extends ZodType {
|
|
3361
3396
|
_parse(input) {
|
|
@@ -3375,16 +3410,18 @@ var ZodLiteral = class extends ZodType {
|
|
|
3375
3410
|
}
|
|
3376
3411
|
};
|
|
3377
3412
|
ZodLiteral.create = (value, params) => {
|
|
3378
|
-
return new ZodLiteral(
|
|
3413
|
+
return new ZodLiteral({
|
|
3379
3414
|
value,
|
|
3380
|
-
typeName: ZodFirstPartyTypeKind.ZodLiteral
|
|
3381
|
-
|
|
3415
|
+
typeName: ZodFirstPartyTypeKind.ZodLiteral,
|
|
3416
|
+
...processCreateParams(params)
|
|
3417
|
+
});
|
|
3382
3418
|
};
|
|
3383
3419
|
function createZodEnum(values, params) {
|
|
3384
|
-
return new ZodEnum(
|
|
3420
|
+
return new ZodEnum({
|
|
3385
3421
|
values,
|
|
3386
|
-
typeName: ZodFirstPartyTypeKind.ZodEnum
|
|
3387
|
-
|
|
3422
|
+
typeName: ZodFirstPartyTypeKind.ZodEnum,
|
|
3423
|
+
...processCreateParams(params)
|
|
3424
|
+
});
|
|
3388
3425
|
}
|
|
3389
3426
|
var ZodEnum = class _ZodEnum extends ZodType {
|
|
3390
3427
|
_parse(input) {
|
|
@@ -3438,10 +3475,16 @@ var ZodEnum = class _ZodEnum extends ZodType {
|
|
|
3438
3475
|
return enumValues;
|
|
3439
3476
|
}
|
|
3440
3477
|
extract(values, newDef = this._def) {
|
|
3441
|
-
return _ZodEnum.create(values,
|
|
3478
|
+
return _ZodEnum.create(values, {
|
|
3479
|
+
...this._def,
|
|
3480
|
+
...newDef
|
|
3481
|
+
});
|
|
3442
3482
|
}
|
|
3443
3483
|
exclude(values, newDef = this._def) {
|
|
3444
|
-
return _ZodEnum.create(this.options.filter((opt) => !values.includes(opt)),
|
|
3484
|
+
return _ZodEnum.create(this.options.filter((opt) => !values.includes(opt)), {
|
|
3485
|
+
...this._def,
|
|
3486
|
+
...newDef
|
|
3487
|
+
});
|
|
3445
3488
|
}
|
|
3446
3489
|
};
|
|
3447
3490
|
ZodEnum.create = createZodEnum;
|
|
@@ -3477,10 +3520,11 @@ var ZodNativeEnum = class extends ZodType {
|
|
|
3477
3520
|
}
|
|
3478
3521
|
};
|
|
3479
3522
|
ZodNativeEnum.create = (values, params) => {
|
|
3480
|
-
return new ZodNativeEnum(
|
|
3523
|
+
return new ZodNativeEnum({
|
|
3481
3524
|
values,
|
|
3482
|
-
typeName: ZodFirstPartyTypeKind.ZodNativeEnum
|
|
3483
|
-
|
|
3525
|
+
typeName: ZodFirstPartyTypeKind.ZodNativeEnum,
|
|
3526
|
+
...processCreateParams(params)
|
|
3527
|
+
});
|
|
3484
3528
|
};
|
|
3485
3529
|
var ZodPromise = class extends ZodType {
|
|
3486
3530
|
unwrap() {
|
|
@@ -3506,10 +3550,11 @@ var ZodPromise = class extends ZodType {
|
|
|
3506
3550
|
}
|
|
3507
3551
|
};
|
|
3508
3552
|
ZodPromise.create = (schema, params) => {
|
|
3509
|
-
return new ZodPromise(
|
|
3553
|
+
return new ZodPromise({
|
|
3510
3554
|
type: schema,
|
|
3511
|
-
typeName: ZodFirstPartyTypeKind.ZodPromise
|
|
3512
|
-
|
|
3555
|
+
typeName: ZodFirstPartyTypeKind.ZodPromise,
|
|
3556
|
+
...processCreateParams(params)
|
|
3557
|
+
});
|
|
3513
3558
|
};
|
|
3514
3559
|
var ZodEffects = class extends ZodType {
|
|
3515
3560
|
innerType() {
|
|
@@ -3538,10 +3583,10 @@ var ZodEffects = class extends ZodType {
|
|
|
3538
3583
|
if (effect.type === "preprocess") {
|
|
3539
3584
|
const processed = effect.transform(ctx.data, checkCtx);
|
|
3540
3585
|
if (ctx.common.async) {
|
|
3541
|
-
return Promise.resolve(processed).then((processed2) =>
|
|
3586
|
+
return Promise.resolve(processed).then(async (processed2) => {
|
|
3542
3587
|
if (status.value === "aborted")
|
|
3543
3588
|
return INVALID;
|
|
3544
|
-
const result =
|
|
3589
|
+
const result = await this._def.schema._parseAsync({
|
|
3545
3590
|
data: processed2,
|
|
3546
3591
|
path: ctx.path,
|
|
3547
3592
|
parent: ctx
|
|
@@ -3553,7 +3598,7 @@ var ZodEffects = class extends ZodType {
|
|
|
3553
3598
|
if (status.value === "dirty")
|
|
3554
3599
|
return DIRTY(result.value);
|
|
3555
3600
|
return result;
|
|
3556
|
-
})
|
|
3601
|
+
});
|
|
3557
3602
|
} else {
|
|
3558
3603
|
if (status.value === "aborted")
|
|
3559
3604
|
return INVALID;
|
|
@@ -3635,18 +3680,20 @@ var ZodEffects = class extends ZodType {
|
|
|
3635
3680
|
}
|
|
3636
3681
|
};
|
|
3637
3682
|
ZodEffects.create = (schema, effect, params) => {
|
|
3638
|
-
return new ZodEffects(
|
|
3683
|
+
return new ZodEffects({
|
|
3639
3684
|
schema,
|
|
3640
3685
|
typeName: ZodFirstPartyTypeKind.ZodEffects,
|
|
3641
|
-
effect
|
|
3642
|
-
|
|
3686
|
+
effect,
|
|
3687
|
+
...processCreateParams(params)
|
|
3688
|
+
});
|
|
3643
3689
|
};
|
|
3644
3690
|
ZodEffects.createWithPreprocess = (preprocess, schema, params) => {
|
|
3645
|
-
return new ZodEffects(
|
|
3691
|
+
return new ZodEffects({
|
|
3646
3692
|
schema,
|
|
3647
3693
|
effect: { type: "preprocess", transform: preprocess },
|
|
3648
|
-
typeName: ZodFirstPartyTypeKind.ZodEffects
|
|
3649
|
-
|
|
3694
|
+
typeName: ZodFirstPartyTypeKind.ZodEffects,
|
|
3695
|
+
...processCreateParams(params)
|
|
3696
|
+
});
|
|
3650
3697
|
};
|
|
3651
3698
|
var ZodOptional = class extends ZodType {
|
|
3652
3699
|
_parse(input) {
|
|
@@ -3661,10 +3708,11 @@ var ZodOptional = class extends ZodType {
|
|
|
3661
3708
|
}
|
|
3662
3709
|
};
|
|
3663
3710
|
ZodOptional.create = (type, params) => {
|
|
3664
|
-
return new ZodOptional(
|
|
3711
|
+
return new ZodOptional({
|
|
3665
3712
|
innerType: type,
|
|
3666
|
-
typeName: ZodFirstPartyTypeKind.ZodOptional
|
|
3667
|
-
|
|
3713
|
+
typeName: ZodFirstPartyTypeKind.ZodOptional,
|
|
3714
|
+
...processCreateParams(params)
|
|
3715
|
+
});
|
|
3668
3716
|
};
|
|
3669
3717
|
var ZodNullable = class extends ZodType {
|
|
3670
3718
|
_parse(input) {
|
|
@@ -3679,10 +3727,11 @@ var ZodNullable = class extends ZodType {
|
|
|
3679
3727
|
}
|
|
3680
3728
|
};
|
|
3681
3729
|
ZodNullable.create = (type, params) => {
|
|
3682
|
-
return new ZodNullable(
|
|
3730
|
+
return new ZodNullable({
|
|
3683
3731
|
innerType: type,
|
|
3684
|
-
typeName: ZodFirstPartyTypeKind.ZodNullable
|
|
3685
|
-
|
|
3732
|
+
typeName: ZodFirstPartyTypeKind.ZodNullable,
|
|
3733
|
+
...processCreateParams(params)
|
|
3734
|
+
});
|
|
3686
3735
|
};
|
|
3687
3736
|
var ZodDefault = class extends ZodType {
|
|
3688
3737
|
_parse(input) {
|
|
@@ -3702,24 +3751,29 @@ var ZodDefault = class extends ZodType {
|
|
|
3702
3751
|
}
|
|
3703
3752
|
};
|
|
3704
3753
|
ZodDefault.create = (type, params) => {
|
|
3705
|
-
return new ZodDefault(
|
|
3754
|
+
return new ZodDefault({
|
|
3706
3755
|
innerType: type,
|
|
3707
3756
|
typeName: ZodFirstPartyTypeKind.ZodDefault,
|
|
3708
|
-
defaultValue: typeof params.default === "function" ? params.default : () => params.default
|
|
3709
|
-
|
|
3757
|
+
defaultValue: typeof params.default === "function" ? params.default : () => params.default,
|
|
3758
|
+
...processCreateParams(params)
|
|
3759
|
+
});
|
|
3710
3760
|
};
|
|
3711
3761
|
var ZodCatch = class extends ZodType {
|
|
3712
3762
|
_parse(input) {
|
|
3713
3763
|
const { ctx } = this._processInputParams(input);
|
|
3714
|
-
const newCtx =
|
|
3715
|
-
|
|
3764
|
+
const newCtx = {
|
|
3765
|
+
...ctx,
|
|
3766
|
+
common: {
|
|
3767
|
+
...ctx.common,
|
|
3716
3768
|
issues: []
|
|
3717
|
-
}
|
|
3718
|
-
}
|
|
3769
|
+
}
|
|
3770
|
+
};
|
|
3719
3771
|
const result = this._def.innerType._parse({
|
|
3720
3772
|
data: newCtx.data,
|
|
3721
3773
|
path: newCtx.path,
|
|
3722
|
-
parent:
|
|
3774
|
+
parent: {
|
|
3775
|
+
...newCtx
|
|
3776
|
+
}
|
|
3723
3777
|
});
|
|
3724
3778
|
if (isAsync(result)) {
|
|
3725
3779
|
return result.then((result2) => {
|
|
@@ -3750,11 +3804,12 @@ var ZodCatch = class extends ZodType {
|
|
|
3750
3804
|
}
|
|
3751
3805
|
};
|
|
3752
3806
|
ZodCatch.create = (type, params) => {
|
|
3753
|
-
return new ZodCatch(
|
|
3807
|
+
return new ZodCatch({
|
|
3754
3808
|
innerType: type,
|
|
3755
3809
|
typeName: ZodFirstPartyTypeKind.ZodCatch,
|
|
3756
|
-
catchValue: typeof params.catch === "function" ? params.catch : () => params.catch
|
|
3757
|
-
|
|
3810
|
+
catchValue: typeof params.catch === "function" ? params.catch : () => params.catch,
|
|
3811
|
+
...processCreateParams(params)
|
|
3812
|
+
});
|
|
3758
3813
|
};
|
|
3759
3814
|
var ZodNaN = class extends ZodType {
|
|
3760
3815
|
_parse(input) {
|
|
@@ -3772,9 +3827,10 @@ var ZodNaN = class extends ZodType {
|
|
|
3772
3827
|
}
|
|
3773
3828
|
};
|
|
3774
3829
|
ZodNaN.create = (params) => {
|
|
3775
|
-
return new ZodNaN(
|
|
3776
|
-
typeName: ZodFirstPartyTypeKind.ZodNaN
|
|
3777
|
-
|
|
3830
|
+
return new ZodNaN({
|
|
3831
|
+
typeName: ZodFirstPartyTypeKind.ZodNaN,
|
|
3832
|
+
...processCreateParams(params)
|
|
3833
|
+
});
|
|
3778
3834
|
};
|
|
3779
3835
|
var BRAND = Symbol("zod_brand");
|
|
3780
3836
|
var ZodBranded = class extends ZodType {
|
|
@@ -3795,8 +3851,8 @@ var ZodPipeline = class _ZodPipeline extends ZodType {
|
|
|
3795
3851
|
_parse(input) {
|
|
3796
3852
|
const { status, ctx } = this._processInputParams(input);
|
|
3797
3853
|
if (ctx.common.async) {
|
|
3798
|
-
const handleAsync = () =>
|
|
3799
|
-
const inResult =
|
|
3854
|
+
const handleAsync = async () => {
|
|
3855
|
+
const inResult = await this._def.in._parseAsync({
|
|
3800
3856
|
data: ctx.data,
|
|
3801
3857
|
path: ctx.path,
|
|
3802
3858
|
parent: ctx
|
|
@@ -3813,7 +3869,7 @@ var ZodPipeline = class _ZodPipeline extends ZodType {
|
|
|
3813
3869
|
parent: ctx
|
|
3814
3870
|
});
|
|
3815
3871
|
}
|
|
3816
|
-
}
|
|
3872
|
+
};
|
|
3817
3873
|
return handleAsync();
|
|
3818
3874
|
} else {
|
|
3819
3875
|
const inResult = this._def.in._parseSync({
|
|
@@ -3862,10 +3918,11 @@ var ZodReadonly = class extends ZodType {
|
|
|
3862
3918
|
}
|
|
3863
3919
|
};
|
|
3864
3920
|
ZodReadonly.create = (type, params) => {
|
|
3865
|
-
return new ZodReadonly(
|
|
3921
|
+
return new ZodReadonly({
|
|
3866
3922
|
innerType: type,
|
|
3867
|
-
typeName: ZodFirstPartyTypeKind.ZodReadonly
|
|
3868
|
-
|
|
3923
|
+
typeName: ZodFirstPartyTypeKind.ZodReadonly,
|
|
3924
|
+
...processCreateParams(params)
|
|
3925
|
+
});
|
|
3869
3926
|
};
|
|
3870
3927
|
function cleanParams(params, data) {
|
|
3871
3928
|
const p = typeof params === "function" ? params(data) : typeof params === "string" ? { message: params } : params;
|
|
@@ -3875,22 +3932,20 @@ function cleanParams(params, data) {
|
|
|
3875
3932
|
function custom(check, _params = {}, fatal) {
|
|
3876
3933
|
if (check)
|
|
3877
3934
|
return ZodAny.create().superRefine((data, ctx) => {
|
|
3878
|
-
var _a, _b;
|
|
3879
3935
|
const r = check(data);
|
|
3880
3936
|
if (r instanceof Promise) {
|
|
3881
3937
|
return r.then((r2) => {
|
|
3882
|
-
var _a2, _b2;
|
|
3883
3938
|
if (!r2) {
|
|
3884
3939
|
const params = cleanParams(_params, data);
|
|
3885
|
-
const _fatal =
|
|
3886
|
-
ctx.addIssue(
|
|
3940
|
+
const _fatal = params.fatal ?? fatal ?? true;
|
|
3941
|
+
ctx.addIssue({ code: "custom", ...params, fatal: _fatal });
|
|
3887
3942
|
}
|
|
3888
3943
|
});
|
|
3889
3944
|
}
|
|
3890
3945
|
if (!r) {
|
|
3891
3946
|
const params = cleanParams(_params, data);
|
|
3892
|
-
const _fatal =
|
|
3893
|
-
ctx.addIssue(
|
|
3947
|
+
const _fatal = params.fatal ?? fatal ?? true;
|
|
3948
|
+
ctx.addIssue({ code: "custom", ...params, fatal: _fatal });
|
|
3894
3949
|
}
|
|
3895
3950
|
return;
|
|
3896
3951
|
});
|
|
@@ -3979,18 +4034,29 @@ var ostring = () => stringType().optional();
|
|
|
3979
4034
|
var onumber = () => numberType().optional();
|
|
3980
4035
|
var oboolean = () => booleanType().optional();
|
|
3981
4036
|
var coerce = {
|
|
3982
|
-
string: ((arg) => ZodString.create(
|
|
3983
|
-
number: ((arg) => ZodNumber.create(
|
|
3984
|
-
boolean: ((arg) => ZodBoolean.create(
|
|
4037
|
+
string: ((arg) => ZodString.create({ ...arg, coerce: true })),
|
|
4038
|
+
number: ((arg) => ZodNumber.create({ ...arg, coerce: true })),
|
|
4039
|
+
boolean: ((arg) => ZodBoolean.create({
|
|
4040
|
+
...arg,
|
|
3985
4041
|
coerce: true
|
|
3986
|
-
}))
|
|
3987
|
-
bigint: ((arg) => ZodBigInt.create(
|
|
3988
|
-
date: ((arg) => ZodDate.create(
|
|
4042
|
+
})),
|
|
4043
|
+
bigint: ((arg) => ZodBigInt.create({ ...arg, coerce: true })),
|
|
4044
|
+
date: ((arg) => ZodDate.create({ ...arg, coerce: true }))
|
|
3989
4045
|
};
|
|
3990
4046
|
var NEVER = INVALID;
|
|
3991
4047
|
|
|
3992
4048
|
// src/client.ts
|
|
3993
4049
|
var RichError = class extends Error {
|
|
4050
|
+
status;
|
|
4051
|
+
// HTTP status code (e.g. 404, 500)
|
|
4052
|
+
code;
|
|
4053
|
+
// Application-level error code (e.g. "USER_NOT_FOUND")
|
|
4054
|
+
title;
|
|
4055
|
+
// Optional title or short summary
|
|
4056
|
+
detail;
|
|
4057
|
+
// Additional human-readable explanation
|
|
4058
|
+
errors;
|
|
4059
|
+
// Field-specific validation errors
|
|
3994
4060
|
constructor(error) {
|
|
3995
4061
|
super(error.message);
|
|
3996
4062
|
Object.assign(this, error);
|
|
@@ -4000,17 +4066,31 @@ var ApiClient = class {
|
|
|
4000
4066
|
constructor(config, contracts) {
|
|
4001
4067
|
this.config = config;
|
|
4002
4068
|
this.contracts = contracts;
|
|
4003
|
-
this.middlewares = [];
|
|
4004
|
-
this.responseTransform = (d) => d;
|
|
4005
|
-
this.useMockData = false;
|
|
4006
|
-
this.mockDelay = { min: 100, max: 1e3 };
|
|
4007
4069
|
this.useMockData = config.useMockData || false;
|
|
4008
4070
|
this.mockDelay = config.mockDelay || { min: 100, max: 1e3 };
|
|
4009
4071
|
this.tokenProvider = config.tokenProvider;
|
|
4010
4072
|
}
|
|
4073
|
+
middlewares = [];
|
|
4074
|
+
// Registered middlewares
|
|
4075
|
+
errorHandler;
|
|
4076
|
+
// Global error handler
|
|
4077
|
+
responseTransform = (d) => d;
|
|
4078
|
+
// Post-parse data transformer
|
|
4079
|
+
useMockData = false;
|
|
4080
|
+
// Whether mock responses are enabled
|
|
4081
|
+
mockDelay = { min: 100, max: 1e3 };
|
|
4082
|
+
// Mock latency range (ms)
|
|
4083
|
+
responseWrapper;
|
|
4084
|
+
// Wrapper for APIs with envelope structures
|
|
4085
|
+
tokenProvider;
|
|
4086
|
+
// Optional async token supplier
|
|
4087
|
+
// Configuration for retry behavior
|
|
4088
|
+
retryConfig;
|
|
4089
|
+
// Holds generated API endpoint methods
|
|
4090
|
+
_modules;
|
|
4011
4091
|
/**
|
|
4012
|
-
* Builds
|
|
4013
|
-
*
|
|
4092
|
+
* Builds all API methods (`modules`) dynamically from the Zod contract definition.
|
|
4093
|
+
* After `init()` is called, each endpoint can be invoked through `client.modules.moduleName.endpointName()`.
|
|
4014
4094
|
*/
|
|
4015
4095
|
init() {
|
|
4016
4096
|
const modules = {};
|
|
@@ -4019,285 +4099,215 @@ var ApiClient = class {
|
|
|
4019
4099
|
modules[moduleName] = {};
|
|
4020
4100
|
for (const endpointName in module) {
|
|
4021
4101
|
const endpoint = module[endpointName];
|
|
4022
|
-
modules[moduleName][endpointName] = (input) => this.request(endpoint, input);
|
|
4102
|
+
modules[moduleName][endpointName] = (input, options) => this.request(endpoint, input, options);
|
|
4023
4103
|
}
|
|
4024
4104
|
}
|
|
4025
4105
|
this._modules = modules;
|
|
4026
4106
|
}
|
|
4027
|
-
/**
|
|
4028
|
-
* Type-safe entrypoint for calling API endpoints.
|
|
4029
|
-
* Populated by `init()` based on the `contracts` passed to the constructor.
|
|
4030
|
-
*/
|
|
4107
|
+
/** Provides access to initialized modules after calling `init()`. */
|
|
4031
4108
|
get modules() {
|
|
4032
4109
|
return this._modules;
|
|
4033
4110
|
}
|
|
4034
|
-
/**
|
|
4035
|
-
* Registers a middleware in the pipeline.
|
|
4036
|
-
* Middlewares are executed in reverse order of registration.
|
|
4037
|
-
*/
|
|
4111
|
+
/** Registers a new middleware in the client’s pipeline. */
|
|
4038
4112
|
use(middleware, options) {
|
|
4039
4113
|
this.middlewares.push({ fn: middleware, options });
|
|
4040
4114
|
}
|
|
4041
|
-
/**
|
|
4042
|
-
* Registers a global error handler.
|
|
4043
|
-
* The handler is invoked for normalized errors before they are re-thrown.
|
|
4044
|
-
*/
|
|
4115
|
+
/** Sets a global error handler function to unify error behavior. */
|
|
4045
4116
|
onError(handler) {
|
|
4046
4117
|
this.errorHandler = handler;
|
|
4047
4118
|
}
|
|
4048
|
-
/**
|
|
4049
|
-
* Registers a transformation function applied to all successful responses
|
|
4050
|
-
* after Zod parsing.
|
|
4051
|
-
*/
|
|
4119
|
+
/** Defines a global transform function applied to all validated responses. */
|
|
4052
4120
|
useResponseTransform(fn) {
|
|
4053
4121
|
this.responseTransform = fn;
|
|
4054
4122
|
}
|
|
4055
|
-
/**
|
|
4056
|
-
|
|
4057
|
-
|
|
4058
|
-
|
|
4123
|
+
/** Configures the retry logic (max attempts, backoff mode, etc.). */
|
|
4124
|
+
setRetryConfig(config) {
|
|
4125
|
+
this.retryConfig = config;
|
|
4126
|
+
}
|
|
4127
|
+
/** Provides a custom token provider that returns tokens dynamically. */
|
|
4128
|
+
setTokenProvider(provider) {
|
|
4129
|
+
this.tokenProvider = provider;
|
|
4130
|
+
}
|
|
4131
|
+
/** Enables mock responses instead of network requests. */
|
|
4059
4132
|
setMockMode(enabled, delay) {
|
|
4060
4133
|
this.useMockData = enabled;
|
|
4061
|
-
if (delay)
|
|
4062
|
-
this.mockDelay = delay;
|
|
4063
|
-
}
|
|
4134
|
+
if (delay) this.mockDelay = delay;
|
|
4064
4135
|
}
|
|
4065
|
-
/**
|
|
4066
|
-
* Registers a schema wrapper for APIs that wrap data in an envelope.
|
|
4067
|
-
* Example: { success, data, message, code, ... }.
|
|
4068
|
-
*/
|
|
4136
|
+
/** Registers a wrapper schema for APIs that nest response data (e.g. `{ data, success, message }`). */
|
|
4069
4137
|
setResponseWrapper(wrapper) {
|
|
4070
4138
|
this.responseWrapper = wrapper;
|
|
4071
4139
|
}
|
|
4072
|
-
/**
|
|
4073
|
-
|
|
4074
|
-
|
|
4075
|
-
|
|
4076
|
-
setTokenProvider(provider) {
|
|
4077
|
-
this.tokenProvider = provider;
|
|
4140
|
+
/** Retrieves the current auth token, using a provider if available. */
|
|
4141
|
+
async getCurrentToken() {
|
|
4142
|
+
if (this.tokenProvider) return await this.tokenProvider();
|
|
4143
|
+
return this.config.token;
|
|
4078
4144
|
}
|
|
4079
4145
|
/**
|
|
4080
|
-
*
|
|
4081
|
-
*
|
|
4146
|
+
* Core request entry point used by auto-generated endpoint methods.
|
|
4147
|
+
* Handles caching, deduplication, and mock mode routing.
|
|
4082
4148
|
*/
|
|
4083
|
-
|
|
4084
|
-
|
|
4085
|
-
|
|
4086
|
-
|
|
4087
|
-
|
|
4088
|
-
|
|
4089
|
-
});
|
|
4149
|
+
async request(endpoint, input, options) {
|
|
4150
|
+
const parsedInput = endpoint.request.parse(input);
|
|
4151
|
+
if (this.useMockData && endpoint.mockData) {
|
|
4152
|
+
return this.handleMockRequest(endpoint);
|
|
4153
|
+
}
|
|
4154
|
+
const { url, body } = this.buildUrlAndBody(endpoint, parsedInput);
|
|
4155
|
+
const requestKey = JSON.stringify({ method: endpoint.method, url, body });
|
|
4156
|
+
const promise = this.performRequestLogic(
|
|
4157
|
+
endpoint,
|
|
4158
|
+
parsedInput,
|
|
4159
|
+
url,
|
|
4160
|
+
body,
|
|
4161
|
+
requestKey,
|
|
4162
|
+
options
|
|
4163
|
+
);
|
|
4164
|
+
return promise;
|
|
4090
4165
|
}
|
|
4091
4166
|
/**
|
|
4092
|
-
*
|
|
4093
|
-
*
|
|
4094
|
-
*
|
|
4095
|
-
*
|
|
4096
|
-
*
|
|
4097
|
-
*
|
|
4098
|
-
*
|
|
4099
|
-
* })
|
|
4100
|
-
*
|
|
4101
|
-
* If the parsed request does not contain `path`, `query` or `body`,
|
|
4102
|
-
* the entire input is treated as the legacy flat request body.
|
|
4167
|
+
* Full HTTP request workflow:
|
|
4168
|
+
* - Token injection
|
|
4169
|
+
* - Timeout support
|
|
4170
|
+
* - Middleware pipeline
|
|
4171
|
+
* - Fetch + response handling
|
|
4172
|
+
* - Zod parsing + transformation
|
|
4173
|
+
* - Caching
|
|
4103
4174
|
*/
|
|
4104
|
-
|
|
4105
|
-
|
|
4106
|
-
|
|
4107
|
-
|
|
4108
|
-
|
|
4109
|
-
|
|
4110
|
-
|
|
4111
|
-
|
|
4112
|
-
|
|
4113
|
-
|
|
4175
|
+
async performRequestLogic(endpoint, parsedInput, url, body, key, options) {
|
|
4176
|
+
const headers = {};
|
|
4177
|
+
const token = await this.getCurrentToken();
|
|
4178
|
+
if (endpoint.auth && !token) {
|
|
4179
|
+
const error = this.createError({
|
|
4180
|
+
message: `Missing token for ${endpoint.path}`,
|
|
4181
|
+
status: 401,
|
|
4182
|
+
code: "NO_TOKEN"
|
|
4183
|
+
});
|
|
4184
|
+
this.errorHandler?.(error);
|
|
4185
|
+
throw error;
|
|
4186
|
+
}
|
|
4187
|
+
if (endpoint.auth && token) headers["Authorization"] = `Bearer ${token}`;
|
|
4188
|
+
if (endpoint.bodyType !== "form-data")
|
|
4189
|
+
headers["Content-Type"] = "application/json";
|
|
4190
|
+
const ctx = {
|
|
4191
|
+
url,
|
|
4192
|
+
init: { method: endpoint.method, headers, body }
|
|
4193
|
+
};
|
|
4194
|
+
let controller;
|
|
4195
|
+
let timeoutId;
|
|
4196
|
+
if (options?.timeout) {
|
|
4197
|
+
controller = new AbortController();
|
|
4198
|
+
timeoutId = setTimeout(() => controller.abort(), options.timeout);
|
|
4199
|
+
}
|
|
4200
|
+
if (options?.signal || controller)
|
|
4201
|
+
ctx.init.signal = options?.signal || controller?.signal;
|
|
4202
|
+
const runner = this.middlewares.reduceRight(
|
|
4203
|
+
(next, mw) => () => mw.fn(ctx, next, mw.options),
|
|
4204
|
+
() => fetch(ctx.url, ctx.init)
|
|
4205
|
+
);
|
|
4206
|
+
const execute = async () => {
|
|
4207
|
+
const res = await runner();
|
|
4208
|
+
const json = await res.json();
|
|
4209
|
+
let responseData = json;
|
|
4210
|
+
if (this.responseWrapper) {
|
|
4211
|
+
const wrappedSchema = this.responseWrapper(endpoint.response);
|
|
4212
|
+
const parsedWrapped = wrappedSchema.parse(json);
|
|
4213
|
+
if (parsedWrapped.success === false) {
|
|
4214
|
+
const error = this.createError({
|
|
4215
|
+
message: parsedWrapped.message || "Request failed",
|
|
4216
|
+
status: parsedWrapped.code || res.status,
|
|
4217
|
+
code: `API_ERROR_${parsedWrapped.code}`
|
|
4218
|
+
});
|
|
4219
|
+
this.errorHandler?.(error);
|
|
4220
|
+
throw error;
|
|
4221
|
+
}
|
|
4222
|
+
responseData = parsedWrapped.data;
|
|
4114
4223
|
}
|
|
4115
|
-
if (
|
|
4224
|
+
if (!res.ok) {
|
|
4116
4225
|
const error = this.createError({
|
|
4117
|
-
message:
|
|
4118
|
-
status:
|
|
4119
|
-
code:
|
|
4226
|
+
message: json.message || res.statusText,
|
|
4227
|
+
status: res.status,
|
|
4228
|
+
code: json.code,
|
|
4229
|
+
title: json.title,
|
|
4230
|
+
detail: json.detail,
|
|
4231
|
+
errors: json.errors
|
|
4120
4232
|
});
|
|
4121
|
-
|
|
4233
|
+
this.errorHandler?.(error);
|
|
4122
4234
|
throw error;
|
|
4123
4235
|
}
|
|
4124
|
-
const
|
|
4125
|
-
|
|
4126
|
-
|
|
4127
|
-
|
|
4128
|
-
|
|
4129
|
-
const
|
|
4130
|
-
|
|
4131
|
-
|
|
4132
|
-
|
|
4133
|
-
|
|
4134
|
-
|
|
4135
|
-
|
|
4136
|
-
|
|
4137
|
-
|
|
4138
|
-
|
|
4139
|
-
|
|
4140
|
-
|
|
4236
|
+
const parsed = endpoint.response.parse(responseData);
|
|
4237
|
+
const result = this.responseTransform(parsed);
|
|
4238
|
+
return result;
|
|
4239
|
+
};
|
|
4240
|
+
try {
|
|
4241
|
+
const result = await this.executeWithRetry(execute);
|
|
4242
|
+
if (timeoutId) clearTimeout(timeoutId);
|
|
4243
|
+
return result;
|
|
4244
|
+
} catch (err) {
|
|
4245
|
+
if (timeoutId) clearTimeout(timeoutId);
|
|
4246
|
+
const error = this.normalizeError(err);
|
|
4247
|
+
this.errorHandler?.(error);
|
|
4248
|
+
throw error;
|
|
4249
|
+
}
|
|
4250
|
+
}
|
|
4251
|
+
// =========================================================
|
|
4252
|
+
// 🔁 RETRY ENGINE
|
|
4253
|
+
// =========================================================
|
|
4254
|
+
/** Executes a function with retry logic and configurable backoff strategy. */
|
|
4255
|
+
async executeWithRetry(fn) {
|
|
4256
|
+
if (!this.retryConfig) return fn();
|
|
4257
|
+
const { maxRetries, backoff, retryCondition } = this.retryConfig;
|
|
4258
|
+
let attempt = 0;
|
|
4259
|
+
while (true) {
|
|
4141
4260
|
try {
|
|
4142
|
-
|
|
4143
|
-
const json = yield res.json();
|
|
4144
|
-
let responseData = json;
|
|
4145
|
-
if (this.responseWrapper) {
|
|
4146
|
-
const wrappedSchema = this.responseWrapper(endpoint.response);
|
|
4147
|
-
const parsedResponse = wrappedSchema.parse(json);
|
|
4148
|
-
if (parsedResponse.success === false) {
|
|
4149
|
-
const error = this.createError({
|
|
4150
|
-
message: parsedResponse.message || "Request failed",
|
|
4151
|
-
status: parsedResponse.code || res.status,
|
|
4152
|
-
code: `API_ERROR_${parsedResponse.code}`
|
|
4153
|
-
});
|
|
4154
|
-
(_b = this.errorHandler) == null ? void 0 : _b.call(this, error);
|
|
4155
|
-
throw error;
|
|
4156
|
-
}
|
|
4157
|
-
responseData = parsedResponse.data;
|
|
4158
|
-
}
|
|
4159
|
-
if (!res.ok) {
|
|
4160
|
-
const error = this.createError({
|
|
4161
|
-
message: json.message || res.statusText,
|
|
4162
|
-
status: res.status,
|
|
4163
|
-
code: json.code,
|
|
4164
|
-
title: json.title,
|
|
4165
|
-
detail: json.detail,
|
|
4166
|
-
errors: json.errors
|
|
4167
|
-
});
|
|
4168
|
-
(_c = this.errorHandler) == null ? void 0 : _c.call(this, error);
|
|
4169
|
-
throw error;
|
|
4170
|
-
}
|
|
4171
|
-
return this.responseTransform(endpoint.response.parse(responseData));
|
|
4261
|
+
return await fn();
|
|
4172
4262
|
} catch (err) {
|
|
4263
|
+
attempt++;
|
|
4173
4264
|
const error = this.normalizeError(err);
|
|
4174
|
-
|
|
4175
|
-
throw error;
|
|
4265
|
+
const shouldRetry = attempt <= maxRetries && (retryCondition?.(error, attempt) ?? (error.status !== void 0 && error.status >= 500));
|
|
4266
|
+
if (!shouldRetry) throw error;
|
|
4267
|
+
const delay = this.getBackoffDelay(backoff, attempt);
|
|
4268
|
+
await new Promise((r) => setTimeout(r, delay));
|
|
4176
4269
|
}
|
|
4177
|
-
}
|
|
4270
|
+
}
|
|
4178
4271
|
}
|
|
4179
|
-
/**
|
|
4180
|
-
|
|
4181
|
-
|
|
4182
|
-
|
|
4183
|
-
|
|
4184
|
-
|
|
4185
|
-
|
|
4186
|
-
|
|
4187
|
-
|
|
4188
|
-
|
|
4189
|
-
const isObject = typeof parsedInput === "object" && parsedInput !== null;
|
|
4190
|
-
const hasNewShape = isObject && ("path" in parsedInput || "query" in parsedInput || "body" in parsedInput);
|
|
4191
|
-
if (!hasNewShape) {
|
|
4192
|
-
const url2 = this.config.baseUrl + endpoint.path;
|
|
4193
|
-
let requestBody2 = void 0;
|
|
4194
|
-
if (endpoint.method !== "GET") {
|
|
4195
|
-
requestBody2 = JSON.stringify(parsedInput);
|
|
4196
|
-
}
|
|
4197
|
-
return { url: url2, body: requestBody2 };
|
|
4272
|
+
/** Calculates retry delay intervals for various backoff strategies. */
|
|
4273
|
+
getBackoffDelay(type, attempt) {
|
|
4274
|
+
const base = 300;
|
|
4275
|
+
switch (type) {
|
|
4276
|
+
case "fixed":
|
|
4277
|
+
return base;
|
|
4278
|
+
case "linear":
|
|
4279
|
+
return base * attempt;
|
|
4280
|
+
case "exponential":
|
|
4281
|
+
return base * Math.pow(2, attempt - 1);
|
|
4198
4282
|
}
|
|
4199
|
-
|
|
4283
|
+
}
|
|
4284
|
+
// =========================================================
|
|
4285
|
+
// 🔧 UTILITIES
|
|
4286
|
+
// =========================================================
|
|
4287
|
+
/** Builds the final URL and request body from the endpoint definition and input payload. */
|
|
4288
|
+
buildUrlAndBody(endpoint, input) {
|
|
4200
4289
|
let url = this.config.baseUrl + endpoint.path;
|
|
4201
|
-
|
|
4202
|
-
for (const [key, value] of Object.entries(path)) {
|
|
4203
|
-
if (value === void 0 || value === null) continue;
|
|
4204
|
-
const token = `:${key}`;
|
|
4205
|
-
if (!url.includes(token)) {
|
|
4206
|
-
continue;
|
|
4207
|
-
}
|
|
4208
|
-
url = url.replace(token, encodeURIComponent(String(value)));
|
|
4209
|
-
}
|
|
4210
|
-
}
|
|
4211
|
-
const templatePlaceholders = Array.from(
|
|
4212
|
-
endpoint.path.matchAll(/:([A-Za-z0-9_]+)/g)
|
|
4213
|
-
).map((m) => m[1]);
|
|
4214
|
-
const missingTokens = templatePlaceholders.filter((name) => {
|
|
4215
|
-
const v = path ? path[name] : void 0;
|
|
4216
|
-
return v === void 0 || v === null;
|
|
4217
|
-
});
|
|
4218
|
-
if (missingTokens.length > 0) {
|
|
4219
|
-
throw this.createError({
|
|
4220
|
-
message: `Missing path params for placeholders: ${missingTokens.join(
|
|
4221
|
-
", "
|
|
4222
|
-
)} in "${endpoint.path}"`,
|
|
4223
|
-
code: "MISSING_PATH_PARAMS"
|
|
4224
|
-
});
|
|
4225
|
-
}
|
|
4226
|
-
if (query) {
|
|
4227
|
-
const searchParams = new URLSearchParams();
|
|
4228
|
-
for (const [key, value] of Object.entries(query)) {
|
|
4229
|
-
if (value === void 0 || value === null) continue;
|
|
4230
|
-
if (Array.isArray(value)) {
|
|
4231
|
-
for (const v of value) {
|
|
4232
|
-
if (v === void 0 || v === null) continue;
|
|
4233
|
-
searchParams.append(key, String(v));
|
|
4234
|
-
}
|
|
4235
|
-
} else if (typeof value === "object") {
|
|
4236
|
-
searchParams.append(key, JSON.stringify(value));
|
|
4237
|
-
} else {
|
|
4238
|
-
searchParams.append(key, String(value));
|
|
4239
|
-
}
|
|
4240
|
-
}
|
|
4241
|
-
const qs = searchParams.toString();
|
|
4242
|
-
if (qs) {
|
|
4243
|
-
url += (url.includes("?") ? "&" : "?") + qs;
|
|
4244
|
-
}
|
|
4245
|
-
}
|
|
4246
|
-
let requestBody = void 0;
|
|
4290
|
+
let body;
|
|
4247
4291
|
if (endpoint.method !== "GET") {
|
|
4248
|
-
if (
|
|
4249
|
-
|
|
4250
|
-
|
|
4251
|
-
|
|
4252
|
-
|
|
4253
|
-
|
|
4254
|
-
/**
|
|
4255
|
-
* Returns a mocked response based on `endpoint.mockData`,
|
|
4256
|
-
* respecting the configured mock delay and response wrapper.
|
|
4257
|
-
*/
|
|
4258
|
-
handleMockRequest(endpoint) {
|
|
4259
|
-
return __async(this, null, function* () {
|
|
4260
|
-
const delay = this.getRandomDelay();
|
|
4261
|
-
yield new Promise((resolve) => setTimeout(resolve, delay));
|
|
4262
|
-
let mockData;
|
|
4263
|
-
if (typeof endpoint.mockData === "function") {
|
|
4264
|
-
mockData = endpoint.mockData();
|
|
4292
|
+
if (endpoint.bodyType === "form-data") {
|
|
4293
|
+
const form = new FormData();
|
|
4294
|
+
for (const [k, v] of Object.entries(input.body || {})) {
|
|
4295
|
+
if (v != null) form.append(k, v);
|
|
4296
|
+
}
|
|
4297
|
+
body = form;
|
|
4265
4298
|
} else {
|
|
4266
|
-
|
|
4299
|
+
body = JSON.stringify(input.body ?? input);
|
|
4267
4300
|
}
|
|
4268
|
-
|
|
4269
|
-
|
|
4270
|
-
const mockWrappedResponse = {
|
|
4271
|
-
success: true,
|
|
4272
|
-
data: mockData,
|
|
4273
|
-
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
4274
|
-
requestId: `mock-${Math.random().toString(36).substr(2, 9)}`
|
|
4275
|
-
};
|
|
4276
|
-
const parsedWrappedResponse = wrappedSchema.parse(mockWrappedResponse);
|
|
4277
|
-
return this.responseTransform(
|
|
4278
|
-
endpoint.response.parse(parsedWrappedResponse.data)
|
|
4279
|
-
);
|
|
4280
|
-
}
|
|
4281
|
-
return this.responseTransform(endpoint.response.parse(mockData));
|
|
4282
|
-
});
|
|
4283
|
-
}
|
|
4284
|
-
/**
|
|
4285
|
-
* Returns a random delay in milliseconds within the current mock delay range.
|
|
4286
|
-
*/
|
|
4287
|
-
getRandomDelay() {
|
|
4288
|
-
return Math.floor(
|
|
4289
|
-
Math.random() * (this.mockDelay.max - this.mockDelay.min + 1)
|
|
4290
|
-
) + this.mockDelay.min;
|
|
4301
|
+
}
|
|
4302
|
+
return { url, body };
|
|
4291
4303
|
}
|
|
4292
|
-
/**
|
|
4293
|
-
* Creates a RichError instance from a partial error description.
|
|
4294
|
-
*/
|
|
4304
|
+
/** Creates and returns a RichError from details. */
|
|
4295
4305
|
createError(error) {
|
|
4296
4306
|
return new RichError(error);
|
|
4297
4307
|
}
|
|
4298
4308
|
/**
|
|
4299
|
-
*
|
|
4300
|
-
* Zod validation errors
|
|
4309
|
+
* Converts any thrown error to a standardized RichError instance.
|
|
4310
|
+
* Also flattens Zod validation errors into readable messages.
|
|
4301
4311
|
*/
|
|
4302
4312
|
normalizeError(err) {
|
|
4303
4313
|
if (err instanceof RichError) return err;
|
|
@@ -4309,73 +4319,106 @@ var ApiClient = class {
|
|
|
4309
4319
|
}
|
|
4310
4320
|
return this.createError({ message: err.message || "Unknown error" });
|
|
4311
4321
|
}
|
|
4322
|
+
/**
|
|
4323
|
+
* Handles mock-mode requests by simulating a delayed network call
|
|
4324
|
+
* and returning validated mock data.
|
|
4325
|
+
*/
|
|
4326
|
+
async handleMockRequest(endpoint) {
|
|
4327
|
+
const delay = Math.floor(
|
|
4328
|
+
Math.random() * (this.mockDelay.max - this.mockDelay.min + 1)
|
|
4329
|
+
) + this.mockDelay.min;
|
|
4330
|
+
await new Promise((r) => setTimeout(r, delay));
|
|
4331
|
+
const data = typeof endpoint.mockData === "function" ? endpoint.mockData() : endpoint.mockData;
|
|
4332
|
+
return this.responseTransform(endpoint.response.parse(data));
|
|
4333
|
+
}
|
|
4312
4334
|
};
|
|
4313
4335
|
|
|
4314
4336
|
// src/middlewares/logging.ts
|
|
4315
|
-
var loggingMiddleware = (ctx, next, options) =>
|
|
4337
|
+
var loggingMiddleware = async (ctx, next, options) => {
|
|
4316
4338
|
const { logRequest = true, logResponse = true, debug = true } = options || {};
|
|
4317
4339
|
if (debug && logRequest) console.log("\u27A1\uFE0F Request:", ctx.url, ctx.init);
|
|
4318
|
-
const res =
|
|
4340
|
+
const res = await next();
|
|
4319
4341
|
if (debug && logResponse) console.log("\u2B05\uFE0F Response:", res.status);
|
|
4320
4342
|
return res;
|
|
4321
|
-
}
|
|
4343
|
+
};
|
|
4322
4344
|
|
|
4323
4345
|
// src/middlewares/retry.ts
|
|
4324
4346
|
var retryMiddleware = (options) => {
|
|
4325
4347
|
const { maxRetries = 3, delay = 500 } = options || {};
|
|
4326
|
-
const middleware = (ctx, next) =>
|
|
4348
|
+
const middleware = async (ctx, next) => {
|
|
4327
4349
|
let attempt = 0;
|
|
4328
4350
|
while (true) {
|
|
4329
4351
|
try {
|
|
4330
|
-
return
|
|
4352
|
+
return await next();
|
|
4331
4353
|
} catch (err) {
|
|
4332
4354
|
if (attempt >= maxRetries) throw err;
|
|
4333
4355
|
attempt++;
|
|
4334
|
-
|
|
4356
|
+
await new Promise((r) => setTimeout(r, delay * 2 ** attempt));
|
|
4335
4357
|
}
|
|
4336
4358
|
}
|
|
4337
|
-
}
|
|
4359
|
+
};
|
|
4338
4360
|
return middleware;
|
|
4339
4361
|
};
|
|
4340
4362
|
|
|
4341
4363
|
// src/middlewares/auth.ts
|
|
4342
|
-
var authMiddleware = (ctx, next, options) =>
|
|
4343
|
-
if (options
|
|
4364
|
+
var authMiddleware = async (ctx, next, options) => {
|
|
4365
|
+
if (options?.refreshToken) {
|
|
4344
4366
|
try {
|
|
4345
|
-
const newToken =
|
|
4346
|
-
ctx.init.headers =
|
|
4367
|
+
const newToken = await options.refreshToken();
|
|
4368
|
+
ctx.init.headers = {
|
|
4369
|
+
...ctx.init.headers,
|
|
4370
|
+
// Preserve any headers set by prior middleware
|
|
4347
4371
|
Authorization: `Bearer ${newToken}`
|
|
4348
|
-
}
|
|
4349
|
-
} catch (
|
|
4372
|
+
};
|
|
4373
|
+
} catch (error) {
|
|
4374
|
+
console.warn(
|
|
4375
|
+
"Authentication token refresh failed, proceeding without authorization header.",
|
|
4376
|
+
error
|
|
4377
|
+
);
|
|
4350
4378
|
}
|
|
4351
4379
|
}
|
|
4352
4380
|
return next();
|
|
4353
|
-
}
|
|
4381
|
+
};
|
|
4354
4382
|
|
|
4355
4383
|
// src/middlewares/cache.ts
|
|
4356
4384
|
var cacheMiddleware = (options = {}) => {
|
|
4357
4385
|
const { ttl = 6e4 } = options;
|
|
4358
4386
|
const cache = /* @__PURE__ */ new Map();
|
|
4359
|
-
return (ctx, next) =>
|
|
4387
|
+
return async (ctx, next) => {
|
|
4360
4388
|
if (ctx.init.method === "GET") {
|
|
4361
|
-
const
|
|
4389
|
+
const key = `${ctx.init.method}:${ctx.url}`;
|
|
4390
|
+
const cached = cache.get(key);
|
|
4362
4391
|
const now = Date.now();
|
|
4363
|
-
if (cached && cached.expires > now)
|
|
4364
|
-
return new Response(JSON.stringify(cached.data)
|
|
4365
|
-
|
|
4366
|
-
|
|
4367
|
-
|
|
4392
|
+
if (cached && cached.expires > now) {
|
|
4393
|
+
return new Response(JSON.stringify(cached.data), {
|
|
4394
|
+
headers: { "Content-Type": "application/json" }
|
|
4395
|
+
});
|
|
4396
|
+
}
|
|
4397
|
+
const res = await next();
|
|
4398
|
+
const data = await res.clone().json().catch(() => null);
|
|
4399
|
+
if (data) {
|
|
4400
|
+
cache.set(key, { data, expires: now + ttl });
|
|
4401
|
+
}
|
|
4368
4402
|
return res;
|
|
4369
4403
|
}
|
|
4370
4404
|
return next();
|
|
4371
|
-
}
|
|
4405
|
+
};
|
|
4372
4406
|
};
|
|
4407
|
+
|
|
4408
|
+
// src/utils/make-request-schema.ts
|
|
4409
|
+
var makeRequestSchema = () => (defs) => external_exports.object({
|
|
4410
|
+
path: (defs.path ?? external_exports.object({}))?.optional(),
|
|
4411
|
+
query: (defs.query ?? external_exports.object({}))?.optional(),
|
|
4412
|
+
body: defs.body ?? external_exports.undefined(),
|
|
4413
|
+
headers: defs.headers ?? external_exports.record(external_exports.string()).optional()
|
|
4414
|
+
});
|
|
4373
4415
|
export {
|
|
4374
4416
|
ApiClient,
|
|
4375
4417
|
RichError,
|
|
4376
4418
|
authMiddleware,
|
|
4377
4419
|
cacheMiddleware,
|
|
4378
4420
|
loggingMiddleware,
|
|
4421
|
+
makeRequestSchema,
|
|
4379
4422
|
retryMiddleware
|
|
4380
4423
|
};
|
|
4381
4424
|
//# sourceMappingURL=index.mjs.map
|