@valbuild/shared 0.67.0 → 0.68.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.
@@ -1,6 +1,5 @@
1
1
  import { Internal, VAL_EXTENSION, FILE_REF_PROP, FILE_REF_SUBTYPE_TAG } from '@valbuild/core';
2
2
  import { z } from 'zod';
3
- import { result } from '@valbuild/core/fp';
4
3
  import { fromZodError } from 'zod-validation-error';
5
4
 
6
5
  function _toPrimitive(t, r) {
@@ -717,46 +716,6 @@ var VAL_SESSION_COOKIE = Internal.VAL_SESSION_COOKIE;
717
716
  var VAL_STATE_COOKIE = Internal.VAL_STATE_COOKIE;
718
717
  var VAL_ENABLE_COOKIE_NAME = Internal.VAL_ENABLE_COOKIE_NAME;
719
718
 
720
- function _arrayWithHoles(r) {
721
- if (Array.isArray(r)) return r;
722
- }
723
-
724
- function _iterableToArrayLimit(r, l) {
725
- var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"];
726
- if (null != t) {
727
- var e,
728
- n,
729
- i,
730
- u,
731
- a = [],
732
- f = !0,
733
- o = !1;
734
- try {
735
- if (i = (t = t.call(r)).next, 0 === l) {
736
- if (Object(t) !== t) return;
737
- f = !1;
738
- } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0);
739
- } catch (r) {
740
- o = !0, n = r;
741
- } finally {
742
- try {
743
- if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return;
744
- } finally {
745
- if (o) throw n;
746
- }
747
- }
748
- return a;
749
- }
750
- }
751
-
752
- function _nonIterableRest() {
753
- throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
754
- }
755
-
756
- function _slicedToArray(r, e) {
757
- return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest();
758
- }
759
-
760
719
  function _regeneratorRuntime() {
761
720
  _regeneratorRuntime = function () {
762
721
  return e;
@@ -1085,522 +1044,52 @@ function _asyncToGenerator(n) {
1085
1044
  };
1086
1045
  }
1087
1046
 
1088
- function _classCallCheck(a, n) {
1089
- if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function");
1090
- }
1091
-
1092
- function _defineProperties(e, r) {
1093
- for (var t = 0; t < r.length; t++) {
1094
- var o = r[t];
1095
- o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o);
1096
- }
1097
- }
1098
- function _createClass(e, r, t) {
1099
- return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", {
1100
- writable: !1
1101
- }), e;
1102
- }
1103
-
1104
- var ValCache = /*#__PURE__*/function () {
1105
- function ValCache(client) {
1106
- _classCallCheck(this, ValCache);
1107
- this.client = client;
1108
- this.drafts = {};
1109
- this.schema = {};
1110
- }
1111
- return _createClass(ValCache, [{
1112
- key: "reloadPaths",
1113
- value: function () {
1114
- var _reloadPaths = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(paths) {
1115
- var patchesResponse, patches, filteredPatches, treeRes, data, _i, _Object$keys, _data$modules, pathS, path;
1116
- return _regeneratorRuntime().wrap(function _callee$(_context) {
1117
- while (1) switch (_context.prev = _context.next) {
1118
- case 0:
1119
- _context.next = 2;
1120
- return this.client("/patches/~", "GET", {
1121
- query: {
1122
- omit_patch: true,
1123
- author: [],
1124
- patch_id: [],
1125
- module_file_path: paths
1126
- }
1127
- });
1128
- case 2:
1129
- patchesResponse = _context.sent;
1130
- if (!(patchesResponse.status !== 200)) {
1131
- _context.next = 6;
1132
- break;
1133
- }
1134
- console.error("Val: failed to get patches", patchesResponse.json.message, patchesResponse.json);
1135
- return _context.abrupt("return");
1136
- case 6:
1137
- patches = patchesResponse.json;
1138
- filteredPatches = Object.keys(patches);
1139
- _context.next = 10;
1140
- return this.client("/sources", "PUT", {
1141
- query: {
1142
- validate_sources: true,
1143
- validate_all: false,
1144
- validate_binary_files: false
1145
- },
1146
- path: undefined,
1147
- body: {
1148
- patchIds: filteredPatches
1149
- }
1150
- });
1151
- case 10:
1152
- treeRes = _context.sent;
1153
- _context.next = 13;
1154
- return this.initialize();
1155
- case 13:
1156
- if (treeRes.status === 200) {
1157
- data = treeRes.json;
1158
- for (_i = 0, _Object$keys = Object.keys(data.modules); _i < _Object$keys.length; _i++) {
1159
- pathS = _Object$keys[_i];
1160
- path = pathS;
1161
- this.drafts[path] = data === null || data === void 0 || (_data$modules = data.modules) === null || _data$modules === void 0 || (_data$modules = _data$modules[path]) === null || _data$modules === void 0 ? void 0 : _data$modules.source;
1162
- this.emitEvent(path, this.drafts[path]);
1163
- }
1164
- } else {
1165
- console.error("Val: failed to reload paths", paths, treeRes.json);
1166
- }
1167
- case 14:
1168
- case "end":
1169
- return _context.stop();
1170
- }
1171
- }, _callee, this);
1172
- }));
1173
- function reloadPaths(_x) {
1174
- return _reloadPaths.apply(this, arguments);
1175
- }
1176
- return reloadPaths;
1177
- }()
1178
- }, {
1179
- key: "reset",
1180
- value: function () {
1181
- var _reset = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee2() {
1182
- var patchesRes, allPatches, treeRes, initRes, _i2, _Object$keys2, _treeRes$json, pathS, path;
1183
- return _regeneratorRuntime().wrap(function _callee2$(_context2) {
1184
- while (1) switch (_context2.prev = _context2.next) {
1185
- case 0:
1186
- _context2.next = 2;
1187
- return this.client("/patches/~", "GET", {
1188
- query: {
1189
- omit_patch: true,
1190
- author: [],
1191
- patch_id: [],
1192
- module_file_path: []
1193
- }
1194
- });
1195
- case 2:
1196
- patchesRes = _context2.sent;
1197
- if (!(patchesRes.status !== 200)) {
1198
- _context2.next = 6;
1199
- break;
1200
- }
1201
- console.error("Val: failed to get patches", patchesRes.json);
1202
- return _context2.abrupt("return", result.err({
1203
- errorType: "other",
1204
- message: "Failed to get patches"
1205
- }));
1206
- case 6:
1207
- allPatches = Object.keys(patchesRes.json.patches);
1208
- _context2.next = 9;
1209
- return this.client("/sources", "PUT", {
1210
- path: undefined,
1211
- query: {
1212
- validate_sources: false,
1213
- validate_all: false,
1214
- validate_binary_files: false
1215
- },
1216
- body: {
1217
- patchIds: allPatches
1218
- }
1219
- });
1220
- case 9:
1221
- treeRes = _context2.sent;
1222
- _context2.next = 12;
1223
- return this.initialize();
1224
- case 12:
1225
- initRes = _context2.sent;
1226
- if (!result.isErr(initRes)) {
1227
- _context2.next = 16;
1228
- break;
1229
- }
1230
- console.error("Failed to initialize inside reset", initRes.error);
1231
- return _context2.abrupt("return", result.err({
1232
- errorType: "other",
1233
- message: initRes.error.message
1234
- }));
1235
- case 16:
1236
- if (!(treeRes.status === 200)) {
1237
- _context2.next = 21;
1238
- break;
1239
- }
1240
- for (_i2 = 0, _Object$keys2 = Object.keys(treeRes.json.modules); _i2 < _Object$keys2.length; _i2++) {
1241
- pathS = _Object$keys2[_i2];
1242
- path = pathS;
1243
- this.drafts[path] = (_treeRes$json = treeRes.json) === null || _treeRes$json === void 0 || (_treeRes$json = _treeRes$json.modules) === null || _treeRes$json === void 0 || (_treeRes$json = _treeRes$json[path]) === null || _treeRes$json === void 0 ? void 0 : _treeRes$json.source;
1244
- this.emitEvent(path, this.drafts[path]);
1245
- }
1246
- return _context2.abrupt("return", result.ok(undefined));
1247
- case 21:
1248
- if (!(treeRes.status === 400 && "type" in treeRes.json)) {
1249
- _context2.next = 25;
1250
- break;
1251
- }
1252
- return _context2.abrupt("return", result.err({
1253
- errorType: "patch-error",
1254
- errors: Object.entries(treeRes.json.errors).flatMap(function (_ref) {
1255
- var _ref2 = _slicedToArray(_ref, 2),
1256
- moduleFilePath = _ref2[0],
1257
- value = _ref2[1];
1258
- return (value !== null && value !== void 0 ? value : []).map(function (item) {
1259
- return {
1260
- moduleFilePath: moduleFilePath,
1261
- patchError: item.error.message,
1262
- patchId: item.patchId,
1263
- skipped: item.skipped
1264
- };
1265
- });
1266
- })
1267
- }));
1268
- case 25:
1269
- console.error("Val: failed to reset", treeRes.json);
1270
- return _context2.abrupt("return", result.err({
1271
- errorType: "other",
1272
- message: "Failed to reset " + JSON.stringify(treeRes.json)
1273
- }));
1274
- case 27:
1275
- case "end":
1276
- return _context2.stop();
1277
- }
1278
- }, _callee2, this);
1279
- }));
1280
- function reset() {
1281
- return _reset.apply(this, arguments);
1282
- }
1283
- return reset;
1284
- }()
1285
- }, {
1286
- key: "getModule",
1287
- value: function () {
1288
- var _getModule = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee3(path) {
1289
- var refetch,
1290
- treeRes,
1291
- _treeRes$json2,
1292
- _treeRes$json3,
1293
- fetchedSource,
1294
- schema,
1295
- _args3 = arguments;
1296
- return _regeneratorRuntime().wrap(function _callee3$(_context3) {
1297
- while (1) switch (_context3.prev = _context3.next) {
1298
- case 0:
1299
- refetch = _args3.length > 1 && _args3[1] !== undefined ? _args3[1] : false;
1300
- if (!(!refetch && this.drafts[path] && this.schema[path])) {
1301
- _context3.next = 3;
1302
- break;
1303
- }
1304
- return _context3.abrupt("return", result.ok({
1305
- source: this.drafts[path],
1306
- schema: this.schema[path]
1307
- }));
1308
- case 3:
1309
- _context3.next = 5;
1310
- return this.client("/sources", "PUT", {
1311
- path: path,
1312
- body: {},
1313
- query: {
1314
- validate_sources: false,
1315
- validate_all: false,
1316
- validate_binary_files: false
1317
- }
1318
- });
1319
- case 5:
1320
- treeRes = _context3.sent;
1321
- if (!(treeRes.status === 200)) {
1322
- _context3.next = 26;
1323
- break;
1324
- }
1325
- if ((_treeRes$json2 = treeRes.json) !== null && _treeRes$json2 !== void 0 && (_treeRes$json2 = _treeRes$json2.modules) !== null && _treeRes$json2 !== void 0 && _treeRes$json2[path]) {
1326
- _context3.next = 10;
1327
- break;
1328
- }
1329
- console.error("Val: could not find the module", {
1330
- moduleIds: Object.keys(treeRes.json.modules),
1331
- moduleId: path,
1332
- data: treeRes
1333
- });
1334
- return _context3.abrupt("return", result.err({
1335
- message: "Could not fetch data.\nCould not find the module:\n" + path + "\n\nVerify that the val.modules file includes this module."
1336
- }));
1337
- case 10:
1338
- fetchedSource = (_treeRes$json3 = treeRes.json) === null || _treeRes$json3 === void 0 || (_treeRes$json3 = _treeRes$json3.modules) === null || _treeRes$json3 === void 0 || (_treeRes$json3 = _treeRes$json3[path]) === null || _treeRes$json3 === void 0 ? void 0 : _treeRes$json3.source;
1339
- schema = this.schema[path];
1340
- if (this.schema[path]) {
1341
- _context3.next = 17;
1342
- break;
1343
- }
1344
- _context3.next = 15;
1345
- return this.initialize();
1346
- case 15:
1347
- if (this.schema[path]) {
1348
- _context3.next = 17;
1349
- break;
1350
- }
1351
- return _context3.abrupt("return", result.err({
1352
- message: "Path not found in schema. Verify that the module exists."
1353
- }));
1354
- case 17:
1355
- if (!(fetchedSource !== undefined)) {
1356
- _context3.next = 22;
1357
- break;
1358
- }
1359
- this.drafts[path] = fetchedSource;
1360
- return _context3.abrupt("return", result.ok({
1361
- source: fetchedSource,
1362
- schema: schema
1363
- }));
1364
- case 22:
1365
- console.error("Val: could not find the module source");
1366
- return _context3.abrupt("return", result.err({
1367
- message: "Could not fetch data. Verify that the module exists."
1368
- }));
1369
- case 24:
1370
- _context3.next = 33;
1371
- break;
1372
- case 26:
1373
- if (!(treeRes.status === 504)) {
1374
- _context3.next = 31;
1375
- break;
1376
- }
1377
- console.error("Val: timeout", treeRes.json);
1378
- return _context3.abrupt("return", result.err({
1379
- message: "Timed out while fetching data. Try again later."
1380
- }));
1381
- case 31:
1382
- console.error("Val: failed to get module", treeRes.json);
1383
- return _context3.abrupt("return", result.err({
1384
- message: "Could not fetch data. Verify that is correctly configured."
1385
- }));
1386
- case 33:
1387
- case "end":
1388
- return _context3.stop();
1389
- }
1390
- }, _callee3, this);
1391
- }));
1392
- function getModule(_x2) {
1393
- return _getModule.apply(this, arguments);
1394
- }
1395
- return getModule;
1396
- }()
1397
- }, {
1398
- key: "deletePatches",
1399
- value: function () {
1400
- var _deletePatches = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee4(patchIds) {
1401
- var patchesRes;
1402
- return _regeneratorRuntime().wrap(function _callee4$(_context4) {
1403
- while (1) switch (_context4.prev = _context4.next) {
1404
- case 0:
1405
- _context4.next = 2;
1406
- return this.client("/patches/~", "DELETE", {
1407
- query: {
1408
- id: patchIds
1409
- }
1410
- });
1411
- case 2:
1412
- patchesRes = _context4.sent;
1413
- if (!(patchesRes.status === 200)) {
1414
- _context4.next = 7;
1415
- break;
1416
- }
1417
- return _context4.abrupt("return", result.ok(patchesRes.json));
1418
- case 7:
1419
- console.error("Val: failed to delete patches", patchesRes.json);
1420
- return _context4.abrupt("return", result.err({
1421
- message: patchesRes.json.message
1422
- }));
1423
- case 9:
1424
- case "end":
1425
- return _context4.stop();
1426
- }
1427
- }, _callee4, this);
1428
- }));
1429
- function deletePatches(_x3) {
1430
- return _deletePatches.apply(this, arguments);
1431
- }
1432
- return deletePatches;
1433
- }()
1434
- }, {
1435
- key: "applyPatch",
1436
- value: function () {
1437
- var _applyPatch = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee5(path, patchIds, patch) {
1438
- var treeRes, _treeRes$json4, newPatchIds, newPatchId, fetchedSource, _treeRes$json5;
1439
- return _regeneratorRuntime().wrap(function _callee5$(_context5) {
1440
- while (1) switch (_context5.prev = _context5.next) {
1441
- case 0:
1442
- _context5.next = 2;
1443
- return this.client("/sources", "PUT", {
1444
- path: path,
1445
- query: {
1446
- validate_sources: false,
1447
- validate_all: false,
1448
- validate_binary_files: false
1449
- },
1450
- body: {
1451
- patchIds: patchIds,
1452
- addPatches: [{
1453
- path: path,
1454
- patch: patch
1455
- }]
1456
- }
1457
- });
1458
- case 2:
1459
- treeRes = _context5.sent;
1460
- if (!(treeRes.status === 200)) {
1461
- _context5.next = 20;
1462
- break;
1463
- }
1464
- newPatchIds = treeRes.json.newPatchIds;
1465
- if (newPatchIds) {
1466
- _context5.next = 8;
1467
- break;
1468
- }
1469
- console.error("Val: could create patch", treeRes);
1470
- return _context5.abrupt("return", result.err({
1471
- errorType: "other",
1472
- message: "Val: could not create patch"
1473
- }));
1474
- case 8:
1475
- newPatchId = newPatchIds[0];
1476
- 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;
1477
- if (!(fetchedSource !== undefined)) {
1478
- _context5.next = 16;
1479
- break;
1480
- }
1481
- this.drafts[path] = fetchedSource;
1482
- this.emitEvent(path, fetchedSource);
1483
- return _context5.abrupt("return", result.ok({
1484
- newPatchId: newPatchId,
1485
- modules: _defineProperty({}, path, {
1486
- 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) || []
1487
- })
1488
- }));
1489
- case 16:
1490
- console.error("Val: could not patch");
1491
- return _context5.abrupt("return", result.err({
1492
- errorType: "other",
1493
- message: "Val: could not fetch data. Verify that the module exists."
1494
- }));
1495
- case 18:
1496
- _context5.next = 26;
1497
- break;
1498
- case 20:
1499
- if (!(treeRes.status === 400 && "type" in treeRes.json)) {
1500
- _context5.next = 24;
1501
- break;
1502
- }
1503
- return _context5.abrupt("return", result.err({
1504
- errorType: "patch-error",
1505
- errors: Object.entries(treeRes.json.errors).flatMap(function (_ref3) {
1506
- var _ref4 = _slicedToArray(_ref3, 2),
1507
- moduleFilePath = _ref4[0],
1508
- value = _ref4[1];
1509
- return (value !== null && value !== void 0 ? value : []).map(function (item) {
1510
- return {
1511
- moduleFilePath: moduleFilePath,
1512
- patchError: item.error.message,
1513
- patchId: item.patchId,
1514
- skipped: item.skipped
1515
- };
1516
- });
1517
- })
1518
- }));
1519
- case 24:
1520
- console.error("Val: failed to get module", treeRes.json);
1521
- return _context5.abrupt("return", result.err({
1522
- errorType: "other",
1523
- message: "Val: could not fetch data. Verify that is correctly configured."
1524
- }));
1525
- case 26:
1526
- case "end":
1527
- return _context5.stop();
1528
- }
1529
- }, _callee5, this);
1530
- }));
1531
- function applyPatch(_x4, _x5, _x6) {
1532
- return _applyPatch.apply(this, arguments);
1533
- }
1534
- return applyPatch;
1535
- }()
1536
- }, {
1537
- key: "emitEvent",
1538
- value: function emitEvent(path, source) {
1539
- var event = new CustomEvent("val-event", {
1540
- detail: {
1541
- type: "module-update",
1542
- path: path,
1543
- source: source
1544
- }
1545
- });
1546
- window.dispatchEvent(event);
1547
- }
1548
- }, {
1549
- key: "initialize",
1550
- value: function () {
1551
- var _initialize = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee6() {
1552
- var schemaRes, paths, _i3, _arr, moduleId, schema, msg;
1553
- return _regeneratorRuntime().wrap(function _callee6$(_context6) {
1554
- while (1) switch (_context6.prev = _context6.next) {
1555
- case 0:
1556
- _context6.next = 2;
1557
- return this.client("/schema", "GET", {});
1558
- case 2:
1559
- schemaRes = _context6.sent;
1560
- if (!(schemaRes.status === 200)) {
1561
- _context6.next = 9;
1562
- break;
1563
- }
1564
- paths = [];
1565
- for (_i3 = 0, _arr = Object.keys(schemaRes.json.schemas); _i3 < _arr.length; _i3++) {
1566
- moduleId = _arr[_i3];
1567
- schema = schemaRes.json.schemas[moduleId];
1568
- if (schema) {
1569
- paths.push(moduleId);
1570
- this.schema[moduleId] = schema;
1571
- }
1572
- }
1573
- return _context6.abrupt("return", result.ok(paths));
1574
- case 9:
1575
- msg = "Failed to fetch content. ";
1576
- if (schemaRes.status === 401) {
1577
- msg += "Authorization failed - check that you are logged in.";
1578
- } else {
1579
- msg += "Get a developer to verify that is correctly setup.";
1580
- }
1581
- return _context6.abrupt("return", result.err({
1582
- message: msg,
1583
- details: {
1584
- fetchError: schemaRes.json
1585
- }
1586
- }));
1587
- case 12:
1588
- case "end":
1589
- return _context6.stop();
1590
- }
1591
- }, _callee6, this);
1592
- }));
1593
- function initialize() {
1594
- return _initialize.apply(this, arguments);
1595
- }
1596
- return initialize;
1597
- }()
1598
- }]);
1599
- }();
1600
-
1601
1047
  var JSONValueT = z.lazy(function () {
1602
1048
  return z.union([z.string(), z.number(), z["boolean"](), z["null"](), z.array(JSONValueT), z.record(JSONValueT)]);
1603
1049
  });
