@voltro/client 0.52.0 → 0.54.0

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.
@@ -0,0 +1,386 @@
1
+ import { JSONSchema as e, Schema as t } from "effect";
2
+ import * as n from "effect/SchemaAST";
3
+ //#region src/schemaFields.ts
4
+ var r = (e) => ({ jsonSchema: { "x-voltro": e } }), i = "#/$defs/", a = (e) => e.replace(/[_-]+/g, " ").replace(/([a-z0-9])([A-Z])/g, "$1 $2").replace(/\s+/g, " ").trim().replace(/^./, (e) => e.toUpperCase()), o = (e, t) => {
5
+ let n = e;
6
+ for (let e = 0; e < 8; e += 1) {
7
+ let e = n.$ref;
8
+ if (typeof e != "string" || !e.startsWith(i)) return n;
9
+ let r = t[e.slice(8)];
10
+ if (!r || typeof r != "object") return n;
11
+ n = r;
12
+ }
13
+ return n;
14
+ }, s = (e) => {
15
+ if (!e || typeof e != "object") return !1;
16
+ let t = e;
17
+ if (t.type === "null" || t.const === null) return !0;
18
+ let n = t.enum;
19
+ return Array.isArray(n) && n.length === 1 && n[0] === null;
20
+ }, c = (e, t) => {
21
+ let n = e.type;
22
+ if (Array.isArray(n) && n.includes("null")) {
23
+ let t = n.filter((e) => e !== "null");
24
+ return {
25
+ node: {
26
+ ...e,
27
+ type: t.length === 1 ? t[0] : t
28
+ },
29
+ nullable: !0
30
+ };
31
+ }
32
+ for (let n of ["anyOf", "oneOf"]) {
33
+ let r = e[n];
34
+ if (!Array.isArray(r)) continue;
35
+ let i = r.filter((e) => !s(e));
36
+ if (i.length !== r.length) return i.length === 1 ? {
37
+ node: o(i[0], t),
38
+ nullable: !0
39
+ } : {
40
+ node: {
41
+ ...e,
42
+ [n]: i
43
+ },
44
+ nullable: !0
45
+ };
46
+ }
47
+ return {
48
+ node: e,
49
+ nullable: !1
50
+ };
51
+ }, l = (e) => e.map((e) => ({
52
+ value: String(e),
53
+ label: a(String(e))
54
+ })), u = (e) => {
55
+ let t = e.enum;
56
+ if (Array.isArray(t) && t.length > 0) return {
57
+ widget: "select",
58
+ options: l(t)
59
+ };
60
+ let n = e.type, r = Array.isArray(n) ? n.find((e) => e !== "null") : n, i = e.format;
61
+ switch (r) {
62
+ case "string": return i === "date" ? { widget: "date" } : i === "date-time" ? { widget: "datetime" } : { widget: "text" };
63
+ case "integer":
64
+ case "number": return { widget: "number" };
65
+ case "boolean": return { widget: "checkbox" };
66
+ case "array": return { widget: "multi-select" };
67
+ default: return { widget: "custom" };
68
+ }
69
+ }, d = (e) => {
70
+ let t = e.$ref;
71
+ if (typeof t != "string" || !t.startsWith(i)) return;
72
+ let n = t.slice(8);
73
+ if (/DateTime/i.test(n) || /Timestamp/i.test(n)) return "datetime";
74
+ if (/Date/.test(n)) return "date";
75
+ }, f = (e) => typeof e["x-voltro"] == "object" && e["x-voltro"] !== null ? e["x-voltro"] : {}, p = 4, m = (e, t, n, r = "", i, s = 0) => Object.entries(e).flatMap(([e, l]) => {
76
+ let { node: h, nullable: g } = c(o(l, n), n), _ = o(h, n), v = f(_), y = r === "" ? e : `${r}.${e}`, b = _.properties;
77
+ if (_.type === "object" && b !== void 0 && s < p && v.widget === void 0) return m(b, new Set(Array.isArray(_.required) ? _.required : []), n, y, {
78
+ path: v.section ?? y,
79
+ label: v.label ?? a(e)
80
+ }, s + 1);
81
+ let x = d(l), { widget: S, options: C } = u(_);
82
+ x !== void 0 && (S = x);
83
+ let w;
84
+ if (_.type === "array") {
85
+ let e = _.items;
86
+ if (typeof e == "object" && e && !Array.isArray(e)) {
87
+ let t = o(e, n), r = t.properties;
88
+ t.type === "object" && r !== void 0 && (S = "array", w = m(r, new Set(Array.isArray(t.required) ? t.required : []), n));
89
+ }
90
+ }
91
+ v.widget !== void 0 && (S = v.widget), v.reference !== void 0 && v.widget === void 0 && (S = "reference");
92
+ let T = typeof _.description == "string" ? _.description : void 0, E = v.section ?? i?.path;
93
+ return [{
94
+ name: y,
95
+ label: v.label ?? a(e),
96
+ widget: S,
97
+ required: t.has(e),
98
+ nullable: g,
99
+ ...C ? { options: C } : {},
100
+ jsonSchema: _,
101
+ ...E === void 0 ? {} : {
102
+ section: E,
103
+ sectionLabel: i?.label ?? a(E)
104
+ },
105
+ ...T === void 0 ? {} : { description: T },
106
+ ...v.order === void 0 ? {} : { order: v.order },
107
+ ...w === void 0 ? {} : { item: w },
108
+ ...v.reference === void 0 ? {} : { reference: v.reference }
109
+ }];
110
+ }), h = (e) => e.map((e, t) => [e, t]).sort((e, t) => (e[0].order ?? e[1]) - (t[0].order ?? t[1]) || e[1] - t[1]).map(([e]) => e), g = (t) => {
111
+ try {
112
+ return e.make(t);
113
+ } catch {
114
+ return;
115
+ }
116
+ }, _ = (e) => {
117
+ let t = g(e);
118
+ if (t === void 0) return [];
119
+ let n = t.$defs ?? {}, r = o(t, n), i = r.properties;
120
+ return i ? h(m(i, new Set(Array.isArray(r.required) ? r.required : []), n)) : [];
121
+ }, v = (e) => {
122
+ let t = g(e);
123
+ if (t === void 0) return [];
124
+ let n = t.$defs ?? {}, r = o(t, n);
125
+ if (r.type !== "array") return [];
126
+ let i = r.items;
127
+ if (!i || typeof i != "object") return [];
128
+ let a = o(i, n), s = a.properties;
129
+ return s ? m(s, new Set(Array.isArray(a.required) ? a.required : []), n) : [];
130
+ }, y = (e) => {
131
+ let t = [];
132
+ for (let n of e) {
133
+ let e = t[t.length - 1];
134
+ if (e !== void 0 && e.name === n.section) {
135
+ e.fields.push(n);
136
+ continue;
137
+ }
138
+ t.push({
139
+ name: n.section,
140
+ label: n.sectionLabel,
141
+ fields: [n]
142
+ });
143
+ }
144
+ return t;
145
+ }, b = {
146
+ en: {
147
+ "validation.required": "Required",
148
+ "validation.invalid": "Invalid value",
149
+ "validation.minLength": "Must be at least {min} characters",
150
+ "validation.maxLength": "Must be at most {max} characters",
151
+ "validation.pattern": "Invalid format",
152
+ "validation.minValue": "Must be at least {min}",
153
+ "validation.maxValue": "Must be at most {max}",
154
+ "validation.minItems": "Add at least {min}",
155
+ "validation.maxItems": "No more than {max}",
156
+ "validation.integer": "Must be a whole number",
157
+ "validation.checking": "Still checking…"
158
+ },
159
+ de: {
160
+ "validation.required": "Pflichtfeld",
161
+ "validation.invalid": "Ungültiger Wert",
162
+ "validation.minLength": "Mindestens {min} Zeichen",
163
+ "validation.maxLength": "Höchstens {max} Zeichen",
164
+ "validation.pattern": "Ungültiges Format",
165
+ "validation.minValue": "Mindestens {min}",
166
+ "validation.maxValue": "Höchstens {max}",
167
+ "validation.minItems": "Mindestens {min}",
168
+ "validation.maxItems": "Höchstens {max}",
169
+ "validation.integer": "Ganze Zahl erforderlich",
170
+ "validation.checking": "Prüfung läuft…"
171
+ }
172
+ }, x = (e, t) => e.replace(/\{(\w+)\}/g, (e, n) => {
173
+ let r = t?.[n];
174
+ return r === void 0 ? e : String(r);
175
+ }), S = () => {
176
+ let e = globalThis.document?.documentElement?.lang ?? "";
177
+ return e === "" ? "en" : e;
178
+ }, C = (e, t) => {
179
+ let n = t?.messages?.(e.id, e.params, e.fallback);
180
+ if (n !== void 0) return n;
181
+ let r = b[(t?.locale ?? S()).toLowerCase().split("-")[0] ?? "en"]?.[e.id] ?? b.en?.[e.id];
182
+ return r === void 0 ? e.fallback : x(r, e.params);
183
+ }, w = (e) => {
184
+ let t = /^([a-z][\w-]*(?:\.[\w-]+)+)\|(\{.*\})$/.exec(e);
185
+ if (t !== null && t[1] !== void 0 && t[2] !== void 0) try {
186
+ return {
187
+ id: t[1],
188
+ params: JSON.parse(t[2]),
189
+ fallback: e
190
+ };
191
+ } catch {}
192
+ return /^[a-z][\w-]*(?:\.[\w-]+)+$/.test(e) ? {
193
+ id: e,
194
+ fallback: e
195
+ } : {
196
+ id: "validation.custom",
197
+ fallback: e
198
+ };
199
+ }, T = (e) => {
200
+ let r = e[n.SchemaIdAnnotationId], i = e[n.JSONSchemaAnnotationId] ?? {};
201
+ switch (r) {
202
+ case t.MinLengthSchemaId: return {
203
+ id: "validation.minLength",
204
+ params: { min: i.minLength }
205
+ };
206
+ case t.MaxLengthSchemaId: return {
207
+ id: "validation.maxLength",
208
+ params: { max: i.maxLength }
209
+ };
210
+ case t.PatternSchemaId: return {
211
+ id: "validation.pattern",
212
+ params: { pattern: i.pattern }
213
+ };
214
+ case t.GreaterThanOrEqualToSchemaId: return {
215
+ id: "validation.minValue",
216
+ params: { min: i.minimum }
217
+ };
218
+ case t.LessThanOrEqualToSchemaId: return {
219
+ id: "validation.maxValue",
220
+ params: { max: i.maximum }
221
+ };
222
+ case t.GreaterThanSchemaId: return {
223
+ id: "validation.minValue",
224
+ params: {
225
+ min: i.exclusiveMinimum,
226
+ exclusive: !0
227
+ }
228
+ };
229
+ case t.LessThanSchemaId: return {
230
+ id: "validation.maxValue",
231
+ params: {
232
+ max: i.exclusiveMaximum,
233
+ exclusive: !0
234
+ }
235
+ };
236
+ case t.IntSchemaId: return { id: "validation.integer" };
237
+ case t.MinItemsSchemaId: return {
238
+ id: "validation.minItems",
239
+ params: { min: i.minItems }
240
+ };
241
+ case t.MaxItemsSchemaId: return {
242
+ id: "validation.maxItems",
243
+ params: { max: i.maxItems }
244
+ };
245
+ case t.BetweenSchemaId: return {
246
+ id: "validation.minValue",
247
+ params: {
248
+ min: i.minimum,
249
+ max: i.maximum
250
+ }
251
+ };
252
+ default: return { id: "validation.invalid" };
253
+ }
254
+ }, E = (e, t) => {
255
+ let r = e[n.MessageAnnotationId];
256
+ if (typeof r == "function") try {
257
+ let e = r(t);
258
+ if (typeof e == "string") return e;
259
+ if (typeof e == "object" && e && typeof e.message == "string") return e.message;
260
+ } catch {}
261
+ }, D = (e) => Array.isArray(e) ? e.map((e) => String(e)) : [String(e)], O = (e) => {
262
+ let t = [], n = (e, n, r, i) => {
263
+ t.push({
264
+ path: e.join("."),
265
+ id: n,
266
+ ...r === void 0 ? {} : { params: r },
267
+ fallback: i
268
+ });
269
+ }, r = (e, t) => {
270
+ let r = w(t);
271
+ n(e, r.id, r.params, r.fallback);
272
+ }, i = (e, t) => {
273
+ switch (e._tag) {
274
+ case "Composite": {
275
+ let n = e.issues;
276
+ if (n === void 0) return;
277
+ for (let e of Array.isArray(n) ? n : [n]) i(e, t);
278
+ return;
279
+ }
280
+ case "Pointer":
281
+ e.issue !== void 0 && i(e.issue, [...t, ...D(e.path)]);
282
+ return;
283
+ case "Refinement": {
284
+ if (e.kind === "From") {
285
+ e.issue !== void 0 && i(e.issue, t);
286
+ return;
287
+ }
288
+ let a = e.ast?.annotations ?? {}, o = E(a, e);
289
+ if (o !== void 0) return r(t, o);
290
+ let s = T(a);
291
+ if (s.id !== "validation.invalid") {
292
+ n(t, s.id, s.params, k(e));
293
+ return;
294
+ }
295
+ if (e.issue !== void 0 && (e.issue._tag === "Pointer" || e.issue._tag === "Composite" || e.issue._tag === "Type")) {
296
+ i(e.issue, t);
297
+ return;
298
+ }
299
+ n(t, "validation.invalid", void 0, k(e));
300
+ return;
301
+ }
302
+ case "Transformation":
303
+ e.issue !== void 0 && i(e.issue, t);
304
+ return;
305
+ case "Missing":
306
+ n(t, "validation.required", void 0, "Required");
307
+ return;
308
+ case "Type": {
309
+ if (typeof e.message == "string" && e.message.length > 0) return r(t, e.message);
310
+ let i = e.ast?.annotations, a = i === void 0 ? void 0 : E(i, e);
311
+ if (a !== void 0) return r(t, a);
312
+ n(t, "validation.invalid", void 0, k(e));
313
+ return;
314
+ }
315
+ default:
316
+ n(t, "validation.invalid", void 0, k(e));
317
+ return;
318
+ }
319
+ };
320
+ return i(e.issue, []), t;
321
+ }, k = (e) => typeof e.message == "string" && e.message.length > 0 ? e.message : "Invalid value", A = (e, n, r) => {
322
+ let i = t.decodeUnknownEither(e, { errors: "all" })(n);
323
+ if (i._tag === "Right") return {
324
+ valid: !0,
325
+ errors: {},
326
+ issues: []
327
+ };
328
+ let a = O(i.left), o = {};
329
+ for (let e of a) e.path !== "" && !(e.path in o) && (o[e.path] = C(e, r));
330
+ return {
331
+ valid: !1,
332
+ errors: o,
333
+ issues: a
334
+ };
335
+ }, j = "__voltro_form_flash__", M = "/form/", N = (e) => `${M}${encodeURIComponent(e)}`, P = "__voltro_form", F = "__voltro_redirect", I = (e) => {
336
+ let t = Number(e);
337
+ return Number.isNaN(t) ? e : t;
338
+ }, L = (e) => {
339
+ let t = e.jsonSchema.items;
340
+ if (!t || typeof t != "object") return;
341
+ let n = t.type;
342
+ return typeof n == "string" ? n : void 0;
343
+ }, R = (e, t) => {
344
+ switch (e.widget) {
345
+ case "number": return t === "" ? void 0 : I(t);
346
+ case "date":
347
+ case "datetime": return t === "" ? void 0 : t;
348
+ default: return t;
349
+ }
350
+ }, z = (e, t) => {
351
+ let n = /* @__PURE__ */ new Map();
352
+ for (let [e, r] of t) {
353
+ let t = n.get(e);
354
+ t === void 0 ? n.set(e, [r]) : t.push(r);
355
+ }
356
+ let r = {}, i = (e, t) => {
357
+ let n = e.split("."), i = r;
358
+ for (let e of n.slice(0, -1)) {
359
+ let t = i[e];
360
+ if (typeof t != "object" || !t) {
361
+ let t = {};
362
+ i[e] = t, i = t;
363
+ } else i = t;
364
+ }
365
+ i[n[n.length - 1]] = t;
366
+ };
367
+ for (let t of _(e)) {
368
+ if (t.widget === "array") continue;
369
+ let e = n.get(t.name);
370
+ if (t.widget === "checkbox" || t.widget === "switch") {
371
+ i(t.name, e !== void 0);
372
+ continue;
373
+ }
374
+ if (t.widget === "multi-select") {
375
+ let n = L(t), r = e ?? [];
376
+ i(t.name, n === "number" || n === "integer" ? r.filter((e) => e !== "").map(I) : r.filter((e) => e !== ""));
377
+ continue;
378
+ }
379
+ if (e === void 0) continue;
380
+ let r = R(t, e[0]);
381
+ r !== void 0 && i(t.name, r);
382
+ }
383
+ return r;
384
+ };
385
+ //#endregion
386
+ export { N as a, S as c, r as d, y as f, F as i, C as l, _ as m, P as n, z as o, v as p, M as r, O as s, j as t, A as u };