@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.
@@ -7,7 +7,7 @@ var zod = require('zod');
7
7
  var fp = require('@valbuild/core/fp');
8
8
  var zodValidationError = require('zod-validation-error');
9
9
 
10
- function toPrimitive(t, r) {
10
+ function _toPrimitive(t, r) {
11
11
  if ("object" != typeof t || !t) return t;
12
12
  var e = t[Symbol.toPrimitive];
13
13
  if (void 0 !== e) {
@@ -18,24 +18,18 @@ function toPrimitive(t, r) {
18
18
  return ("string" === r ? String : Number)(t);
19
19
  }
20
20
 
21
- function toPropertyKey(t) {
22
- var i = toPrimitive(t, "string");
23
- return "symbol" == typeof i ? i : String(i);
21
+ function _toPropertyKey(t) {
22
+ var i = _toPrimitive(t, "string");
23
+ return "symbol" == typeof i ? i : i + "";
24
24
  }
25
25
 
26
- function _defineProperty(obj, key, value) {
27
- key = toPropertyKey(key);
28
- if (key in obj) {
29
- Object.defineProperty(obj, key, {
30
- value: value,
31
- enumerable: true,
32
- configurable: true,
33
- writable: true
34
- });
35
- } else {
36
- obj[key] = value;
37
- }
38
- return obj;
26
+ function _defineProperty(e, r, t) {
27
+ return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
28
+ value: t,
29
+ enumerable: !0,
30
+ configurable: !0,
31
+ writable: !0
32
+ }) : e[r] = t, e;
39
33
  }
40
34
 
41
35
  function ownKeys(e, r) {
@@ -60,68 +54,64 @@ function _objectSpread2(e) {
60
54
  return e;
61
55
  }
62
56
 
63
- function _arrayLikeToArray(arr, len) {
64
- if (len == null || len > arr.length) len = arr.length;
65
- for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
66
- return arr2;
57
+ function _arrayLikeToArray(r, a) {
58
+ (null == a || a > r.length) && (a = r.length);
59
+ for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e];
60
+ return n;
67
61
  }
68
62
 
69
- function _unsupportedIterableToArray(o, minLen) {
70
- if (!o) return;
71
- if (typeof o === "string") return _arrayLikeToArray(o, minLen);
72
- var n = Object.prototype.toString.call(o).slice(8, -1);
73
- if (n === "Object" && o.constructor) n = o.constructor.name;
74
- if (n === "Map" || n === "Set") return Array.from(o);
75
- if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
63
+ function _unsupportedIterableToArray(r, a) {
64
+ if (r) {
65
+ if ("string" == typeof r) return _arrayLikeToArray(r, a);
66
+ var t = {}.toString.call(r).slice(8, -1);
67
+ 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;
68
+ }
76
69
  }
77
70
 
78
- function _createForOfIteratorHelper(o, allowArrayLike) {
79
- var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"];
80
- if (!it) {
81
- if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") {
82
- if (it) o = it;
83
- var i = 0;
84
- var F = function () {};
71
+ function _createForOfIteratorHelper(r, e) {
72
+ var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"];
73
+ if (!t) {
74
+ if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) {
75
+ t && (r = t);
76
+ var n = 0,
77
+ F = function () {};
85
78
  return {
86
79
  s: F,
87
80
  n: function () {
88
- if (i >= o.length) return {
89
- done: true
90
- };
91
- return {
92
- done: false,
93
- value: o[i++]
81
+ return n >= r.length ? {
82
+ done: !0
83
+ } : {
84
+ done: !1,
85
+ value: r[n++]
94
86
  };
95
87
  },
96
- e: function (e) {
97
- throw e;
88
+ e: function (r) {
89
+ throw r;
98
90
  },
99
91
  f: F
100
92
  };
101
93
  }
102
94
  throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
103
95
  }
104
- var normalCompletion = true,
105
- didErr = false,
106
- err;
96
+ var o,
97
+ a = !0,
98
+ u = !1;
107
99
  return {
108
100
  s: function () {
109
- it = it.call(o);
101
+ t = t.call(r);
110
102
  },
111
103
  n: function () {
112
- var step = it.next();
113
- normalCompletion = step.done;
114
- return step;
104
+ var r = t.next();
105
+ return a = r.done, r;
115
106
  },
116
- e: function (e) {
117
- didErr = true;
118
- err = e;
107
+ e: function (r) {
108
+ u = !0, o = r;
119
109
  },
120
110
  f: function () {
121
111
  try {
122
- if (!normalCompletion && it.return != null) it.return();
112
+ a || null == t.return || t.return();
123
113
  } finally {
124
- if (didErr) throw err;
114
+ if (u) throw o;
125
115
  }
126
116
  }
127
117
  };
@@ -131,7 +121,7 @@ function remirrorToRichTextSource(node) {
131
121
  var files = {};
132
122
  var blocks = [];
133
123
  var i = 0;
134
- var _iterator = _createForOfIteratorHelper(node.content),
124
+ var _iterator = _createForOfIteratorHelper((node === null || node === void 0 ? void 0 : node.content) || []),
135
125
  _step;
136
126
  try {
137
127
  for (_iterator.s(); !(_step = _iterator.n()).done;) {
@@ -312,9 +302,12 @@ function convertImageNode(path, node, files) {
312
302
  if (mimeType === undefined) {
313
303
  throw new Error("Could not detect Mime Type for image: ".concat(node.attrs.src));
314
304
  }
315
- var fileExt = core.Internal.mimeTypeToFileExt(mimeType);
316
- var fileName = node.attrs.fileName || "".concat(sha256, ".").concat(fileExt);
317
- var filePath = "/public/".concat(fileName);
305
+ var fileName = core.Internal.createFilename(node.attrs.src, node.attrs.fileName || "", {
306
+ width: typeof node.attrs.width === "number" ? node.attrs.width : 0,
307
+ height: typeof node.attrs.height === "number" ? node.attrs.height : 0,
308
+ mimeType: mimeType
309
+ }, sha256);
310
+ var filePath = "/public/val/".concat(fileName);
318
311
  var existingFilesEntry = files[filePath];
319
312
  var thisPath = path
320
313
  // file is added as src (see below):
@@ -332,21 +325,32 @@ function convertImageNode(path, node, files) {
332
325
  src: _defineProperty(_defineProperty(_defineProperty({}, core.VAL_EXTENSION, "file"), core.FILE_REF_PROP, filePath), "metadata", {
333
326
  width: typeof node.attrs.width === "number" ? node.attrs.width : 0,
334
327
  height: typeof node.attrs.height === "number" ? node.attrs.height : 0,
335
- sha256: sha256 || "",
336
328
  mimeType: mimeType
337
329
  })
338
330
  };
339
331
  } else if (node.attrs) {
340
- var _sha = getParam("sha256", node.attrs.src);
341
- var patchId = getParam("patch_id", node.attrs.src);
342
- var noParamsSrc = node.attrs.src.split("?")[0];
332
+ var url = node.attrs.src;
333
+ var patchId = getParam("patch_id", url);
334
+ var noParamsUrl = url.split("?")[0];
335
+ if (patchId) {
336
+ if (noParamsUrl.startsWith("/api/val/files/public")) {
337
+ noParamsUrl = noParamsUrl.slice("/api/val/files".length);
338
+ } else {
339
+ console.error("Patched image URL does not start with /api/val/files: " + url);
340
+ }
341
+ } else {
342
+ if (!noParamsUrl.startsWith("/public")) {
343
+ noParamsUrl = "/public".concat(noParamsUrl);
344
+ } else {
345
+ console.error("Unpatched image URL starts with /public: " + url);
346
+ }
347
+ }
343
348
  var tag = {
344
349
  tag: "img",
345
- src: _objectSpread2(_defineProperty(_defineProperty(_defineProperty({}, core.VAL_EXTENSION, "file"), core.FILE_REF_PROP, "/public".concat(node.attrs.src.split("?")[0])), "metadata", {
350
+ src: _objectSpread2(_defineProperty(_defineProperty(_defineProperty({}, core.VAL_EXTENSION, "file"), core.FILE_REF_PROP, noParamsUrl), "metadata", {
346
351
  width: typeof node.attrs.width === "number" ? node.attrs.width : 0,
347
352
  height: typeof node.attrs.height === "number" ? node.attrs.height : 0,
348
- sha256: _sha || "",
349
- mimeType: noParamsSrc && core.Internal.filenameToMimeType(noParamsSrc) || ""
353
+ mimeType: noParamsUrl && core.Internal.filenameToMimeType(noParamsUrl) || ""
350
354
  }), patchId ? {
351
355
  patch_id: patchId
352
356
  } : {})
@@ -451,20 +455,20 @@ var RemirrorJSON = zod.z.object({
451
455
  content: zod.z.array(zod.z.union([RemirrorParagraph, RemirrorHeading, RemirrorBulletList, RemirrorOrderedList]))
452
456
  });
453
457
 
454
- function _arrayWithoutHoles(arr) {
455
- if (Array.isArray(arr)) return _arrayLikeToArray(arr);
458
+ function _arrayWithoutHoles(r) {
459
+ if (Array.isArray(r)) return _arrayLikeToArray(r);
456
460
  }
457
461
 
458
- function _iterableToArray(iter) {
459
- if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
462
+ function _iterableToArray(r) {
463
+ if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r);
460
464
  }
461
465
 
462
466
  function _nonIterableSpread() {
463
467
  throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
464
468
  }
465
469
 
466
- function _toConsumableArray(arr) {
467
- return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread();
470
+ function _toConsumableArray(r) {
471
+ return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread();
468
472
  }
469
473
 
470
474
  function richTextToRemirror(richtext) {
@@ -716,8 +720,8 @@ var VAL_SESSION_COOKIE = core.Internal.VAL_SESSION_COOKIE;
716
720
  var VAL_STATE_COOKIE = core.Internal.VAL_STATE_COOKIE;
717
721
  var VAL_ENABLE_COOKIE_NAME = core.Internal.VAL_ENABLE_COOKIE_NAME;
718
722
 
719
- function _arrayWithHoles(arr) {
720
- if (Array.isArray(arr)) return arr;
723
+ function _arrayWithHoles(r) {
724
+ if (Array.isArray(r)) return r;
721
725
  }
722
726
 
723
727
  function _iterableToArrayLimit(r, l) {
@@ -752,8 +756,8 @@ function _nonIterableRest() {
752
756
  throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
753
757
  }
754
758
 
755
- function _slicedToArray(arr, i) {
756
- return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest();
759
+ function _slicedToArray(r, e) {
760
+ return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest();
757
761
  }
758
762
 
759
763
  function _regeneratorRuntime() {
@@ -864,7 +868,7 @@ function _regeneratorRuntime() {
864
868
  function makeInvokeMethod(e, r, n) {
865
869
  var o = h;
866
870
  return function (i, a) {
867
- if (o === f) throw new Error("Generator is already running");
871
+ if (o === f) throw Error("Generator is already running");
868
872
  if (o === s) {
869
873
  if ("throw" === i) throw a;
870
874
  return {
@@ -1006,7 +1010,7 @@ function _regeneratorRuntime() {
1006
1010
  } else if (c) {
1007
1011
  if (this.prev < i.catchLoc) return handle(i.catchLoc, !0);
1008
1012
  } else {
1009
- if (!u) throw new Error("try statement without catch or finally");
1013
+ if (!u) throw Error("try statement without catch or finally");
1010
1014
  if (this.prev < i.finallyLoc) return handle(i.finallyLoc);
1011
1015
  }
1012
1016
  }
@@ -1046,7 +1050,7 @@ function _regeneratorRuntime() {
1046
1050
  return o;
1047
1051
  }
1048
1052
  }
1049
- throw new Error("illegal catch attempt");
1053
+ throw Error("illegal catch attempt");
1050
1054
  },
1051
1055
  delegateYield: function (e, r, n) {
1052
1056
  return this.delegate = {
@@ -1058,59 +1062,46 @@ function _regeneratorRuntime() {
1058
1062
  }, e;
1059
1063
  }
1060
1064
 
1061
- function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
1065
+ function asyncGeneratorStep(n, t, e, r, o, a, c) {
1062
1066
  try {
1063
- var info = gen[key](arg);
1064
- var value = info.value;
1065
- } catch (error) {
1066
- reject(error);
1067
- return;
1068
- }
1069
- if (info.done) {
1070
- resolve(value);
1071
- } else {
1072
- Promise.resolve(value).then(_next, _throw);
1067
+ var i = n[a](c),
1068
+ u = i.value;
1069
+ } catch (n) {
1070
+ return void e(n);
1073
1071
  }
1072
+ i.done ? t(u) : Promise.resolve(u).then(r, o);
1074
1073
  }
1075
- function _asyncToGenerator(fn) {
1074
+ function _asyncToGenerator(n) {
1076
1075
  return function () {
1077
- var self = this,
1078
- args = arguments;
1079
- return new Promise(function (resolve, reject) {
1080
- var gen = fn.apply(self, args);
1081
- function _next(value) {
1082
- asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
1076
+ var t = this,
1077
+ e = arguments;
1078
+ return new Promise(function (r, o) {
1079
+ var a = n.apply(t, e);
1080
+ function _next(n) {
1081
+ asyncGeneratorStep(a, r, o, _next, _throw, "next", n);
1083
1082
  }
1084
- function _throw(err) {
1085
- asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
1083
+ function _throw(n) {
1084
+ asyncGeneratorStep(a, r, o, _next, _throw, "throw", n);
1086
1085
  }
1087
- _next(undefined);
1086
+ _next(void 0);
1088
1087
  });
1089
1088
  };
1090
1089
  }
1091
1090
 
1092
- function _classCallCheck(instance, Constructor) {
1093
- if (!(instance instanceof Constructor)) {
1094
- throw new TypeError("Cannot call a class as a function");
1095
- }
1091
+ function _classCallCheck(a, n) {
1092
+ if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function");
1096
1093
  }
1097
1094
 
1098
- function _defineProperties(target, props) {
1099
- for (var i = 0; i < props.length; i++) {
1100
- var descriptor = props[i];
1101
- descriptor.enumerable = descriptor.enumerable || false;
1102
- descriptor.configurable = true;
1103
- if ("value" in descriptor) descriptor.writable = true;
1104
- Object.defineProperty(target, toPropertyKey(descriptor.key), descriptor);
1095
+ function _defineProperties(e, r) {
1096
+ for (var t = 0; t < r.length; t++) {
1097
+ var o = r[t];
1098
+ o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o);
1105
1099
  }
1106
1100
  }
1107
- function _createClass(Constructor, protoProps, staticProps) {
1108
- if (protoProps) _defineProperties(Constructor.prototype, protoProps);
1109
- if (staticProps) _defineProperties(Constructor, staticProps);
1110
- Object.defineProperty(Constructor, "prototype", {
1111
- writable: false
1112
- });
1113
- return Constructor;
1101
+ function _createClass(e, r, t) {
1102
+ return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", {
1103
+ writable: !1
1104
+ }), e;
1114
1105
  }
1115
1106
 
1116
1107
  var ValCache = /*#__PURE__*/function () {
@@ -1120,10 +1111,10 @@ var ValCache = /*#__PURE__*/function () {
1120
1111
  this.drafts = {};
1121
1112
  this.schema = {};
1122
1113
  }
1123
- _createClass(ValCache, [{
1114
+ return _createClass(ValCache, [{
1124
1115
  key: "reloadPaths",
1125
1116
  value: function () {
1126
- var _reloadPaths = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(paths) {
1117
+ var _reloadPaths = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(paths) {
1127
1118
  var patchesResponse, patches, filteredPatches, treeRes, data, _i, _Object$keys, _data$modules, pathS, path;
1128
1119
  return _regeneratorRuntime().wrap(function _callee$(_context) {
1129
1120
  while (1) switch (_context.prev = _context.next) {
@@ -1149,14 +1140,13 @@ var ValCache = /*#__PURE__*/function () {
1149
1140
  patches = patchesResponse.json;
1150
1141
  filteredPatches = Object.keys(patches);
1151
1142
  _context.next = 10;
1152
- return this.client("/tree/~", "PUT", {
1143
+ return this.client("/sources", "PUT", {
1153
1144
  query: {
1154
1145
  validate_sources: true,
1155
1146
  validate_all: false,
1156
1147
  validate_binary_files: false
1157
1148
  },
1158
1149
  path: undefined,
1159
- // TODO: reload only the paths the requested paths
1160
1150
  body: {
1161
1151
  patchIds: filteredPatches
1162
1152
  }
@@ -1191,7 +1181,7 @@ var ValCache = /*#__PURE__*/function () {
1191
1181
  }, {
1192
1182
  key: "reset",
1193
1183
  value: function () {
1194
- var _reset = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2() {
1184
+ var _reset = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee2() {
1195
1185
  var patchesRes, allPatches, treeRes, initRes, _i2, _Object$keys2, _treeRes$json, pathS, path;
1196
1186
  return _regeneratorRuntime().wrap(function _callee2$(_context2) {
1197
1187
  while (1) switch (_context2.prev = _context2.next) {
@@ -1219,7 +1209,7 @@ var ValCache = /*#__PURE__*/function () {
1219
1209
  case 6:
1220
1210
  allPatches = Object.keys(patchesRes.json.patches);
1221
1211
  _context2.next = 9;
1222
- return this.client("/tree/~", "PUT", {
1212
+ return this.client("/sources", "PUT", {
1223
1213
  path: undefined,
1224
1214
  query: {
1225
1215
  validate_sources: false,
@@ -1298,7 +1288,7 @@ var ValCache = /*#__PURE__*/function () {
1298
1288
  }, {
1299
1289
  key: "getModule",
1300
1290
  value: function () {
1301
- var _getModule = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3(path) {
1291
+ var _getModule = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee3(path) {
1302
1292
  var refetch,
1303
1293
  treeRes,
1304
1294
  _treeRes$json2,
@@ -1320,7 +1310,7 @@ var ValCache = /*#__PURE__*/function () {
1320
1310
  }));
1321
1311
  case 3:
1322
1312
  _context3.next = 5;
1323
- return this.client("/tree/~", "PUT", {
1313
+ return this.client("/sources", "PUT", {
1324
1314
  path: path,
1325
1315
  body: {},
1326
1316
  query: {
@@ -1410,7 +1400,7 @@ var ValCache = /*#__PURE__*/function () {
1410
1400
  }, {
1411
1401
  key: "deletePatches",
1412
1402
  value: function () {
1413
- var _deletePatches = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee4(patchIds) {
1403
+ var _deletePatches = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee4(patchIds) {
1414
1404
  var patchesRes;
1415
1405
  return _regeneratorRuntime().wrap(function _callee4$(_context4) {
1416
1406
  while (1) switch (_context4.prev = _context4.next) {
@@ -1447,13 +1437,13 @@ var ValCache = /*#__PURE__*/function () {
1447
1437
  }, {
1448
1438
  key: "applyPatch",
1449
1439
  value: function () {
1450
- var _applyPatch = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee5(path, patchIds, patch) {
1451
- var treeRes, _treeRes$json4, newPatchId, fetchedSource, _treeRes$json5;
1440
+ var _applyPatch = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee5(path, patchIds, patch) {
1441
+ var treeRes, _treeRes$json4, newPatchIds, newPatchId, fetchedSource, _treeRes$json5;
1452
1442
  return _regeneratorRuntime().wrap(function _callee5$(_context5) {
1453
1443
  while (1) switch (_context5.prev = _context5.next) {
1454
1444
  case 0:
1455
1445
  _context5.next = 2;
1456
- return this.client("/tree/~", "PUT", {
1446
+ return this.client("/sources", "PUT", {
1457
1447
  path: path,
1458
1448
  query: {
1459
1449
  validate_sources: false,
@@ -1462,20 +1452,20 @@ var ValCache = /*#__PURE__*/function () {
1462
1452
  },
1463
1453
  body: {
1464
1454
  patchIds: patchIds,
1465
- addPatch: {
1455
+ addPatches: [{
1466
1456
  path: path,
1467
1457
  patch: patch
1468
- }
1458
+ }]
1469
1459
  }
1470
1460
  });
1471
1461
  case 2:
1472
1462
  treeRes = _context5.sent;
1473
1463
  if (!(treeRes.status === 200)) {
1474
- _context5.next = 19;
1464
+ _context5.next = 20;
1475
1465
  break;
1476
1466
  }
1477
- newPatchId = treeRes.json.newPatchId;
1478
- if (newPatchId) {
1467
+ newPatchIds = treeRes.json.newPatchIds;
1468
+ if (newPatchIds) {
1479
1469
  _context5.next = 8;
1480
1470
  break;
1481
1471
  }
@@ -1485,9 +1475,10 @@ var ValCache = /*#__PURE__*/function () {
1485
1475
  message: "Val: could not create patch"
1486
1476
  }));
1487
1477
  case 8:
1478
+ newPatchId = newPatchIds[0];
1488
1479
  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;
1489
1480
  if (!(fetchedSource !== undefined)) {
1490
- _context5.next = 15;
1481
+ _context5.next = 16;
1491
1482
  break;
1492
1483
  }
1493
1484
  this.drafts[path] = fetchedSource;
@@ -1498,18 +1489,18 @@ var ValCache = /*#__PURE__*/function () {
1498
1489
  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) || []
1499
1490
  })
1500
1491
  }));
1501
- case 15:
1492
+ case 16:
1502
1493
  console.error("Val: could not patch");
1503
1494
  return _context5.abrupt("return", fp.result.err({
1504
1495
  errorType: "other",
1505
1496
  message: "Val: could not fetch data. Verify that the module exists."
1506
1497
  }));
1507
- case 17:
1508
- _context5.next = 25;
1498
+ case 18:
1499
+ _context5.next = 26;
1509
1500
  break;
1510
- case 19:
1501
+ case 20:
1511
1502
  if (!(treeRes.status === 400 && "type" in treeRes.json)) {
1512
- _context5.next = 23;
1503
+ _context5.next = 24;
1513
1504
  break;
1514
1505
  }
1515
1506
  return _context5.abrupt("return", fp.result.err({
@@ -1528,13 +1519,13 @@ var ValCache = /*#__PURE__*/function () {
1528
1519
  });
1529
1520
  })
1530
1521
  }));
1531
- case 23:
1522
+ case 24:
1532
1523
  console.error("Val: failed to get module", treeRes.json);
1533
1524
  return _context5.abrupt("return", fp.result.err({
1534
1525
  errorType: "other",
1535
1526
  message: "Val: could not fetch data. Verify that is correctly configured."
1536
1527
  }));
1537
- case 25:
1528
+ case 26:
1538
1529
  case "end":
1539
1530
  return _context5.stop();
1540
1531
  }
@@ -1560,7 +1551,7 @@ var ValCache = /*#__PURE__*/function () {
1560
1551
  }, {
1561
1552
  key: "initialize",
1562
1553
  value: function () {
1563
- var _initialize = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee6() {
1554
+ var _initialize = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee6() {
1564
1555
  var schemaRes, paths, _i3, _arr, moduleId, schema, msg;
1565
1556
  return _regeneratorRuntime().wrap(function _callee6$(_context6) {
1566
1557
  while (1) switch (_context6.prev = _context6.next) {
@@ -1608,7 +1599,6 @@ var ValCache = /*#__PURE__*/function () {
1608
1599
  return initialize;
1609
1600
  }()
1610
1601
  }]);
1611
- return ValCache;
1612
1602
  }();
1613
1603
 
1614
1604
  var JSONValueT = zod.z.lazy(function () {
@@ -1697,12 +1687,17 @@ var SerializedArraySchema = zod.z.lazy(function () {
1697
1687
  });
1698
1688
  });
1699
1689
  var SerializedUnionSchema = zod.z.lazy(function () {
1700
- return zod.z.object({
1690
+ return zod.z.union([zod.z.object({
1701
1691
  type: zod.z.literal("union"),
1702
- key: zod.z.union([zod.z.string(), SerializedSchema]),
1703
- items: zod.z.array(SerializedSchema),
1692
+ key: SerializedLiteralSchema,
1693
+ items: zod.z.array(SerializedLiteralSchema),
1704
1694
  opt: zod.z["boolean"]()
1705
- });
1695
+ }), zod.z.object({
1696
+ type: zod.z.literal("union"),
1697
+ key: zod.z.string(),
1698
+ items: zod.z.array(SerializedObjectSchema),
1699
+ opt: zod.z["boolean"]()
1700
+ })]);
1706
1701
  });
1707
1702
  var RichTextOptions = zod.z.object({
1708
1703
  style: zod.z.object({
@@ -1742,11 +1737,13 @@ var SerializedKeyOfSchema = zod.z.lazy(function () {
1742
1737
  type: zod.z.literal("keyOf"),
1743
1738
  path: SourcePath,
1744
1739
  schema: SerializedSchema,
1745
- values: zod.z.union([zod.z.literal("string"), zod.z.literal("number"), zod.z.array(zod.z.string())]),
1740
+ values: zod.z.union([zod.z.literal("string"), zod.z.array(zod.z.string())]),
1746
1741
  opt: zod.z["boolean"]()
1747
1742
  });
1748
1743
  });
1749
- var FileOptions = zod.z.record(zod.z.never());
1744
+ var FileOptions = zod.z.object({
1745
+ accept: zod.z.string().optional()
1746
+ });
1750
1747
  var SerializedFileSchema = zod.z.object({
1751
1748
  type: zod.z.literal("file"),
1752
1749
  options: FileOptions.optional(),
@@ -1759,7 +1756,8 @@ var SerializedDateSchema = zod.z.object({
1759
1756
  var ImageOptions = zod.z.object({
1760
1757
  ext: zod.z.union([zod.z.tuple([zod.z.literal("jpg")]), zod.z.tuple([zod.z.literal("webp")])]).optional(),
1761
1758
  directory: zod.z.string().optional(),
1762
- prefix: zod.z.string().optional()
1759
+ prefix: zod.z.string().optional(),
1760
+ accept: zod.z.string().optional()
1763
1761
  });
1764
1762
  var SerializedImageSchema = zod.z.object({
1765
1763
  type: zod.z.literal("image"),
@@ -1776,6 +1774,15 @@ var ModuleFilePath = zod.z.string().refine(function (_path) {
1776
1774
  return true;
1777
1775
  } // TODO:
1778
1776
  );
1777
+ var ValConfig = zod.z.object({
1778
+ project: zod.z.string().optional(),
1779
+ root: zod.z.string().optional(),
1780
+ files: zod.z.object({
1781
+ directory: zod.z.string() // TODO: validate that it is prefixed by /public/
1782
+ }).optional(),
1783
+ gitCommit: zod.z.string().optional(),
1784
+ gitBranch: zod.z.string().optional()
1785
+ });
1779
1786
  var ValidationFixZ = zod.z.union([zod.z.literal("image:add-metadata"), zod.z.literal("image:replace-metadata"), zod.z.literal("file:add-metadata"), zod.z.literal("file:check-metadata"), zod.z.literal("fix:deprecated-richtext")]);
1780
1787
  var ValidationError = zod.z.object({
1781
1788
  message: zod.z.string(),
@@ -1805,14 +1812,89 @@ var enableCookieValue = zod.z.object({
1805
1812
  })
1806
1813
  });
1807
1814
  var Api = {
1815
+ "/draft/enable": {
1816
+ GET: {
1817
+ req: {
1818
+ query: {
1819
+ redirect_to: zod.z.string().optional()
1820
+ },
1821
+ cookies: _defineProperty({}, VAL_SESSION_COOKIE, zod.z.string().optional())
1822
+ },
1823
+ res: zod.z.union([zod.z.object({
1824
+ status: zod.z.literal(401),
1825
+ json: GenericError
1826
+ }), zod.z.object({
1827
+ status: zod.z.literal(302),
1828
+ redirectTo: zod.z.string()
1829
+ }), zod.z.object({
1830
+ status: zod.z.literal(400),
1831
+ json: zod.z.object({
1832
+ message: zod.z.string()
1833
+ })
1834
+ })])
1835
+ }
1836
+ },
1837
+ "/draft/disable": {
1838
+ GET: {
1839
+ req: {
1840
+ query: {
1841
+ redirect_to: zod.z.string().optional()
1842
+ },
1843
+ cookies: _defineProperty({}, VAL_SESSION_COOKIE, zod.z.string().optional())
1844
+ },
1845
+ res: zod.z.union([zod.z.object({
1846
+ status: zod.z.literal(401),
1847
+ json: GenericError
1848
+ }), zod.z.object({
1849
+ status: zod.z.literal(302),
1850
+ redirectTo: zod.z.string()
1851
+ }), zod.z.object({
1852
+ status: zod.z.literal(400),
1853
+ json: zod.z.object({
1854
+ message: zod.z.string()
1855
+ })
1856
+ })])
1857
+ }
1858
+ },
1859
+ "/draft/stat": {
1860
+ GET: {
1861
+ req: {
1862
+ cookies: _defineProperty({}, VAL_SESSION_COOKIE, zod.z.string().optional())
1863
+ },
1864
+ res: zod.z.union([zod.z.object({
1865
+ status: zod.z.literal(401),
1866
+ json: GenericError
1867
+ }), zod.z.object({
1868
+ status: zod.z.literal(200),
1869
+ json: zod.z.object({
1870
+ draftMode: zod.z["boolean"]()
1871
+ })
1872
+ })])
1873
+ }
1874
+ },
1808
1875
  "/enable": {
1809
1876
  GET: {
1810
1877
  req: {
1811
1878
  query: {
1812
1879
  redirect_to: zod.z.string().optional()
1813
- }
1880
+ },
1881
+ cookies: _defineProperty({}, VAL_SESSION_COOKIE, zod.z.string().optional())
1814
1882
  },
1815
1883
  res: zod.z.union([zod.z.object({
1884
+ status: zod.z.literal(401),
1885
+ json: GenericError
1886
+ }), zod.z.object({
1887
+ status: zod.z.literal(302),
1888
+ redirectTo: zod.z.string(),
1889
+ cookies: zod.z.object(_defineProperty(_defineProperty({}, VAL_ENABLE_COOKIE_NAME, enableCookieValue), VAL_STATE_COOKIE, zod.z.object({
1890
+ value: zod.z.string(),
1891
+ options: zod.z.object({
1892
+ httpOnly: zod.z.literal(true),
1893
+ sameSite: zod.z.literal("lax"),
1894
+ expires: zod.z["instanceof"](Date)
1895
+ })
1896
+ })))
1897
+ }), zod.z.object({
1816
1898
  status: zod.z.literal(302),
1817
1899
  redirectTo: zod.z.string(),
1818
1900
  cookies: zod.z.object(_defineProperty({}, VAL_ENABLE_COOKIE_NAME, enableCookieValue))
@@ -1829,9 +1911,13 @@ var Api = {
1829
1911
  req: {
1830
1912
  query: {
1831
1913
  redirect_to: zod.z.string().optional()
1832
- }
1914
+ },
1915
+ cookies: _defineProperty({}, VAL_SESSION_COOKIE, zod.z.string().optional())
1833
1916
  },
1834
1917
  res: zod.z.union([zod.z.object({
1918
+ status: zod.z.literal(401),
1919
+ json: GenericError
1920
+ }), zod.z.object({
1835
1921
  status: zod.z.literal(302),
1836
1922
  redirectTo: zod.z.string(),
1837
1923
  cookies: zod.z.object(_defineProperty({}, VAL_ENABLE_COOKIE_NAME, zod.z.object({
@@ -1954,6 +2040,45 @@ var Api = {
1954
2040
  })
1955
2041
  }
1956
2042
  },
2043
+ "/stat": {
2044
+ POST: {
2045
+ req: {
2046
+ body: zod.z.object({
2047
+ schemaSha: zod.z.string(),
2048
+ baseSha: zod.z.string(),
2049
+ patches: zod.z.array(zod.z.string()).optional()
2050
+ }).nullable(),
2051
+ cookies: {
2052
+ val_session: zod.z.string().optional()
2053
+ }
2054
+ },
2055
+ res: zod.z.union([zod.z.object({
2056
+ status: zod.z.literal(401),
2057
+ json: GenericError
2058
+ }), zod.z.object({
2059
+ status: zod.z.literal(500),
2060
+ json: GenericError
2061
+ }), zod.z.object({
2062
+ status: zod.z.literal(200),
2063
+ json: zod.z.union([zod.z.object({
2064
+ type: zod.z.union([zod.z.literal("request-again"), zod.z.literal("no-change"), zod.z.literal("did-change")]),
2065
+ baseSha: zod.z.string(),
2066
+ schemaSha: zod.z.string(),
2067
+ patches: zod.z.array(PatchId),
2068
+ config: ValConfig
2069
+ }), zod.z.object({
2070
+ type: zod.z.literal("use-websocket"),
2071
+ url: zod.z.string(),
2072
+ nonce: zod.z.string(),
2073
+ baseSha: zod.z.string(),
2074
+ schemaSha: zod.z.string(),
2075
+ commitSha: zod.z.string(),
2076
+ patches: zod.z.array(PatchId),
2077
+ config: ValConfig
2078
+ })])
2079
+ })])
2080
+ }
2081
+ },
1957
2082
  "/patches/~": {
1958
2083
  DELETE: {
1959
2084
  req: {
@@ -2028,6 +2153,12 @@ var Api = {
2028
2153
  message: zod.z.string(),
2029
2154
  details: zod.z.array(ModulesError)
2030
2155
  })
2156
+ }), zod.z.object({
2157
+ status: zod.z.literal(500),
2158
+ json: zod.z.object({
2159
+ message: zod.z.string(),
2160
+ details: zod.z.array(GenericError)
2161
+ })
2031
2162
  }), zod.z.object({
2032
2163
  status: zod.z.literal(200),
2033
2164
  json: zod.z.object({
@@ -2037,16 +2168,16 @@ var Api = {
2037
2168
  })])
2038
2169
  }
2039
2170
  },
2040
- "/tree/~": {
2171
+ "/sources": {
2041
2172
  PUT: {
2042
2173
  req: {
2043
2174
  path: zod.z.string().optional(),
2044
2175
  body: zod.z.object({
2045
2176
  patchIds: zod.z.array(PatchId).optional(),
2046
- addPatch: zod.z.object({
2177
+ addPatches: zod.z.array(zod.z.object({
2047
2178
  path: ModuleFilePath,
2048
2179
  patch: Patch
2049
- }).optional()
2180
+ })).optional()
2050
2181
  }).optional(),
2051
2182
  query: {
2052
2183
  validate_all: zod.z["boolean"]().optional(),
@@ -2060,6 +2191,10 @@ var Api = {
2060
2191
  res: zod.z.union([notFoundResponse, zod.z.object({
2061
2192
  status: zod.z.literal(401),
2062
2193
  json: GenericError
2194
+ }), zod.z.object({
2195
+ status: zod.z.literal(409),
2196
+ // conflict: i.e. not a head of patches
2197
+ json: GenericError
2063
2198
  }), zod.z.object({
2064
2199
  status: zod.z.literal(500),
2065
2200
  json: zod.z.object({
@@ -2077,6 +2212,17 @@ var Api = {
2077
2212
  json: zod.z.object({
2078
2213
  type: zod.z.literal("patch-error"),
2079
2214
  message: zod.z.string(),
2215
+ schemaSha: zod.z.string(),
2216
+ modules: zod.z.record(ModuleFilePath, zod.z.object({
2217
+ source: zod.z.any(),
2218
+ //.optional(), // TODO: Json zod type
2219
+ patches: zod.z.object({
2220
+ applied: zod.z.array(PatchId),
2221
+ skipped: zod.z.array(PatchId).optional(),
2222
+ errors: zod.z.record(PatchId, GenericError).optional()
2223
+ }).optional(),
2224
+ validationErrors: zod.z.record(SourcePath, zod.z.array(ValidationError)).optional()
2225
+ })),
2080
2226
  errors: zod.z.record(ModuleFilePath, zod.z.array(zod.z.object({
2081
2227
  patchId: PatchId,
2082
2228
  skipped: zod.z["boolean"](),
@@ -2097,7 +2243,7 @@ var Api = {
2097
2243
  }).optional(),
2098
2244
  validationErrors: zod.z.record(SourcePath, zod.z.array(ValidationError)).optional()
2099
2245
  })),
2100
- newPatchId: PatchId.optional()
2246
+ newPatchIds: zod.z.array(PatchId).optional()
2101
2247
  })
2102
2248
  })])
2103
2249
  }
@@ -2115,6 +2261,12 @@ var Api = {
2115
2261
  res: zod.z.union([notFoundResponse, zod.z.object({
2116
2262
  status: zod.z.literal(200),
2117
2263
  json: zod.z.object({}) // TODO:
2264
+ }), zod.z.object({
2265
+ status: zod.z.literal(409),
2266
+ json: zod.z.object({
2267
+ message: zod.z.string(),
2268
+ isNotFastForward: zod.z.literal(true)
2269
+ })
2118
2270
  }), zod.z.object({
2119
2271
  status: zod.z.literal(400),
2120
2272
  json: zod.z.object({
@@ -2178,7 +2330,7 @@ var createValClient = function createValClient(host) {
2178
2330
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
2179
2331
  var anyApi = Api;
2180
2332
  return /*#__PURE__*/function () {
2181
- var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(path, method, req) {
2333
+ var _ref = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee2(path, method, req) {
2182
2334
  var _anyApi$path, _apiEndpoint$req$body;
2183
2335
  var anyReq, fullPath, params, _i, _Object$keys, _anyReq$query, key, _anyReq$query2, _iterator, _step, value, _anyReq$query3, apiEndpoint, reqBodyResult, res;
2184
2336
  return _regeneratorRuntime().wrap(function _callee2$(_context2) {
@@ -2260,9 +2412,9 @@ var createValClient = function createValClient(host) {
2260
2412
  headers: {
2261
2413
  "Content-Type": "application/json"
2262
2414
  },
2263
- body: anyReq.body ? JSON.stringify(anyReq.body) : undefined
2264
- }).then( /*#__PURE__*/function () {
2265
- var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(res) {
2415
+ body: anyReq.body !== undefined ? JSON.stringify(anyReq.body) : undefined
2416
+ }).then(/*#__PURE__*/function () {
2417
+ var _ref2 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(res) {
2266
2418
  var _apiEndpoint$res;
2267
2419
  var contentTypeHeaders, json, valClientResult, responseResult;
2268
2420
  return _regeneratorRuntime().wrap(function _callee$(_context) {
@@ -2277,7 +2429,7 @@ var createValClient = function createValClient(host) {
2277
2429
  status: null,
2278
2430
  json: {
2279
2431
  type: "client_side_validation_error",
2280
- message: "Invalid content type. This could be a result of mismatched Val versions.",
2432
+ 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."),
2281
2433
  details: {
2282
2434
  validationError: "Invalid content type",
2283
2435
  data: {
@@ -2296,15 +2448,28 @@ var createValClient = function createValClient(host) {
2296
2448
  status: res.status,
2297
2449
  json: json
2298
2450
  };
2451
+ if (!(res.status === 500)) {
2452
+ _context.next = 10;
2453
+ break;
2454
+ }
2455
+ console.log("Server responded with an error", json);
2456
+ return _context.abrupt("return", {
2457
+ status: 500,
2458
+ json: {
2459
+ message: json.message,
2460
+ type: "unknown"
2461
+ }
2462
+ });
2463
+ case 10:
2299
2464
  responseResult = (_apiEndpoint$res = apiEndpoint.res) === null || _apiEndpoint$res === void 0 ? void 0 : _apiEndpoint$res.safeParse(valClientResult);
2300
2465
  if (!(responseResult && !responseResult.success)) {
2301
- _context.next = 10;
2466
+ _context.next = 13;
2302
2467
  break;
2303
2468
  }
2304
2469
  return _context.abrupt("return", {
2305
2470
  status: null,
2306
2471
  json: {
2307
- message: "Response could not be validated. This could be a result of mismatched Val versions.",
2472
+ message: "Response could not be validated. This could also be a result of mismatched Val versions.",
2308
2473
  type: "client_side_validation_error",
2309
2474
  details: {
2310
2475
  validationError: zodValidationError.fromZodError(responseResult.error).toString(),
@@ -2312,12 +2477,12 @@ var createValClient = function createValClient(host) {
2312
2477
  }
2313
2478
  }
2314
2479
  });
2315
- case 10:
2480
+ case 13:
2316
2481
  return _context.abrupt("return", {
2317
2482
  status: res.status,
2318
2483
  json: json
2319
2484
  });
2320
- case 11:
2485
+ case 14:
2321
2486
  case "end":
2322
2487
  return _context.stop();
2323
2488
  }
@@ -2336,8 +2501,9 @@ var createValClient = function createValClient(host) {
2336
2501
  return _context2.abrupt("return", {
2337
2502
  status: null,
2338
2503
  json: {
2339
- message: "Failed to fetch data. This is likely a network error.",
2504
+ message: "Failed to fetch data",
2340
2505
  type: "network_error",
2506
+ retryable: isRetryable(_context2.t0),
2341
2507
  details: _context2.t0 instanceof Error ? _context2.t0.message : JSON.stringify(_context2.t0)
2342
2508
  }
2343
2509
  });
@@ -2352,6 +2518,20 @@ var createValClient = function createValClient(host) {
2352
2518
  };
2353
2519
  }();
2354
2520
  };
2521
+ function isRetryable(error) {
2522
+ if (error instanceof TypeError) {
2523
+ // TypeError is usually thrown by fetch when the network request fails.
2524
+ return true;
2525
+ }
2526
+ if (error instanceof Error && "code" in error) {
2527
+ var errorCode = error.code;
2528
+
2529
+ // Network-specific errors (Node.js specific)
2530
+ var retryableErrorCodes = ["ECONNRESET", "ETIMEDOUT", "ENOTFOUND", "EAI_AGAIN"];
2531
+ return retryableErrorCodes.includes(errorCode);
2532
+ }
2533
+ return false;
2534
+ }
2355
2535
 
2356
2536
  var urlOf = function urlOf() {
2357
2537
  var route = arguments.length <= 0 ? undefined : arguments[0];