1050
+
1051
+ /**
1052
+ * Raw JSON patch operation.
1053
+ */
1054
+ var OperationJSONT = z.discriminatedUnion("op", [z.object({
1055
+ op: z.literal("add"),
1056
+ path: z.string(),
1057
+ value: JSONValueT
1058
+ }).strict(), z.object({
1059
+ op: z.literal("remove"),
1060
+ /**
1061
+ * Must be non-root
1062
+ */
1063
+ path: z.string()
1064
+ }).strict(), z.object({
1065
+ op: z.literal("replace"),
1066
+ path: z.string(),
1067
+ value: JSONValueT
1068
+ }).strict(), z.object({
1069
+ op: z.literal("move"),
1070
+ /**
1071
+ * Must be non-root and not a proper prefix of "path".
1072
+ */
1073
+ from: z.string(),
1074
+ path: z.string()
1075
+ }).strict(), z.object({
1076
+ op: z.literal("copy"),
1077
+ from: z.string(),
1078
+ path: z.string()
1079
+ }).strict(), z.object({
1080
+ op: z.literal("test"),
1081
+ path: z.string(),
1082
+ value: JSONValueT
1083
+ }).strict(), z.object({
1084
+ op: z.literal("file"),
1085
+ path: z.string(),
1086
+ filePath: z.string(),
1087
+ value: z.string()
1088
+ }).strict()]);
1089
+ var PatchJSON = z.array(OperationJSONT);
1090
+ /**
1091
+ * Raw JSON patch operation.
1092
+ */
1604
1093
  var OperationT = z.discriminatedUnion("op", [z.object({
1605
1094
  op: z.literal("add"),
1606
1095
  path: z.array(z.string()),
@@ -1629,11 +1118,24 @@ var OperationT = z.discriminatedUnion("op", [z.object({
1629
1118
  path: z.array(z.string()),
1630
1119
  filePath: z.string(),
1631
1120
  nestedFilePath: z.array(z.string()).optional(),
1632
- value: z.union([z.string(), z.object({
1633
- sha256: z.string()
1634
- })])
1121
+ value: z.string()
1635
1122
  }).strict()]);
1636
1123
  var Patch = z.array(OperationT);
1124
+ var PatchId = z.string().refine(function (_id) {
1125
+ return true;
1126
+ } // TODO: validation
1127
+ );
1128
+ var ParentRef$1 = z.union([z.object({
1129
+ type: z.literal("head"),
1130
+ headBaseSha: z.string()
1131
+ }), z.object({
1132
+ type: z.literal("patch"),
1133
+ patchId: PatchId
1134
+ })]);
1135
+ var PatchBlock = z.object({
1136
+ patch: Patch,
1137
+ parentRef: ParentRef$1
1138
+ });
1637
1139
 
1638
1140
  var SourcePath = z.string().transform(function (path) {
1639
1141
  return path;
@@ -1763,14 +1265,17 @@ var SerializedImageSchema = z.object({
1763
1265
  });
1764
1266
  var SerializedSchema = z.union([SerializedStringSchema, SerializedLiteralSchema, SerializedBooleanSchema, SerializedNumberSchema, SerializedObjectSchema, SerializedArraySchema, SerializedUnionSchema, SerializedRichTextSchema, SerializedRecordSchema, SerializedKeyOfSchema, SerializedFileSchema, SerializedDateSchema, SerializedImageSchema]);
1765
1267
 
1766
- var PatchId = z.string().refine(function (_id) {
1767
- return true;
1768
- } // TODO:
1769
- );
1770
1268
  var ModuleFilePath = z.string().refine(function (_path) {
1771
1269
  return true;
1772
- } // TODO:
1270
+ } // TODO: validation
1773
1271
  );
1272
+ var ParentRef = z.union([z.object({
1273
+ type: z.literal("head"),
1274
+ headBaseSha: z.string()
1275
+ }), z.object({
1276
+ type: z.literal("patch"),
1277
+ patchId: PatchId
1278
+ })]);
1774
1279
  var ValConfig = z.object({
1775
1280
  project: z.string().optional(),
1776
1281
  root: z.string().optional(),
@@ -1780,7 +1285,7 @@ var ValConfig = z.object({
1780
1285
  gitCommit: z.string().optional(),
1781
1286
  gitBranch: z.string().optional()
1782
1287
  });
1783
- 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")]);
1288
+ var ValidationFixZ = z.union([z.literal("image:change-extension"), z.literal("image:add-metadata"), z.literal("image:check-metadata"), z.literal("file:change-extension"), z.literal("file:add-metadata"), z.literal("file:check-metadata")]);
1784
1289
  var ValidationError = z.object({
1785
1290
  message: z.string(),
1786
1291
  value: z.unknown().optional(),
@@ -1796,6 +1301,13 @@ var notFoundResponse = z.object({
1796
1301
  var GenericError = z.object({
1797
1302
  message: z.string()
1798
1303
  });
1304
+ var GenericPatchError = z.union([z.object({
1305
+ patchId: PatchId,
1306
+ message: z.string()
1307
+ }), z.object({
1308
+ parentPatchId: z.string(),
1309
+ message: z.string()
1310
+ })]);
1799
1311
  var ModulesError = z.object({
1800
1312
  message: z.string(),
1801
1313
  path: ModuleFilePath.optional()
@@ -2076,7 +1588,7 @@ var Api = {
2076
1588
  })])
2077
1589
  }
2078
1590
  },
2079
- "/patches/~": {
1591
+ "/patches": {
2080
1592
  DELETE: {
2081
1593
  req: {
2082
1594
  query: {
@@ -2090,13 +1602,50 @@ var Api = {
2090
1602
  status: z.literal(500),
2091
1603
  json: z.object({
2092
1604
  message: z.string(),
2093
- details: z.record(PatchId, GenericError)
1605
+ errors: z.array(GenericPatchError)
2094
1606
  })
2095
1607
  }), z.object({
2096
1608
  status: z.literal(200),
2097
1609
  json: z.array(PatchId)
2098
1610
  })])
2099
1611
  },
1612
+ PUT: {
1613
+ req: {
1614
+ body: z.object({
1615
+ parentRef: ParentRef,
1616
+ patches: z.array(z.object({
1617
+ path: ModuleFilePath,
1618
+ patch: Patch
1619
+ }))
1620
+ }),
1621
+ cookies: {
1622
+ val_session: z.string().optional()
1623
+ }
1624
+ },
1625
+ res: z.union([notFoundResponse, z.object({
1626
+ status: z.literal(409),
1627
+ // conflict: i.e. not a head of patches
1628
+ json: z.object({
1629
+ type: z.literal("patch-head-conflict"),
1630
+ message: z.string()
1631
+ })
1632
+ }), z.object({
1633
+ status: z.literal(400),
1634
+ json: z.object({
1635
+ type: z.literal("patch-error"),
1636
+ message: z.string(),
1637
+ errors: z.record(ModuleFilePath, z.array(z.object({
1638
+ error: GenericError
1639
+ })))
1640
+ })
1641
+ }), z.object({
1642
+ status: z.literal(200),
1643
+ json: z.object({
1644
+ newPatchIds: z.array(PatchId),
1645
+ parentRef: ParentRef
1646
+ })
1647
+ })])
1648
+ },
2100
1649
  GET: {
2101
1650
  req: {
2102
1651
  query: {
@@ -2113,14 +1662,21 @@ var Api = {
2113
1662
  status: z.literal(500),
2114
1663
  json: z.object({
2115
1664
  message: z.string(),
2116
- details: z.record(PatchId, GenericError)
1665
+ patchErrors: z.array(GenericPatchError)
1666
+ })
1667
+ }), z.object({
1668
+ status: z.literal(500),
1669
+ json: z.object({
1670
+ message: z.string(),
1671
+ error: GenericError
2117
1672
  })
2118
1673
  }), z.object({
2119
1674
  status: z.literal(200),
2120
1675
  json: z.object({
2121
- patches: z.record(PatchId, z.object({
1676
+ patches: z.array(z.object({
2122
1677
  path: ModuleFilePath,
2123
1678
  patch: Patch.optional(),
1679
+ patchId: PatchId,
2124
1680
  createdAt: z.string(),
2125
1681
  authorId: z.string().nullable(),
2126
1682
  appliedAt: z.object({
@@ -2131,6 +1687,7 @@ var Api = {
2131
1687
  timestamp: z.string()
2132
1688
  }).nullable()
2133
1689
  })),
1690
+ baseSha: z.string(),
2134
1691
  error: GenericError.optional(),
2135
1692
  errors: z.record(PatchId, GenericError).optional()
2136
1693
  })
@@ -2165,16 +1722,12 @@ var Api = {
2165
1722
  })])
2166
1723
  }
2167
1724
  },
2168
- "/sources": {
1725
+ "/sources/~": {
2169
1726
  PUT: {
2170
1727
  req: {
2171
1728
  path: z.string().optional(),
2172
1729
  body: z.object({
2173
- patchIds: z.array(PatchId).optional(),
2174
- addPatches: z.array(z.object({
2175
- path: ModuleFilePath,
2176
- patch: Patch
2177
- })).optional()
1730
+ patchIds: z.array(PatchId).optional()
2178
1731
  }).optional(),
2179
1732
  query: {
2180
1733
  validate_all: z["boolean"]().optional(),
@@ -2188,10 +1741,6 @@ var Api = {
2188
1741
  res: z.union([notFoundResponse, z.object({
2189
1742
  status: z.literal(401),
2190
1743
  json: GenericError
2191
- }), z.object({
2192
- status: z.literal(409),
2193
- // conflict: i.e. not a head of patches
2194
- json: GenericError
2195
1744
  }), z.object({
2196
1745
  status: z.literal(500),
2197
1746
  json: z.object({
@@ -2199,32 +1748,15 @@ var Api = {
2199
1748
  details: z.union([z.array(ModulesError), GenericError])
2200
1749
  })
2201
1750
  }), z.object({
2202
- status: z.literal(400),
1751
+ status: z.literal(409),
2203
1752
  json: z.object({
2204
- message: z.string(),
2205
- details: z.array(ModulesError)
1753
+ message: z.string()
2206
1754
  })
2207
1755
  }), z.object({
2208
1756
  status: z.literal(400),
2209
1757
  json: z.object({
2210
- type: z.literal("patch-error"),
2211
1758
  message: z.string(),
2212
- schemaSha: z.string(),
2213
- modules: z.record(ModuleFilePath, z.object({
2214
- source: z.any(),
2215
- //.optional(), // TODO: Json zod type
2216
- patches: z.object({
2217
- applied: z.array(PatchId),
2218
- skipped: z.array(PatchId).optional(),
2219
- errors: z.record(PatchId, GenericError).optional()
2220
- }).optional(),
2221
- validationErrors: z.record(SourcePath, z.array(ValidationError)).optional()
2222
- })),
2223
- errors: z.record(ModuleFilePath, z.array(z.object({
2224
- patchId: PatchId,
2225
- skipped: z["boolean"](),
2226
- error: GenericError
2227
- })))
1759
+ details: z.array(ModulesError)
2228
1760
  })
2229
1761
  }), z.object({
2230
1762
  status: z.literal(200),
@@ -2239,8 +1771,7 @@ var Api = {
2239
1771
  errors: z.record(PatchId, GenericError).optional()
2240
1772
  }).optional(),
2241
1773
  validationErrors: z.record(SourcePath, z.array(ValidationError)).optional()
2242
- })),
2243
- newPatchIds: z.array(PatchId).optional()
1774
+ }))
2244
1775
  })
2245
1776
  })])
2246
1777
  }
@@ -2387,9 +1918,13 @@ var createValClient = function createValClient(host) {
2387
1918
  case 7:
2388
1919
  reqBodyResult = (_apiEndpoint$req$body = apiEndpoint.req.body) === null || _apiEndpoint$req$body === void 0 ? void 0 : _apiEndpoint$req$body.safeParse(anyReq.body);
2389
1920
  if (!(reqBodyResult && !reqBodyResult.success)) {
2390
- _context2.next = 10;
1921
+ _context2.next = 11;
2391
1922
  break;
2392
1923
  }
1924
+ console.error("Invalid request body", {
1925
+ body: anyReq.body,
1926
+ error: reqBodyResult.error
1927
+ });
2393
1928
  return _context2.abrupt("return", {
2394
1929
  status: null,
2395
1930
  json: {
@@ -2401,9 +1936,9 @@ var createValClient = function createValClient(host) {
2401
1936
  }
2402
1937
  }
2403
1938
  });
2404
- case 10:
2405
- _context2.prev = 10;
2406
- _context2.next = 13;
1939
+ case 11:
1940
+ _context2.prev = 11;
1941
+ _context2.next = 14;
2407
1942
  return fetch("".concat(host).concat(fullPath), {
2408
1943
  method: method,
2409
1944
  headers: {
@@ -2426,7 +1961,7 @@ var createValClient = function createValClient(host) {
2426
1961
  status: null,
2427
1962
  json: {
2428
1963
  type: "client_side_validation_error",
2429
- 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."),
1964
+ message: "Invalid response (expected JSON, but got something else). This could be a result of mismatched Val versions or a transient error.",
2430
1965
  details: {
2431
1966
  validationError: "Invalid content type",
2432
1967
  data: {
@@ -2489,12 +2024,12 @@ var createValClient = function createValClient(host) {
2489
2024
  return _ref2.apply(this, arguments);
2490
2025
  };
2491
2026
  }());
2492
- case 13:
2027
+ case 14:
2493
2028
  res = _context2.sent;
2494
2029
  return _context2.abrupt("return", res);
2495
- case 17:
2496
- _context2.prev = 17;
2497
- _context2.t0 = _context2["catch"](10);
2030
+ case 18:
2031
+ _context2.prev = 18;
2032
+ _context2.t0 = _context2["catch"](11);
2498
2033
  return _context2.abrupt("return", {
2499
2034
  status: null,
2500
2035
  json: {
@@ -2504,11 +2039,11 @@ var createValClient = function createValClient(host) {
2504
2039
  details: _context2.t0 instanceof Error ? _context2.t0.message : JSON.stringify(_context2.t0)
2505
2040
  }
2506
2041
  });
2507
- case 20:
2042
+ case 21:
2508
2043
  case "end":
2509
2044
  return _context2.stop();
2510
2045
  }
2511
- }, _callee2, null, [[10, 17]]);
2046
+ }, _callee2, null, [[11, 18]]);
2512
2047
  }));
2513
2048
  return function (_x, _x2, _x3) {
2514
2049
  return _ref.apply(this, arguments);
@@ -2530,6 +2065,46 @@ function isRetryable(error) {
2530
2065
  return false;
2531
2066
  }
2532
2067
 
2068
+ function _arrayWithHoles(r) {
2069
+ if (Array.isArray(r)) return r;
2070
+ }
2071
+
2072
+ function _iterableToArrayLimit(r, l) {
2073
+ var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"];
2074
+ if (null != t) {
2075
+ var e,
2076
+ n,
2077
+ i,
2078
+ u,
2079
+ a = [],
2080
+ f = !0,
2081
+ o = !1;
2082
+ try {
2083
+ if (i = (t = t.call(r)).next, 0 === l) {
2084
+ if (Object(t) !== t) return;
2085
+ f = !1;
2086
+ } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0);
2087
+ } catch (r) {
2088
+ o = !0, n = r;
2089
+ } finally {
2090
+ try {
2091
+ if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return;
2092
+ } finally {
2093
+ if (o) throw n;
2094
+ }
2095
+ }
2096
+ return a;
2097
+ }
2098
+ }
2099
+
2100
+ function _nonIterableRest() {
2101
+ throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
2102
+ }
2103
+
2104
+ function _slicedToArray(r, e) {
2105
+ return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest();
2106
+ }
2107
+
2533
2108
  var urlOf = function urlOf() {
2534
2109
  var route = arguments.length <= 0 ? undefined : arguments[0];
2535
2110
  var query = arguments.length <= 1 ? undefined : arguments[1];
@@ -2549,4 +2124,4 @@ var urlOf = function urlOf() {
2549
2124
  return "".concat(route);
2550
2125
  };
2551
2126
 
2552
- export { Api, RemirrorBr, RemirrorBulletList, RemirrorHeading, RemirrorImage, RemirrorJSON, RemirrorLinkMark, RemirrorListItem, RemirrorOrderedList, RemirrorParagraph, RemirrorText, RemirrorTextMark, VAL_ENABLE_COOKIE_NAME, VAL_SESSION_COOKIE, VAL_STATE_COOKIE, ValCache, createValClient, remirrorToRichTextSource, richTextToRemirror, urlOf };
2127
+ export { Api, ParentRef$1 as ParentRef, Patch, PatchBlock, PatchId, PatchJSON, RemirrorBr, RemirrorBulletList, RemirrorHeading, RemirrorImage, RemirrorJSON, RemirrorLinkMark, RemirrorListItem, RemirrorOrderedList, RemirrorParagraph, RemirrorText, RemirrorTextMark, VAL_ENABLE_COOKIE_NAME, VAL_SESSION_COOKIE, VAL_STATE_COOKIE, createValClient, remirrorToRichTextSource, richTextToRemirror, urlOf };