@rivetkit/engine-api-full 25.5.3
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/dist/browser/cjs/core.js +2297 -0
- package/dist/browser/cjs/index.js +3752 -0
- package/dist/browser/cjs/serialization.js +1744 -0
- package/dist/browser/esm/core.js +2271 -0
- package/dist/browser/esm/index.js +3726 -0
- package/dist/browser/esm/serialization.js +1717 -0
- package/dist/node/core.js +2310 -0
- package/dist/node/index.js +3765 -0
- package/dist/node/serialization.js +1782 -0
- package/package.json +59 -0
- package/types/Client.d.ts +241 -0
- package/types/api/client/index.d.ts +1 -0
- package/types/api/client/requests/ActorsCreateRequest.d.ts +22 -0
- package/types/api/client/requests/ActorsDeleteRequest.d.ts +10 -0
- package/types/api/client/requests/ActorsGetByIdRequest.d.ts +16 -0
- package/types/api/client/requests/ActorsGetOrCreateByIdRequest.d.ts +23 -0
- package/types/api/client/requests/ActorsGetOrCreateRequest.d.ts +23 -0
- package/types/api/client/requests/ActorsGetRequest.d.ts +10 -0
- package/types/api/client/requests/ActorsListNamesRequest.d.ts +14 -0
- package/types/api/client/requests/ActorsListRequest.d.ts +18 -0
- package/types/api/client/requests/index.d.ts +8 -0
- package/types/api/index.d.ts +3 -0
- package/types/api/resources/datacenters/client/Client.d.ts +34 -0
- package/types/api/resources/datacenters/client/index.d.ts +1 -0
- package/types/api/resources/datacenters/index.d.ts +1 -0
- package/types/api/resources/index.d.ts +5 -0
- package/types/api/resources/namespaces/client/Client.d.ts +54 -0
- package/types/api/resources/namespaces/client/index.d.ts +1 -0
- package/types/api/resources/namespaces/client/requests/NamespacesCreateRequest.d.ts +14 -0
- package/types/api/resources/namespaces/client/requests/NamespacesListRequest.d.ts +12 -0
- package/types/api/resources/namespaces/client/requests/index.d.ts +2 -0
- package/types/api/resources/namespaces/index.d.ts +1 -0
- package/types/api/resources/runners/client/Client.d.ts +61 -0
- package/types/api/resources/runners/client/index.d.ts +1 -0
- package/types/api/resources/runners/client/requests/RunnersGetRequest.d.ts +10 -0
- package/types/api/resources/runners/client/requests/RunnersListNamesRequest.d.ts +14 -0
- package/types/api/resources/runners/client/requests/RunnersListRequest.d.ts +16 -0
- package/types/api/resources/runners/client/requests/index.d.ts +3 -0
- package/types/api/resources/runners/index.d.ts +1 -0
- package/types/api/types/Actor.d.ts +19 -0
- package/types/api/types/ActorName.d.ts +6 -0
- package/types/api/types/ActorsCreateResponse.d.ts +7 -0
- package/types/api/types/ActorsDeleteResponse.d.ts +4 -0
- package/types/api/types/ActorsGetByIdResponse.d.ts +7 -0
- package/types/api/types/ActorsGetOrCreateByIdResponse.d.ts +8 -0
- package/types/api/types/ActorsGetOrCreateResponse.d.ts +8 -0
- package/types/api/types/ActorsGetResponse.d.ts +7 -0
- package/types/api/types/ActorsListNamesResponse.d.ts +8 -0
- package/types/api/types/ActorsListResponse.d.ts +8 -0
- package/types/api/types/CrashPolicy.d.ts +9 -0
- package/types/api/types/Datacenter.d.ts +8 -0
- package/types/api/types/DatacentersListResponse.d.ts +8 -0
- package/types/api/types/Namespace.d.ts +10 -0
- package/types/api/types/NamespacesCreateResponse.d.ts +7 -0
- package/types/api/types/NamespacesGetResponse.d.ts +7 -0
- package/types/api/types/NamespacesListResponse.d.ts +8 -0
- package/types/api/types/Pagination.d.ts +6 -0
- package/types/api/types/RivetId.d.ts +4 -0
- package/types/api/types/Runner.d.ts +24 -0
- package/types/api/types/RunnersGetResponse.d.ts +7 -0
- package/types/api/types/RunnersListNamesResponse.d.ts +8 -0
- package/types/api/types/RunnersListResponse.d.ts +8 -0
- package/types/api/types/StringHttpAddressHashableMap.d.ts +5 -0
- package/types/api/types/StringHttpAddressHashableMapValue.d.ts +7 -0
- package/types/api/types/StringTcpAddressHashableMap.d.ts +5 -0
- package/types/api/types/StringTcpAddressHashableMapValue.d.ts +7 -0
- package/types/api/types/StringUdpAddressHashableMap.d.ts +5 -0
- package/types/api/types/StringUdpAddressHashableMapValue.d.ts +7 -0
- package/types/api/types/index.d.ts +29 -0
- package/types/core/fetcher/APIResponse.d.ts +10 -0
- package/types/core/fetcher/Fetcher.d.ts +39 -0
- package/types/core/fetcher/Supplier.d.ts +4 -0
- package/types/core/fetcher/createRequestUrl.d.ts +1 -0
- package/types/core/fetcher/getFetchFn.d.ts +4 -0
- package/types/core/fetcher/getHeader.d.ts +1 -0
- package/types/core/fetcher/getRequestBody.d.ts +7 -0
- package/types/core/fetcher/getResponseBody.d.ts +1 -0
- package/types/core/fetcher/index.d.ts +5 -0
- package/types/core/fetcher/makeRequest.d.ts +1 -0
- package/types/core/fetcher/requestWithRetries.d.ts +1 -0
- package/types/core/fetcher/signals.d.ts +11 -0
- package/types/core/fetcher/stream-wrappers/Node18UniversalStreamWrapper.d.ts +30 -0
- package/types/core/fetcher/stream-wrappers/NodePre18StreamWrapper.d.ts +21 -0
- package/types/core/fetcher/stream-wrappers/UndiciStreamWrapper.d.ts +31 -0
- package/types/core/fetcher/stream-wrappers/chooseStreamWrapper.d.ts +18 -0
- package/types/core/index.d.ts +3 -0
- package/types/core/json.d.ts +15 -0
- package/types/core/runtime/index.d.ts +1 -0
- package/types/core/runtime/runtime.d.ts +9 -0
- package/types/core/schemas/Schema.d.ts +87 -0
- package/types/core/schemas/builders/bigint/bigint.d.ts +2 -0
- package/types/core/schemas/builders/bigint/index.d.ts +1 -0
- package/types/core/schemas/builders/date/date.d.ts +2 -0
- package/types/core/schemas/builders/date/index.d.ts +1 -0
- package/types/core/schemas/builders/enum/enum.d.ts +2 -0
- package/types/core/schemas/builders/enum/index.d.ts +1 -0
- package/types/core/schemas/builders/index.d.ts +14 -0
- package/types/core/schemas/builders/lazy/index.d.ts +3 -0
- package/types/core/schemas/builders/lazy/lazy.d.ts +5 -0
- package/types/core/schemas/builders/lazy/lazyObject.d.ts +3 -0
- package/types/core/schemas/builders/list/index.d.ts +1 -0
- package/types/core/schemas/builders/list/list.d.ts +2 -0
- package/types/core/schemas/builders/literals/booleanLiteral.d.ts +2 -0
- package/types/core/schemas/builders/literals/index.d.ts +2 -0
- package/types/core/schemas/builders/literals/stringLiteral.d.ts +2 -0
- package/types/core/schemas/builders/object/index.d.ts +6 -0
- package/types/core/schemas/builders/object/object.d.ts +3 -0
- package/types/core/schemas/builders/object/objectWithoutOptionalProperties.d.ts +6 -0
- package/types/core/schemas/builders/object/property.d.ts +8 -0
- package/types/core/schemas/builders/object/types.d.ts +31 -0
- package/types/core/schemas/builders/object-like/getObjectLikeUtils.d.ts +9 -0
- package/types/core/schemas/builders/object-like/index.d.ts +2 -0
- package/types/core/schemas/builders/object-like/types.d.ts +7 -0
- package/types/core/schemas/builders/primitives/any.d.ts +1 -0
- package/types/core/schemas/builders/primitives/boolean.d.ts +1 -0
- package/types/core/schemas/builders/primitives/index.d.ts +5 -0
- package/types/core/schemas/builders/primitives/number.d.ts +1 -0
- package/types/core/schemas/builders/primitives/string.d.ts +1 -0
- package/types/core/schemas/builders/primitives/unknown.d.ts +1 -0
- package/types/core/schemas/builders/record/index.d.ts +2 -0
- package/types/core/schemas/builders/record/record.d.ts +3 -0
- package/types/core/schemas/builders/record/types.d.ts +4 -0
- package/types/core/schemas/builders/schema-utils/JsonError.d.ts +5 -0
- package/types/core/schemas/builders/schema-utils/ParseError.d.ts +5 -0
- package/types/core/schemas/builders/schema-utils/getSchemaUtils.d.ts +21 -0
- package/types/core/schemas/builders/schema-utils/index.d.ts +4 -0
- package/types/core/schemas/builders/schema-utils/stringifyValidationErrors.d.ts +2 -0
- package/types/core/schemas/builders/set/index.d.ts +1 -0
- package/types/core/schemas/builders/set/set.d.ts +2 -0
- package/types/core/schemas/builders/undiscriminated-union/index.d.ts +2 -0
- package/types/core/schemas/builders/undiscriminated-union/types.d.ts +4 -0
- package/types/core/schemas/builders/undiscriminated-union/undiscriminatedUnion.d.ts +3 -0
- package/types/core/schemas/builders/union/discriminant.d.ts +5 -0
- package/types/core/schemas/builders/union/index.d.ts +4 -0
- package/types/core/schemas/builders/union/types.d.ts +13 -0
- package/types/core/schemas/builders/union/union.d.ts +4 -0
- package/types/core/schemas/index.d.ts +2 -0
- package/types/core/schemas/utils/MaybePromise.d.ts +1 -0
- package/types/core/schemas/utils/addQuestionMarksToNullableProperties.d.ts +7 -0
- package/types/core/schemas/utils/createIdentitySchemaCreator.d.ts +2 -0
- package/types/core/schemas/utils/entries.d.ts +1 -0
- package/types/core/schemas/utils/filterObject.d.ts +1 -0
- package/types/core/schemas/utils/getErrorMessageForIncorrectType.d.ts +1 -0
- package/types/core/schemas/utils/isPlainObject.d.ts +1 -0
- package/types/core/schemas/utils/keys.d.ts +1 -0
- package/types/core/schemas/utils/maybeSkipValidation.d.ts +2 -0
- package/types/core/schemas/utils/partition.d.ts +1 -0
- package/types/errors/RivetError.d.ts +12 -0
- package/types/errors/RivetTimeoutError.d.ts +6 -0
- package/types/errors/index.d.ts +2 -0
- package/types/index.d.ts +4 -0
- package/types/serialization/client/index.d.ts +1 -0
- package/types/serialization/client/requests/ActorsCreateRequest.d.ts +17 -0
- package/types/serialization/client/requests/ActorsGetOrCreateByIdRequest.d.ts +17 -0
- package/types/serialization/client/requests/ActorsGetOrCreateRequest.d.ts +17 -0
- package/types/serialization/client/requests/index.d.ts +3 -0
- package/types/serialization/index.d.ts +3 -0
- package/types/serialization/resources/index.d.ts +2 -0
- package/types/serialization/resources/namespaces/client/index.d.ts +1 -0
- package/types/serialization/resources/namespaces/client/requests/NamespacesCreateRequest.d.ts +13 -0
- package/types/serialization/resources/namespaces/client/requests/index.d.ts +1 -0
- package/types/serialization/resources/namespaces/index.d.ts +1 -0
- package/types/serialization/types/Actor.d.ts +26 -0
- package/types/serialization/types/ActorName.d.ts +12 -0
- package/types/serialization/types/ActorsCreateResponse.d.ts +13 -0
- package/types/serialization/types/ActorsDeleteResponse.d.ts +10 -0
- package/types/serialization/types/ActorsGetByIdResponse.d.ts +13 -0
- package/types/serialization/types/ActorsGetOrCreateByIdResponse.d.ts +14 -0
- package/types/serialization/types/ActorsGetOrCreateResponse.d.ts +14 -0
- package/types/serialization/types/ActorsGetResponse.d.ts +13 -0
- package/types/serialization/types/ActorsListNamesResponse.d.ts +15 -0
- package/types/serialization/types/ActorsListResponse.d.ts +15 -0
- package/types/serialization/types/CrashPolicy.d.ts +10 -0
- package/types/serialization/types/Datacenter.d.ts +14 -0
- package/types/serialization/types/DatacentersListResponse.d.ts +15 -0
- package/types/serialization/types/Namespace.d.ts +16 -0
- package/types/serialization/types/NamespacesCreateResponse.d.ts +13 -0
- package/types/serialization/types/NamespacesGetResponse.d.ts +13 -0
- package/types/serialization/types/NamespacesListResponse.d.ts +15 -0
- package/types/serialization/types/Pagination.d.ts +12 -0
- package/types/serialization/types/RivetId.d.ts +10 -0
- package/types/serialization/types/Runner.d.ts +33 -0
- package/types/serialization/types/RunnersGetResponse.d.ts +13 -0
- package/types/serialization/types/RunnersListNamesResponse.d.ts +14 -0
- package/types/serialization/types/RunnersListResponse.d.ts +15 -0
- package/types/serialization/types/StringHttpAddressHashableMap.d.ts +11 -0
- package/types/serialization/types/StringHttpAddressHashableMapValue.d.ts +13 -0
- package/types/serialization/types/StringTcpAddressHashableMap.d.ts +11 -0
- package/types/serialization/types/StringTcpAddressHashableMapValue.d.ts +13 -0
- package/types/serialization/types/StringUdpAddressHashableMap.d.ts +11 -0
- package/types/serialization/types/StringUdpAddressHashableMapValue.d.ts +13 -0
- package/types/serialization/types/index.d.ts +29 -0
|
@@ -0,0 +1,2297 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __esm = (fn, res) => function __init() {
|
|
9
|
+
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
|
|
10
|
+
};
|
|
11
|
+
var __export = (target, all) => {
|
|
12
|
+
for (var name in all)
|
|
13
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
14
|
+
};
|
|
15
|
+
var __copyProps = (to, from, except, desc) => {
|
|
16
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
17
|
+
for (let key of __getOwnPropNames(from))
|
|
18
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
19
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
20
|
+
}
|
|
21
|
+
return to;
|
|
22
|
+
};
|
|
23
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
24
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
25
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
26
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
27
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
28
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
29
|
+
mod
|
|
30
|
+
));
|
|
31
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
32
|
+
|
|
33
|
+
// src/core/fetcher/stream-wrappers/Node18UniversalStreamWrapper.ts
|
|
34
|
+
var Node18UniversalStreamWrapper_exports = {};
|
|
35
|
+
__export(Node18UniversalStreamWrapper_exports, {
|
|
36
|
+
Node18UniversalStreamWrapper: () => Node18UniversalStreamWrapper
|
|
37
|
+
});
|
|
38
|
+
var Node18UniversalStreamWrapper;
|
|
39
|
+
var init_Node18UniversalStreamWrapper = __esm({
|
|
40
|
+
"src/core/fetcher/stream-wrappers/Node18UniversalStreamWrapper.ts"() {
|
|
41
|
+
"use strict";
|
|
42
|
+
Node18UniversalStreamWrapper = class _Node18UniversalStreamWrapper {
|
|
43
|
+
readableStream;
|
|
44
|
+
reader;
|
|
45
|
+
events;
|
|
46
|
+
paused;
|
|
47
|
+
resumeCallback;
|
|
48
|
+
encoding;
|
|
49
|
+
constructor(readableStream) {
|
|
50
|
+
this.readableStream = readableStream;
|
|
51
|
+
this.reader = this.readableStream.getReader();
|
|
52
|
+
this.events = {
|
|
53
|
+
data: [],
|
|
54
|
+
end: [],
|
|
55
|
+
error: [],
|
|
56
|
+
readable: [],
|
|
57
|
+
close: [],
|
|
58
|
+
pause: [],
|
|
59
|
+
resume: []
|
|
60
|
+
};
|
|
61
|
+
this.paused = false;
|
|
62
|
+
this.resumeCallback = null;
|
|
63
|
+
this.encoding = null;
|
|
64
|
+
}
|
|
65
|
+
on(event, callback) {
|
|
66
|
+
this.events[event]?.push(callback);
|
|
67
|
+
}
|
|
68
|
+
off(event, callback) {
|
|
69
|
+
this.events[event] = this.events[event]?.filter((cb) => cb !== callback);
|
|
70
|
+
}
|
|
71
|
+
pipe(dest) {
|
|
72
|
+
this.on("data", async (chunk) => {
|
|
73
|
+
if (dest instanceof _Node18UniversalStreamWrapper) {
|
|
74
|
+
dest._write(chunk);
|
|
75
|
+
} else if (dest instanceof WritableStream) {
|
|
76
|
+
const writer = dest.getWriter();
|
|
77
|
+
writer.write(chunk).then(() => writer.releaseLock());
|
|
78
|
+
} else {
|
|
79
|
+
dest.write(chunk);
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
this.on("end", async () => {
|
|
83
|
+
if (dest instanceof _Node18UniversalStreamWrapper) {
|
|
84
|
+
dest._end();
|
|
85
|
+
} else if (dest instanceof WritableStream) {
|
|
86
|
+
const writer = dest.getWriter();
|
|
87
|
+
writer.close();
|
|
88
|
+
} else {
|
|
89
|
+
dest.end();
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
this.on("error", async (error) => {
|
|
93
|
+
if (dest instanceof _Node18UniversalStreamWrapper) {
|
|
94
|
+
dest._error(error);
|
|
95
|
+
} else if (dest instanceof WritableStream) {
|
|
96
|
+
const writer = dest.getWriter();
|
|
97
|
+
writer.abort(error);
|
|
98
|
+
} else {
|
|
99
|
+
dest.destroy(error);
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
this._startReading();
|
|
103
|
+
return dest;
|
|
104
|
+
}
|
|
105
|
+
pipeTo(dest) {
|
|
106
|
+
return this.pipe(dest);
|
|
107
|
+
}
|
|
108
|
+
unpipe(dest) {
|
|
109
|
+
this.off("data", async (chunk) => {
|
|
110
|
+
if (dest instanceof _Node18UniversalStreamWrapper) {
|
|
111
|
+
dest._write(chunk);
|
|
112
|
+
} else if (dest instanceof WritableStream) {
|
|
113
|
+
const writer = dest.getWriter();
|
|
114
|
+
writer.write(chunk).then(() => writer.releaseLock());
|
|
115
|
+
} else {
|
|
116
|
+
dest.write(chunk);
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
this.off("end", async () => {
|
|
120
|
+
if (dest instanceof _Node18UniversalStreamWrapper) {
|
|
121
|
+
dest._end();
|
|
122
|
+
} else if (dest instanceof WritableStream) {
|
|
123
|
+
const writer = dest.getWriter();
|
|
124
|
+
writer.close();
|
|
125
|
+
} else {
|
|
126
|
+
dest.end();
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
this.off("error", async (error) => {
|
|
130
|
+
if (dest instanceof _Node18UniversalStreamWrapper) {
|
|
131
|
+
dest._error(error);
|
|
132
|
+
} else if (dest instanceof WritableStream) {
|
|
133
|
+
const writer = dest.getWriter();
|
|
134
|
+
writer.abort(error);
|
|
135
|
+
} else {
|
|
136
|
+
dest.destroy(error);
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
destroy(error) {
|
|
141
|
+
this.reader.cancel(error).then(() => {
|
|
142
|
+
this._emit("close");
|
|
143
|
+
}).catch((err) => {
|
|
144
|
+
this._emit("error", err);
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
pause() {
|
|
148
|
+
this.paused = true;
|
|
149
|
+
this._emit("pause");
|
|
150
|
+
}
|
|
151
|
+
resume() {
|
|
152
|
+
if (this.paused) {
|
|
153
|
+
this.paused = false;
|
|
154
|
+
this._emit("resume");
|
|
155
|
+
if (this.resumeCallback) {
|
|
156
|
+
this.resumeCallback();
|
|
157
|
+
this.resumeCallback = null;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
get isPaused() {
|
|
162
|
+
return this.paused;
|
|
163
|
+
}
|
|
164
|
+
async read() {
|
|
165
|
+
if (this.paused) {
|
|
166
|
+
await new Promise((resolve) => {
|
|
167
|
+
this.resumeCallback = resolve;
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
const { done, value } = await this.reader.read();
|
|
171
|
+
if (done) {
|
|
172
|
+
return void 0;
|
|
173
|
+
}
|
|
174
|
+
return value;
|
|
175
|
+
}
|
|
176
|
+
setEncoding(encoding) {
|
|
177
|
+
this.encoding = encoding;
|
|
178
|
+
}
|
|
179
|
+
async text() {
|
|
180
|
+
const chunks = [];
|
|
181
|
+
while (true) {
|
|
182
|
+
const { done, value } = await this.reader.read();
|
|
183
|
+
if (done) {
|
|
184
|
+
break;
|
|
185
|
+
}
|
|
186
|
+
if (value) {
|
|
187
|
+
chunks.push(value);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
const decoder = new TextDecoder(this.encoding || "utf-8");
|
|
191
|
+
return decoder.decode(await new Blob(chunks).arrayBuffer());
|
|
192
|
+
}
|
|
193
|
+
async json() {
|
|
194
|
+
const text = await this.text();
|
|
195
|
+
return JSON.parse(text);
|
|
196
|
+
}
|
|
197
|
+
_write(chunk) {
|
|
198
|
+
this._emit("data", chunk);
|
|
199
|
+
}
|
|
200
|
+
_end() {
|
|
201
|
+
this._emit("end");
|
|
202
|
+
}
|
|
203
|
+
_error(error) {
|
|
204
|
+
this._emit("error", error);
|
|
205
|
+
}
|
|
206
|
+
_emit(event, data) {
|
|
207
|
+
if (this.events[event]) {
|
|
208
|
+
for (const callback of this.events[event] || []) {
|
|
209
|
+
callback(data);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
async _startReading() {
|
|
214
|
+
try {
|
|
215
|
+
this._emit("readable");
|
|
216
|
+
while (true) {
|
|
217
|
+
if (this.paused) {
|
|
218
|
+
await new Promise((resolve) => {
|
|
219
|
+
this.resumeCallback = resolve;
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
const { done, value } = await this.reader.read();
|
|
223
|
+
if (done) {
|
|
224
|
+
this._emit("end");
|
|
225
|
+
this._emit("close");
|
|
226
|
+
break;
|
|
227
|
+
}
|
|
228
|
+
if (value) {
|
|
229
|
+
this._emit("data", value);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
} catch (error) {
|
|
233
|
+
this._emit("error", error);
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
[Symbol.asyncIterator]() {
|
|
237
|
+
return {
|
|
238
|
+
next: async () => {
|
|
239
|
+
if (this.paused) {
|
|
240
|
+
await new Promise((resolve) => {
|
|
241
|
+
this.resumeCallback = resolve;
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
const { done, value } = await this.reader.read();
|
|
245
|
+
if (done) {
|
|
246
|
+
return { done: true, value: void 0 };
|
|
247
|
+
}
|
|
248
|
+
return { done: false, value };
|
|
249
|
+
},
|
|
250
|
+
[Symbol.asyncIterator]() {
|
|
251
|
+
return this;
|
|
252
|
+
}
|
|
253
|
+
};
|
|
254
|
+
}
|
|
255
|
+
};
|
|
256
|
+
}
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
// src/core/fetcher/stream-wrappers/UndiciStreamWrapper.ts
|
|
260
|
+
var UndiciStreamWrapper_exports = {};
|
|
261
|
+
__export(UndiciStreamWrapper_exports, {
|
|
262
|
+
UndiciStreamWrapper: () => UndiciStreamWrapper
|
|
263
|
+
});
|
|
264
|
+
var UndiciStreamWrapper;
|
|
265
|
+
var init_UndiciStreamWrapper = __esm({
|
|
266
|
+
"src/core/fetcher/stream-wrappers/UndiciStreamWrapper.ts"() {
|
|
267
|
+
"use strict";
|
|
268
|
+
UndiciStreamWrapper = class _UndiciStreamWrapper {
|
|
269
|
+
readableStream;
|
|
270
|
+
reader;
|
|
271
|
+
events;
|
|
272
|
+
paused;
|
|
273
|
+
resumeCallback;
|
|
274
|
+
encoding;
|
|
275
|
+
constructor(readableStream) {
|
|
276
|
+
this.readableStream = readableStream;
|
|
277
|
+
this.reader = this.readableStream.getReader();
|
|
278
|
+
this.events = {
|
|
279
|
+
data: [],
|
|
280
|
+
end: [],
|
|
281
|
+
error: [],
|
|
282
|
+
readable: [],
|
|
283
|
+
close: [],
|
|
284
|
+
pause: [],
|
|
285
|
+
resume: []
|
|
286
|
+
};
|
|
287
|
+
this.paused = false;
|
|
288
|
+
this.resumeCallback = null;
|
|
289
|
+
this.encoding = null;
|
|
290
|
+
}
|
|
291
|
+
on(event, callback) {
|
|
292
|
+
this.events[event]?.push(callback);
|
|
293
|
+
}
|
|
294
|
+
off(event, callback) {
|
|
295
|
+
this.events[event] = this.events[event]?.filter((cb) => cb !== callback);
|
|
296
|
+
}
|
|
297
|
+
pipe(dest) {
|
|
298
|
+
this.on("data", (chunk) => {
|
|
299
|
+
if (dest instanceof _UndiciStreamWrapper) {
|
|
300
|
+
dest._write(chunk);
|
|
301
|
+
} else {
|
|
302
|
+
const writer = dest.getWriter();
|
|
303
|
+
writer.write(chunk).then(() => writer.releaseLock());
|
|
304
|
+
}
|
|
305
|
+
});
|
|
306
|
+
this.on("end", () => {
|
|
307
|
+
if (dest instanceof _UndiciStreamWrapper) {
|
|
308
|
+
dest._end();
|
|
309
|
+
} else {
|
|
310
|
+
const writer = dest.getWriter();
|
|
311
|
+
writer.close();
|
|
312
|
+
}
|
|
313
|
+
});
|
|
314
|
+
this.on("error", (error) => {
|
|
315
|
+
if (dest instanceof _UndiciStreamWrapper) {
|
|
316
|
+
dest._error(error);
|
|
317
|
+
} else {
|
|
318
|
+
const writer = dest.getWriter();
|
|
319
|
+
writer.abort(error);
|
|
320
|
+
}
|
|
321
|
+
});
|
|
322
|
+
this._startReading();
|
|
323
|
+
return dest;
|
|
324
|
+
}
|
|
325
|
+
pipeTo(dest) {
|
|
326
|
+
return this.pipe(dest);
|
|
327
|
+
}
|
|
328
|
+
unpipe(dest) {
|
|
329
|
+
this.off("data", (chunk) => {
|
|
330
|
+
if (dest instanceof _UndiciStreamWrapper) {
|
|
331
|
+
dest._write(chunk);
|
|
332
|
+
} else {
|
|
333
|
+
const writer = dest.getWriter();
|
|
334
|
+
writer.write(chunk).then(() => writer.releaseLock());
|
|
335
|
+
}
|
|
336
|
+
});
|
|
337
|
+
this.off("end", () => {
|
|
338
|
+
if (dest instanceof _UndiciStreamWrapper) {
|
|
339
|
+
dest._end();
|
|
340
|
+
} else {
|
|
341
|
+
const writer = dest.getWriter();
|
|
342
|
+
writer.close();
|
|
343
|
+
}
|
|
344
|
+
});
|
|
345
|
+
this.off("error", (error) => {
|
|
346
|
+
if (dest instanceof _UndiciStreamWrapper) {
|
|
347
|
+
dest._error(error);
|
|
348
|
+
} else {
|
|
349
|
+
const writer = dest.getWriter();
|
|
350
|
+
writer.abort(error);
|
|
351
|
+
}
|
|
352
|
+
});
|
|
353
|
+
}
|
|
354
|
+
destroy(error) {
|
|
355
|
+
this.reader.cancel(error).then(() => {
|
|
356
|
+
this._emit("close");
|
|
357
|
+
}).catch((err) => {
|
|
358
|
+
this._emit("error", err);
|
|
359
|
+
});
|
|
360
|
+
}
|
|
361
|
+
pause() {
|
|
362
|
+
this.paused = true;
|
|
363
|
+
this._emit("pause");
|
|
364
|
+
}
|
|
365
|
+
resume() {
|
|
366
|
+
if (this.paused) {
|
|
367
|
+
this.paused = false;
|
|
368
|
+
this._emit("resume");
|
|
369
|
+
if (this.resumeCallback) {
|
|
370
|
+
this.resumeCallback();
|
|
371
|
+
this.resumeCallback = null;
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
get isPaused() {
|
|
376
|
+
return this.paused;
|
|
377
|
+
}
|
|
378
|
+
async read() {
|
|
379
|
+
if (this.paused) {
|
|
380
|
+
await new Promise((resolve) => {
|
|
381
|
+
this.resumeCallback = resolve;
|
|
382
|
+
});
|
|
383
|
+
}
|
|
384
|
+
const { done, value } = await this.reader.read();
|
|
385
|
+
if (done) {
|
|
386
|
+
return void 0;
|
|
387
|
+
}
|
|
388
|
+
return value;
|
|
389
|
+
}
|
|
390
|
+
setEncoding(encoding) {
|
|
391
|
+
this.encoding = encoding;
|
|
392
|
+
}
|
|
393
|
+
async text() {
|
|
394
|
+
const chunks = [];
|
|
395
|
+
while (true) {
|
|
396
|
+
const { done, value } = await this.reader.read();
|
|
397
|
+
if (done) {
|
|
398
|
+
break;
|
|
399
|
+
}
|
|
400
|
+
if (value) {
|
|
401
|
+
chunks.push(value);
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
const decoder = new TextDecoder(this.encoding || "utf-8");
|
|
405
|
+
return decoder.decode(await new Blob(chunks).arrayBuffer());
|
|
406
|
+
}
|
|
407
|
+
async json() {
|
|
408
|
+
const text = await this.text();
|
|
409
|
+
return JSON.parse(text);
|
|
410
|
+
}
|
|
411
|
+
_write(chunk) {
|
|
412
|
+
this._emit("data", chunk);
|
|
413
|
+
}
|
|
414
|
+
_end() {
|
|
415
|
+
this._emit("end");
|
|
416
|
+
}
|
|
417
|
+
_error(error) {
|
|
418
|
+
this._emit("error", error);
|
|
419
|
+
}
|
|
420
|
+
_emit(event, data) {
|
|
421
|
+
if (this.events[event]) {
|
|
422
|
+
for (const callback of this.events[event] || []) {
|
|
423
|
+
callback(data);
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
async _startReading() {
|
|
428
|
+
try {
|
|
429
|
+
this._emit("readable");
|
|
430
|
+
while (true) {
|
|
431
|
+
if (this.paused) {
|
|
432
|
+
await new Promise((resolve) => {
|
|
433
|
+
this.resumeCallback = resolve;
|
|
434
|
+
});
|
|
435
|
+
}
|
|
436
|
+
const { done, value } = await this.reader.read();
|
|
437
|
+
if (done) {
|
|
438
|
+
this._emit("end");
|
|
439
|
+
this._emit("close");
|
|
440
|
+
break;
|
|
441
|
+
}
|
|
442
|
+
if (value) {
|
|
443
|
+
this._emit("data", value);
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
} catch (error) {
|
|
447
|
+
this._emit("error", error);
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
[Symbol.asyncIterator]() {
|
|
451
|
+
return {
|
|
452
|
+
next: async () => {
|
|
453
|
+
if (this.paused) {
|
|
454
|
+
await new Promise((resolve) => {
|
|
455
|
+
this.resumeCallback = resolve;
|
|
456
|
+
});
|
|
457
|
+
}
|
|
458
|
+
const { done, value } = await this.reader.read();
|
|
459
|
+
if (done) {
|
|
460
|
+
return { done: true, value: void 0 };
|
|
461
|
+
}
|
|
462
|
+
return { done: false, value };
|
|
463
|
+
},
|
|
464
|
+
[Symbol.asyncIterator]() {
|
|
465
|
+
return this;
|
|
466
|
+
}
|
|
467
|
+
};
|
|
468
|
+
}
|
|
469
|
+
};
|
|
470
|
+
}
|
|
471
|
+
});
|
|
472
|
+
|
|
473
|
+
// src/core/fetcher/stream-wrappers/NodePre18StreamWrapper.ts
|
|
474
|
+
var NodePre18StreamWrapper_exports = {};
|
|
475
|
+
__export(NodePre18StreamWrapper_exports, {
|
|
476
|
+
NodePre18StreamWrapper: () => NodePre18StreamWrapper
|
|
477
|
+
});
|
|
478
|
+
var NodePre18StreamWrapper;
|
|
479
|
+
var init_NodePre18StreamWrapper = __esm({
|
|
480
|
+
"src/core/fetcher/stream-wrappers/NodePre18StreamWrapper.ts"() {
|
|
481
|
+
"use strict";
|
|
482
|
+
NodePre18StreamWrapper = class {
|
|
483
|
+
readableStream;
|
|
484
|
+
encoding;
|
|
485
|
+
constructor(readableStream) {
|
|
486
|
+
this.readableStream = readableStream;
|
|
487
|
+
}
|
|
488
|
+
on(event, callback) {
|
|
489
|
+
this.readableStream.on(event, callback);
|
|
490
|
+
}
|
|
491
|
+
off(event, callback) {
|
|
492
|
+
this.readableStream.off(event, callback);
|
|
493
|
+
}
|
|
494
|
+
pipe(dest) {
|
|
495
|
+
this.readableStream.pipe(dest);
|
|
496
|
+
return dest;
|
|
497
|
+
}
|
|
498
|
+
pipeTo(dest) {
|
|
499
|
+
return this.pipe(dest);
|
|
500
|
+
}
|
|
501
|
+
unpipe(dest) {
|
|
502
|
+
if (dest) {
|
|
503
|
+
this.readableStream.unpipe(dest);
|
|
504
|
+
} else {
|
|
505
|
+
this.readableStream.unpipe();
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
destroy(error) {
|
|
509
|
+
this.readableStream.destroy(error);
|
|
510
|
+
}
|
|
511
|
+
pause() {
|
|
512
|
+
this.readableStream.pause();
|
|
513
|
+
}
|
|
514
|
+
resume() {
|
|
515
|
+
this.readableStream.resume();
|
|
516
|
+
}
|
|
517
|
+
get isPaused() {
|
|
518
|
+
return this.readableStream.isPaused();
|
|
519
|
+
}
|
|
520
|
+
async read() {
|
|
521
|
+
return new Promise((resolve, reject) => {
|
|
522
|
+
const chunk = this.readableStream.read();
|
|
523
|
+
if (chunk) {
|
|
524
|
+
resolve(chunk);
|
|
525
|
+
} else {
|
|
526
|
+
this.readableStream.once("readable", () => {
|
|
527
|
+
const chunk2 = this.readableStream.read();
|
|
528
|
+
resolve(chunk2);
|
|
529
|
+
});
|
|
530
|
+
this.readableStream.once("error", reject);
|
|
531
|
+
}
|
|
532
|
+
});
|
|
533
|
+
}
|
|
534
|
+
setEncoding(encoding) {
|
|
535
|
+
this.readableStream.setEncoding(encoding);
|
|
536
|
+
this.encoding = encoding;
|
|
537
|
+
}
|
|
538
|
+
async text() {
|
|
539
|
+
const chunks = [];
|
|
540
|
+
const encoder = new TextEncoder();
|
|
541
|
+
this.readableStream.setEncoding(this.encoding || "utf-8");
|
|
542
|
+
for await (const chunk of this.readableStream) {
|
|
543
|
+
chunks.push(encoder.encode(chunk));
|
|
544
|
+
}
|
|
545
|
+
const decoder = new TextDecoder(this.encoding || "utf-8");
|
|
546
|
+
return decoder.decode(Buffer.concat(chunks));
|
|
547
|
+
}
|
|
548
|
+
async json() {
|
|
549
|
+
const text = await this.text();
|
|
550
|
+
return JSON.parse(text);
|
|
551
|
+
}
|
|
552
|
+
[Symbol.asyncIterator]() {
|
|
553
|
+
const readableStream = this.readableStream;
|
|
554
|
+
const iterator = readableStream[Symbol.asyncIterator]();
|
|
555
|
+
return {
|
|
556
|
+
async next() {
|
|
557
|
+
const { value, done } = await iterator.next();
|
|
558
|
+
return { value, done };
|
|
559
|
+
},
|
|
560
|
+
[Symbol.asyncIterator]() {
|
|
561
|
+
return this;
|
|
562
|
+
}
|
|
563
|
+
};
|
|
564
|
+
}
|
|
565
|
+
};
|
|
566
|
+
}
|
|
567
|
+
});
|
|
568
|
+
|
|
569
|
+
// src/core/index.ts
|
|
570
|
+
var core_exports = {};
|
|
571
|
+
__export(core_exports, {
|
|
572
|
+
RUNTIME: () => RUNTIME,
|
|
573
|
+
Supplier: () => Supplier,
|
|
574
|
+
fetcher: () => fetcher,
|
|
575
|
+
getHeader: () => getHeader,
|
|
576
|
+
serialization: () => schemas_exports
|
|
577
|
+
});
|
|
578
|
+
module.exports = __toCommonJS(core_exports);
|
|
579
|
+
|
|
580
|
+
// src/core/json.ts
|
|
581
|
+
var toJson = (value, replacer, space) => {
|
|
582
|
+
return JSON.stringify(value, replacer, space);
|
|
583
|
+
};
|
|
584
|
+
|
|
585
|
+
// src/core/fetcher/createRequestUrl.ts
|
|
586
|
+
var import_qs = __toESM(require("qs"));
|
|
587
|
+
function createRequestUrl(baseUrl, queryParameters) {
|
|
588
|
+
return Object.keys(queryParameters ?? {}).length > 0 ? `${baseUrl}?${import_qs.default.stringify(queryParameters, { arrayFormat: "repeat" })}` : baseUrl;
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
// src/core/runtime/runtime.ts
|
|
592
|
+
var RUNTIME = evaluateRuntime();
|
|
593
|
+
function evaluateRuntime() {
|
|
594
|
+
const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined";
|
|
595
|
+
if (isBrowser) {
|
|
596
|
+
return {
|
|
597
|
+
type: "browser",
|
|
598
|
+
version: window.navigator.userAgent
|
|
599
|
+
};
|
|
600
|
+
}
|
|
601
|
+
const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers";
|
|
602
|
+
if (isCloudflare) {
|
|
603
|
+
return {
|
|
604
|
+
type: "workerd"
|
|
605
|
+
};
|
|
606
|
+
}
|
|
607
|
+
const isEdgeRuntime = typeof EdgeRuntime === "string";
|
|
608
|
+
if (isEdgeRuntime) {
|
|
609
|
+
return {
|
|
610
|
+
type: "edge-runtime"
|
|
611
|
+
};
|
|
612
|
+
}
|
|
613
|
+
const isWebWorker = typeof self === "object" && // @ts-ignore
|
|
614
|
+
typeof self?.importScripts === "function" && (self.constructor?.name === "DedicatedWorkerGlobalScope" || self.constructor?.name === "ServiceWorkerGlobalScope" || self.constructor?.name === "SharedWorkerGlobalScope");
|
|
615
|
+
if (isWebWorker) {
|
|
616
|
+
return {
|
|
617
|
+
type: "web-worker"
|
|
618
|
+
};
|
|
619
|
+
}
|
|
620
|
+
const isDeno = typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined";
|
|
621
|
+
if (isDeno) {
|
|
622
|
+
return {
|
|
623
|
+
type: "deno",
|
|
624
|
+
version: Deno.version.deno
|
|
625
|
+
};
|
|
626
|
+
}
|
|
627
|
+
const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined";
|
|
628
|
+
if (isBun) {
|
|
629
|
+
return {
|
|
630
|
+
type: "bun",
|
|
631
|
+
version: Bun.version
|
|
632
|
+
};
|
|
633
|
+
}
|
|
634
|
+
const isNode = typeof process !== "undefined" && "version" in process && !!process.version && "versions" in process && !!process.versions?.node;
|
|
635
|
+
if (isNode) {
|
|
636
|
+
return {
|
|
637
|
+
type: "node",
|
|
638
|
+
version: process.versions.node,
|
|
639
|
+
parsedVersion: Number(process.versions.node.split(".")[0])
|
|
640
|
+
};
|
|
641
|
+
}
|
|
642
|
+
const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative";
|
|
643
|
+
if (isReactNative) {
|
|
644
|
+
return {
|
|
645
|
+
type: "react-native"
|
|
646
|
+
};
|
|
647
|
+
}
|
|
648
|
+
return {
|
|
649
|
+
type: "unknown"
|
|
650
|
+
};
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
// src/core/fetcher/getFetchFn.ts
|
|
654
|
+
async function getFetchFn() {
|
|
655
|
+
if (RUNTIME.type === "node" && RUNTIME.parsedVersion != null && RUNTIME.parsedVersion >= 18) {
|
|
656
|
+
return fetch;
|
|
657
|
+
}
|
|
658
|
+
if (RUNTIME.type === "node") {
|
|
659
|
+
return (await import("node-fetch")).default;
|
|
660
|
+
}
|
|
661
|
+
if (typeof fetch == "function") {
|
|
662
|
+
return fetch;
|
|
663
|
+
}
|
|
664
|
+
return (await import("node-fetch")).default;
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
// src/core/fetcher/getRequestBody.ts
|
|
668
|
+
async function getRequestBody({ body, type }) {
|
|
669
|
+
if (type.includes("json")) {
|
|
670
|
+
return toJson(body);
|
|
671
|
+
} else {
|
|
672
|
+
return body;
|
|
673
|
+
}
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
// src/core/fetcher/stream-wrappers/chooseStreamWrapper.ts
|
|
677
|
+
async function chooseStreamWrapper(responseBody) {
|
|
678
|
+
if (RUNTIME.type === "node" && RUNTIME.parsedVersion != null && RUNTIME.parsedVersion >= 18) {
|
|
679
|
+
return new (await Promise.resolve().then(() => (init_Node18UniversalStreamWrapper(), Node18UniversalStreamWrapper_exports))).Node18UniversalStreamWrapper(
|
|
680
|
+
responseBody
|
|
681
|
+
);
|
|
682
|
+
} else if (RUNTIME.type !== "node" && typeof fetch === "function") {
|
|
683
|
+
return new (await Promise.resolve().then(() => (init_UndiciStreamWrapper(), UndiciStreamWrapper_exports))).UndiciStreamWrapper(responseBody);
|
|
684
|
+
} else {
|
|
685
|
+
return new (await Promise.resolve().then(() => (init_NodePre18StreamWrapper(), NodePre18StreamWrapper_exports))).NodePre18StreamWrapper(responseBody);
|
|
686
|
+
}
|
|
687
|
+
}
|
|
688
|
+
|
|
689
|
+
// src/core/fetcher/getResponseBody.ts
|
|
690
|
+
async function getResponseBody(response, responseType) {
|
|
691
|
+
if (response.body != null && responseType === "blob") {
|
|
692
|
+
return await response.blob();
|
|
693
|
+
} else if (response.body != null && responseType === "arrayBuffer") {
|
|
694
|
+
return await response.arrayBuffer();
|
|
695
|
+
} else if (response.body != null && responseType === "sse") {
|
|
696
|
+
return response.body;
|
|
697
|
+
} else if (response.body != null && responseType === "streaming") {
|
|
698
|
+
return chooseStreamWrapper(response.body);
|
|
699
|
+
} else if (response.body != null && responseType === "text") {
|
|
700
|
+
return await response.text();
|
|
701
|
+
} else {
|
|
702
|
+
const text = await response.text();
|
|
703
|
+
if (text.length > 0) {
|
|
704
|
+
try {
|
|
705
|
+
let responseBody = JSON.parse(text);
|
|
706
|
+
return responseBody;
|
|
707
|
+
} catch (err) {
|
|
708
|
+
return {
|
|
709
|
+
ok: false,
|
|
710
|
+
error: {
|
|
711
|
+
reason: "non-json",
|
|
712
|
+
statusCode: response.status,
|
|
713
|
+
rawBody: text
|
|
714
|
+
}
|
|
715
|
+
};
|
|
716
|
+
}
|
|
717
|
+
} else {
|
|
718
|
+
return void 0;
|
|
719
|
+
}
|
|
720
|
+
}
|
|
721
|
+
}
|
|
722
|
+
|
|
723
|
+
// src/core/fetcher/signals.ts
|
|
724
|
+
var TIMEOUT = "timeout";
|
|
725
|
+
function getTimeoutSignal(timeoutMs) {
|
|
726
|
+
const controller = new AbortController();
|
|
727
|
+
const abortId = setTimeout(() => controller.abort(TIMEOUT), timeoutMs);
|
|
728
|
+
return { signal: controller.signal, abortId };
|
|
729
|
+
}
|
|
730
|
+
function anySignal(...args) {
|
|
731
|
+
const signals = args.length === 1 && Array.isArray(args[0]) ? args[0] : args;
|
|
732
|
+
const controller = new AbortController();
|
|
733
|
+
for (const signal of signals) {
|
|
734
|
+
if (signal.aborted) {
|
|
735
|
+
controller.abort(signal?.reason);
|
|
736
|
+
break;
|
|
737
|
+
}
|
|
738
|
+
signal.addEventListener("abort", () => controller.abort(signal?.reason), {
|
|
739
|
+
signal: controller.signal
|
|
740
|
+
});
|
|
741
|
+
}
|
|
742
|
+
return controller.signal;
|
|
743
|
+
}
|
|
744
|
+
|
|
745
|
+
// src/core/fetcher/makeRequest.ts
|
|
746
|
+
var makeRequest = async (fetchFn, url, method, headers, requestBody, timeoutMs, abortSignal, withCredentials, duplex) => {
|
|
747
|
+
const signals = [];
|
|
748
|
+
let timeoutAbortId = void 0;
|
|
749
|
+
if (timeoutMs != null) {
|
|
750
|
+
const { signal, abortId } = getTimeoutSignal(timeoutMs);
|
|
751
|
+
timeoutAbortId = abortId;
|
|
752
|
+
signals.push(signal);
|
|
753
|
+
}
|
|
754
|
+
if (abortSignal != null) {
|
|
755
|
+
signals.push(abortSignal);
|
|
756
|
+
}
|
|
757
|
+
let newSignals = anySignal(signals);
|
|
758
|
+
const response = await fetchFn(url, {
|
|
759
|
+
method,
|
|
760
|
+
headers,
|
|
761
|
+
body: requestBody,
|
|
762
|
+
signal: newSignals,
|
|
763
|
+
credentials: withCredentials ? "include" : void 0,
|
|
764
|
+
// @ts-ignore
|
|
765
|
+
duplex
|
|
766
|
+
});
|
|
767
|
+
if (timeoutAbortId != null) {
|
|
768
|
+
clearTimeout(timeoutAbortId);
|
|
769
|
+
}
|
|
770
|
+
return response;
|
|
771
|
+
};
|
|
772
|
+
|
|
773
|
+
// src/core/fetcher/requestWithRetries.ts
|
|
774
|
+
var INITIAL_RETRY_DELAY = 1e3;
|
|
775
|
+
var MAX_RETRY_DELAY = 6e4;
|
|
776
|
+
var DEFAULT_MAX_RETRIES = 2;
|
|
777
|
+
var JITTER_FACTOR = 0.2;
|
|
778
|
+
function addJitter(delay) {
|
|
779
|
+
const jitterMultiplier = 1 + (Math.random() * 2 - 1) * JITTER_FACTOR;
|
|
780
|
+
return delay * jitterMultiplier;
|
|
781
|
+
}
|
|
782
|
+
async function requestWithRetries(requestFn, maxRetries = DEFAULT_MAX_RETRIES) {
|
|
783
|
+
let response = await requestFn();
|
|
784
|
+
for (let i = 0; i < maxRetries; ++i) {
|
|
785
|
+
if ([408, 429].includes(response.status) || response.status >= 500) {
|
|
786
|
+
const baseDelay = Math.min(INITIAL_RETRY_DELAY * Math.pow(2, i), MAX_RETRY_DELAY);
|
|
787
|
+
const delayWithJitter = addJitter(baseDelay);
|
|
788
|
+
await new Promise((resolve) => setTimeout(resolve, delayWithJitter));
|
|
789
|
+
response = await requestFn();
|
|
790
|
+
} else {
|
|
791
|
+
break;
|
|
792
|
+
}
|
|
793
|
+
}
|
|
794
|
+
return response;
|
|
795
|
+
}
|
|
796
|
+
|
|
797
|
+
// src/core/fetcher/Fetcher.ts
|
|
798
|
+
async function fetcherImpl(args) {
|
|
799
|
+
const headers = {};
|
|
800
|
+
if (args.body !== void 0 && args.contentType != null) {
|
|
801
|
+
headers["Content-Type"] = args.contentType;
|
|
802
|
+
}
|
|
803
|
+
if (args.headers != null) {
|
|
804
|
+
for (const [key, value] of Object.entries(args.headers)) {
|
|
805
|
+
if (value != null) {
|
|
806
|
+
headers[key] = value;
|
|
807
|
+
}
|
|
808
|
+
}
|
|
809
|
+
}
|
|
810
|
+
const url = createRequestUrl(args.url, args.queryParameters);
|
|
811
|
+
const requestBody = await getRequestBody({
|
|
812
|
+
body: args.body,
|
|
813
|
+
type: args.requestType === "json" ? "json" : "other"
|
|
814
|
+
});
|
|
815
|
+
const fetchFn = await getFetchFn();
|
|
816
|
+
try {
|
|
817
|
+
const response = await requestWithRetries(
|
|
818
|
+
async () => makeRequest(
|
|
819
|
+
fetchFn,
|
|
820
|
+
url,
|
|
821
|
+
args.method,
|
|
822
|
+
headers,
|
|
823
|
+
requestBody,
|
|
824
|
+
args.timeoutMs,
|
|
825
|
+
args.abortSignal,
|
|
826
|
+
args.withCredentials,
|
|
827
|
+
args.duplex
|
|
828
|
+
),
|
|
829
|
+
args.maxRetries
|
|
830
|
+
);
|
|
831
|
+
const responseBody = await getResponseBody(response, args.responseType);
|
|
832
|
+
if (response.status >= 200 && response.status < 400) {
|
|
833
|
+
return {
|
|
834
|
+
ok: true,
|
|
835
|
+
body: responseBody,
|
|
836
|
+
headers: response.headers
|
|
837
|
+
};
|
|
838
|
+
} else {
|
|
839
|
+
return {
|
|
840
|
+
ok: false,
|
|
841
|
+
error: {
|
|
842
|
+
reason: "status-code",
|
|
843
|
+
statusCode: response.status,
|
|
844
|
+
body: responseBody
|
|
845
|
+
}
|
|
846
|
+
};
|
|
847
|
+
}
|
|
848
|
+
} catch (error) {
|
|
849
|
+
if (args.abortSignal != null && args.abortSignal.aborted) {
|
|
850
|
+
return {
|
|
851
|
+
ok: false,
|
|
852
|
+
error: {
|
|
853
|
+
reason: "unknown",
|
|
854
|
+
errorMessage: "The user aborted a request"
|
|
855
|
+
}
|
|
856
|
+
};
|
|
857
|
+
} else if (error instanceof Error && error.name === "AbortError") {
|
|
858
|
+
return {
|
|
859
|
+
ok: false,
|
|
860
|
+
error: {
|
|
861
|
+
reason: "timeout"
|
|
862
|
+
}
|
|
863
|
+
};
|
|
864
|
+
} else if (error instanceof Error) {
|
|
865
|
+
return {
|
|
866
|
+
ok: false,
|
|
867
|
+
error: {
|
|
868
|
+
reason: "unknown",
|
|
869
|
+
errorMessage: error.message
|
|
870
|
+
}
|
|
871
|
+
};
|
|
872
|
+
}
|
|
873
|
+
return {
|
|
874
|
+
ok: false,
|
|
875
|
+
error: {
|
|
876
|
+
reason: "unknown",
|
|
877
|
+
errorMessage: toJson(error)
|
|
878
|
+
}
|
|
879
|
+
};
|
|
880
|
+
}
|
|
881
|
+
}
|
|
882
|
+
var fetcher = fetcherImpl;
|
|
883
|
+
|
|
884
|
+
// src/core/fetcher/getHeader.ts
|
|
885
|
+
function getHeader(headers, header) {
|
|
886
|
+
for (const [headerKey, headerValue] of Object.entries(headers)) {
|
|
887
|
+
if (headerKey.toLowerCase() === header.toLowerCase()) {
|
|
888
|
+
return headerValue;
|
|
889
|
+
}
|
|
890
|
+
}
|
|
891
|
+
return void 0;
|
|
892
|
+
}
|
|
893
|
+
|
|
894
|
+
// src/core/fetcher/Supplier.ts
|
|
895
|
+
var Supplier = {
|
|
896
|
+
get: async (supplier) => {
|
|
897
|
+
if (typeof supplier === "function") {
|
|
898
|
+
return supplier();
|
|
899
|
+
} else {
|
|
900
|
+
return supplier;
|
|
901
|
+
}
|
|
902
|
+
}
|
|
903
|
+
};
|
|
904
|
+
|
|
905
|
+
// src/core/schemas/index.ts
|
|
906
|
+
var schemas_exports = {};
|
|
907
|
+
__export(schemas_exports, {
|
|
908
|
+
JsonError: () => JsonError,
|
|
909
|
+
ParseError: () => ParseError,
|
|
910
|
+
any: () => any,
|
|
911
|
+
bigint: () => bigint,
|
|
912
|
+
boolean: () => boolean,
|
|
913
|
+
booleanLiteral: () => booleanLiteral,
|
|
914
|
+
date: () => date,
|
|
915
|
+
discriminant: () => discriminant,
|
|
916
|
+
enum_: () => enum_,
|
|
917
|
+
getObjectLikeUtils: () => getObjectLikeUtils,
|
|
918
|
+
getObjectUtils: () => getObjectUtils,
|
|
919
|
+
getSchemaUtils: () => getSchemaUtils,
|
|
920
|
+
isProperty: () => isProperty,
|
|
921
|
+
lazy: () => lazy,
|
|
922
|
+
lazyObject: () => lazyObject,
|
|
923
|
+
list: () => list,
|
|
924
|
+
number: () => number,
|
|
925
|
+
object: () => object,
|
|
926
|
+
objectWithoutOptionalProperties: () => objectWithoutOptionalProperties,
|
|
927
|
+
optional: () => optional,
|
|
928
|
+
property: () => property,
|
|
929
|
+
record: () => record,
|
|
930
|
+
set: () => set,
|
|
931
|
+
string: () => string,
|
|
932
|
+
stringLiteral: () => stringLiteral,
|
|
933
|
+
transform: () => transform,
|
|
934
|
+
undiscriminatedUnion: () => undiscriminatedUnion,
|
|
935
|
+
union: () => union,
|
|
936
|
+
unknown: () => unknown,
|
|
937
|
+
withParsedProperties: () => withParsedProperties
|
|
938
|
+
});
|
|
939
|
+
|
|
940
|
+
// src/core/schemas/Schema.ts
|
|
941
|
+
var SchemaType = {
|
|
942
|
+
BIGINT: "bigint",
|
|
943
|
+
DATE: "date",
|
|
944
|
+
ENUM: "enum",
|
|
945
|
+
LIST: "list",
|
|
946
|
+
STRING_LITERAL: "stringLiteral",
|
|
947
|
+
BOOLEAN_LITERAL: "booleanLiteral",
|
|
948
|
+
OBJECT: "object",
|
|
949
|
+
ANY: "any",
|
|
950
|
+
BOOLEAN: "boolean",
|
|
951
|
+
NUMBER: "number",
|
|
952
|
+
STRING: "string",
|
|
953
|
+
UNKNOWN: "unknown",
|
|
954
|
+
RECORD: "record",
|
|
955
|
+
SET: "set",
|
|
956
|
+
UNION: "union",
|
|
957
|
+
UNDISCRIMINATED_UNION: "undiscriminatedUnion",
|
|
958
|
+
NULLABLE: "nullable",
|
|
959
|
+
OPTIONAL: "optional",
|
|
960
|
+
OPTIONAL_NULLABLE: "optionalNullable"
|
|
961
|
+
};
|
|
962
|
+
|
|
963
|
+
// src/core/schemas/utils/getErrorMessageForIncorrectType.ts
|
|
964
|
+
function getErrorMessageForIncorrectType(value, expectedType) {
|
|
965
|
+
return `Expected ${expectedType}. Received ${getTypeAsString(value)}.`;
|
|
966
|
+
}
|
|
967
|
+
function getTypeAsString(value) {
|
|
968
|
+
if (Array.isArray(value)) {
|
|
969
|
+
return "list";
|
|
970
|
+
}
|
|
971
|
+
if (value === null) {
|
|
972
|
+
return "null";
|
|
973
|
+
}
|
|
974
|
+
if (value instanceof BigInt) {
|
|
975
|
+
return "BigInt";
|
|
976
|
+
}
|
|
977
|
+
switch (typeof value) {
|
|
978
|
+
case "string":
|
|
979
|
+
return `"${value}"`;
|
|
980
|
+
case "bigint":
|
|
981
|
+
case "number":
|
|
982
|
+
case "boolean":
|
|
983
|
+
case "undefined":
|
|
984
|
+
return `${value}`;
|
|
985
|
+
}
|
|
986
|
+
return typeof value;
|
|
987
|
+
}
|
|
988
|
+
|
|
989
|
+
// src/core/schemas/utils/maybeSkipValidation.ts
|
|
990
|
+
function maybeSkipValidation(schema) {
|
|
991
|
+
return {
|
|
992
|
+
...schema,
|
|
993
|
+
json: transformAndMaybeSkipValidation(schema.json),
|
|
994
|
+
parse: transformAndMaybeSkipValidation(schema.parse)
|
|
995
|
+
};
|
|
996
|
+
}
|
|
997
|
+
function transformAndMaybeSkipValidation(transform2) {
|
|
998
|
+
return (value, opts) => {
|
|
999
|
+
const transformed = transform2(value, opts);
|
|
1000
|
+
const { skipValidation = false } = opts ?? {};
|
|
1001
|
+
if (!transformed.ok && skipValidation) {
|
|
1002
|
+
console.warn(
|
|
1003
|
+
[
|
|
1004
|
+
"Failed to validate.",
|
|
1005
|
+
...transformed.errors.map(
|
|
1006
|
+
(error) => " - " + (error.path.length > 0 ? `${error.path.join(".")}: ${error.message}` : error.message)
|
|
1007
|
+
)
|
|
1008
|
+
].join("\n")
|
|
1009
|
+
);
|
|
1010
|
+
return {
|
|
1011
|
+
ok: true,
|
|
1012
|
+
value
|
|
1013
|
+
};
|
|
1014
|
+
} else {
|
|
1015
|
+
return transformed;
|
|
1016
|
+
}
|
|
1017
|
+
};
|
|
1018
|
+
}
|
|
1019
|
+
|
|
1020
|
+
// src/core/schemas/builders/schema-utils/stringifyValidationErrors.ts
|
|
1021
|
+
function stringifyValidationError(error) {
|
|
1022
|
+
if (error.path.length === 0) {
|
|
1023
|
+
return error.message;
|
|
1024
|
+
}
|
|
1025
|
+
return `${error.path.join(" -> ")}: ${error.message}`;
|
|
1026
|
+
}
|
|
1027
|
+
|
|
1028
|
+
// src/core/schemas/builders/schema-utils/JsonError.ts
|
|
1029
|
+
var JsonError = class _JsonError extends Error {
|
|
1030
|
+
constructor(errors) {
|
|
1031
|
+
super(errors.map(stringifyValidationError).join("; "));
|
|
1032
|
+
this.errors = errors;
|
|
1033
|
+
Object.setPrototypeOf(this, _JsonError.prototype);
|
|
1034
|
+
}
|
|
1035
|
+
};
|
|
1036
|
+
|
|
1037
|
+
// src/core/schemas/builders/schema-utils/ParseError.ts
|
|
1038
|
+
var ParseError = class _ParseError extends Error {
|
|
1039
|
+
constructor(errors) {
|
|
1040
|
+
super(errors.map(stringifyValidationError).join("; "));
|
|
1041
|
+
this.errors = errors;
|
|
1042
|
+
Object.setPrototypeOf(this, _ParseError.prototype);
|
|
1043
|
+
}
|
|
1044
|
+
};
|
|
1045
|
+
|
|
1046
|
+
// src/core/schemas/builders/schema-utils/getSchemaUtils.ts
|
|
1047
|
+
function getSchemaUtils(schema) {
|
|
1048
|
+
return {
|
|
1049
|
+
nullable: () => nullable(schema),
|
|
1050
|
+
optional: () => optional(schema),
|
|
1051
|
+
optionalNullable: () => optionalNullable(schema),
|
|
1052
|
+
transform: (transformer) => transform(schema, transformer),
|
|
1053
|
+
parseOrThrow: (raw, opts) => {
|
|
1054
|
+
const parsed = schema.parse(raw, opts);
|
|
1055
|
+
if (parsed.ok) {
|
|
1056
|
+
return parsed.value;
|
|
1057
|
+
}
|
|
1058
|
+
throw new ParseError(parsed.errors);
|
|
1059
|
+
},
|
|
1060
|
+
jsonOrThrow: (parsed, opts) => {
|
|
1061
|
+
const raw = schema.json(parsed, opts);
|
|
1062
|
+
if (raw.ok) {
|
|
1063
|
+
return raw.value;
|
|
1064
|
+
}
|
|
1065
|
+
throw new JsonError(raw.errors);
|
|
1066
|
+
}
|
|
1067
|
+
};
|
|
1068
|
+
}
|
|
1069
|
+
function nullable(schema) {
|
|
1070
|
+
const baseSchema = {
|
|
1071
|
+
parse: (raw, opts) => {
|
|
1072
|
+
if (raw == null) {
|
|
1073
|
+
return {
|
|
1074
|
+
ok: true,
|
|
1075
|
+
value: null
|
|
1076
|
+
};
|
|
1077
|
+
}
|
|
1078
|
+
return schema.parse(raw, opts);
|
|
1079
|
+
},
|
|
1080
|
+
json: (parsed, opts) => {
|
|
1081
|
+
if (parsed == null) {
|
|
1082
|
+
return {
|
|
1083
|
+
ok: true,
|
|
1084
|
+
value: null
|
|
1085
|
+
};
|
|
1086
|
+
}
|
|
1087
|
+
return schema.json(parsed, opts);
|
|
1088
|
+
},
|
|
1089
|
+
getType: () => SchemaType.NULLABLE
|
|
1090
|
+
};
|
|
1091
|
+
return {
|
|
1092
|
+
...baseSchema,
|
|
1093
|
+
...getSchemaUtils(baseSchema)
|
|
1094
|
+
};
|
|
1095
|
+
}
|
|
1096
|
+
function optional(schema) {
|
|
1097
|
+
const baseSchema = {
|
|
1098
|
+
parse: (raw, opts) => {
|
|
1099
|
+
if (raw == null) {
|
|
1100
|
+
return {
|
|
1101
|
+
ok: true,
|
|
1102
|
+
value: void 0
|
|
1103
|
+
};
|
|
1104
|
+
}
|
|
1105
|
+
return schema.parse(raw, opts);
|
|
1106
|
+
},
|
|
1107
|
+
json: (parsed, opts) => {
|
|
1108
|
+
if (opts?.omitUndefined && parsed === void 0) {
|
|
1109
|
+
return {
|
|
1110
|
+
ok: true,
|
|
1111
|
+
value: void 0
|
|
1112
|
+
};
|
|
1113
|
+
}
|
|
1114
|
+
if (parsed == null) {
|
|
1115
|
+
return {
|
|
1116
|
+
ok: true,
|
|
1117
|
+
value: null
|
|
1118
|
+
};
|
|
1119
|
+
}
|
|
1120
|
+
return schema.json(parsed, opts);
|
|
1121
|
+
},
|
|
1122
|
+
getType: () => SchemaType.OPTIONAL
|
|
1123
|
+
};
|
|
1124
|
+
return {
|
|
1125
|
+
...baseSchema,
|
|
1126
|
+
...getSchemaUtils(baseSchema)
|
|
1127
|
+
};
|
|
1128
|
+
}
|
|
1129
|
+
function optionalNullable(schema) {
|
|
1130
|
+
const baseSchema = {
|
|
1131
|
+
parse: (raw, opts) => {
|
|
1132
|
+
if (raw === void 0) {
|
|
1133
|
+
return {
|
|
1134
|
+
ok: true,
|
|
1135
|
+
value: void 0
|
|
1136
|
+
};
|
|
1137
|
+
}
|
|
1138
|
+
if (raw === null) {
|
|
1139
|
+
return {
|
|
1140
|
+
ok: true,
|
|
1141
|
+
value: null
|
|
1142
|
+
};
|
|
1143
|
+
}
|
|
1144
|
+
return schema.parse(raw, opts);
|
|
1145
|
+
},
|
|
1146
|
+
json: (parsed, opts) => {
|
|
1147
|
+
if (parsed === void 0) {
|
|
1148
|
+
return {
|
|
1149
|
+
ok: true,
|
|
1150
|
+
value: void 0
|
|
1151
|
+
};
|
|
1152
|
+
}
|
|
1153
|
+
if (parsed === null) {
|
|
1154
|
+
return {
|
|
1155
|
+
ok: true,
|
|
1156
|
+
value: null
|
|
1157
|
+
};
|
|
1158
|
+
}
|
|
1159
|
+
return schema.json(parsed, opts);
|
|
1160
|
+
},
|
|
1161
|
+
getType: () => SchemaType.OPTIONAL_NULLABLE
|
|
1162
|
+
};
|
|
1163
|
+
return {
|
|
1164
|
+
...baseSchema,
|
|
1165
|
+
...getSchemaUtils(baseSchema)
|
|
1166
|
+
};
|
|
1167
|
+
}
|
|
1168
|
+
function transform(schema, transformer) {
|
|
1169
|
+
const baseSchema = {
|
|
1170
|
+
parse: (raw, opts) => {
|
|
1171
|
+
const parsed = schema.parse(raw, opts);
|
|
1172
|
+
if (!parsed.ok) {
|
|
1173
|
+
return parsed;
|
|
1174
|
+
}
|
|
1175
|
+
return {
|
|
1176
|
+
ok: true,
|
|
1177
|
+
value: transformer.transform(parsed.value)
|
|
1178
|
+
};
|
|
1179
|
+
},
|
|
1180
|
+
json: (transformed, opts) => {
|
|
1181
|
+
const parsed = transformer.untransform(transformed);
|
|
1182
|
+
return schema.json(parsed, opts);
|
|
1183
|
+
},
|
|
1184
|
+
getType: () => schema.getType()
|
|
1185
|
+
};
|
|
1186
|
+
return {
|
|
1187
|
+
...baseSchema,
|
|
1188
|
+
...getSchemaUtils(baseSchema)
|
|
1189
|
+
};
|
|
1190
|
+
}
|
|
1191
|
+
|
|
1192
|
+
// src/core/schemas/builders/bigint/bigint.ts
|
|
1193
|
+
function bigint() {
|
|
1194
|
+
const baseSchema = {
|
|
1195
|
+
parse: (raw, { breadcrumbsPrefix = [] } = {}) => {
|
|
1196
|
+
if (typeof raw === "bigint") {
|
|
1197
|
+
return {
|
|
1198
|
+
ok: true,
|
|
1199
|
+
value: raw
|
|
1200
|
+
};
|
|
1201
|
+
}
|
|
1202
|
+
if (typeof raw === "number") {
|
|
1203
|
+
return {
|
|
1204
|
+
ok: true,
|
|
1205
|
+
value: BigInt(raw)
|
|
1206
|
+
};
|
|
1207
|
+
}
|
|
1208
|
+
return {
|
|
1209
|
+
ok: false,
|
|
1210
|
+
errors: [
|
|
1211
|
+
{
|
|
1212
|
+
path: breadcrumbsPrefix,
|
|
1213
|
+
message: getErrorMessageForIncorrectType(raw, "bigint | number")
|
|
1214
|
+
}
|
|
1215
|
+
]
|
|
1216
|
+
};
|
|
1217
|
+
},
|
|
1218
|
+
json: (bigint2, { breadcrumbsPrefix = [] } = {}) => {
|
|
1219
|
+
if (typeof bigint2 !== "bigint") {
|
|
1220
|
+
return {
|
|
1221
|
+
ok: false,
|
|
1222
|
+
errors: [
|
|
1223
|
+
{
|
|
1224
|
+
path: breadcrumbsPrefix,
|
|
1225
|
+
message: getErrorMessageForIncorrectType(bigint2, "bigint")
|
|
1226
|
+
}
|
|
1227
|
+
]
|
|
1228
|
+
};
|
|
1229
|
+
}
|
|
1230
|
+
return {
|
|
1231
|
+
ok: true,
|
|
1232
|
+
value: bigint2
|
|
1233
|
+
};
|
|
1234
|
+
},
|
|
1235
|
+
getType: () => SchemaType.BIGINT
|
|
1236
|
+
};
|
|
1237
|
+
return {
|
|
1238
|
+
...maybeSkipValidation(baseSchema),
|
|
1239
|
+
...getSchemaUtils(baseSchema)
|
|
1240
|
+
};
|
|
1241
|
+
}
|
|
1242
|
+
|
|
1243
|
+
// src/core/schemas/builders/date/date.ts
|
|
1244
|
+
var ISO_8601_REGEX = /^([+-]?\d{4}(?!\d{2}\b))((-?)((0[1-9]|1[0-2])(\3([12]\d|0[1-9]|3[01]))?|W([0-4]\d|5[0-2])(-?[1-7])?|(00[1-9]|0[1-9]\d|[12]\d{2}|3([0-5]\d|6[1-6])))([T\s]((([01]\d|2[0-3])((:?)[0-5]\d)?|24:?00)([.,]\d+(?!:))?)?(\17[0-5]\d([.,]\d+)?)?([zZ]|([+-])([01]\d|2[0-3]):?([0-5]\d)?)?)?)?$/;
|
|
1245
|
+
function date() {
|
|
1246
|
+
const baseSchema = {
|
|
1247
|
+
parse: (raw, { breadcrumbsPrefix = [] } = {}) => {
|
|
1248
|
+
if (typeof raw !== "string") {
|
|
1249
|
+
return {
|
|
1250
|
+
ok: false,
|
|
1251
|
+
errors: [
|
|
1252
|
+
{
|
|
1253
|
+
path: breadcrumbsPrefix,
|
|
1254
|
+
message: getErrorMessageForIncorrectType(raw, "string")
|
|
1255
|
+
}
|
|
1256
|
+
]
|
|
1257
|
+
};
|
|
1258
|
+
}
|
|
1259
|
+
if (!ISO_8601_REGEX.test(raw)) {
|
|
1260
|
+
return {
|
|
1261
|
+
ok: false,
|
|
1262
|
+
errors: [
|
|
1263
|
+
{
|
|
1264
|
+
path: breadcrumbsPrefix,
|
|
1265
|
+
message: getErrorMessageForIncorrectType(raw, "ISO 8601 date string")
|
|
1266
|
+
}
|
|
1267
|
+
]
|
|
1268
|
+
};
|
|
1269
|
+
}
|
|
1270
|
+
return {
|
|
1271
|
+
ok: true,
|
|
1272
|
+
value: new Date(raw)
|
|
1273
|
+
};
|
|
1274
|
+
},
|
|
1275
|
+
json: (date2, { breadcrumbsPrefix = [] } = {}) => {
|
|
1276
|
+
if (date2 instanceof Date) {
|
|
1277
|
+
return {
|
|
1278
|
+
ok: true,
|
|
1279
|
+
value: date2.toISOString()
|
|
1280
|
+
};
|
|
1281
|
+
} else {
|
|
1282
|
+
return {
|
|
1283
|
+
ok: false,
|
|
1284
|
+
errors: [
|
|
1285
|
+
{
|
|
1286
|
+
path: breadcrumbsPrefix,
|
|
1287
|
+
message: getErrorMessageForIncorrectType(date2, "Date object")
|
|
1288
|
+
}
|
|
1289
|
+
]
|
|
1290
|
+
};
|
|
1291
|
+
}
|
|
1292
|
+
},
|
|
1293
|
+
getType: () => SchemaType.DATE
|
|
1294
|
+
};
|
|
1295
|
+
return {
|
|
1296
|
+
...maybeSkipValidation(baseSchema),
|
|
1297
|
+
...getSchemaUtils(baseSchema)
|
|
1298
|
+
};
|
|
1299
|
+
}
|
|
1300
|
+
|
|
1301
|
+
// src/core/schemas/utils/createIdentitySchemaCreator.ts
|
|
1302
|
+
function createIdentitySchemaCreator(schemaType, validate) {
|
|
1303
|
+
return () => {
|
|
1304
|
+
const baseSchema = {
|
|
1305
|
+
parse: validate,
|
|
1306
|
+
json: validate,
|
|
1307
|
+
getType: () => schemaType
|
|
1308
|
+
};
|
|
1309
|
+
return {
|
|
1310
|
+
...maybeSkipValidation(baseSchema),
|
|
1311
|
+
...getSchemaUtils(baseSchema)
|
|
1312
|
+
};
|
|
1313
|
+
};
|
|
1314
|
+
}
|
|
1315
|
+
|
|
1316
|
+
// src/core/schemas/builders/enum/enum.ts
|
|
1317
|
+
function enum_(values) {
|
|
1318
|
+
const validValues = new Set(values);
|
|
1319
|
+
const schemaCreator = createIdentitySchemaCreator(
|
|
1320
|
+
SchemaType.ENUM,
|
|
1321
|
+
(value, { allowUnrecognizedEnumValues, breadcrumbsPrefix = [] } = {}) => {
|
|
1322
|
+
if (typeof value !== "string") {
|
|
1323
|
+
return {
|
|
1324
|
+
ok: false,
|
|
1325
|
+
errors: [
|
|
1326
|
+
{
|
|
1327
|
+
path: breadcrumbsPrefix,
|
|
1328
|
+
message: getErrorMessageForIncorrectType(value, "string")
|
|
1329
|
+
}
|
|
1330
|
+
]
|
|
1331
|
+
};
|
|
1332
|
+
}
|
|
1333
|
+
if (!validValues.has(value) && !allowUnrecognizedEnumValues) {
|
|
1334
|
+
return {
|
|
1335
|
+
ok: false,
|
|
1336
|
+
errors: [
|
|
1337
|
+
{
|
|
1338
|
+
path: breadcrumbsPrefix,
|
|
1339
|
+
message: getErrorMessageForIncorrectType(value, "enum")
|
|
1340
|
+
}
|
|
1341
|
+
]
|
|
1342
|
+
};
|
|
1343
|
+
}
|
|
1344
|
+
return {
|
|
1345
|
+
ok: true,
|
|
1346
|
+
value
|
|
1347
|
+
};
|
|
1348
|
+
}
|
|
1349
|
+
);
|
|
1350
|
+
return schemaCreator();
|
|
1351
|
+
}
|
|
1352
|
+
|
|
1353
|
+
// src/core/schemas/builders/lazy/lazy.ts
|
|
1354
|
+
function lazy(getter) {
|
|
1355
|
+
const baseSchema = constructLazyBaseSchema(getter);
|
|
1356
|
+
return {
|
|
1357
|
+
...baseSchema,
|
|
1358
|
+
...getSchemaUtils(baseSchema)
|
|
1359
|
+
};
|
|
1360
|
+
}
|
|
1361
|
+
function constructLazyBaseSchema(getter) {
|
|
1362
|
+
return {
|
|
1363
|
+
parse: (raw, opts) => getMemoizedSchema(getter).parse(raw, opts),
|
|
1364
|
+
json: (parsed, opts) => getMemoizedSchema(getter).json(parsed, opts),
|
|
1365
|
+
getType: () => getMemoizedSchema(getter).getType()
|
|
1366
|
+
};
|
|
1367
|
+
}
|
|
1368
|
+
function getMemoizedSchema(getter) {
|
|
1369
|
+
const castedGetter = getter;
|
|
1370
|
+
if (castedGetter.__zurg_memoized == null) {
|
|
1371
|
+
castedGetter.__zurg_memoized = getter();
|
|
1372
|
+
}
|
|
1373
|
+
return castedGetter.__zurg_memoized;
|
|
1374
|
+
}
|
|
1375
|
+
|
|
1376
|
+
// src/core/schemas/utils/entries.ts
|
|
1377
|
+
function entries(object2) {
|
|
1378
|
+
return Object.entries(object2);
|
|
1379
|
+
}
|
|
1380
|
+
|
|
1381
|
+
// src/core/schemas/utils/filterObject.ts
|
|
1382
|
+
function filterObject(obj, keysToInclude) {
|
|
1383
|
+
const keysToIncludeSet = new Set(keysToInclude);
|
|
1384
|
+
return Object.entries(obj).reduce(
|
|
1385
|
+
(acc, [key, value]) => {
|
|
1386
|
+
if (keysToIncludeSet.has(key)) {
|
|
1387
|
+
acc[key] = value;
|
|
1388
|
+
}
|
|
1389
|
+
return acc;
|
|
1390
|
+
},
|
|
1391
|
+
{}
|
|
1392
|
+
);
|
|
1393
|
+
}
|
|
1394
|
+
|
|
1395
|
+
// src/core/schemas/utils/isPlainObject.ts
|
|
1396
|
+
function isPlainObject(value) {
|
|
1397
|
+
if (typeof value !== "object" || value === null) {
|
|
1398
|
+
return false;
|
|
1399
|
+
}
|
|
1400
|
+
if (Object.getPrototypeOf(value) === null) {
|
|
1401
|
+
return true;
|
|
1402
|
+
}
|
|
1403
|
+
let proto = value;
|
|
1404
|
+
while (Object.getPrototypeOf(proto) !== null) {
|
|
1405
|
+
proto = Object.getPrototypeOf(proto);
|
|
1406
|
+
}
|
|
1407
|
+
return Object.getPrototypeOf(value) === proto;
|
|
1408
|
+
}
|
|
1409
|
+
|
|
1410
|
+
// src/core/schemas/utils/keys.ts
|
|
1411
|
+
function keys(object2) {
|
|
1412
|
+
return Object.keys(object2);
|
|
1413
|
+
}
|
|
1414
|
+
|
|
1415
|
+
// src/core/schemas/utils/partition.ts
|
|
1416
|
+
function partition(items, predicate) {
|
|
1417
|
+
const trueItems = [], falseItems = [];
|
|
1418
|
+
for (const item of items) {
|
|
1419
|
+
if (predicate(item)) {
|
|
1420
|
+
trueItems.push(item);
|
|
1421
|
+
} else {
|
|
1422
|
+
falseItems.push(item);
|
|
1423
|
+
}
|
|
1424
|
+
}
|
|
1425
|
+
return [trueItems, falseItems];
|
|
1426
|
+
}
|
|
1427
|
+
|
|
1428
|
+
// src/core/schemas/builders/object-like/getObjectLikeUtils.ts
|
|
1429
|
+
function getObjectLikeUtils(schema) {
|
|
1430
|
+
return {
|
|
1431
|
+
withParsedProperties: (properties) => withParsedProperties(schema, properties)
|
|
1432
|
+
};
|
|
1433
|
+
}
|
|
1434
|
+
function withParsedProperties(objectLike, properties) {
|
|
1435
|
+
const objectSchema = {
|
|
1436
|
+
parse: (raw, opts) => {
|
|
1437
|
+
const parsedObject = objectLike.parse(raw, opts);
|
|
1438
|
+
if (!parsedObject.ok) {
|
|
1439
|
+
return parsedObject;
|
|
1440
|
+
}
|
|
1441
|
+
const additionalProperties = Object.entries(properties).reduce(
|
|
1442
|
+
(processed, [key, value]) => {
|
|
1443
|
+
return {
|
|
1444
|
+
...processed,
|
|
1445
|
+
[key]: typeof value === "function" ? value(parsedObject.value) : value
|
|
1446
|
+
};
|
|
1447
|
+
},
|
|
1448
|
+
{}
|
|
1449
|
+
);
|
|
1450
|
+
return {
|
|
1451
|
+
ok: true,
|
|
1452
|
+
value: {
|
|
1453
|
+
...parsedObject.value,
|
|
1454
|
+
...additionalProperties
|
|
1455
|
+
}
|
|
1456
|
+
};
|
|
1457
|
+
},
|
|
1458
|
+
json: (parsed, opts) => {
|
|
1459
|
+
if (!isPlainObject(parsed)) {
|
|
1460
|
+
return {
|
|
1461
|
+
ok: false,
|
|
1462
|
+
errors: [
|
|
1463
|
+
{
|
|
1464
|
+
path: opts?.breadcrumbsPrefix ?? [],
|
|
1465
|
+
message: getErrorMessageForIncorrectType(parsed, "object")
|
|
1466
|
+
}
|
|
1467
|
+
]
|
|
1468
|
+
};
|
|
1469
|
+
}
|
|
1470
|
+
const addedPropertyKeys = new Set(Object.keys(properties));
|
|
1471
|
+
const parsedWithoutAddedProperties = filterObject(
|
|
1472
|
+
parsed,
|
|
1473
|
+
Object.keys(parsed).filter((key) => !addedPropertyKeys.has(key))
|
|
1474
|
+
);
|
|
1475
|
+
return objectLike.json(parsedWithoutAddedProperties, opts);
|
|
1476
|
+
},
|
|
1477
|
+
getType: () => objectLike.getType()
|
|
1478
|
+
};
|
|
1479
|
+
return {
|
|
1480
|
+
...objectSchema,
|
|
1481
|
+
...getSchemaUtils(objectSchema),
|
|
1482
|
+
...getObjectLikeUtils(objectSchema)
|
|
1483
|
+
};
|
|
1484
|
+
}
|
|
1485
|
+
|
|
1486
|
+
// src/core/schemas/builders/object/property.ts
|
|
1487
|
+
function property(rawKey, valueSchema) {
|
|
1488
|
+
return {
|
|
1489
|
+
rawKey,
|
|
1490
|
+
valueSchema,
|
|
1491
|
+
isProperty: true
|
|
1492
|
+
};
|
|
1493
|
+
}
|
|
1494
|
+
function isProperty(maybeProperty) {
|
|
1495
|
+
return maybeProperty.isProperty;
|
|
1496
|
+
}
|
|
1497
|
+
|
|
1498
|
+
// src/core/schemas/builders/object/object.ts
|
|
1499
|
+
function object(schemas) {
|
|
1500
|
+
const baseSchema = {
|
|
1501
|
+
_getRawProperties: () => Object.entries(schemas).map(
|
|
1502
|
+
([parsedKey, propertySchema]) => isProperty(propertySchema) ? propertySchema.rawKey : parsedKey
|
|
1503
|
+
),
|
|
1504
|
+
_getParsedProperties: () => keys(schemas),
|
|
1505
|
+
parse: (raw, opts) => {
|
|
1506
|
+
const rawKeyToProperty = {};
|
|
1507
|
+
const requiredKeys = [];
|
|
1508
|
+
for (const [parsedKey, schemaOrObjectProperty] of entries(schemas)) {
|
|
1509
|
+
const rawKey = isProperty(schemaOrObjectProperty) ? schemaOrObjectProperty.rawKey : parsedKey;
|
|
1510
|
+
const valueSchema = isProperty(schemaOrObjectProperty) ? schemaOrObjectProperty.valueSchema : schemaOrObjectProperty;
|
|
1511
|
+
const property2 = {
|
|
1512
|
+
rawKey,
|
|
1513
|
+
parsedKey,
|
|
1514
|
+
valueSchema
|
|
1515
|
+
};
|
|
1516
|
+
rawKeyToProperty[rawKey] = property2;
|
|
1517
|
+
if (isSchemaRequired(valueSchema)) {
|
|
1518
|
+
requiredKeys.push(rawKey);
|
|
1519
|
+
}
|
|
1520
|
+
}
|
|
1521
|
+
return validateAndTransformObject({
|
|
1522
|
+
value: raw,
|
|
1523
|
+
requiredKeys,
|
|
1524
|
+
getProperty: (rawKey) => {
|
|
1525
|
+
const property2 = rawKeyToProperty[rawKey];
|
|
1526
|
+
if (property2 == null) {
|
|
1527
|
+
return void 0;
|
|
1528
|
+
}
|
|
1529
|
+
return {
|
|
1530
|
+
transformedKey: property2.parsedKey,
|
|
1531
|
+
transform: (propertyValue) => property2.valueSchema.parse(propertyValue, {
|
|
1532
|
+
...opts,
|
|
1533
|
+
breadcrumbsPrefix: [...opts?.breadcrumbsPrefix ?? [], rawKey]
|
|
1534
|
+
})
|
|
1535
|
+
};
|
|
1536
|
+
},
|
|
1537
|
+
unrecognizedObjectKeys: opts?.unrecognizedObjectKeys,
|
|
1538
|
+
skipValidation: opts?.skipValidation,
|
|
1539
|
+
breadcrumbsPrefix: opts?.breadcrumbsPrefix,
|
|
1540
|
+
omitUndefined: opts?.omitUndefined
|
|
1541
|
+
});
|
|
1542
|
+
},
|
|
1543
|
+
json: (parsed, opts) => {
|
|
1544
|
+
const requiredKeys = [];
|
|
1545
|
+
for (const [parsedKey, schemaOrObjectProperty] of entries(schemas)) {
|
|
1546
|
+
const valueSchema = isProperty(schemaOrObjectProperty) ? schemaOrObjectProperty.valueSchema : schemaOrObjectProperty;
|
|
1547
|
+
if (isSchemaRequired(valueSchema)) {
|
|
1548
|
+
requiredKeys.push(parsedKey);
|
|
1549
|
+
}
|
|
1550
|
+
}
|
|
1551
|
+
return validateAndTransformObject({
|
|
1552
|
+
value: parsed,
|
|
1553
|
+
requiredKeys,
|
|
1554
|
+
getProperty: (parsedKey) => {
|
|
1555
|
+
const property2 = schemas[parsedKey];
|
|
1556
|
+
if (property2 == null) {
|
|
1557
|
+
return void 0;
|
|
1558
|
+
}
|
|
1559
|
+
if (isProperty(property2)) {
|
|
1560
|
+
return {
|
|
1561
|
+
transformedKey: property2.rawKey,
|
|
1562
|
+
transform: (propertyValue) => property2.valueSchema.json(propertyValue, {
|
|
1563
|
+
...opts,
|
|
1564
|
+
breadcrumbsPrefix: [...opts?.breadcrumbsPrefix ?? [], parsedKey]
|
|
1565
|
+
})
|
|
1566
|
+
};
|
|
1567
|
+
} else {
|
|
1568
|
+
return {
|
|
1569
|
+
transformedKey: parsedKey,
|
|
1570
|
+
transform: (propertyValue) => property2.json(propertyValue, {
|
|
1571
|
+
...opts,
|
|
1572
|
+
breadcrumbsPrefix: [...opts?.breadcrumbsPrefix ?? [], parsedKey]
|
|
1573
|
+
})
|
|
1574
|
+
};
|
|
1575
|
+
}
|
|
1576
|
+
},
|
|
1577
|
+
unrecognizedObjectKeys: opts?.unrecognizedObjectKeys,
|
|
1578
|
+
skipValidation: opts?.skipValidation,
|
|
1579
|
+
breadcrumbsPrefix: opts?.breadcrumbsPrefix,
|
|
1580
|
+
omitUndefined: opts?.omitUndefined
|
|
1581
|
+
});
|
|
1582
|
+
},
|
|
1583
|
+
getType: () => SchemaType.OBJECT
|
|
1584
|
+
};
|
|
1585
|
+
return {
|
|
1586
|
+
...maybeSkipValidation(baseSchema),
|
|
1587
|
+
...getSchemaUtils(baseSchema),
|
|
1588
|
+
...getObjectLikeUtils(baseSchema),
|
|
1589
|
+
...getObjectUtils(baseSchema)
|
|
1590
|
+
};
|
|
1591
|
+
}
|
|
1592
|
+
function validateAndTransformObject({
|
|
1593
|
+
value,
|
|
1594
|
+
requiredKeys,
|
|
1595
|
+
getProperty,
|
|
1596
|
+
unrecognizedObjectKeys = "fail",
|
|
1597
|
+
skipValidation = false,
|
|
1598
|
+
breadcrumbsPrefix = []
|
|
1599
|
+
}) {
|
|
1600
|
+
if (!isPlainObject(value)) {
|
|
1601
|
+
return {
|
|
1602
|
+
ok: false,
|
|
1603
|
+
errors: [
|
|
1604
|
+
{
|
|
1605
|
+
path: breadcrumbsPrefix,
|
|
1606
|
+
message: getErrorMessageForIncorrectType(value, "object")
|
|
1607
|
+
}
|
|
1608
|
+
]
|
|
1609
|
+
};
|
|
1610
|
+
}
|
|
1611
|
+
const missingRequiredKeys = new Set(requiredKeys);
|
|
1612
|
+
const errors = [];
|
|
1613
|
+
const transformed = {};
|
|
1614
|
+
for (const [preTransformedKey, preTransformedItemValue] of Object.entries(value)) {
|
|
1615
|
+
const property2 = getProperty(preTransformedKey);
|
|
1616
|
+
if (property2 != null) {
|
|
1617
|
+
missingRequiredKeys.delete(preTransformedKey);
|
|
1618
|
+
const value2 = property2.transform(preTransformedItemValue);
|
|
1619
|
+
if (value2.ok) {
|
|
1620
|
+
transformed[property2.transformedKey] = value2.value;
|
|
1621
|
+
} else {
|
|
1622
|
+
transformed[preTransformedKey] = preTransformedItemValue;
|
|
1623
|
+
errors.push(...value2.errors);
|
|
1624
|
+
}
|
|
1625
|
+
} else {
|
|
1626
|
+
switch (unrecognizedObjectKeys) {
|
|
1627
|
+
case "fail":
|
|
1628
|
+
errors.push({
|
|
1629
|
+
path: [...breadcrumbsPrefix, preTransformedKey],
|
|
1630
|
+
message: `Unexpected key "${preTransformedKey}"`
|
|
1631
|
+
});
|
|
1632
|
+
break;
|
|
1633
|
+
case "strip":
|
|
1634
|
+
break;
|
|
1635
|
+
case "passthrough":
|
|
1636
|
+
transformed[preTransformedKey] = preTransformedItemValue;
|
|
1637
|
+
break;
|
|
1638
|
+
}
|
|
1639
|
+
}
|
|
1640
|
+
}
|
|
1641
|
+
errors.push(
|
|
1642
|
+
...requiredKeys.filter((key) => missingRequiredKeys.has(key)).map((key) => ({
|
|
1643
|
+
path: breadcrumbsPrefix,
|
|
1644
|
+
message: `Missing required key "${key}"`
|
|
1645
|
+
}))
|
|
1646
|
+
);
|
|
1647
|
+
if (errors.length === 0 || skipValidation) {
|
|
1648
|
+
return {
|
|
1649
|
+
ok: true,
|
|
1650
|
+
value: transformed
|
|
1651
|
+
};
|
|
1652
|
+
} else {
|
|
1653
|
+
return {
|
|
1654
|
+
ok: false,
|
|
1655
|
+
errors
|
|
1656
|
+
};
|
|
1657
|
+
}
|
|
1658
|
+
}
|
|
1659
|
+
function getObjectUtils(schema) {
|
|
1660
|
+
return {
|
|
1661
|
+
extend: (extension) => {
|
|
1662
|
+
const baseSchema = {
|
|
1663
|
+
_getParsedProperties: () => [...schema._getParsedProperties(), ...extension._getParsedProperties()],
|
|
1664
|
+
_getRawProperties: () => [...schema._getRawProperties(), ...extension._getRawProperties()],
|
|
1665
|
+
parse: (raw, opts) => {
|
|
1666
|
+
return validateAndTransformExtendedObject({
|
|
1667
|
+
extensionKeys: extension._getRawProperties(),
|
|
1668
|
+
value: raw,
|
|
1669
|
+
transformBase: (rawBase) => schema.parse(rawBase, opts),
|
|
1670
|
+
transformExtension: (rawExtension) => extension.parse(rawExtension, opts)
|
|
1671
|
+
});
|
|
1672
|
+
},
|
|
1673
|
+
json: (parsed, opts) => {
|
|
1674
|
+
return validateAndTransformExtendedObject({
|
|
1675
|
+
extensionKeys: extension._getParsedProperties(),
|
|
1676
|
+
value: parsed,
|
|
1677
|
+
transformBase: (parsedBase) => schema.json(parsedBase, opts),
|
|
1678
|
+
transformExtension: (parsedExtension) => extension.json(parsedExtension, opts)
|
|
1679
|
+
});
|
|
1680
|
+
},
|
|
1681
|
+
getType: () => SchemaType.OBJECT
|
|
1682
|
+
};
|
|
1683
|
+
return {
|
|
1684
|
+
...baseSchema,
|
|
1685
|
+
...getSchemaUtils(baseSchema),
|
|
1686
|
+
...getObjectLikeUtils(baseSchema),
|
|
1687
|
+
...getObjectUtils(baseSchema)
|
|
1688
|
+
};
|
|
1689
|
+
},
|
|
1690
|
+
passthrough: () => {
|
|
1691
|
+
const baseSchema = {
|
|
1692
|
+
_getParsedProperties: () => schema._getParsedProperties(),
|
|
1693
|
+
_getRawProperties: () => schema._getRawProperties(),
|
|
1694
|
+
parse: (raw, opts) => {
|
|
1695
|
+
const transformed = schema.parse(raw, { ...opts, unrecognizedObjectKeys: "passthrough" });
|
|
1696
|
+
if (!transformed.ok) {
|
|
1697
|
+
return transformed;
|
|
1698
|
+
}
|
|
1699
|
+
return {
|
|
1700
|
+
ok: true,
|
|
1701
|
+
value: {
|
|
1702
|
+
...raw,
|
|
1703
|
+
...transformed.value
|
|
1704
|
+
}
|
|
1705
|
+
};
|
|
1706
|
+
},
|
|
1707
|
+
json: (parsed, opts) => {
|
|
1708
|
+
const transformed = schema.json(parsed, { ...opts, unrecognizedObjectKeys: "passthrough" });
|
|
1709
|
+
if (!transformed.ok) {
|
|
1710
|
+
return transformed;
|
|
1711
|
+
}
|
|
1712
|
+
return {
|
|
1713
|
+
ok: true,
|
|
1714
|
+
value: {
|
|
1715
|
+
...parsed,
|
|
1716
|
+
...transformed.value
|
|
1717
|
+
}
|
|
1718
|
+
};
|
|
1719
|
+
},
|
|
1720
|
+
getType: () => SchemaType.OBJECT
|
|
1721
|
+
};
|
|
1722
|
+
return {
|
|
1723
|
+
...baseSchema,
|
|
1724
|
+
...getSchemaUtils(baseSchema),
|
|
1725
|
+
...getObjectLikeUtils(baseSchema),
|
|
1726
|
+
...getObjectUtils(baseSchema)
|
|
1727
|
+
};
|
|
1728
|
+
}
|
|
1729
|
+
};
|
|
1730
|
+
}
|
|
1731
|
+
function validateAndTransformExtendedObject({
|
|
1732
|
+
extensionKeys,
|
|
1733
|
+
value,
|
|
1734
|
+
transformBase,
|
|
1735
|
+
transformExtension
|
|
1736
|
+
}) {
|
|
1737
|
+
const extensionPropertiesSet = new Set(extensionKeys);
|
|
1738
|
+
const [extensionProperties, baseProperties] = partition(
|
|
1739
|
+
keys(value),
|
|
1740
|
+
(key) => extensionPropertiesSet.has(key)
|
|
1741
|
+
);
|
|
1742
|
+
const transformedBase = transformBase(filterObject(value, baseProperties));
|
|
1743
|
+
const transformedExtension = transformExtension(filterObject(value, extensionProperties));
|
|
1744
|
+
if (transformedBase.ok && transformedExtension.ok) {
|
|
1745
|
+
return {
|
|
1746
|
+
ok: true,
|
|
1747
|
+
value: {
|
|
1748
|
+
...transformedBase.value,
|
|
1749
|
+
...transformedExtension.value
|
|
1750
|
+
}
|
|
1751
|
+
};
|
|
1752
|
+
} else {
|
|
1753
|
+
return {
|
|
1754
|
+
ok: false,
|
|
1755
|
+
errors: [
|
|
1756
|
+
...transformedBase.ok ? [] : transformedBase.errors,
|
|
1757
|
+
...transformedExtension.ok ? [] : transformedExtension.errors
|
|
1758
|
+
]
|
|
1759
|
+
};
|
|
1760
|
+
}
|
|
1761
|
+
}
|
|
1762
|
+
function isSchemaRequired(schema) {
|
|
1763
|
+
return !isSchemaOptional(schema);
|
|
1764
|
+
}
|
|
1765
|
+
function isSchemaOptional(schema) {
|
|
1766
|
+
switch (schema.getType()) {
|
|
1767
|
+
case SchemaType.ANY:
|
|
1768
|
+
case SchemaType.UNKNOWN:
|
|
1769
|
+
case SchemaType.OPTIONAL:
|
|
1770
|
+
case SchemaType.OPTIONAL_NULLABLE:
|
|
1771
|
+
return true;
|
|
1772
|
+
default:
|
|
1773
|
+
return false;
|
|
1774
|
+
}
|
|
1775
|
+
}
|
|
1776
|
+
|
|
1777
|
+
// src/core/schemas/builders/object/objectWithoutOptionalProperties.ts
|
|
1778
|
+
function objectWithoutOptionalProperties(schemas) {
|
|
1779
|
+
return object(schemas);
|
|
1780
|
+
}
|
|
1781
|
+
|
|
1782
|
+
// src/core/schemas/builders/lazy/lazyObject.ts
|
|
1783
|
+
function lazyObject(getter) {
|
|
1784
|
+
const baseSchema = {
|
|
1785
|
+
...constructLazyBaseSchema(getter),
|
|
1786
|
+
_getRawProperties: () => getMemoizedSchema(getter)._getRawProperties(),
|
|
1787
|
+
_getParsedProperties: () => getMemoizedSchema(getter)._getParsedProperties()
|
|
1788
|
+
};
|
|
1789
|
+
return {
|
|
1790
|
+
...baseSchema,
|
|
1791
|
+
...getSchemaUtils(baseSchema),
|
|
1792
|
+
...getObjectLikeUtils(baseSchema),
|
|
1793
|
+
...getObjectUtils(baseSchema)
|
|
1794
|
+
};
|
|
1795
|
+
}
|
|
1796
|
+
|
|
1797
|
+
// src/core/schemas/builders/list/list.ts
|
|
1798
|
+
function list(schema) {
|
|
1799
|
+
const baseSchema = {
|
|
1800
|
+
parse: (raw, opts) => validateAndTransformArray(
|
|
1801
|
+
raw,
|
|
1802
|
+
(item, index) => schema.parse(item, {
|
|
1803
|
+
...opts,
|
|
1804
|
+
breadcrumbsPrefix: [...opts?.breadcrumbsPrefix ?? [], `[${index}]`]
|
|
1805
|
+
})
|
|
1806
|
+
),
|
|
1807
|
+
json: (parsed, opts) => validateAndTransformArray(
|
|
1808
|
+
parsed,
|
|
1809
|
+
(item, index) => schema.json(item, {
|
|
1810
|
+
...opts,
|
|
1811
|
+
breadcrumbsPrefix: [...opts?.breadcrumbsPrefix ?? [], `[${index}]`]
|
|
1812
|
+
})
|
|
1813
|
+
),
|
|
1814
|
+
getType: () => SchemaType.LIST
|
|
1815
|
+
};
|
|
1816
|
+
return {
|
|
1817
|
+
...maybeSkipValidation(baseSchema),
|
|
1818
|
+
...getSchemaUtils(baseSchema)
|
|
1819
|
+
};
|
|
1820
|
+
}
|
|
1821
|
+
function validateAndTransformArray(value, transformItem) {
|
|
1822
|
+
if (!Array.isArray(value)) {
|
|
1823
|
+
return {
|
|
1824
|
+
ok: false,
|
|
1825
|
+
errors: [
|
|
1826
|
+
{
|
|
1827
|
+
message: getErrorMessageForIncorrectType(value, "list"),
|
|
1828
|
+
path: []
|
|
1829
|
+
}
|
|
1830
|
+
]
|
|
1831
|
+
};
|
|
1832
|
+
}
|
|
1833
|
+
const maybeValidItems = value.map((item, index) => transformItem(item, index));
|
|
1834
|
+
return maybeValidItems.reduce(
|
|
1835
|
+
(acc, item) => {
|
|
1836
|
+
if (acc.ok && item.ok) {
|
|
1837
|
+
return {
|
|
1838
|
+
ok: true,
|
|
1839
|
+
value: [...acc.value, item.value]
|
|
1840
|
+
};
|
|
1841
|
+
}
|
|
1842
|
+
const errors = [];
|
|
1843
|
+
if (!acc.ok) {
|
|
1844
|
+
errors.push(...acc.errors);
|
|
1845
|
+
}
|
|
1846
|
+
if (!item.ok) {
|
|
1847
|
+
errors.push(...item.errors);
|
|
1848
|
+
}
|
|
1849
|
+
return {
|
|
1850
|
+
ok: false,
|
|
1851
|
+
errors
|
|
1852
|
+
};
|
|
1853
|
+
},
|
|
1854
|
+
{ ok: true, value: [] }
|
|
1855
|
+
);
|
|
1856
|
+
}
|
|
1857
|
+
|
|
1858
|
+
// src/core/schemas/builders/literals/stringLiteral.ts
|
|
1859
|
+
function stringLiteral(literal) {
|
|
1860
|
+
const schemaCreator = createIdentitySchemaCreator(
|
|
1861
|
+
SchemaType.STRING_LITERAL,
|
|
1862
|
+
(value, { breadcrumbsPrefix = [] } = {}) => {
|
|
1863
|
+
if (value === literal) {
|
|
1864
|
+
return {
|
|
1865
|
+
ok: true,
|
|
1866
|
+
value: literal
|
|
1867
|
+
};
|
|
1868
|
+
} else {
|
|
1869
|
+
return {
|
|
1870
|
+
ok: false,
|
|
1871
|
+
errors: [
|
|
1872
|
+
{
|
|
1873
|
+
path: breadcrumbsPrefix,
|
|
1874
|
+
message: getErrorMessageForIncorrectType(value, `"${literal}"`)
|
|
1875
|
+
}
|
|
1876
|
+
]
|
|
1877
|
+
};
|
|
1878
|
+
}
|
|
1879
|
+
}
|
|
1880
|
+
);
|
|
1881
|
+
return schemaCreator();
|
|
1882
|
+
}
|
|
1883
|
+
|
|
1884
|
+
// src/core/schemas/builders/literals/booleanLiteral.ts
|
|
1885
|
+
function booleanLiteral(literal) {
|
|
1886
|
+
const schemaCreator = createIdentitySchemaCreator(
|
|
1887
|
+
SchemaType.BOOLEAN_LITERAL,
|
|
1888
|
+
(value, { breadcrumbsPrefix = [] } = {}) => {
|
|
1889
|
+
if (value === literal) {
|
|
1890
|
+
return {
|
|
1891
|
+
ok: true,
|
|
1892
|
+
value: literal
|
|
1893
|
+
};
|
|
1894
|
+
} else {
|
|
1895
|
+
return {
|
|
1896
|
+
ok: false,
|
|
1897
|
+
errors: [
|
|
1898
|
+
{
|
|
1899
|
+
path: breadcrumbsPrefix,
|
|
1900
|
+
message: getErrorMessageForIncorrectType(value, `${literal.toString()}`)
|
|
1901
|
+
}
|
|
1902
|
+
]
|
|
1903
|
+
};
|
|
1904
|
+
}
|
|
1905
|
+
}
|
|
1906
|
+
);
|
|
1907
|
+
return schemaCreator();
|
|
1908
|
+
}
|
|
1909
|
+
|
|
1910
|
+
// src/core/schemas/builders/primitives/any.ts
|
|
1911
|
+
var any = createIdentitySchemaCreator(SchemaType.ANY, (value) => ({ ok: true, value }));
|
|
1912
|
+
|
|
1913
|
+
// src/core/schemas/builders/primitives/boolean.ts
|
|
1914
|
+
var boolean = createIdentitySchemaCreator(
|
|
1915
|
+
SchemaType.BOOLEAN,
|
|
1916
|
+
(value, { breadcrumbsPrefix = [] } = {}) => {
|
|
1917
|
+
if (typeof value === "boolean") {
|
|
1918
|
+
return {
|
|
1919
|
+
ok: true,
|
|
1920
|
+
value
|
|
1921
|
+
};
|
|
1922
|
+
} else {
|
|
1923
|
+
return {
|
|
1924
|
+
ok: false,
|
|
1925
|
+
errors: [
|
|
1926
|
+
{
|
|
1927
|
+
path: breadcrumbsPrefix,
|
|
1928
|
+
message: getErrorMessageForIncorrectType(value, "boolean")
|
|
1929
|
+
}
|
|
1930
|
+
]
|
|
1931
|
+
};
|
|
1932
|
+
}
|
|
1933
|
+
}
|
|
1934
|
+
);
|
|
1935
|
+
|
|
1936
|
+
// src/core/schemas/builders/primitives/number.ts
|
|
1937
|
+
var number = createIdentitySchemaCreator(
|
|
1938
|
+
SchemaType.NUMBER,
|
|
1939
|
+
(value, { breadcrumbsPrefix = [] } = {}) => {
|
|
1940
|
+
if (typeof value === "number") {
|
|
1941
|
+
return {
|
|
1942
|
+
ok: true,
|
|
1943
|
+
value
|
|
1944
|
+
};
|
|
1945
|
+
} else {
|
|
1946
|
+
return {
|
|
1947
|
+
ok: false,
|
|
1948
|
+
errors: [
|
|
1949
|
+
{
|
|
1950
|
+
path: breadcrumbsPrefix,
|
|
1951
|
+
message: getErrorMessageForIncorrectType(value, "number")
|
|
1952
|
+
}
|
|
1953
|
+
]
|
|
1954
|
+
};
|
|
1955
|
+
}
|
|
1956
|
+
}
|
|
1957
|
+
);
|
|
1958
|
+
|
|
1959
|
+
// src/core/schemas/builders/primitives/string.ts
|
|
1960
|
+
var string = createIdentitySchemaCreator(
|
|
1961
|
+
SchemaType.STRING,
|
|
1962
|
+
(value, { breadcrumbsPrefix = [] } = {}) => {
|
|
1963
|
+
if (typeof value === "string") {
|
|
1964
|
+
return {
|
|
1965
|
+
ok: true,
|
|
1966
|
+
value
|
|
1967
|
+
};
|
|
1968
|
+
} else {
|
|
1969
|
+
return {
|
|
1970
|
+
ok: false,
|
|
1971
|
+
errors: [
|
|
1972
|
+
{
|
|
1973
|
+
path: breadcrumbsPrefix,
|
|
1974
|
+
message: getErrorMessageForIncorrectType(value, "string")
|
|
1975
|
+
}
|
|
1976
|
+
]
|
|
1977
|
+
};
|
|
1978
|
+
}
|
|
1979
|
+
}
|
|
1980
|
+
);
|
|
1981
|
+
|
|
1982
|
+
// src/core/schemas/builders/primitives/unknown.ts
|
|
1983
|
+
var unknown = createIdentitySchemaCreator(SchemaType.UNKNOWN, (value) => ({ ok: true, value }));
|
|
1984
|
+
|
|
1985
|
+
// src/core/schemas/builders/record/record.ts
|
|
1986
|
+
function record(keySchema, valueSchema) {
|
|
1987
|
+
const baseSchema = {
|
|
1988
|
+
parse: (raw, opts) => {
|
|
1989
|
+
return validateAndTransformRecord({
|
|
1990
|
+
value: raw,
|
|
1991
|
+
isKeyNumeric: keySchema.getType() === SchemaType.NUMBER,
|
|
1992
|
+
transformKey: (key) => keySchema.parse(key, {
|
|
1993
|
+
...opts,
|
|
1994
|
+
breadcrumbsPrefix: [...opts?.breadcrumbsPrefix ?? [], `${key} (key)`]
|
|
1995
|
+
}),
|
|
1996
|
+
transformValue: (value, key) => valueSchema.parse(value, {
|
|
1997
|
+
...opts,
|
|
1998
|
+
breadcrumbsPrefix: [...opts?.breadcrumbsPrefix ?? [], `${key}`]
|
|
1999
|
+
}),
|
|
2000
|
+
breadcrumbsPrefix: opts?.breadcrumbsPrefix
|
|
2001
|
+
});
|
|
2002
|
+
},
|
|
2003
|
+
json: (parsed, opts) => {
|
|
2004
|
+
return validateAndTransformRecord({
|
|
2005
|
+
value: parsed,
|
|
2006
|
+
isKeyNumeric: keySchema.getType() === SchemaType.NUMBER,
|
|
2007
|
+
transformKey: (key) => keySchema.json(key, {
|
|
2008
|
+
...opts,
|
|
2009
|
+
breadcrumbsPrefix: [...opts?.breadcrumbsPrefix ?? [], `${key} (key)`]
|
|
2010
|
+
}),
|
|
2011
|
+
transformValue: (value, key) => valueSchema.json(value, {
|
|
2012
|
+
...opts,
|
|
2013
|
+
breadcrumbsPrefix: [...opts?.breadcrumbsPrefix ?? [], `${key}`]
|
|
2014
|
+
}),
|
|
2015
|
+
breadcrumbsPrefix: opts?.breadcrumbsPrefix
|
|
2016
|
+
});
|
|
2017
|
+
},
|
|
2018
|
+
getType: () => SchemaType.RECORD
|
|
2019
|
+
};
|
|
2020
|
+
return {
|
|
2021
|
+
...maybeSkipValidation(baseSchema),
|
|
2022
|
+
...getSchemaUtils(baseSchema)
|
|
2023
|
+
};
|
|
2024
|
+
}
|
|
2025
|
+
function validateAndTransformRecord({
|
|
2026
|
+
value,
|
|
2027
|
+
isKeyNumeric,
|
|
2028
|
+
transformKey,
|
|
2029
|
+
transformValue,
|
|
2030
|
+
breadcrumbsPrefix = []
|
|
2031
|
+
}) {
|
|
2032
|
+
if (!isPlainObject(value)) {
|
|
2033
|
+
return {
|
|
2034
|
+
ok: false,
|
|
2035
|
+
errors: [
|
|
2036
|
+
{
|
|
2037
|
+
path: breadcrumbsPrefix,
|
|
2038
|
+
message: getErrorMessageForIncorrectType(value, "object")
|
|
2039
|
+
}
|
|
2040
|
+
]
|
|
2041
|
+
};
|
|
2042
|
+
}
|
|
2043
|
+
return entries(value).reduce(
|
|
2044
|
+
(accPromise, [stringKey, value2]) => {
|
|
2045
|
+
if (value2 === void 0) {
|
|
2046
|
+
return accPromise;
|
|
2047
|
+
}
|
|
2048
|
+
const acc = accPromise;
|
|
2049
|
+
let key = stringKey;
|
|
2050
|
+
if (isKeyNumeric) {
|
|
2051
|
+
const numberKey = stringKey.length > 0 ? Number(stringKey) : NaN;
|
|
2052
|
+
if (!isNaN(numberKey)) {
|
|
2053
|
+
key = numberKey;
|
|
2054
|
+
}
|
|
2055
|
+
}
|
|
2056
|
+
const transformedKey = transformKey(key);
|
|
2057
|
+
const transformedValue = transformValue(value2, key);
|
|
2058
|
+
if (acc.ok && transformedKey.ok && transformedValue.ok) {
|
|
2059
|
+
return {
|
|
2060
|
+
ok: true,
|
|
2061
|
+
value: {
|
|
2062
|
+
...acc.value,
|
|
2063
|
+
[transformedKey.value]: transformedValue.value
|
|
2064
|
+
}
|
|
2065
|
+
};
|
|
2066
|
+
}
|
|
2067
|
+
const errors = [];
|
|
2068
|
+
if (!acc.ok) {
|
|
2069
|
+
errors.push(...acc.errors);
|
|
2070
|
+
}
|
|
2071
|
+
if (!transformedKey.ok) {
|
|
2072
|
+
errors.push(...transformedKey.errors);
|
|
2073
|
+
}
|
|
2074
|
+
if (!transformedValue.ok) {
|
|
2075
|
+
errors.push(...transformedValue.errors);
|
|
2076
|
+
}
|
|
2077
|
+
return {
|
|
2078
|
+
ok: false,
|
|
2079
|
+
errors
|
|
2080
|
+
};
|
|
2081
|
+
},
|
|
2082
|
+
{ ok: true, value: {} }
|
|
2083
|
+
);
|
|
2084
|
+
}
|
|
2085
|
+
|
|
2086
|
+
// src/core/schemas/builders/set/set.ts
|
|
2087
|
+
function set(schema) {
|
|
2088
|
+
const listSchema = list(schema);
|
|
2089
|
+
const baseSchema = {
|
|
2090
|
+
parse: (raw, opts) => {
|
|
2091
|
+
const parsedList = listSchema.parse(raw, opts);
|
|
2092
|
+
if (parsedList.ok) {
|
|
2093
|
+
return {
|
|
2094
|
+
ok: true,
|
|
2095
|
+
value: new Set(parsedList.value)
|
|
2096
|
+
};
|
|
2097
|
+
} else {
|
|
2098
|
+
return parsedList;
|
|
2099
|
+
}
|
|
2100
|
+
},
|
|
2101
|
+
json: (parsed, opts) => {
|
|
2102
|
+
if (!(parsed instanceof Set)) {
|
|
2103
|
+
return {
|
|
2104
|
+
ok: false,
|
|
2105
|
+
errors: [
|
|
2106
|
+
{
|
|
2107
|
+
path: opts?.breadcrumbsPrefix ?? [],
|
|
2108
|
+
message: getErrorMessageForIncorrectType(parsed, "Set")
|
|
2109
|
+
}
|
|
2110
|
+
]
|
|
2111
|
+
};
|
|
2112
|
+
}
|
|
2113
|
+
const jsonList = listSchema.json([...parsed], opts);
|
|
2114
|
+
return jsonList;
|
|
2115
|
+
},
|
|
2116
|
+
getType: () => SchemaType.SET
|
|
2117
|
+
};
|
|
2118
|
+
return {
|
|
2119
|
+
...maybeSkipValidation(baseSchema),
|
|
2120
|
+
...getSchemaUtils(baseSchema)
|
|
2121
|
+
};
|
|
2122
|
+
}
|
|
2123
|
+
|
|
2124
|
+
// src/core/schemas/builders/undiscriminated-union/undiscriminatedUnion.ts
|
|
2125
|
+
function undiscriminatedUnion(schemas) {
|
|
2126
|
+
const baseSchema = {
|
|
2127
|
+
parse: (raw, opts) => {
|
|
2128
|
+
return validateAndTransformUndiscriminatedUnion(
|
|
2129
|
+
(schema, opts2) => schema.parse(raw, opts2),
|
|
2130
|
+
schemas,
|
|
2131
|
+
opts
|
|
2132
|
+
);
|
|
2133
|
+
},
|
|
2134
|
+
json: (parsed, opts) => {
|
|
2135
|
+
return validateAndTransformUndiscriminatedUnion(
|
|
2136
|
+
(schema, opts2) => schema.json(parsed, opts2),
|
|
2137
|
+
schemas,
|
|
2138
|
+
opts
|
|
2139
|
+
);
|
|
2140
|
+
},
|
|
2141
|
+
getType: () => SchemaType.UNDISCRIMINATED_UNION
|
|
2142
|
+
};
|
|
2143
|
+
return {
|
|
2144
|
+
...maybeSkipValidation(baseSchema),
|
|
2145
|
+
...getSchemaUtils(baseSchema)
|
|
2146
|
+
};
|
|
2147
|
+
}
|
|
2148
|
+
function validateAndTransformUndiscriminatedUnion(transform2, schemas, opts) {
|
|
2149
|
+
const errors = [];
|
|
2150
|
+
for (const [index, schema] of schemas.entries()) {
|
|
2151
|
+
const transformed = transform2(schema, { ...opts, skipValidation: false });
|
|
2152
|
+
if (transformed.ok) {
|
|
2153
|
+
return transformed;
|
|
2154
|
+
} else {
|
|
2155
|
+
for (const error of transformed.errors) {
|
|
2156
|
+
errors.push({
|
|
2157
|
+
path: error.path,
|
|
2158
|
+
message: `[Variant ${index}] ${error.message}`
|
|
2159
|
+
});
|
|
2160
|
+
}
|
|
2161
|
+
}
|
|
2162
|
+
}
|
|
2163
|
+
return {
|
|
2164
|
+
ok: false,
|
|
2165
|
+
errors
|
|
2166
|
+
};
|
|
2167
|
+
}
|
|
2168
|
+
|
|
2169
|
+
// src/core/schemas/builders/union/discriminant.ts
|
|
2170
|
+
function discriminant(parsedDiscriminant, rawDiscriminant) {
|
|
2171
|
+
return {
|
|
2172
|
+
parsedDiscriminant,
|
|
2173
|
+
rawDiscriminant
|
|
2174
|
+
};
|
|
2175
|
+
}
|
|
2176
|
+
|
|
2177
|
+
// src/core/schemas/builders/union/union.ts
|
|
2178
|
+
function union(discriminant2, union2) {
|
|
2179
|
+
const rawDiscriminant = typeof discriminant2 === "string" ? discriminant2 : discriminant2.rawDiscriminant;
|
|
2180
|
+
const parsedDiscriminant = typeof discriminant2 === "string" ? discriminant2 : discriminant2.parsedDiscriminant;
|
|
2181
|
+
const discriminantValueSchema = enum_(keys(union2));
|
|
2182
|
+
const baseSchema = {
|
|
2183
|
+
parse: (raw, opts) => {
|
|
2184
|
+
return transformAndValidateUnion({
|
|
2185
|
+
value: raw,
|
|
2186
|
+
discriminant: rawDiscriminant,
|
|
2187
|
+
transformedDiscriminant: parsedDiscriminant,
|
|
2188
|
+
transformDiscriminantValue: (discriminantValue) => discriminantValueSchema.parse(discriminantValue, {
|
|
2189
|
+
allowUnrecognizedEnumValues: opts?.allowUnrecognizedUnionMembers,
|
|
2190
|
+
breadcrumbsPrefix: [...opts?.breadcrumbsPrefix ?? [], rawDiscriminant]
|
|
2191
|
+
}),
|
|
2192
|
+
getAdditionalPropertiesSchema: (discriminantValue) => union2[discriminantValue],
|
|
2193
|
+
allowUnrecognizedUnionMembers: opts?.allowUnrecognizedUnionMembers,
|
|
2194
|
+
transformAdditionalProperties: (additionalProperties, additionalPropertiesSchema) => additionalPropertiesSchema.parse(additionalProperties, opts),
|
|
2195
|
+
breadcrumbsPrefix: opts?.breadcrumbsPrefix
|
|
2196
|
+
});
|
|
2197
|
+
},
|
|
2198
|
+
json: (parsed, opts) => {
|
|
2199
|
+
return transformAndValidateUnion({
|
|
2200
|
+
value: parsed,
|
|
2201
|
+
discriminant: parsedDiscriminant,
|
|
2202
|
+
transformedDiscriminant: rawDiscriminant,
|
|
2203
|
+
transformDiscriminantValue: (discriminantValue) => discriminantValueSchema.json(discriminantValue, {
|
|
2204
|
+
allowUnrecognizedEnumValues: opts?.allowUnrecognizedUnionMembers,
|
|
2205
|
+
breadcrumbsPrefix: [...opts?.breadcrumbsPrefix ?? [], parsedDiscriminant]
|
|
2206
|
+
}),
|
|
2207
|
+
getAdditionalPropertiesSchema: (discriminantValue) => union2[discriminantValue],
|
|
2208
|
+
allowUnrecognizedUnionMembers: opts?.allowUnrecognizedUnionMembers,
|
|
2209
|
+
transformAdditionalProperties: (additionalProperties, additionalPropertiesSchema) => additionalPropertiesSchema.json(additionalProperties, opts),
|
|
2210
|
+
breadcrumbsPrefix: opts?.breadcrumbsPrefix
|
|
2211
|
+
});
|
|
2212
|
+
},
|
|
2213
|
+
getType: () => SchemaType.UNION
|
|
2214
|
+
};
|
|
2215
|
+
return {
|
|
2216
|
+
...maybeSkipValidation(baseSchema),
|
|
2217
|
+
...getSchemaUtils(baseSchema),
|
|
2218
|
+
...getObjectLikeUtils(baseSchema)
|
|
2219
|
+
};
|
|
2220
|
+
}
|
|
2221
|
+
function transformAndValidateUnion({
|
|
2222
|
+
value,
|
|
2223
|
+
discriminant: discriminant2,
|
|
2224
|
+
transformedDiscriminant,
|
|
2225
|
+
transformDiscriminantValue,
|
|
2226
|
+
getAdditionalPropertiesSchema,
|
|
2227
|
+
allowUnrecognizedUnionMembers = false,
|
|
2228
|
+
transformAdditionalProperties,
|
|
2229
|
+
breadcrumbsPrefix = []
|
|
2230
|
+
}) {
|
|
2231
|
+
if (!isPlainObject(value)) {
|
|
2232
|
+
return {
|
|
2233
|
+
ok: false,
|
|
2234
|
+
errors: [
|
|
2235
|
+
{
|
|
2236
|
+
path: breadcrumbsPrefix,
|
|
2237
|
+
message: getErrorMessageForIncorrectType(value, "object")
|
|
2238
|
+
}
|
|
2239
|
+
]
|
|
2240
|
+
};
|
|
2241
|
+
}
|
|
2242
|
+
const { [discriminant2]: discriminantValue, ...additionalProperties } = value;
|
|
2243
|
+
if (discriminantValue == null) {
|
|
2244
|
+
return {
|
|
2245
|
+
ok: false,
|
|
2246
|
+
errors: [
|
|
2247
|
+
{
|
|
2248
|
+
path: breadcrumbsPrefix,
|
|
2249
|
+
message: `Missing discriminant ("${discriminant2}")`
|
|
2250
|
+
}
|
|
2251
|
+
]
|
|
2252
|
+
};
|
|
2253
|
+
}
|
|
2254
|
+
const transformedDiscriminantValue = transformDiscriminantValue(discriminantValue);
|
|
2255
|
+
if (!transformedDiscriminantValue.ok) {
|
|
2256
|
+
return {
|
|
2257
|
+
ok: false,
|
|
2258
|
+
errors: transformedDiscriminantValue.errors
|
|
2259
|
+
};
|
|
2260
|
+
}
|
|
2261
|
+
const additionalPropertiesSchema = getAdditionalPropertiesSchema(transformedDiscriminantValue.value);
|
|
2262
|
+
if (additionalPropertiesSchema == null) {
|
|
2263
|
+
if (allowUnrecognizedUnionMembers) {
|
|
2264
|
+
return {
|
|
2265
|
+
ok: true,
|
|
2266
|
+
value: {
|
|
2267
|
+
[transformedDiscriminant]: transformedDiscriminantValue.value,
|
|
2268
|
+
...additionalProperties
|
|
2269
|
+
}
|
|
2270
|
+
};
|
|
2271
|
+
} else {
|
|
2272
|
+
return {
|
|
2273
|
+
ok: false,
|
|
2274
|
+
errors: [
|
|
2275
|
+
{
|
|
2276
|
+
path: [...breadcrumbsPrefix, discriminant2],
|
|
2277
|
+
message: "Unexpected discriminant value"
|
|
2278
|
+
}
|
|
2279
|
+
]
|
|
2280
|
+
};
|
|
2281
|
+
}
|
|
2282
|
+
}
|
|
2283
|
+
const transformedAdditionalProperties = transformAdditionalProperties(
|
|
2284
|
+
additionalProperties,
|
|
2285
|
+
additionalPropertiesSchema
|
|
2286
|
+
);
|
|
2287
|
+
if (!transformedAdditionalProperties.ok) {
|
|
2288
|
+
return transformedAdditionalProperties;
|
|
2289
|
+
}
|
|
2290
|
+
return {
|
|
2291
|
+
ok: true,
|
|
2292
|
+
value: {
|
|
2293
|
+
[transformedDiscriminant]: discriminantValue,
|
|
2294
|
+
...transformedAdditionalProperties.value
|
|
2295
|
+
}
|
|
2296
|
+
};
|
|
2297
|
+
}
|