@valbuild/shared 0.64.0 → 0.65.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.
@@ -3,7 +3,7 @@ import { z } from 'zod';
3
3
  import { result } from '@valbuild/core/fp';
4
4
  import { fromZodError } from 'zod-validation-error';
5
5
 
6
- function toPrimitive(t, r) {
6
+ function _toPrimitive(t, r) {
7
7
  if ("object" != typeof t || !t) return t;
8
8
  var e = t[Symbol.toPrimitive];
9
9
  if (void 0 !== e) {
@@ -14,24 +14,18 @@ function toPrimitive(t, r) {
14
14
  return ("string" === r ? String : Number)(t);
15
15
  }
16
16
 
17
- function toPropertyKey(t) {
18
- var i = toPrimitive(t, "string");
19
- return "symbol" == typeof i ? i : String(i);
17
+ function _toPropertyKey(t) {
18
+ var i = _toPrimitive(t, "string");
19
+ return "symbol" == typeof i ? i : i + "";
20
20
  }
21
21
 
22
- function _defineProperty(obj, key, value) {
23
- key = toPropertyKey(key);
24
- if (key in obj) {
25
- Object.defineProperty(obj, key, {
26
- value: value,
27
- enumerable: true,
28
- configurable: true,
29
- writable: true
30
- });
31
- } else {
32
- obj[key] = value;
33
- }
34
- return obj;
22
+ function _defineProperty(e, r, t) {
23
+ return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
24
+ value: t,
25
+ enumerable: !0,
26
+ configurable: !0,
27
+ writable: !0
28
+ }) : e[r] = t, e;
35
29
  }
36
30
 
37
31
  function ownKeys(e, r) {
@@ -56,68 +50,64 @@ function _objectSpread2(e) {
56
50
  return e;
57
51
  }
58
52
 
59
- function _arrayLikeToArray(arr, len) {
60
- if (len == null || len > arr.length) len = arr.length;
61
- for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
62
- return arr2;
53
+ function _arrayLikeToArray(r, a) {
54
+ (null == a || a > r.length) && (a = r.length);
55
+ for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e];
56
+ return n;
63
57
  }
64
58
 
65
- function _unsupportedIterableToArray(o, minLen) {
66
- if (!o) return;
67
- if (typeof o === "string") return _arrayLikeToArray(o, minLen);
68
- var n = Object.prototype.toString.call(o).slice(8, -1);
69
- if (n === "Object" && o.constructor) n = o.constructor.name;
70
- if (n === "Map" || n === "Set") return Array.from(o);
71
- if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
59
+ function _unsupportedIterableToArray(r, a) {
60
+ if (r) {
61
+ if ("string" == typeof r) return _arrayLikeToArray(r, a);
62
+ var t = {}.toString.call(r).slice(8, -1);
63
+ return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0;
64
+ }
72
65
  }
73
66
 
74
- function _createForOfIteratorHelper(o, allowArrayLike) {
75
- var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"];
76
- if (!it) {
77
- if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") {
78
- if (it) o = it;
79
- var i = 0;
80
- var F = function () {};
67
+ function _createForOfIteratorHelper(r, e) {
68
+ var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"];
69
+ if (!t) {
70
+ if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) {
71
+ t && (r = t);
72
+ var n = 0,
73
+ F = function () {};
81
74
  return {
82
75
  s: F,
83
76
  n: function () {
84
- if (i >= o.length) return {
85
- done: true
86
- };
87
- return {
88
- done: false,
89
- value: o[i++]
77
+ return n >= r.length ? {
78
+ done: !0
79
+ } : {
80
+ done: !1,
81
+ value: r[n++]
90
82
  };
91
83
  },
92
- e: function (e) {
93
- throw e;
84
+ e: function (r) {
85
+ throw r;
94
86
  },
95
87
  f: F
96
88
  };
97
89
  }
98
90
  throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
99
91
  }
100
- var normalCompletion = true,
101
- didErr = false,
102
- err;
92
+ var o,
93
+ a = !0,
94
+ u = !1;
103
95
  return {
104
96
  s: function () {
105
- it = it.call(o);
97
+ t = t.call(r);
106
98
  },
107
99
  n: function () {
108
- var step = it.next();
109
- normalCompletion = step.done;
110
- return step;
100
+ var r = t.next();
101
+ return a = r.done, r;
111
102
  },
112
- e: function (e) {
113
- didErr = true;
114
- err = e;
103
+ e: function (r) {
104
+ u = !0, o = r;
115
105
  },
116
106
  f: function () {
117
107
  try {
118
- if (!normalCompletion && it.return != null) it.return();
108
+ a || null == t.return || t.return();
119
109
  } finally {
120
- if (didErr) throw err;
110
+ if (u) throw o;
121
111
  }
122
112
  }
123
113
  };
@@ -127,7 +117,7 @@ function remirrorToRichTextSource(node) {
127
117
  var files = {};
128
118
  var blocks = [];
129
119
  var i = 0;
130
- var _iterator = _createForOfIteratorHelper(node.content),
120
+ var _iterator = _createForOfIteratorHelper((node === null || node === void 0 ? void 0 : node.content) || []),
131
121
  _step;
132
122
  try {
133
123
  for (_iterator.s(); !(_step = _iterator.n()).done;) {
@@ -308,9 +298,12 @@ function convertImageNode(path, node, files) {
308
298
  if (mimeType === undefined) {
309
299
  throw new Error("Could not detect Mime Type for image: ".concat(node.attrs.src));
310
300
  }
311
- var fileExt = Internal.mimeTypeToFileExt(mimeType);
312
- var fileName = node.attrs.fileName || "".concat(sha256, ".").concat(fileExt);
313
- var filePath = "/public/".concat(fileName);
301
+ var fileName = Internal.createFilename(node.attrs.src, node.attrs.fileName || "", {
302
+ width: typeof node.attrs.width === "number" ? node.attrs.width : 0,
303
+ height: typeof node.attrs.height === "number" ? node.attrs.height : 0,
304
+ mimeType: mimeType
305
+ }, sha256);
306
+ var filePath = "/public/val/".concat(fileName);
314
307
  var existingFilesEntry = files[filePath];
315
308
  var thisPath = path
316
309
  // file is added as src (see below):
@@ -328,21 +321,32 @@ function convertImageNode(path, node, files) {
328
321
  src: _defineProperty(_defineProperty(_defineProperty({}, VAL_EXTENSION, "file"), FILE_REF_PROP, filePath), "metadata", {
329
322
  width: typeof node.attrs.width === "number" ? node.attrs.width : 0,
330
323
  height: typeof node.attrs.height === "number" ? node.attrs.height : 0,
331
- sha256: sha256 || "",
332
324
  mimeType: mimeType
333
325
  })
334
326
  };
335
327
  } else if (node.attrs) {
336
- var _sha = getParam("sha256", node.attrs.src);
337
- var patchId = getParam("patch_id", node.attrs.src);
338
- var noParamsSrc = node.attrs.src.split("?")[0];
328
+ var url = node.attrs.src;
329
+ var patchId = getParam("patch_id", url);
330
+ var noParamsUrl = url.split("?")[0];
331
+ if (patchId) {
332
+ if (noParamsUrl.startsWith("/api/val/files/public")) {
333
+ noParamsUrl = noParamsUrl.slice("/api/val/files".length);
334
+ } else {
335
+ console.error("Patched image URL does not start with /api/val/files: " + url);
336
+ }
337
+ } else {
338
+ if (!noParamsUrl.startsWith("/public")) {
339
+ noParamsUrl = "/public".concat(noParamsUrl);
340
+ } else {
341
+ console.error("Unpatched image URL starts with /public: " + url);
342
+ }
343
+ }
339
344
  var tag = {
340
345
  tag: "img",
341
- src: _objectSpread2(_defineProperty(_defineProperty(_defineProperty({}, VAL_EXTENSION, "file"), FILE_REF_PROP, "/public".concat(node.attrs.src.split("?")[0])), "metadata", {
346
+ src: _objectSpread2(_defineProperty(_defineProperty(_defineProperty({}, VAL_EXTENSION, "file"), FILE_REF_PROP, noParamsUrl), "metadata", {
342
347
  width: typeof node.attrs.width === "number" ? node.attrs.width : 0,
343
348
  height: typeof node.attrs.height === "number" ? node.attrs.height : 0,
344
- sha256: _sha || "",
345
- mimeType: noParamsSrc && Internal.filenameToMimeType(noParamsSrc) || ""
349
+ mimeType: noParamsUrl && Internal.filenameToMimeType(noParamsUrl) || ""
346
350
  }), patchId ? {
347
351
  patch_id: patchId
348
352
  } : {})
@@ -447,20 +451,20 @@ var RemirrorJSON = z.object({
447
451
  content: z.array(z.union([RemirrorParagraph, RemirrorHeading, RemirrorBulletList, RemirrorOrderedList]))
448
452
  });
449
453
 
450
- function _arrayWithoutHoles(arr) {
451
- if (Array.isArray(arr)) return _arrayLikeToArray(arr);
454
+ function _arrayWithoutHoles(r) {
455
+ if (Array.isArray(r)) return _arrayLikeToArray(r);
452
456
  }
453
457
 
454
- function _iterableToArray(iter) {
455
- if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
458
+ function _iterableToArray(r) {
459
+ if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r);
456
460
  }
457
461
 
458
462
  function _nonIterableSpread() {
459
463
  throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
460
464
  }
461
465
 
462
- function _toConsumableArray(arr) {
463
- return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread();
466
+ function _toConsumableArray(r) {
467
+ return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread();
464
468
  }
465
469
 
466
470
  function richTextToRemirror(richtext) {
@@ -712,8 +716,8 @@ var VAL_SESSION_COOKIE = Internal.VAL_SESSION_COOKIE;
712
716
  var VAL_STATE_COOKIE = Internal.VAL_STATE_COOKIE;
713
717
  var VAL_ENABLE_COOKIE_NAME = Internal.VAL_ENABLE_COOKIE_NAME;
714
718
 
715
- function _arrayWithHoles(arr) {
716
- if (Array.isArray(arr)) return arr;
719
+ function _arrayWithHoles(r) {
720
+ if (Array.isArray(r)) return r;
717
721
  }
718
722
 
719
723
  function _iterableToArrayLimit(r, l) {
@@ -748,8 +752,8 @@ function _nonIterableRest() {
748
752
  throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
749
753
  }
750
754
 
751
- function _slicedToArray(arr, i) {
752
- return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest();
755
+ function _slicedToArray(r, e) {
756
+ return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest();
753
757
  }
754
758
 
755
759
  function _regeneratorRuntime() {
@@ -860,7 +864,7 @@ function _regeneratorRuntime() {
860
864
  function makeInvokeMethod(e, r, n) {
861
865
  var o = h;
862
866
  return function (i, a) {
863
- if (o === f) throw new Error("Generator is already running");
867
+ if (o === f) throw Error("Generator is already running");
864
868
  if (o === s) {
865
869
  if ("throw" === i) throw a;
866
870
  return {
@@ -1002,7 +1006,7 @@ function _regeneratorRuntime() {
1002
1006
  } else if (c) {
1003
1007
  if (this.prev < i.catchLoc) return handle(i.catchLoc, !0);
1004
1008
  } else {
1005
- if (!u) throw new Error("try statement without catch or finally");
1009
+ if (!u) throw Error("try statement without catch or finally");
1006
1010
  if (this.prev < i.finallyLoc) return handle(i.finallyLoc);
1007
1011
  }
1008
1012
  }
@@ -1042,7 +1046,7 @@ function _regeneratorRuntime() {
1042
1046
  return o;
1043
1047
  }
1044
1048
  }
1045
- throw new Error("illegal catch attempt");
1049
+ throw Error("illegal catch attempt");
1046
1050
  },
1047
1051
  delegateYield: function (e, r, n) {
1048
1052
  return this.delegate = {
@@ -1054,59 +1058,46 @@ function _regeneratorRuntime() {
1054
1058
  }, e;
1055
1059
  }
1056
1060
 
1057
- function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
1061
+ function asyncGeneratorStep(n, t, e, r, o, a, c) {
1058
1062
  try {
1059
- var info = gen[key](arg);
1060
- var value = info.value;
1061
- } catch (error) {
1062
- reject(error);
1063
- return;
1064
- }
1065
- if (info.done) {
1066
- resolve(value);
1067
- } else {
1068
- Promise.resolve(value).then(_next, _throw);
1063
+ var i = n[a](c),
1064
+ u = i.value;
1065
+ } catch (n) {
1066
+ return void e(n);
1069
1067
  }
1068
+ i.done ? t(u) : Promise.resolve(u).then(r, o);
1070
1069
  }
1071
- function _asyncToGenerator(fn) {
1070
+ function _asyncToGenerator(n) {
1072
1071
  return function () {
1073
- var self = this,
1074
- args = arguments;
1075
- return new Promise(function (resolve, reject) {
1076
- var gen = fn.apply(self, args);
1077
- function _next(value) {
1078
- asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
1072
+ var t = this,
1073
+ e = arguments;
1074
+ return new Promise(function (r, o) {
1075
+ var a = n.apply(t, e);
1076
+ function _next(n) {
1077
+ asyncGeneratorStep(a, r, o, _next, _throw, "next", n);
1079
1078
  }
1080
- function _throw(err) {
1081
- asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
1079
+ function _throw(n) {
1080
+ asyncGeneratorStep(a, r, o, _next, _throw, "throw", n);
1082
1081
  }
1083
- _next(undefined);
1082
+ _next(void 0);
1084
1083
  });
1085
1084
  };
1086
1085
  }
1087
1086
 
1088
- function _classCallCheck(instance, Constructor) {
1089
- if (!(instance instanceof Constructor)) {
1090
- throw new TypeError("Cannot call a class as a function");
1091
- }
1087
+ function _classCallCheck(a, n) {
1088
+ if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function");
1092
1089
  }
1093
1090
 
1094
- function _defineProperties(target, props) {
1095
- for (var i = 0; i < props.length; i++) {
1096
- var descriptor = props[i];
1097
- descriptor.enumerable = descriptor.enumerable || false;
1098
- descriptor.configurable = true;
1099
- if ("value" in descriptor) descriptor.writable = true;
1100
- Object.defineProperty(target, toPropertyKey(descriptor.key), descriptor);
1091
+ function _defineProperties(e, r) {
1092
+ for (var t = 0; t < r.length; t++) {
1093
+ var o = r[t];
1094
+ o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o);
1101
1095
  }
1102
1096
  }
1103
- function _createClass(Constructor, protoProps, staticProps) {
1104
- if (protoProps) _defineProperties(Constructor.prototype, protoProps);
1105
- if (staticProps) _defineProperties(Constructor, staticProps);
1106
- Object.defineProperty(Constructor, "prototype", {
1107
- writable: false
1108
- });
1109
- return Constructor;
1097
+ function _createClass(e, r, t) {
1098
+ return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", {
1099
+ writable: !1
1100
+ }), e;
1110
1101
  }
1111
1102
 
1112
1103
  var ValCache = /*#__PURE__*/function () {
@@ -1116,10 +1107,10 @@ var ValCache = /*#__PURE__*/function () {
1116
1107
  this.drafts = {};
1117
1108
  this.schema = {};
1118
1109
  }
1119
- _createClass(ValCache, [{
1110
+ return _createClass(ValCache, [{
1120
1111
  key: "reloadPaths",
1121
1112
  value: function () {
1122
- var _reloadPaths = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(paths) {
1113
+ var _reloadPaths = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(paths) {
1123
1114
  var patchesResponse, patches, filteredPatches, treeRes, data, _i, _Object$keys, _data$modules, pathS, path;
1124
1115
  return _regeneratorRuntime().wrap(function _callee$(_context) {
1125
1116
  while (1) switch (_context.prev = _context.next) {
@@ -1145,14 +1136,13 @@ var ValCache = /*#__PURE__*/function () {
1145
1136
  patches = patchesResponse.json;
1146
1137
  filteredPatches = Object.keys(patches);
1147
1138
  _context.next = 10;
1148
- return this.client("/tree/~", "PUT", {
1139
+ return this.client("/sources", "PUT", {
1149
1140
  query: {
1150
1141
  validate_sources: true,
1151
1142
  validate_all: false,
1152
1143
  validate_binary_files: false
1153
1144
  },
1154
1145
  path: undefined,
1155
- // TODO: reload only the paths the requested paths
1156
1146
  body: {
1157
1147
  patchIds: filteredPatches
1158
1148
  }
@@ -1187,7 +1177,7 @@ var ValCache = /*#__PURE__*/function () {
1187
1177
  }, {
1188
1178
  key: "reset",
1189
1179
  value: function () {
1190
- var _reset = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2() {
1180
+ var _reset = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee2() {
1191
1181
  var patchesRes, allPatches, treeRes, initRes, _i2, _Object$keys2, _treeRes$json, pathS, path;
1192
1182
  return _regeneratorRuntime().wrap(function _callee2$(_context2) {
1193
1183
  while (1) switch (_context2.prev = _context2.next) {
@@ -1215,7 +1205,7 @@ var ValCache = /*#__PURE__*/function () {
1215
1205
  case 6:
1216
1206
  allPatches = Object.keys(patchesRes.json.patches);
1217
1207
  _context2.next = 9;
1218
- return this.client("/tree/~", "PUT", {
1208
+ return this.client("/sources", "PUT", {
1219
1209
  path: undefined,
1220
1210
  query: {
1221
1211
  validate_sources: false,
@@ -1294,7 +1284,7 @@ var ValCache = /*#__PURE__*/function () {
1294
1284
  }, {
1295
1285
  key: "getModule",
1296
1286
  value: function () {
1297
- var _getModule = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3(path) {
1287
+ var _getModule = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee3(path) {
1298
1288
  var refetch,
1299
1289
  treeRes,
1300
1290
  _treeRes$json2,
@@ -1316,7 +1306,7 @@ var ValCache = /*#__PURE__*/function () {
1316
1306
  }));
1317
1307
  case 3:
1318
1308
  _context3.next = 5;
1319
- return this.client("/tree/~", "PUT", {
1309
+ return this.client("/sources", "PUT", {
1320
1310
  path: path,
1321
1311
  body: {},
1322
1312
  query: {
@@ -1406,7 +1396,7 @@ var ValCache = /*#__PURE__*/function () {
1406
1396
  }, {
1407
1397
  key: "deletePatches",
1408
1398
  value: function () {
1409
- var _deletePatches = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee4(patchIds) {
1399
+ var _deletePatches = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee4(patchIds) {
1410
1400
  var patchesRes;
1411
1401
  return _regeneratorRuntime().wrap(function _callee4$(_context4) {
1412
1402
  while (1) switch (_context4.prev = _context4.next) {
@@ -1443,13 +1433,13 @@ var ValCache = /*#__PURE__*/function () {
1443
1433
  }, {
1444
1434
  key: "applyPatch",
1445
1435
  value: function () {
1446
- var _applyPatch = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee5(path, patchIds, patch) {
1447
- var treeRes, _treeRes$json4, newPatchId, fetchedSource, _treeRes$json5;
1436
+ var _applyPatch = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee5(path, patchIds, patch) {
1437
+ var treeRes, _treeRes$json4, newPatchIds, newPatchId, fetchedSource, _treeRes$json5;
1448
1438
  return _regeneratorRuntime().wrap(function _callee5$(_context5) {
1449
1439
  while (1) switch (_context5.prev = _context5.next) {
1450
1440
  case 0:
1451
1441
  _context5.next = 2;
1452
- return this.client("/tree/~", "PUT", {
1442
+ return this.client("/sources", "PUT", {
1453
1443
  path: path,
1454
1444
  query: {
1455
1445
  validate_sources: false,
@@ -1458,20 +1448,20 @@ var ValCache = /*#__PURE__*/function () {
1458
1448
  },
1459
1449
  body: {
1460
1450
  patchIds: patchIds,
1461
- addPatch: {
1451
+ addPatches: [{
1462
1452
  path: path,
1463
1453
  patch: patch
1464
- }
1454
+ }]
1465
1455
  }
1466
1456
  });
1467
1457
  case 2:
1468
1458
  treeRes = _context5.sent;
1469
1459
  if (!(treeRes.status === 200)) {
1470
- _context5.next = 19;
1460
+ _context5.next = 20;
1471
1461
  break;
1472
1462
  }
1473
- newPatchId = treeRes.json.newPatchId;
1474
- if (newPatchId) {
1463
+ newPatchIds = treeRes.json.newPatchIds;
1464
+ if (newPatchIds) {
1475
1465
  _context5.next = 8;
1476
1466
  break;
1477
1467
  }
@@ -1481,9 +1471,10 @@ var ValCache = /*#__PURE__*/function () {
1481
1471
  message: "Val: could not create patch"
1482
1472
  }));
1483
1473
  case 8:
1474
+ newPatchId = newPatchIds[0];
1484
1475
  fetchedSource = (_treeRes$json4 = treeRes.json) === null || _treeRes$json4 === void 0 || (_treeRes$json4 = _treeRes$json4.modules) === null || _treeRes$json4 === void 0 || (_treeRes$json4 = _treeRes$json4[path]) === null || _treeRes$json4 === void 0 ? void 0 : _treeRes$json4.source;
1485
1476
  if (!(fetchedSource !== undefined)) {
1486
- _context5.next = 15;
1477
+ _context5.next = 16;
1487
1478
  break;
1488
1479
  }
1489
1480
  this.drafts[path] = fetchedSource;
@@ -1494,18 +1485,18 @@ var ValCache = /*#__PURE__*/function () {
1494
1485
  patchIds: ((_treeRes$json5 = treeRes.json) === null || _treeRes$json5 === void 0 || (_treeRes$json5 = _treeRes$json5.modules) === null || _treeRes$json5 === void 0 || (_treeRes$json5 = _treeRes$json5[path]) === null || _treeRes$json5 === void 0 || (_treeRes$json5 = _treeRes$json5.patches) === null || _treeRes$json5 === void 0 ? void 0 : _treeRes$json5.applied) || []
1495
1486
  })
1496
1487
  }));
1497
- case 15:
1488
+ case 16:
1498
1489
  console.error("Val: could not patch");
1499
1490
  return _context5.abrupt("return", result.err({
1500
1491
  errorType: "other",
1501
1492
  message: "Val: could not fetch data. Verify that the module exists."
1502
1493
  }));
1503
- case 17:
1504
- _context5.next = 25;
1494
+ case 18:
1495
+ _context5.next = 26;
1505
1496
  break;
1506
- case 19:
1497
+ case 20:
1507
1498
  if (!(treeRes.status === 400 && "type" in treeRes.json)) {
1508
- _context5.next = 23;
1499
+ _context5.next = 24;
1509
1500
  break;
1510
1501
  }
1511
1502
  return _context5.abrupt("return", result.err({
@@ -1524,13 +1515,13 @@ var ValCache = /*#__PURE__*/function () {
1524
1515
  });
1525
1516
  })
1526
1517
  }));
1527
- case 23:
1518
+ case 24:
1528
1519
  console.error("Val: failed to get module", treeRes.json);
1529
1520
  return _context5.abrupt("return", result.err({
1530
1521
  errorType: "other",
1531
1522
  message: "Val: could not fetch data. Verify that is correctly configured."
1532
1523
  }));
1533
- case 25:
1524
+ case 26:
1534
1525
  case "end":
1535
1526
  return _context5.stop();
1536
1527
  }
@@ -1556,7 +1547,7 @@ var ValCache = /*#__PURE__*/function () {
1556
1547
  }, {
1557
1548
  key: "initialize",
1558
1549
  value: function () {
1559
- var _initialize = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee6() {
1550
+ var _initialize = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee6() {
1560
1551
  var schemaRes, paths, _i3, _arr, moduleId, schema, msg;
1561
1552
  return _regeneratorRuntime().wrap(function _callee6$(_context6) {
1562
1553
  while (1) switch (_context6.prev = _context6.next) {
@@ -1604,7 +1595,6 @@ var ValCache = /*#__PURE__*/function () {
1604
1595
  return initialize;
1605
1596
  }()
1606
1597
  }]);
1607
- return ValCache;
1608
1598
  }();
1609
1599
 
1610
1600
  var JSONValueT = z.lazy(function () {
@@ -1693,12 +1683,17 @@ var SerializedArraySchema = z.lazy(function () {
1693
1683
  });
1694
1684
  });
1695
1685
  var SerializedUnionSchema = z.lazy(function () {
1696
- return z.object({
1686
+ return z.union([z.object({
1697
1687
  type: z.literal("union"),
1698
- key: z.union([z.string(), SerializedSchema]),
1699
- items: z.array(SerializedSchema),
1688
+ key: SerializedLiteralSchema,
1689
+ items: z.array(SerializedLiteralSchema),
1700
1690
  opt: z["boolean"]()
1701
- });
1691
+ }), z.object({
1692
+ type: z.literal("union"),
1693
+ key: z.string(),
1694
+ items: z.array(SerializedObjectSchema),
1695
+ opt: z["boolean"]()
1696
+ })]);
1702
1697
  });
1703
1698
  var RichTextOptions = z.object({
1704
1699
  style: z.object({
@@ -1738,11 +1733,13 @@ var SerializedKeyOfSchema = z.lazy(function () {
1738
1733
  type: z.literal("keyOf"),
1739
1734
  path: SourcePath,
1740
1735
  schema: SerializedSchema,
1741
- values: z.union([z.literal("string"), z.literal("number"), z.array(z.string())]),
1736
+ values: z.union([z.literal("string"), z.array(z.string())]),
1742
1737
  opt: z["boolean"]()
1743
1738
  });
1744
1739
  });
1745
- var FileOptions = z.record(z.never());
1740
+ var FileOptions = z.object({
1741
+ accept: z.string().optional()
1742
+ });
1746
1743
  var SerializedFileSchema = z.object({
1747
1744
  type: z.literal("file"),
1748
1745
  options: FileOptions.optional(),
@@ -1755,7 +1752,8 @@ var SerializedDateSchema = z.object({
1755
1752
  var ImageOptions = z.object({
1756
1753
  ext: z.union([z.tuple([z.literal("jpg")]), z.tuple([z.literal("webp")])]).optional(),
1757
1754
  directory: z.string().optional(),
1758
- prefix: z.string().optional()
1755
+ prefix: z.string().optional(),
1756
+ accept: z.string().optional()
1759
1757
  });
1760
1758
  var SerializedImageSchema = z.object({
1761
1759
  type: z.literal("image"),
@@ -1772,6 +1770,15 @@ var ModuleFilePath = z.string().refine(function (_path) {
1772
1770
  return true;
1773
1771
  } // TODO:
1774
1772
  );
1773
+ var ValConfig = z.object({
1774
+ project: z.string().optional(),
1775
+ root: z.string().optional(),
1776
+ files: z.object({
1777
+ directory: z.string() // TODO: validate that it is prefixed by /public/
1778
+ }).optional(),
1779
+ gitCommit: z.string().optional(),
1780
+ gitBranch: z.string().optional()
1781
+ });
1775
1782
  var ValidationFixZ = z.union([z.literal("image:add-metadata"), z.literal("image:replace-metadata"), z.literal("file:add-metadata"), z.literal("file:check-metadata"), z.literal("fix:deprecated-richtext")]);
1776
1783
  var ValidationError = z.object({
1777
1784
  message: z.string(),
@@ -1801,14 +1808,89 @@ var enableCookieValue = z.object({
1801
1808
  })
1802
1809
  });
1803
1810
  var Api = {
1811
+ "/draft/enable": {
1812
+ GET: {
1813
+ req: {
1814
+ query: {
1815
+ redirect_to: z.string().optional()
1816
+ },
1817
+ cookies: _defineProperty({}, VAL_SESSION_COOKIE, z.string().optional())
1818
+ },
1819
+ res: z.union([z.object({
1820
+ status: z.literal(401),
1821
+ json: GenericError
1822
+ }), z.object({
1823
+ status: z.literal(302),
1824
+ redirectTo: z.string()
1825
+ }), z.object({
1826
+ status: z.literal(400),
1827
+ json: z.object({
1828
+ message: z.string()
1829
+ })
1830
+ })])
1831
+ }
1832
+ },
1833
+ "/draft/disable": {
1834
+ GET: {
1835
+ req: {
1836
+ query: {
1837
+ redirect_to: z.string().optional()
1838
+ },
1839
+ cookies: _defineProperty({}, VAL_SESSION_COOKIE, z.string().optional())
1840
+ },
1841
+ res: z.union([z.object({
1842
+ status: z.literal(401),
1843
+ json: GenericError
1844
+ }), z.object({
1845
+ status: z.literal(302),
1846
+ redirectTo: z.string()
1847
+ }), z.object({
1848
+ status: z.literal(400),
1849
+ json: z.object({
1850
+ message: z.string()
1851
+ })
1852
+ })])
1853
+ }
1854
+ },
1855
+ "/draft/stat": {
1856
+ GET: {
1857
+ req: {
1858
+ cookies: _defineProperty({}, VAL_SESSION_COOKIE, z.string().optional())
1859
+ },
1860
+ res: z.union([z.object({
1861
+ status: z.literal(401),
1862
+ json: GenericError
1863
+ }), z.object({
1864
+ status: z.literal(200),
1865
+ json: z.object({
1866
+ draftMode: z["boolean"]()
1867
+ })
1868
+ })])
1869
+ }
1870
+ },
1804
1871
  "/enable": {
1805
1872
  GET: {
1806
1873
  req: {
1807
1874
  query: {
1808
1875
  redirect_to: z.string().optional()
1809
- }
1876
+ },
1877
+ cookies: _defineProperty({}, VAL_SESSION_COOKIE, z.string().optional())
1810
1878
  },
1811
1879
  res: z.union([z.object({
1880
+ status: z.literal(401),
1881
+ json: GenericError
1882
+ }), z.object({
1883
+ status: z.literal(302),
1884
+ redirectTo: z.string(),
1885
+ cookies: z.object(_defineProperty(_defineProperty({}, VAL_ENABLE_COOKIE_NAME, enableCookieValue), VAL_STATE_COOKIE, z.object({
1886
+ value: z.string(),
1887
+ options: z.object({
1888
+ httpOnly: z.literal(true),
1889
+ sameSite: z.literal("lax"),
1890
+ expires: z["instanceof"](Date)
1891
+ })
1892
+ })))
1893
+ }), z.object({
1812
1894
  status: z.literal(302),
1813
1895
  redirectTo: z.string(),
1814
1896
  cookies: z.object(_defineProperty({}, VAL_ENABLE_COOKIE_NAME, enableCookieValue))
@@ -1825,9 +1907,13 @@ var Api = {
1825
1907
  req: {
1826
1908
  query: {
1827
1909
  redirect_to: z.string().optional()
1828
- }
1910
+ },
1911
+ cookies: _defineProperty({}, VAL_SESSION_COOKIE, z.string().optional())
1829
1912
  },
1830
1913
  res: z.union([z.object({
1914
+ status: z.literal(401),
1915
+ json: GenericError
1916
+ }), z.object({
1831
1917
  status: z.literal(302),
1832
1918
  redirectTo: z.string(),
1833
1919
  cookies: z.object(_defineProperty({}, VAL_ENABLE_COOKIE_NAME, z.object({
@@ -1950,6 +2036,45 @@ var Api = {
1950
2036
  })
1951
2037
  }
1952
2038
  },
2039
+ "/stat": {
2040
+ POST: {
2041
+ req: {
2042
+ body: z.object({
2043
+ schemaSha: z.string(),
2044
+ baseSha: z.string(),
2045
+ patches: z.array(z.string()).optional()
2046
+ }).nullable(),
2047
+ cookies: {
2048
+ val_session: z.string().optional()
2049
+ }
2050
+ },
2051
+ res: z.union([z.object({
2052
+ status: z.literal(401),
2053
+ json: GenericError
2054
+ }), z.object({
2055
+ status: z.literal(500),
2056
+ json: GenericError
2057
+ }), z.object({
2058
+ status: z.literal(200),
2059
+ json: z.union([z.object({
2060
+ type: z.union([z.literal("request-again"), z.literal("no-change"), z.literal("did-change")]),
2061
+ baseSha: z.string(),
2062
+ schemaSha: z.string(),
2063
+ patches: z.array(PatchId),
2064
+ config: ValConfig
2065
+ }), z.object({
2066
+ type: z.literal("use-websocket"),
2067
+ url: z.string(),
2068
+ nonce: z.string(),
2069
+ baseSha: z.string(),
2070
+ schemaSha: z.string(),
2071
+ commitSha: z.string(),
2072
+ patches: z.array(PatchId),
2073
+ config: ValConfig
2074
+ })])
2075
+ })])
2076
+ }
2077
+ },
1953
2078
  "/patches/~": {
1954
2079
  DELETE: {
1955
2080
  req: {
@@ -2024,6 +2149,12 @@ var Api = {
2024
2149
  message: z.string(),
2025
2150
  details: z.array(ModulesError)
2026
2151
  })
2152
+ }), z.object({
2153
+ status: z.literal(500),
2154
+ json: z.object({
2155
+ message: z.string(),
2156
+ details: z.array(GenericError)
2157
+ })
2027
2158
  }), z.object({
2028
2159
  status: z.literal(200),
2029
2160
  json: z.object({
@@ -2033,16 +2164,16 @@ var Api = {
2033
2164
  })])
2034
2165
  }
2035
2166
  },
2036
- "/tree/~": {
2167
+ "/sources": {
2037
2168
  PUT: {
2038
2169
  req: {
2039
2170
  path: z.string().optional(),
2040
2171
  body: z.object({
2041
2172
  patchIds: z.array(PatchId).optional(),
2042
- addPatch: z.object({
2173
+ addPatches: z.array(z.object({
2043
2174
  path: ModuleFilePath,
2044
2175
  patch: Patch
2045
- }).optional()
2176
+ })).optional()
2046
2177
  }).optional(),
2047
2178
  query: {
2048
2179
  validate_all: z["boolean"]().optional(),
@@ -2056,6 +2187,10 @@ var Api = {
2056
2187
  res: z.union([notFoundResponse, z.object({
2057
2188
  status: z.literal(401),
2058
2189
  json: GenericError
2190
+ }), z.object({
2191
+ status: z.literal(409),
2192
+ // conflict: i.e. not a head of patches
2193
+ json: GenericError
2059
2194
  }), z.object({
2060
2195
  status: z.literal(500),
2061
2196
  json: z.object({
@@ -2073,6 +2208,17 @@ var Api = {
2073
2208
  json: z.object({
2074
2209
  type: z.literal("patch-error"),
2075
2210
  message: z.string(),
2211
+ schemaSha: z.string(),
2212
+ modules: z.record(ModuleFilePath, z.object({
2213
+ source: z.any(),
2214
+ //.optional(), // TODO: Json zod type
2215
+ patches: z.object({
2216
+ applied: z.array(PatchId),
2217
+ skipped: z.array(PatchId).optional(),
2218
+ errors: z.record(PatchId, GenericError).optional()
2219
+ }).optional(),
2220
+ validationErrors: z.record(SourcePath, z.array(ValidationError)).optional()
2221
+ })),
2076
2222
  errors: z.record(ModuleFilePath, z.array(z.object({
2077
2223
  patchId: PatchId,
2078
2224
  skipped: z["boolean"](),
@@ -2093,7 +2239,7 @@ var Api = {
2093
2239
  }).optional(),
2094
2240
  validationErrors: z.record(SourcePath, z.array(ValidationError)).optional()
2095
2241
  })),
2096
- newPatchId: PatchId.optional()
2242
+ newPatchIds: z.array(PatchId).optional()
2097
2243
  })
2098
2244
  })])
2099
2245
  }
@@ -2111,6 +2257,12 @@ var Api = {
2111
2257
  res: z.union([notFoundResponse, z.object({
2112
2258
  status: z.literal(200),
2113
2259
  json: z.object({}) // TODO:
2260
+ }), z.object({
2261
+ status: z.literal(409),
2262
+ json: z.object({
2263
+ message: z.string(),
2264
+ isNotFastForward: z.literal(true)
2265
+ })
2114
2266
  }), z.object({
2115
2267
  status: z.literal(400),
2116
2268
  json: z.object({
@@ -2174,7 +2326,7 @@ var createValClient = function createValClient(host) {
2174
2326
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
2175
2327
  var anyApi = Api;
2176
2328
  return /*#__PURE__*/function () {
2177
- var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(path, method, req) {
2329
+ var _ref = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee2(path, method, req) {
2178
2330
  var _anyApi$path, _apiEndpoint$req$body;
2179
2331
  var anyReq, fullPath, params, _i, _Object$keys, _anyReq$query, key, _anyReq$query2, _iterator, _step, value, _anyReq$query3, apiEndpoint, reqBodyResult, res;
2180
2332
  return _regeneratorRuntime().wrap(function _callee2$(_context2) {
@@ -2256,9 +2408,9 @@ var createValClient = function createValClient(host) {
2256
2408
  headers: {
2257
2409
  "Content-Type": "application/json"
2258
2410
  },
2259
- body: anyReq.body ? JSON.stringify(anyReq.body) : undefined
2260
- }).then( /*#__PURE__*/function () {
2261
- var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(res) {
2411
+ body: anyReq.body !== undefined ? JSON.stringify(anyReq.body) : undefined
2412
+ }).then(/*#__PURE__*/function () {
2413
+ var _ref2 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(res) {
2262
2414
  var _apiEndpoint$res;
2263
2415
  var contentTypeHeaders, json, valClientResult, responseResult;
2264
2416
  return _regeneratorRuntime().wrap(function _callee$(_context) {
@@ -2273,7 +2425,7 @@ var createValClient = function createValClient(host) {
2273
2425
  status: null,
2274
2426
  json: {
2275
2427
  type: "client_side_validation_error",
2276
- message: "Invalid content type. This could be a result of mismatched Val versions.",
2428
+ message: "Invalid content type header in response. Could not find application/json in ".concat(Array.from(res.headers.entries()), ". This could be a result of mismatched Val versions."),
2277
2429
  details: {
2278
2430
  validationError: "Invalid content type",
2279
2431
  data: {
@@ -2292,15 +2444,28 @@ var createValClient = function createValClient(host) {
2292
2444
  status: res.status,
2293
2445
  json: json
2294
2446
  };
2447
+ if (!(res.status === 500)) {
2448
+ _context.next = 10;
2449
+ break;
2450
+ }
2451
+ console.log("Server responded with an error", json);
2452
+ return _context.abrupt("return", {
2453
+ status: 500,
2454
+ json: {
2455
+ message: json.message,
2456
+ type: "unknown"
2457
+ }
2458
+ });
2459
+ case 10:
2295
2460
  responseResult = (_apiEndpoint$res = apiEndpoint.res) === null || _apiEndpoint$res === void 0 ? void 0 : _apiEndpoint$res.safeParse(valClientResult);
2296
2461
  if (!(responseResult && !responseResult.success)) {
2297
- _context.next = 10;
2462
+ _context.next = 13;
2298
2463
  break;
2299
2464
  }
2300
2465
  return _context.abrupt("return", {
2301
2466
  status: null,
2302
2467
  json: {
2303
- message: "Response could not be validated. This could be a result of mismatched Val versions.",
2468
+ message: "Response could not be validated. This could also be a result of mismatched Val versions.",
2304
2469
  type: "client_side_validation_error",
2305
2470
  details: {
2306
2471
  validationError: fromZodError(responseResult.error).toString(),
@@ -2308,12 +2473,12 @@ var createValClient = function createValClient(host) {
2308
2473
  }
2309
2474
  }
2310
2475
  });
2311
- case 10:
2476
+ case 13:
2312
2477
  return _context.abrupt("return", {
2313
2478
  status: res.status,
2314
2479
  json: json
2315
2480
  });
2316
- case 11:
2481
+ case 14:
2317
2482
  case "end":
2318
2483
  return _context.stop();
2319
2484
  }
@@ -2332,8 +2497,9 @@ var createValClient = function createValClient(host) {
2332
2497
  return _context2.abrupt("return", {
2333
2498
  status: null,
2334
2499
  json: {
2335
- message: "Failed to fetch data. This is likely a network error.",
2500
+ message: "Failed to fetch data",
2336
2501
  type: "network_error",
2502
+ retryable: isRetryable(_context2.t0),
2337
2503
  details: _context2.t0 instanceof Error ? _context2.t0.message : JSON.stringify(_context2.t0)
2338
2504
  }
2339
2505
  });
@@ -2348,6 +2514,20 @@ var createValClient = function createValClient(host) {
2348
2514
  };
2349
2515
  }();
2350
2516
  };
2517
+ function isRetryable(error) {
2518
+ if (error instanceof TypeError) {
2519
+ // TypeError is usually thrown by fetch when the network request fails.
2520
+ return true;
2521
+ }
2522
+ if (error instanceof Error && "code" in error) {
2523
+ var errorCode = error.code;
2524
+
2525
+ // Network-specific errors (Node.js specific)
2526
+ var retryableErrorCodes = ["ECONNRESET", "ETIMEDOUT", "ENOTFOUND", "EAI_AGAIN"];
2527
+ return retryableErrorCodes.includes(errorCode);
2528
+ }
2529
+ return false;
2530
+ }
2351
2531
 
2352
2532
  var urlOf = function urlOf() {
2353
2533
  var route = arguments.length <= 0 ? undefined : arguments[0];