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