@embeddable.com/sdk-react 2.2.1 → 2.2.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/lib/index.esm.js +285 -22
- package/lib/index.esm.js.map +1 -1
- package/lib/index.js +285 -22
- package/lib/index.js.map +1 -1
- package/package.json +2 -1
- package/lib/validate/errorFormatter.d.ts +0 -3
package/lib/index.js
CHANGED
|
@@ -132,37 +132,283 @@ var findFiles = async (initialSrcDir, regex) => {
|
|
|
132
132
|
return filesList;
|
|
133
133
|
};
|
|
134
134
|
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
135
|
+
var util;
|
|
136
|
+
(function (util) {
|
|
137
|
+
util.assertEqual = (val) => val;
|
|
138
|
+
function assertIs(_arg) { }
|
|
139
|
+
util.assertIs = assertIs;
|
|
140
|
+
function assertNever(_x) {
|
|
141
|
+
throw new Error();
|
|
142
|
+
}
|
|
143
|
+
util.assertNever = assertNever;
|
|
144
|
+
util.arrayToEnum = (items) => {
|
|
145
|
+
const obj = {};
|
|
146
|
+
for (const item of items) {
|
|
147
|
+
obj[item] = item;
|
|
148
|
+
}
|
|
149
|
+
return obj;
|
|
150
|
+
};
|
|
151
|
+
util.getValidEnumValues = (obj) => {
|
|
152
|
+
const validKeys = util.objectKeys(obj).filter((k) => typeof obj[obj[k]] !== "number");
|
|
153
|
+
const filtered = {};
|
|
154
|
+
for (const k of validKeys) {
|
|
155
|
+
filtered[k] = obj[k];
|
|
156
|
+
}
|
|
157
|
+
return util.objectValues(filtered);
|
|
158
|
+
};
|
|
159
|
+
util.objectValues = (obj) => {
|
|
160
|
+
return util.objectKeys(obj).map(function (e) {
|
|
161
|
+
return obj[e];
|
|
162
|
+
});
|
|
163
|
+
};
|
|
164
|
+
util.objectKeys = typeof Object.keys === "function" // eslint-disable-line ban/ban
|
|
165
|
+
? (obj) => Object.keys(obj) // eslint-disable-line ban/ban
|
|
166
|
+
: (object) => {
|
|
167
|
+
const keys = [];
|
|
168
|
+
for (const key in object) {
|
|
169
|
+
if (Object.prototype.hasOwnProperty.call(object, key)) {
|
|
170
|
+
keys.push(key);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
return keys;
|
|
174
|
+
};
|
|
175
|
+
util.find = (arr, checker) => {
|
|
176
|
+
for (const item of arr) {
|
|
177
|
+
if (checker(item))
|
|
178
|
+
return item;
|
|
179
|
+
}
|
|
180
|
+
return undefined;
|
|
181
|
+
};
|
|
182
|
+
util.isInteger = typeof Number.isInteger === "function"
|
|
183
|
+
? (val) => Number.isInteger(val) // eslint-disable-line ban/ban
|
|
184
|
+
: (val) => typeof val === "number" && isFinite(val) && Math.floor(val) === val;
|
|
185
|
+
function joinValues(array, separator = " | ") {
|
|
186
|
+
return array
|
|
187
|
+
.map((val) => (typeof val === "string" ? `'${val}'` : val))
|
|
188
|
+
.join(separator);
|
|
189
|
+
}
|
|
190
|
+
util.joinValues = joinValues;
|
|
191
|
+
util.jsonStringifyReplacer = (_, value) => {
|
|
192
|
+
if (typeof value === "bigint") {
|
|
193
|
+
return value.toString();
|
|
194
|
+
}
|
|
195
|
+
return value;
|
|
196
|
+
};
|
|
197
|
+
})(util || (util = {}));
|
|
198
|
+
var objectUtil;
|
|
199
|
+
(function (objectUtil) {
|
|
200
|
+
objectUtil.mergeShapes = (first, second) => {
|
|
201
|
+
return {
|
|
202
|
+
...first,
|
|
203
|
+
...second, // second overwrites first
|
|
204
|
+
};
|
|
205
|
+
};
|
|
206
|
+
})(objectUtil || (objectUtil = {}));
|
|
207
|
+
util.arrayToEnum([
|
|
208
|
+
"string",
|
|
209
|
+
"nan",
|
|
210
|
+
"number",
|
|
211
|
+
"integer",
|
|
212
|
+
"float",
|
|
213
|
+
"boolean",
|
|
214
|
+
"date",
|
|
215
|
+
"bigint",
|
|
216
|
+
"symbol",
|
|
217
|
+
"function",
|
|
218
|
+
"undefined",
|
|
219
|
+
"null",
|
|
220
|
+
"array",
|
|
221
|
+
"object",
|
|
222
|
+
"unknown",
|
|
223
|
+
"promise",
|
|
224
|
+
"void",
|
|
225
|
+
"never",
|
|
226
|
+
"map",
|
|
227
|
+
"set",
|
|
228
|
+
]);
|
|
143
229
|
|
|
144
|
-
const
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
230
|
+
const ZodIssueCode = util.arrayToEnum([
|
|
231
|
+
"invalid_type",
|
|
232
|
+
"invalid_literal",
|
|
233
|
+
"custom",
|
|
234
|
+
"invalid_union",
|
|
235
|
+
"invalid_union_discriminator",
|
|
236
|
+
"invalid_enum_value",
|
|
237
|
+
"unrecognized_keys",
|
|
238
|
+
"invalid_arguments",
|
|
239
|
+
"invalid_return_type",
|
|
240
|
+
"invalid_date",
|
|
241
|
+
"invalid_string",
|
|
242
|
+
"too_small",
|
|
243
|
+
"too_big",
|
|
244
|
+
"invalid_intersection_types",
|
|
245
|
+
"not_multiple_of",
|
|
246
|
+
"not_finite",
|
|
247
|
+
]);
|
|
248
|
+
class ZodError extends Error {
|
|
249
|
+
constructor(issues) {
|
|
250
|
+
super();
|
|
251
|
+
this.issues = [];
|
|
252
|
+
this.addIssue = (sub) => {
|
|
253
|
+
this.issues = [...this.issues, sub];
|
|
254
|
+
};
|
|
255
|
+
this.addIssues = (subs = []) => {
|
|
256
|
+
this.issues = [...this.issues, ...subs];
|
|
257
|
+
};
|
|
258
|
+
const actualProto = new.target.prototype;
|
|
259
|
+
if (Object.setPrototypeOf) {
|
|
260
|
+
// eslint-disable-next-line ban/ban
|
|
261
|
+
Object.setPrototypeOf(this, actualProto);
|
|
262
|
+
}
|
|
263
|
+
else {
|
|
264
|
+
this.__proto__ = actualProto;
|
|
265
|
+
}
|
|
266
|
+
this.name = "ZodError";
|
|
267
|
+
this.issues = issues;
|
|
268
|
+
}
|
|
269
|
+
get errors() {
|
|
270
|
+
return this.issues;
|
|
271
|
+
}
|
|
272
|
+
format(_mapper) {
|
|
273
|
+
const mapper = _mapper ||
|
|
274
|
+
function (issue) {
|
|
275
|
+
return issue.message;
|
|
276
|
+
};
|
|
277
|
+
const fieldErrors = { _errors: [] };
|
|
278
|
+
const processError = (error) => {
|
|
279
|
+
for (const issue of error.issues) {
|
|
280
|
+
if (issue.code === "invalid_union") {
|
|
281
|
+
issue.unionErrors.map(processError);
|
|
282
|
+
}
|
|
283
|
+
else if (issue.code === "invalid_return_type") {
|
|
284
|
+
processError(issue.returnTypeError);
|
|
285
|
+
}
|
|
286
|
+
else if (issue.code === "invalid_arguments") {
|
|
287
|
+
processError(issue.argumentsError);
|
|
288
|
+
}
|
|
289
|
+
else if (issue.path.length === 0) {
|
|
290
|
+
fieldErrors._errors.push(mapper(issue));
|
|
291
|
+
}
|
|
292
|
+
else {
|
|
293
|
+
let curr = fieldErrors;
|
|
294
|
+
let i = 0;
|
|
295
|
+
while (i < issue.path.length) {
|
|
296
|
+
const el = issue.path[i];
|
|
297
|
+
const terminal = i === issue.path.length - 1;
|
|
298
|
+
if (!terminal) {
|
|
299
|
+
curr[el] = curr[el] || { _errors: [] };
|
|
300
|
+
// if (typeof el === "string") {
|
|
301
|
+
// curr[el] = curr[el] || { _errors: [] };
|
|
302
|
+
// } else if (typeof el === "number") {
|
|
303
|
+
// const errorArray: any = [];
|
|
304
|
+
// errorArray._errors = [];
|
|
305
|
+
// curr[el] = curr[el] || errorArray;
|
|
306
|
+
// }
|
|
307
|
+
}
|
|
308
|
+
else {
|
|
309
|
+
curr[el] = curr[el] || { _errors: [] };
|
|
310
|
+
curr[el]._errors.push(mapper(issue));
|
|
311
|
+
}
|
|
312
|
+
curr = curr[el];
|
|
313
|
+
i++;
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
};
|
|
318
|
+
processError(this);
|
|
319
|
+
return fieldErrors;
|
|
320
|
+
}
|
|
321
|
+
toString() {
|
|
322
|
+
return this.message;
|
|
323
|
+
}
|
|
324
|
+
get message() {
|
|
325
|
+
return JSON.stringify(this.issues, util.jsonStringifyReplacer, 2);
|
|
326
|
+
}
|
|
327
|
+
get isEmpty() {
|
|
328
|
+
return this.issues.length === 0;
|
|
329
|
+
}
|
|
330
|
+
flatten(mapper = (issue) => issue.message) {
|
|
331
|
+
const fieldErrors = {};
|
|
332
|
+
const formErrors = [];
|
|
333
|
+
for (const sub of this.issues) {
|
|
334
|
+
if (sub.path.length > 0) {
|
|
335
|
+
fieldErrors[sub.path[0]] = fieldErrors[sub.path[0]] || [];
|
|
336
|
+
fieldErrors[sub.path[0]].push(mapper(sub));
|
|
337
|
+
}
|
|
338
|
+
else {
|
|
339
|
+
formErrors.push(mapper(sub));
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
return { formErrors, fieldErrors };
|
|
343
|
+
}
|
|
344
|
+
get formErrors() {
|
|
345
|
+
return this.flatten();
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
ZodError.create = (issues) => {
|
|
349
|
+
const error = new ZodError(issues);
|
|
350
|
+
return error;
|
|
351
|
+
};
|
|
352
|
+
|
|
353
|
+
var errorUtil;
|
|
354
|
+
(function (errorUtil) {
|
|
355
|
+
errorUtil.errToObj = (message) => typeof message === "string" ? { message } : message || {};
|
|
356
|
+
errorUtil.toString = (message) => typeof message === "string" ? message : message === null || message === void 0 ? void 0 : message.message;
|
|
357
|
+
})(errorUtil || (errorUtil = {}));
|
|
358
|
+
var ZodFirstPartyTypeKind;
|
|
359
|
+
(function (ZodFirstPartyTypeKind) {
|
|
360
|
+
ZodFirstPartyTypeKind["ZodString"] = "ZodString";
|
|
361
|
+
ZodFirstPartyTypeKind["ZodNumber"] = "ZodNumber";
|
|
362
|
+
ZodFirstPartyTypeKind["ZodNaN"] = "ZodNaN";
|
|
363
|
+
ZodFirstPartyTypeKind["ZodBigInt"] = "ZodBigInt";
|
|
364
|
+
ZodFirstPartyTypeKind["ZodBoolean"] = "ZodBoolean";
|
|
365
|
+
ZodFirstPartyTypeKind["ZodDate"] = "ZodDate";
|
|
366
|
+
ZodFirstPartyTypeKind["ZodSymbol"] = "ZodSymbol";
|
|
367
|
+
ZodFirstPartyTypeKind["ZodUndefined"] = "ZodUndefined";
|
|
368
|
+
ZodFirstPartyTypeKind["ZodNull"] = "ZodNull";
|
|
369
|
+
ZodFirstPartyTypeKind["ZodAny"] = "ZodAny";
|
|
370
|
+
ZodFirstPartyTypeKind["ZodUnknown"] = "ZodUnknown";
|
|
371
|
+
ZodFirstPartyTypeKind["ZodNever"] = "ZodNever";
|
|
372
|
+
ZodFirstPartyTypeKind["ZodVoid"] = "ZodVoid";
|
|
373
|
+
ZodFirstPartyTypeKind["ZodArray"] = "ZodArray";
|
|
374
|
+
ZodFirstPartyTypeKind["ZodObject"] = "ZodObject";
|
|
375
|
+
ZodFirstPartyTypeKind["ZodUnion"] = "ZodUnion";
|
|
376
|
+
ZodFirstPartyTypeKind["ZodDiscriminatedUnion"] = "ZodDiscriminatedUnion";
|
|
377
|
+
ZodFirstPartyTypeKind["ZodIntersection"] = "ZodIntersection";
|
|
378
|
+
ZodFirstPartyTypeKind["ZodTuple"] = "ZodTuple";
|
|
379
|
+
ZodFirstPartyTypeKind["ZodRecord"] = "ZodRecord";
|
|
380
|
+
ZodFirstPartyTypeKind["ZodMap"] = "ZodMap";
|
|
381
|
+
ZodFirstPartyTypeKind["ZodSet"] = "ZodSet";
|
|
382
|
+
ZodFirstPartyTypeKind["ZodFunction"] = "ZodFunction";
|
|
383
|
+
ZodFirstPartyTypeKind["ZodLazy"] = "ZodLazy";
|
|
384
|
+
ZodFirstPartyTypeKind["ZodLiteral"] = "ZodLiteral";
|
|
385
|
+
ZodFirstPartyTypeKind["ZodEnum"] = "ZodEnum";
|
|
386
|
+
ZodFirstPartyTypeKind["ZodEffects"] = "ZodEffects";
|
|
387
|
+
ZodFirstPartyTypeKind["ZodNativeEnum"] = "ZodNativeEnum";
|
|
388
|
+
ZodFirstPartyTypeKind["ZodOptional"] = "ZodOptional";
|
|
389
|
+
ZodFirstPartyTypeKind["ZodNullable"] = "ZodNullable";
|
|
390
|
+
ZodFirstPartyTypeKind["ZodDefault"] = "ZodDefault";
|
|
391
|
+
ZodFirstPartyTypeKind["ZodCatch"] = "ZodCatch";
|
|
392
|
+
ZodFirstPartyTypeKind["ZodPromise"] = "ZodPromise";
|
|
393
|
+
ZodFirstPartyTypeKind["ZodBranded"] = "ZodBranded";
|
|
394
|
+
ZodFirstPartyTypeKind["ZodPipeline"] = "ZodPipeline";
|
|
395
|
+
ZodFirstPartyTypeKind["ZodReadonly"] = "ZodReadonly";
|
|
396
|
+
})(ZodFirstPartyTypeKind || (ZodFirstPartyTypeKind = {}));
|
|
151
397
|
|
|
152
398
|
const errorFormatter = (issues) => {
|
|
153
399
|
const errors = [];
|
|
154
400
|
for (let issue of issues) {
|
|
155
|
-
if (issue.code ===
|
|
401
|
+
if (issue.code === ZodIssueCode.invalid_union && issue.unionErrors.length) {
|
|
156
402
|
const error = issue.unionErrors[issue.unionErrors.length - 1];
|
|
157
403
|
issue = error.issues[error.issues.length - 1];
|
|
158
404
|
}
|
|
159
|
-
const path =
|
|
405
|
+
const path = formatErrorPath(issue.path);
|
|
160
406
|
const message = issue.message;
|
|
161
407
|
errors.push(`"${path}": ${message}`);
|
|
162
408
|
}
|
|
163
409
|
return errors;
|
|
164
410
|
};
|
|
165
|
-
const
|
|
411
|
+
const formatErrorPath = (path) => {
|
|
166
412
|
let formatted = "";
|
|
167
413
|
for (const pathElement of path) {
|
|
168
414
|
if (formatted.length === 0) {
|
|
@@ -178,6 +424,23 @@ const formatPath = (path) => {
|
|
|
178
424
|
return formatted;
|
|
179
425
|
};
|
|
180
426
|
|
|
427
|
+
const embeddableTypeSchema = zod.z
|
|
428
|
+
.object({
|
|
429
|
+
typeConfig: zod.z.object({
|
|
430
|
+
label: zod.z.string(),
|
|
431
|
+
toString: zod.z.function(),
|
|
432
|
+
}),
|
|
433
|
+
})
|
|
434
|
+
.or(zod.z.enum(core.ALL_NATIVE_TYPES));
|
|
435
|
+
|
|
436
|
+
const editorMetaSchema = zod.z
|
|
437
|
+
.object({
|
|
438
|
+
name: zod.z.string(),
|
|
439
|
+
label: zod.z.string(),
|
|
440
|
+
type: embeddableTypeSchema,
|
|
441
|
+
})
|
|
442
|
+
.strict();
|
|
443
|
+
|
|
181
444
|
const editorMetaValidator = (meta) => {
|
|
182
445
|
const result = editorMetaSchema.safeParse(meta);
|
|
183
446
|
if (result.success)
|
|
@@ -267,7 +530,7 @@ const validateVariableInputs = (meta) => {
|
|
|
267
530
|
inputs.forEach((input, inputIdx) => {
|
|
268
531
|
const definedInput = definedInputs === null || definedInputs === void 0 ? void 0 : definedInputs.find((d) => d.name === input);
|
|
269
532
|
if (!definedInput) {
|
|
270
|
-
const path =
|
|
533
|
+
const path = formatErrorPath(["variables", idx, "inputs", inputIdx]);
|
|
271
534
|
errors.push(`${path}: input "${input}" is not defined`);
|
|
272
535
|
}
|
|
273
536
|
});
|
|
@@ -287,7 +550,7 @@ const validateVariableEvents = (meta) => {
|
|
|
287
550
|
events.forEach((event, eventIdx) => {
|
|
288
551
|
const definedEvent = definedEvents === null || definedEvents === void 0 ? void 0 : definedEvents.find((d) => d.name === event.name);
|
|
289
552
|
if (!definedEvent) {
|
|
290
|
-
const path =
|
|
553
|
+
const path = formatErrorPath([
|
|
291
554
|
"variables",
|
|
292
555
|
idx,
|
|
293
556
|
"events",
|
|
@@ -300,7 +563,7 @@ const validateVariableEvents = (meta) => {
|
|
|
300
563
|
const definedProperties = definedEvent.properties;
|
|
301
564
|
const definedProperty = definedProperties === null || definedProperties === void 0 ? void 0 : definedProperties.find((p) => p.name === event.property);
|
|
302
565
|
if (!definedProperty) {
|
|
303
|
-
const path =
|
|
566
|
+
const path = formatErrorPath([
|
|
304
567
|
"variables",
|
|
305
568
|
idx,
|
|
306
569
|
"events",
|
|
@@ -311,7 +574,7 @@ const validateVariableEvents = (meta) => {
|
|
|
311
574
|
return;
|
|
312
575
|
}
|
|
313
576
|
if (definedProperty.type !== variableConfig.type) {
|
|
314
|
-
const path =
|
|
577
|
+
const path = formatErrorPath(["variables", idx]);
|
|
315
578
|
errors.push(`${path}: the type of the variable "${variableConfig.name}" and the type of the property "${event.property}" do not match`);
|
|
316
579
|
}
|
|
317
580
|
});
|