sanity-plugin-studio-smartling 1.2.1 → 1.5.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,5 +1,5 @@
1
1
  export { TranslationsTab } from 'sanity-translations-tab';
2
- import { BaseDocumentMerger, BaseDocumentDeserializer, BaseDocumentSerializer } from 'sanity-naive-html-serializer';
2
+ import { BaseDocumentMerger, BaseDocumentSerializer, BaseDocumentDeserializer } from 'sanity-naive-html-serializer';
3
3
  export { BaseDocumentDeserializer, BaseDocumentMerger, BaseDocumentSerializer, customSerializers, defaultStopTypes } from 'sanity-naive-html-serializer';
4
4
  import sanityClient from 'part:@sanity/base/client';
5
5
 
@@ -929,7 +929,7 @@ var getLocales = /*#__PURE__*/function () {
929
929
 
930
930
  var getTranslationTask = /*#__PURE__*/function () {
931
931
  var _ref = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee(documentId, secrets) {
932
- var accessToken, taskId, url, smartlingTask, locales;
932
+ var accessToken, taskId, projectId, progressUrl, smartlingTask, locales;
933
933
  return runtime_1.wrap(function _callee$(_context) {
934
934
  while (1) {
935
935
  switch (_context.prev = _context.next) {
@@ -957,18 +957,19 @@ var getTranslationTask = /*#__PURE__*/function () {
957
957
  });
958
958
 
959
959
  case 8:
960
- url = "https://api.smartling.com/jobs-api/v3/projects/" + secrets.project + "/jobs/" + taskId + "/progress";
961
- _context.next = 11;
960
+ projectId = secrets.project;
961
+ progressUrl = "https://api.smartling.com/jobs-api/v3/projects/" + projectId + "/jobs/" + taskId + "/progress";
962
+ _context.next = 12;
962
963
  return fetch(smartlingProxy, {
963
964
  method: 'POST',
964
- headers: getHeaders(url, accessToken)
965
+ headers: getHeaders(progressUrl, accessToken)
965
966
  }).then(function (res) {
966
967
  return res.json();
967
968
  }).then(function (res) {
968
969
  return res.response.data;
969
970
  });
970
971
 
971
- case 11:
972
+ case 12:
972
973
  smartlingTask = _context.sent;
973
974
  locales = [];
974
975
 
@@ -985,10 +986,11 @@ var getTranslationTask = /*#__PURE__*/function () {
985
986
  documentId: documentId,
986
987
  locales: locales,
987
988
  //since our download is tied to document id for smartling, keep track of it as a task
988
- taskId: documentId
989
+ taskId: documentId,
990
+ linkToVendorTask: "https://dashboard.smartling.com/app/projects/" + projectId + "/account-jobs/" + projectId + ":" + taskId
989
991
  });
990
992
 
991
- case 15:
993
+ case 16:
992
994
  case "end":
993
995
  return _context.stop();
994
996
  }
@@ -1044,30 +1046,41 @@ var createJob = /*#__PURE__*/function () {
1044
1046
 
1045
1047
 
1046
1048
  var createJobBatch = /*#__PURE__*/function () {
1047
- var _ref2 = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee2(jobId, projectId, documentName, accessToken) {
1048
- var url;
1049
+ var _ref2 = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee2(jobId, projectId, documentName, accessToken, localeIds, workflowUid) {
1050
+ var url, reqBody;
1049
1051
  return runtime_1.wrap(function _callee2$(_context2) {
1050
1052
  while (1) {
1051
1053
  switch (_context2.prev = _context2.next) {
1052
1054
  case 0:
1053
1055
  url = "https://api.smartling.com/job-batches-api/v2/projects/" + projectId + "/batches";
1056
+ reqBody = {
1057
+ authorize: true,
1058
+ translationJobUid: jobId,
1059
+ fileUris: [documentName]
1060
+ };
1061
+
1062
+ if (workflowUid) {
1063
+ reqBody.localeWorkflows = localeIds.map(function (l) {
1064
+ return {
1065
+ targetLocaleId: l,
1066
+ workflowUid: workflowUid
1067
+ };
1068
+ });
1069
+ }
1070
+
1054
1071
  return _context2.abrupt("return", fetch(smartlingProxy, {
1055
1072
  method: 'POST',
1056
1073
  headers: _extends({}, getHeaders(url, accessToken), {
1057
1074
  'content-type': 'application/json'
1058
1075
  }),
1059
- body: JSON.stringify({
1060
- authorize: true,
1061
- translationJobUid: jobId,
1062
- fileUris: [documentName]
1063
- })
1076
+ body: JSON.stringify(reqBody)
1064
1077
  }).then(function (res) {
1065
1078
  return res.json();
1066
1079
  }).then(function (res) {
1067
1080
  return res.response.data.batchUid;
1068
1081
  }));
1069
1082
 
1070
- case 2:
1083
+ case 4:
1071
1084
  case "end":
1072
1085
  return _context2.stop();
1073
1086
  }
@@ -1075,7 +1088,7 @@ var createJobBatch = /*#__PURE__*/function () {
1075
1088
  }, _callee2);
1076
1089
  }));
1077
1090
 
1078
- return function createJobBatch(_x5, _x6, _x7, _x8) {
1091
+ return function createJobBatch(_x5, _x6, _x7, _x8, _x9, _x10) {
1079
1092
  return _ref2.apply(this, arguments);
1080
1093
  };
1081
1094
  }();
@@ -1112,13 +1125,13 @@ var uploadFileToBatch = /*#__PURE__*/function () {
1112
1125
  }, _callee3);
1113
1126
  }));
1114
1127
 
1115
- return function uploadFileToBatch(_x9, _x10, _x11, _x12, _x13) {
1128
+ return function uploadFileToBatch(_x11, _x12, _x13, _x14, _x15) {
1116
1129
  return _ref3.apply(this, arguments);
1117
1130
  };
1118
1131
  }();
1119
1132
 
1120
1133
  var createTask = /*#__PURE__*/function () {
1121
- var _ref4 = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee4(documentId, document, localeIds, secrets) {
1134
+ var _ref4 = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee4(documentId, document, localeIds, secrets, workflowUid) {
1122
1135
  var accessToken, taskId, batchUid, uploadFileRes;
1123
1136
  return runtime_1.wrap(function _callee4$(_context4) {
1124
1137
  while (1) {
@@ -1148,7 +1161,7 @@ var createTask = /*#__PURE__*/function () {
1148
1161
 
1149
1162
  case 10:
1150
1163
  _context4.next = 12;
1151
- return createJobBatch(taskId, secrets.project, document.name, accessToken);
1164
+ return createJobBatch(taskId, secrets.project, document.name, accessToken, localeIds, workflowUid);
1152
1165
 
1153
1166
  case 12:
1154
1167
  batchUid = _context4.sent;
@@ -1168,7 +1181,7 @@ var createTask = /*#__PURE__*/function () {
1168
1181
  }, _callee4);
1169
1182
  }));
1170
1183
 
1171
- return function createTask(_x14, _x15, _x16, _x17) {
1184
+ return function createTask(_x16, _x17, _x18, _x19, _x20) {
1172
1185
  return _ref4.apply(this, arguments);
1173
1186
  };
1174
1187
  }();
@@ -1193,7 +1206,14 @@ var getTranslation = /*#__PURE__*/function () {
1193
1206
  }).then(function (res) {
1194
1207
  return res.json();
1195
1208
  }).then(function (res) {
1196
- return res.body;
1209
+ if (res.body) {
1210
+ return res.body;
1211
+ } else if (res.response.errors) {
1212
+ var _res$response$errors$;
1213
+
1214
+ var errMsg = ((_res$response$errors$ = res.response.errors[0]) == null ? void 0 : _res$response$errors$.message) || 'Error retrieving translation from Smartling';
1215
+ throw new Error(errMsg);
1216
+ }
1197
1217
  });
1198
1218
 
1199
1219
  case 6:
@@ -1259,7 +1279,7 @@ var findDocumentAtRevision = /*#__PURE__*/function () {
1259
1279
  }).then(function (req) {
1260
1280
  return req.json();
1261
1281
  }).then(function (req) {
1262
- return req.documents[0];
1282
+ return req.documents && req.documents[0];
1263
1283
  });
1264
1284
 
1265
1285
  case 5:
@@ -1336,34 +1356,43 @@ var documentLevelPatch = /*#__PURE__*/function () {
1336
1356
  case 13:
1337
1357
  i18nDoc = _context2.sent;
1338
1358
 
1339
- if (i18nDoc) {
1340
- cleanedMerge = {}; //don't overwrite any existing values on the i18n doc
1359
+ if (!i18nDoc) {
1360
+ _context2.next = 21;
1361
+ break;
1362
+ }
1341
1363
 
1342
- Object.entries(merged).forEach(function (_ref3) {
1343
- var key = _ref3[0],
1344
- value = _ref3[1];
1364
+ cleanedMerge = {}; //don't overwrite any existing values on the i18n doc
1345
1365
 
1346
- if (Object.keys(translatedFields).includes(key) && !['_id', '_rev', '_updatedAt'].includes(key)) {
1347
- cleanedMerge[key] = value;
1348
- }
1349
- });
1350
- client.transaction() //@ts-ignore
1351
- .patch(i18nDoc._id, function (p) {
1352
- return p.set(cleanedMerge);
1353
- }).commit();
1354
- } else {
1355
- merged._id = "drafts." + targetId; //account for legacy implementations of i18n plugin lang
1356
-
1357
- if (baseDoc._lang) {
1358
- merged._lang = localeId;
1359
- } else if (baseDoc.__i18n_lang) {
1360
- merged.__i18n_lang = localeId;
1366
+ Object.entries(merged).forEach(function (_ref3) {
1367
+ var key = _ref3[0],
1368
+ value = _ref3[1];
1369
+
1370
+ if (Object.keys(translatedFields).includes(key) && !['_id', '_rev', '_updatedAt'].includes(key)) {
1371
+ cleanedMerge[key] = value;
1361
1372
  }
1373
+ });
1374
+ _context2.next = 19;
1375
+ return client.transaction() //@ts-ignore
1376
+ .patch(i18nDoc._id, function (p) {
1377
+ return p.set(cleanedMerge);
1378
+ }).commit();
1379
+
1380
+ case 19:
1381
+ _context2.next = 24;
1382
+ break;
1362
1383
 
1363
- client.create(merged);
1384
+ case 21:
1385
+ merged._id = "drafts." + targetId; //account for legacy implementations of i18n plugin lang
1386
+
1387
+ if (baseDoc._lang) {
1388
+ merged._lang = localeId;
1389
+ } else if (baseDoc.__i18n_lang) {
1390
+ merged.__i18n_lang = localeId;
1364
1391
  }
1365
1392
 
1366
- case 15:
1393
+ client.create(merged);
1394
+
1395
+ case 24:
1367
1396
  case "end":
1368
1397
  return _context2.stop();
1369
1398
  }
@@ -1405,9 +1434,10 @@ var fieldLevelPatch = /*#__PURE__*/function () {
1405
1434
 
1406
1435
  case 9:
1407
1436
  merged = BaseDocumentMerger.fieldLevelMerge(translatedFields, baseDoc, localeId, 'en');
1408
- client.patch(baseDoc._id).set(merged).commit();
1437
+ _context3.next = 12;
1438
+ return client.patch(baseDoc._id).set(merged).commit();
1409
1439
 
1410
- case 11:
1440
+ case 12:
1411
1441
  case "end":
1412
1442
  return _context3.stop();
1413
1443
  }
@@ -1452,48 +1482,90 @@ var defaultDocumentLevelConfig = {
1452
1482
 
1453
1483
  return exportForTranslation;
1454
1484
  }(),
1455
- importTranslation: function importTranslation(id, localeId, document) {
1456
- var deserialized = BaseDocumentDeserializer.deserializeDocument(document);
1457
- documentLevelPatch(id, deserialized, localeId);
1458
- },
1485
+ importTranslation: /*#__PURE__*/function () {
1486
+ var _importTranslation = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee2(id, localeId, document) {
1487
+ var deserialized;
1488
+ return runtime_1.wrap(function _callee2$(_context2) {
1489
+ while (1) {
1490
+ switch (_context2.prev = _context2.next) {
1491
+ case 0:
1492
+ deserialized = BaseDocumentDeserializer.deserializeDocument(document);
1493
+ _context2.next = 3;
1494
+ return documentLevelPatch(id, deserialized, localeId);
1495
+
1496
+ case 3:
1497
+ case "end":
1498
+ return _context2.stop();
1499
+ }
1500
+ }
1501
+ }, _callee2);
1502
+ }));
1503
+
1504
+ function importTranslation(_x2, _x3, _x4) {
1505
+ return _importTranslation.apply(this, arguments);
1506
+ }
1507
+
1508
+ return importTranslation;
1509
+ }(),
1459
1510
  adapter: SmartlingAdapter
1460
1511
  };
1461
1512
  var defaultFieldLevelConfig = {
1462
1513
  exportForTranslation: /*#__PURE__*/function () {
1463
- var _exportForTranslation2 = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee2(id) {
1514
+ var _exportForTranslation2 = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee3(id) {
1464
1515
  var doc, serialized;
1465
- return runtime_1.wrap(function _callee2$(_context2) {
1516
+ return runtime_1.wrap(function _callee3$(_context3) {
1466
1517
  while (1) {
1467
- switch (_context2.prev = _context2.next) {
1518
+ switch (_context3.prev = _context3.next) {
1468
1519
  case 0:
1469
- _context2.next = 2;
1520
+ _context3.next = 2;
1470
1521
  return findLatestDraft(id);
1471
1522
 
1472
1523
  case 2:
1473
- doc = _context2.sent;
1524
+ doc = _context3.sent;
1474
1525
  serialized = BaseDocumentSerializer.serializeDocument(doc, 'field'); //needed for lookup by translation tab
1475
1526
 
1476
1527
  serialized.name = id;
1477
- return _context2.abrupt("return", serialized);
1528
+ return _context3.abrupt("return", serialized);
1478
1529
 
1479
1530
  case 6:
1480
1531
  case "end":
1481
- return _context2.stop();
1532
+ return _context3.stop();
1482
1533
  }
1483
1534
  }
1484
- }, _callee2);
1535
+ }, _callee3);
1485
1536
  }));
1486
1537
 
1487
- function exportForTranslation(_x2) {
1538
+ function exportForTranslation(_x5) {
1488
1539
  return _exportForTranslation2.apply(this, arguments);
1489
1540
  }
1490
1541
 
1491
1542
  return exportForTranslation;
1492
1543
  }(),
1493
- importTranslation: function importTranslation(id, localeId, document) {
1494
- var deserialized = BaseDocumentDeserializer.deserializeDocument(document);
1495
- fieldLevelPatch(id, deserialized, localeId);
1496
- },
1544
+ importTranslation: /*#__PURE__*/function () {
1545
+ var _importTranslation2 = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee4(id, localeId, document) {
1546
+ var deserialized;
1547
+ return runtime_1.wrap(function _callee4$(_context4) {
1548
+ while (1) {
1549
+ switch (_context4.prev = _context4.next) {
1550
+ case 0:
1551
+ deserialized = BaseDocumentDeserializer.deserializeDocument(document);
1552
+ _context4.next = 3;
1553
+ return fieldLevelPatch(id, deserialized, localeId);
1554
+
1555
+ case 3:
1556
+ case "end":
1557
+ return _context4.stop();
1558
+ }
1559
+ }
1560
+ }, _callee4);
1561
+ }));
1562
+
1563
+ function importTranslation(_x6, _x7, _x8) {
1564
+ return _importTranslation2.apply(this, arguments);
1565
+ }
1566
+
1567
+ return importTranslation;
1568
+ }(),
1497
1569
  adapter: SmartlingAdapter
1498
1570
  };
1499
1571