sanity-plugin-studio-smartling 1.1.6 → 1.4.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
  import { Secrets } from 'sanity-translations-tab';
2
- export declare const createTask: (documentId: string, document: Record<string, any>, localeIds: string[], secrets: Secrets) => Promise<{
2
+ export declare const createTask: (documentId: string, document: Record<string, any>, localeIds: string[], secrets: Secrets, workflowUid?: string) => Promise<{
3
3
  documentId: string;
4
4
  locales: any[];
5
5
  taskId: string;
@@ -0,0 +1,5 @@
1
+ import { SanityDocument } from '@sanity/types';
2
+ export declare const findLatestDraft: (documentId: string, ignoreI18n?: boolean) => any;
3
+ export declare const findDocumentAtRevision: (documentId: string, rev: string) => Promise<any>;
4
+ export declare const documentLevelPatch: (documentId: string, translatedFields: SanityDocument, localeId: string) => Promise<void>;
5
+ export declare const fieldLevelPatch: (documentId: string, translatedFields: SanityDocument, localeId: string) => Promise<void>;
package/dist/index.d.ts CHANGED
@@ -1,14 +1,14 @@
1
1
  import { TranslationsTab } from 'sanity-translations-tab';
2
- import { BaseDocumentDeserializer, BaseDocumentSerializer, BaseDocumentPatcher, defaultStopTypes, customSerializers } from 'sanity-naive-html-serializer';
2
+ import { BaseDocumentDeserializer, BaseDocumentSerializer, BaseDocumentMerger, defaultStopTypes, customSerializers } from 'sanity-naive-html-serializer';
3
3
  import { SmartlingAdapter } from './adapter';
4
4
  declare const defaultDocumentLevelConfig: {
5
5
  exportForTranslation: (id: string) => Promise<import("sanity-naive-html-serializer/dist/types").SerializedDocument>;
6
- importTranslation: (id: string, localeId: string, document: string) => any;
6
+ importTranslation: (id: string, localeId: string, document: string) => void;
7
7
  adapter: import("sanity-translations-tab").Adapter;
8
8
  };
9
9
  declare const defaultFieldLevelConfig: {
10
10
  exportForTranslation: (id: string) => Promise<import("sanity-naive-html-serializer/dist/types").SerializedDocument>;
11
- importTranslation: (id: string, localeId: string, document: string) => any;
11
+ importTranslation: (id: string, localeId: string, document: string) => void;
12
12
  adapter: import("sanity-translations-tab").Adapter;
13
13
  };
14
- export { TranslationsTab, BaseDocumentDeserializer, BaseDocumentSerializer, BaseDocumentPatcher, defaultStopTypes, customSerializers, SmartlingAdapter, defaultDocumentLevelConfig, defaultFieldLevelConfig, };
14
+ export { TranslationsTab, BaseDocumentDeserializer, BaseDocumentSerializer, BaseDocumentMerger, defaultStopTypes, customSerializers, SmartlingAdapter, defaultDocumentLevelConfig, defaultFieldLevelConfig, };
@@ -1,7 +1,10 @@
1
1
  'use strict';
2
2
 
3
+ function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
4
+
3
5
  var sanityTranslationsTab = require('sanity-translations-tab');
4
6
  var sanityNaiveHtmlSerializer = require('sanity-naive-html-serializer');
7
+ var sanityClient = _interopDefault(require('part:@sanity/base/client'));
5
8
 
6
9
  function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
7
10
  try {
@@ -976,7 +979,7 @@ var getTranslationTask = /*#__PURE__*/function () {
976
979
  locales = smartlingTask.contentProgressReport.map(function (item) {
977
980
  return {
978
981
  localeId: item.targetLocaleId,
979
- progress: item.progress.percentComplete
982
+ progress: item.progress ? item.progress.percentComplete : 0
980
983
  };
981
984
  });
982
985
  }
@@ -1044,30 +1047,41 @@ var createJob = /*#__PURE__*/function () {
1044
1047
 
1045
1048
 
1046
1049
  var createJobBatch = /*#__PURE__*/function () {
1047
- var _ref2 = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee2(jobId, projectId, documentName, accessToken) {
1048
- var url;
1050
+ var _ref2 = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee2(jobId, projectId, documentName, accessToken, localeIds, workflowUid) {
1051
+ var url, reqBody;
1049
1052
  return runtime_1.wrap(function _callee2$(_context2) {
1050
1053
  while (1) {
1051
1054
  switch (_context2.prev = _context2.next) {
1052
1055
  case 0:
1053
1056
  url = "https://api.smartling.com/job-batches-api/v2/projects/" + projectId + "/batches";
1057
+ reqBody = {
1058
+ authorize: true,
1059
+ translationJobUid: jobId,
1060
+ fileUris: [documentName]
1061
+ };
1062
+
1063
+ if (workflowUid) {
1064
+ reqBody.localeWorkflows = localeIds.map(function (l) {
1065
+ return {
1066
+ targetLocaleId: l,
1067
+ workflowUid: workflowUid
1068
+ };
1069
+ });
1070
+ }
1071
+
1054
1072
  return _context2.abrupt("return", fetch(smartlingProxy, {
1055
1073
  method: 'POST',
1056
1074
  headers: _extends({}, getHeaders(url, accessToken), {
1057
1075
  'content-type': 'application/json'
1058
1076
  }),
1059
- body: JSON.stringify({
1060
- authorize: true,
1061
- translationJobUid: jobId,
1062
- fileUris: [documentName]
1063
- })
1077
+ body: JSON.stringify(reqBody)
1064
1078
  }).then(function (res) {
1065
1079
  return res.json();
1066
1080
  }).then(function (res) {
1067
1081
  return res.response.data.batchUid;
1068
1082
  }));
1069
1083
 
1070
- case 2:
1084
+ case 4:
1071
1085
  case "end":
1072
1086
  return _context2.stop();
1073
1087
  }
@@ -1075,7 +1089,7 @@ var createJobBatch = /*#__PURE__*/function () {
1075
1089
  }, _callee2);
1076
1090
  }));
1077
1091
 
1078
- return function createJobBatch(_x5, _x6, _x7, _x8) {
1092
+ return function createJobBatch(_x5, _x6, _x7, _x8, _x9, _x10) {
1079
1093
  return _ref2.apply(this, arguments);
1080
1094
  };
1081
1095
  }();
@@ -1112,13 +1126,13 @@ var uploadFileToBatch = /*#__PURE__*/function () {
1112
1126
  }, _callee3);
1113
1127
  }));
1114
1128
 
1115
- return function uploadFileToBatch(_x9, _x10, _x11, _x12, _x13) {
1129
+ return function uploadFileToBatch(_x11, _x12, _x13, _x14, _x15) {
1116
1130
  return _ref3.apply(this, arguments);
1117
1131
  };
1118
1132
  }();
1119
1133
 
1120
1134
  var createTask = /*#__PURE__*/function () {
1121
- var _ref4 = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee4(documentId, document, localeIds, secrets) {
1135
+ var _ref4 = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee4(documentId, document, localeIds, secrets, workflowUid) {
1122
1136
  var accessToken, taskId, batchUid, uploadFileRes;
1123
1137
  return runtime_1.wrap(function _callee4$(_context4) {
1124
1138
  while (1) {
@@ -1148,7 +1162,7 @@ var createTask = /*#__PURE__*/function () {
1148
1162
 
1149
1163
  case 10:
1150
1164
  _context4.next = 12;
1151
- return createJobBatch(taskId, secrets.project, document.name, accessToken);
1165
+ return createJobBatch(taskId, secrets.project, document.name, accessToken, localeIds, workflowUid);
1152
1166
 
1153
1167
  case 12:
1154
1168
  batchUid = _context4.sent;
@@ -1168,7 +1182,7 @@ var createTask = /*#__PURE__*/function () {
1168
1182
  }, _callee4);
1169
1183
  }));
1170
1184
 
1171
- return function createTask(_x14, _x15, _x16, _x17) {
1185
+ return function createTask(_x16, _x17, _x18, _x19, _x20) {
1172
1186
  return _ref4.apply(this, arguments);
1173
1187
  };
1174
1188
  }();
@@ -1220,25 +1234,279 @@ var SmartlingAdapter = {
1220
1234
  getTranslation: getTranslation
1221
1235
  };
1222
1236
 
1237
+ var client = /*#__PURE__*/sanityClient.withConfig({
1238
+ apiVersion: '2021-03-25'
1239
+ }); //document fetch
1240
+
1241
+ var findLatestDraft = function findLatestDraft(documentId, ignoreI18n) {
1242
+ if (ignoreI18n === void 0) {
1243
+ ignoreI18n = true;
1244
+ }
1245
+
1246
+ //eliminates i18n versions
1247
+ var query = "*[_id match $id " + (ignoreI18n ? ' && (_id in path("drafts.*") || _id in path("*"))' : '') + "]";
1248
+ var params = {
1249
+ id: "*" + documentId
1250
+ };
1251
+ return client.fetch(query, params).then(function (docs) {
1252
+ var _docs$find;
1253
+
1254
+ return (_docs$find = docs.find(function (doc) {
1255
+ return doc._id.includes('draft');
1256
+ })) != null ? _docs$find : docs[0];
1257
+ });
1258
+ }; //revision fetch
1259
+
1260
+ var findDocumentAtRevision = /*#__PURE__*/function () {
1261
+ var _ref = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee(documentId, rev) {
1262
+ var dataset, baseUrl, url, revisionDoc;
1263
+ return runtime_1.wrap(function _callee$(_context) {
1264
+ while (1) {
1265
+ switch (_context.prev = _context.next) {
1266
+ case 0:
1267
+ dataset = client.config().dataset;
1268
+ baseUrl = "/data/history/" + dataset + "/documents/" + documentId + "?revision=" + rev;
1269
+ url = client.getUrl(baseUrl);
1270
+ _context.next = 5;
1271
+ return fetch(url, {
1272
+ credentials: 'include'
1273
+ }).then(function (req) {
1274
+ return req.json();
1275
+ }).then(function (req) {
1276
+ return req.documents && req.documents[0];
1277
+ });
1278
+
1279
+ case 5:
1280
+ revisionDoc = _context.sent;
1281
+
1282
+ if (!(!revisionDoc || revisionDoc._rev !== rev)) {
1283
+ _context.next = 12;
1284
+ break;
1285
+ }
1286
+
1287
+ baseUrl = "/data/history/" + dataset + "/documents/drafts." + documentId + "?revision=" + rev;
1288
+ url = client.getUrl(baseUrl);
1289
+ _context.next = 11;
1290
+ return fetch(url, {
1291
+ credentials: 'include'
1292
+ }).then(function (req) {
1293
+ return req.json();
1294
+ }).then(function (req) {
1295
+ return req.documents[0];
1296
+ });
1297
+
1298
+ case 11:
1299
+ revisionDoc = _context.sent;
1300
+
1301
+ case 12:
1302
+ return _context.abrupt("return", revisionDoc);
1303
+
1304
+ case 13:
1305
+ case "end":
1306
+ return _context.stop();
1307
+ }
1308
+ }
1309
+ }, _callee);
1310
+ }));
1311
+
1312
+ return function findDocumentAtRevision(_x, _x2) {
1313
+ return _ref.apply(this, arguments);
1314
+ };
1315
+ }(); //document-level patch
1316
+
1317
+ var documentLevelPatch = /*#__PURE__*/function () {
1318
+ var _ref2 = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee2(documentId, translatedFields, localeId) {
1319
+ var baseDoc, merged, targetId, i18nDoc, cleanedMerge;
1320
+ return runtime_1.wrap(function _callee2$(_context2) {
1321
+ while (1) {
1322
+ switch (_context2.prev = _context2.next) {
1323
+ case 0:
1324
+ if (!translatedFields._rev) {
1325
+ _context2.next = 6;
1326
+ break;
1327
+ }
1328
+
1329
+ _context2.next = 3;
1330
+ return findDocumentAtRevision(documentId, translatedFields._rev);
1331
+
1332
+ case 3:
1333
+ baseDoc = _context2.sent;
1334
+ _context2.next = 9;
1335
+ break;
1336
+
1337
+ case 6:
1338
+ _context2.next = 8;
1339
+ return findLatestDraft(documentId);
1340
+
1341
+ case 8:
1342
+ baseDoc = _context2.sent;
1343
+
1344
+ case 9:
1345
+ merged = sanityNaiveHtmlSerializer.BaseDocumentMerger.documentLevelMerge(translatedFields, baseDoc);
1346
+ targetId = "i18n." + documentId + "." + localeId;
1347
+ _context2.next = 13;
1348
+ return findLatestDraft(targetId, false);
1349
+
1350
+ case 13:
1351
+ i18nDoc = _context2.sent;
1352
+
1353
+ if (i18nDoc) {
1354
+ cleanedMerge = {}; //don't overwrite any existing values on the i18n doc
1355
+
1356
+ Object.entries(merged).forEach(function (_ref3) {
1357
+ var key = _ref3[0],
1358
+ value = _ref3[1];
1359
+
1360
+ if (Object.keys(translatedFields).includes(key) && !['_id', '_rev', '_updatedAt'].includes(key)) {
1361
+ cleanedMerge[key] = value;
1362
+ }
1363
+ });
1364
+ client.transaction() //@ts-ignore
1365
+ .patch(i18nDoc._id, function (p) {
1366
+ return p.set(cleanedMerge);
1367
+ }).commit();
1368
+ } else {
1369
+ merged._id = "drafts." + targetId; //account for legacy implementations of i18n plugin lang
1370
+
1371
+ if (baseDoc._lang) {
1372
+ merged._lang = localeId;
1373
+ } else if (baseDoc.__i18n_lang) {
1374
+ merged.__i18n_lang = localeId;
1375
+ }
1376
+
1377
+ client.create(merged);
1378
+ }
1379
+
1380
+ case 15:
1381
+ case "end":
1382
+ return _context2.stop();
1383
+ }
1384
+ }
1385
+ }, _callee2);
1386
+ }));
1387
+
1388
+ return function documentLevelPatch(_x3, _x4, _x5) {
1389
+ return _ref2.apply(this, arguments);
1390
+ };
1391
+ }(); //field level patch
1392
+
1393
+ var fieldLevelPatch = /*#__PURE__*/function () {
1394
+ var _ref4 = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee3(documentId, translatedFields, localeId) {
1395
+ var baseDoc, merged;
1396
+ return runtime_1.wrap(function _callee3$(_context3) {
1397
+ while (1) {
1398
+ switch (_context3.prev = _context3.next) {
1399
+ case 0:
1400
+ if (!translatedFields._rev) {
1401
+ _context3.next = 6;
1402
+ break;
1403
+ }
1404
+
1405
+ _context3.next = 3;
1406
+ return findDocumentAtRevision(documentId, translatedFields._rev);
1407
+
1408
+ case 3:
1409
+ baseDoc = _context3.sent;
1410
+ _context3.next = 9;
1411
+ break;
1412
+
1413
+ case 6:
1414
+ _context3.next = 8;
1415
+ return findLatestDraft(documentId);
1416
+
1417
+ case 8:
1418
+ baseDoc = _context3.sent;
1419
+
1420
+ case 9:
1421
+ merged = sanityNaiveHtmlSerializer.BaseDocumentMerger.fieldLevelMerge(translatedFields, baseDoc, localeId, 'en');
1422
+ client.patch(baseDoc._id).set(merged).commit();
1423
+
1424
+ case 11:
1425
+ case "end":
1426
+ return _context3.stop();
1427
+ }
1428
+ }
1429
+ }, _callee3);
1430
+ }));
1431
+
1432
+ return function fieldLevelPatch(_x6, _x7, _x8) {
1433
+ return _ref4.apply(this, arguments);
1434
+ };
1435
+ }();
1436
+
1223
1437
  var defaultDocumentLevelConfig = {
1224
- exportForTranslation: function exportForTranslation(id) {
1225
- return sanityNaiveHtmlSerializer.BaseDocumentSerializer.serializeDocument(id, 'document', 'en', sanityNaiveHtmlSerializer.defaultStopTypes, sanityNaiveHtmlSerializer.customSerializers);
1226
- },
1438
+ exportForTranslation: /*#__PURE__*/function () {
1439
+ var _exportForTranslation = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee(id) {
1440
+ var doc, serialized;
1441
+ return runtime_1.wrap(function _callee$(_context) {
1442
+ while (1) {
1443
+ switch (_context.prev = _context.next) {
1444
+ case 0:
1445
+ _context.next = 2;
1446
+ return findLatestDraft(id);
1447
+
1448
+ case 2:
1449
+ doc = _context.sent;
1450
+ serialized = sanityNaiveHtmlSerializer.BaseDocumentSerializer.serializeDocument(doc, 'document'); //needed for lookup by translation tab
1451
+
1452
+ serialized.name = id;
1453
+ return _context.abrupt("return", serialized);
1454
+
1455
+ case 6:
1456
+ case "end":
1457
+ return _context.stop();
1458
+ }
1459
+ }
1460
+ }, _callee);
1461
+ }));
1462
+
1463
+ function exportForTranslation(_x) {
1464
+ return _exportForTranslation.apply(this, arguments);
1465
+ }
1466
+
1467
+ return exportForTranslation;
1468
+ }(),
1227
1469
  importTranslation: function importTranslation(id, localeId, document) {
1228
- return sanityNaiveHtmlSerializer.BaseDocumentDeserializer.deserializeDocument(id, document).then(function (deserialized) {
1229
- return sanityNaiveHtmlSerializer.BaseDocumentPatcher.documentLevelPatch(deserialized, id, localeId);
1230
- });
1470
+ var deserialized = sanityNaiveHtmlSerializer.BaseDocumentDeserializer.deserializeDocument(document);
1471
+ documentLevelPatch(id, deserialized, localeId);
1231
1472
  },
1232
1473
  adapter: SmartlingAdapter
1233
1474
  };
1234
1475
  var defaultFieldLevelConfig = {
1235
- exportForTranslation: function exportForTranslation(id) {
1236
- return sanityNaiveHtmlSerializer.BaseDocumentSerializer.serializeDocument(id, 'field', 'en', sanityNaiveHtmlSerializer.defaultStopTypes, sanityNaiveHtmlSerializer.customSerializers);
1237
- },
1476
+ exportForTranslation: /*#__PURE__*/function () {
1477
+ var _exportForTranslation2 = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee2(id) {
1478
+ var doc, serialized;
1479
+ return runtime_1.wrap(function _callee2$(_context2) {
1480
+ while (1) {
1481
+ switch (_context2.prev = _context2.next) {
1482
+ case 0:
1483
+ _context2.next = 2;
1484
+ return findLatestDraft(id);
1485
+
1486
+ case 2:
1487
+ doc = _context2.sent;
1488
+ serialized = sanityNaiveHtmlSerializer.BaseDocumentSerializer.serializeDocument(doc, 'field'); //needed for lookup by translation tab
1489
+
1490
+ serialized.name = id;
1491
+ return _context2.abrupt("return", serialized);
1492
+
1493
+ case 6:
1494
+ case "end":
1495
+ return _context2.stop();
1496
+ }
1497
+ }
1498
+ }, _callee2);
1499
+ }));
1500
+
1501
+ function exportForTranslation(_x2) {
1502
+ return _exportForTranslation2.apply(this, arguments);
1503
+ }
1504
+
1505
+ return exportForTranslation;
1506
+ }(),
1238
1507
  importTranslation: function importTranslation(id, localeId, document) {
1239
- return sanityNaiveHtmlSerializer.BaseDocumentDeserializer.deserializeDocument(id, document).then(function (deserialized) {
1240
- return sanityNaiveHtmlSerializer.BaseDocumentPatcher.fieldLevelPatch(deserialized, id, localeId, 'en');
1241
- });
1508
+ var deserialized = sanityNaiveHtmlSerializer.BaseDocumentDeserializer.deserializeDocument(document);
1509
+ fieldLevelPatch(id, deserialized, localeId);
1242
1510
  },
1243
1511
  adapter: SmartlingAdapter
1244
1512
  };
@@ -1255,10 +1523,10 @@ Object.defineProperty(exports, 'BaseDocumentDeserializer', {
1255
1523
  return sanityNaiveHtmlSerializer.BaseDocumentDeserializer;
1256
1524
  }
1257
1525
  });
1258
- Object.defineProperty(exports, 'BaseDocumentPatcher', {
1526
+ Object.defineProperty(exports, 'BaseDocumentMerger', {
1259
1527
  enumerable: true,
1260
1528
  get: function () {
1261
- return sanityNaiveHtmlSerializer.BaseDocumentPatcher;
1529
+ return sanityNaiveHtmlSerializer.BaseDocumentMerger;
1262
1530
  }
1263
1531
  });
1264
1532
  Object.defineProperty(exports, 'BaseDocumentSerializer', {