sanity-plugin-studio-smartling 1.4.0 → 2.0.1

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,12 @@
1
1
  import { Secrets } from 'sanity-translations-tab';
2
2
  export declare const createTask: (documentId: string, document: Record<string, any>, localeIds: string[], secrets: Secrets, workflowUid?: string) => Promise<{
3
+ documentId: string;
4
+ taskId: string;
5
+ locales: any[];
6
+ linkToVendorTask?: undefined;
7
+ } | {
3
8
  documentId: string;
4
9
  locales: any[];
5
10
  taskId: string;
11
+ linkToVendorTask: string;
6
12
  }>;
@@ -1,6 +1,12 @@
1
1
  import { Secrets } from 'sanity-translations-tab';
2
2
  export declare const getTranslationTask: (documentId: string, secrets: Secrets) => Promise<{
3
+ documentId: string;
4
+ taskId: string;
5
+ locales: any[];
6
+ linkToVendorTask?: undefined;
7
+ } | {
3
8
  documentId: string;
4
9
  locales: any[];
5
10
  taskId: string;
11
+ linkToVendorTask: string;
6
12
  }>;
package/dist/index.d.ts CHANGED
@@ -1,14 +1,11 @@
1
- import { TranslationsTab } from 'sanity-translations-tab';
2
- import { BaseDocumentDeserializer, BaseDocumentSerializer, BaseDocumentMerger, defaultStopTypes, customSerializers } from 'sanity-naive-html-serializer';
1
+ import { TranslationsTab, findLatestDraft, BaseDocumentDeserializer, BaseDocumentSerializer, BaseDocumentMerger, defaultStopTypes, customSerializers, Adapter } from 'sanity-translations-tab';
3
2
  import { SmartlingAdapter } from './adapter';
4
- declare const defaultDocumentLevelConfig: {
5
- exportForTranslation: (id: string) => Promise<import("sanity-naive-html-serializer/dist/types").SerializedDocument>;
6
- importTranslation: (id: string, localeId: string, document: string) => void;
7
- adapter: import("sanity-translations-tab").Adapter;
8
- };
9
- declare const defaultFieldLevelConfig: {
10
- exportForTranslation: (id: string) => Promise<import("sanity-naive-html-serializer/dist/types").SerializedDocument>;
11
- importTranslation: (id: string, localeId: string, document: string) => void;
12
- adapter: import("sanity-translations-tab").Adapter;
13
- };
14
- export { TranslationsTab, BaseDocumentDeserializer, BaseDocumentSerializer, BaseDocumentMerger, defaultStopTypes, customSerializers, SmartlingAdapter, defaultDocumentLevelConfig, defaultFieldLevelConfig, };
3
+ interface ConfigOptions {
4
+ adapter: Adapter;
5
+ secretsNamespace: string | null;
6
+ exportForTranslation: (id: string) => Promise<Record<string, any>>;
7
+ importTranslation: (id: string, localeId: string, doc: string) => Promise<void>;
8
+ }
9
+ declare const defaultDocumentLevelConfig: ConfigOptions;
10
+ declare const defaultFieldLevelConfig: ConfigOptions;
11
+ export { TranslationsTab, findLatestDraft, BaseDocumentDeserializer, BaseDocumentSerializer, BaseDocumentMerger, defaultStopTypes, customSerializers, SmartlingAdapter, defaultDocumentLevelConfig, defaultFieldLevelConfig, };
@@ -1,10 +1,6 @@
1
1
  'use strict';
2
2
 
3
- function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
4
-
5
3
  var sanityTranslationsTab = require('sanity-translations-tab');
6
- var sanityNaiveHtmlSerializer = require('sanity-naive-html-serializer');
7
- var sanityClient = _interopDefault(require('part:@sanity/base/client'));
8
4
 
9
5
  function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
10
6
  try {
@@ -876,7 +872,16 @@ var findExistingJob = /*#__PURE__*/function () {
876
872
  return res.json();
877
873
  }).then(function (res) {
878
874
  if (res.response.data.items.length) {
879
- return res.response.data.items[0].translationJobUid;
875
+ //smartling will fuzzy match job names. We need to be precise.
876
+ var correctJob = res.response.data.items.find(function (item) {
877
+ return item.jobName && item.jobName === documentId;
878
+ });
879
+
880
+ if (correctJob) {
881
+ return correctJob.translationJobUid;
882
+ } else {
883
+ return '';
884
+ }
880
885
  } else {
881
886
  return '';
882
887
  }
@@ -932,7 +937,7 @@ var getLocales = /*#__PURE__*/function () {
932
937
 
933
938
  var getTranslationTask = /*#__PURE__*/function () {
934
939
  var _ref = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee(documentId, secrets) {
935
- var accessToken, taskId, url, smartlingTask, locales;
940
+ var accessToken, taskId, projectId, progressUrl, smartlingTask, locales;
936
941
  return runtime_1.wrap(function _callee$(_context) {
937
942
  while (1) {
938
943
  switch (_context.prev = _context.next) {
@@ -960,18 +965,19 @@ var getTranslationTask = /*#__PURE__*/function () {
960
965
  });
961
966
 
962
967
  case 8:
963
- url = "https://api.smartling.com/jobs-api/v3/projects/" + secrets.project + "/jobs/" + taskId + "/progress";
964
- _context.next = 11;
968
+ projectId = secrets.project;
969
+ progressUrl = "https://api.smartling.com/jobs-api/v3/projects/" + projectId + "/jobs/" + taskId + "/progress";
970
+ _context.next = 12;
965
971
  return fetch(smartlingProxy, {
966
972
  method: 'POST',
967
- headers: getHeaders(url, accessToken)
973
+ headers: getHeaders(progressUrl, accessToken)
968
974
  }).then(function (res) {
969
975
  return res.json();
970
976
  }).then(function (res) {
971
977
  return res.response.data;
972
978
  });
973
979
 
974
- case 11:
980
+ case 12:
975
981
  smartlingTask = _context.sent;
976
982
  locales = [];
977
983
 
@@ -988,10 +994,11 @@ var getTranslationTask = /*#__PURE__*/function () {
988
994
  documentId: documentId,
989
995
  locales: locales,
990
996
  //since our download is tied to document id for smartling, keep track of it as a task
991
- taskId: documentId
997
+ taskId: documentId,
998
+ linkToVendorTask: "https://dashboard.smartling.com/app/projects/" + projectId + "/account-jobs/" + projectId + ":" + taskId
992
999
  });
993
1000
 
994
- case 15:
1001
+ case 16:
995
1002
  case "end":
996
1003
  return _context.stop();
997
1004
  }
@@ -1207,7 +1214,14 @@ var getTranslation = /*#__PURE__*/function () {
1207
1214
  }).then(function (res) {
1208
1215
  return res.json();
1209
1216
  }).then(function (res) {
1210
- return res.body;
1217
+ if (res.body) {
1218
+ return res.body;
1219
+ } else if (res.response.errors) {
1220
+ var _res$response$errors$;
1221
+
1222
+ var errMsg = ((_res$response$errors$ = res.response.errors[0]) == null ? void 0 : _res$response$errors$.message) || 'Error retrieving translation from Smartling';
1223
+ throw new Error(errMsg);
1224
+ }
1211
1225
  });
1212
1226
 
1213
1227
  case 6:
@@ -1234,317 +1248,54 @@ var SmartlingAdapter = {
1234
1248
  getTranslation: getTranslation
1235
1249
  };
1236
1250
 
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
-
1437
- var defaultDocumentLevelConfig = {
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
- }(),
1469
- importTranslation: function importTranslation(id, localeId, document) {
1470
- var deserialized = sanityNaiveHtmlSerializer.BaseDocumentDeserializer.deserializeDocument(document);
1471
- documentLevelPatch(id, deserialized, localeId);
1472
- },
1251
+ var defaultDocumentLevelConfig = /*#__PURE__*/_extends({}, sanityTranslationsTab.baseDocumentLevelConfig, {
1473
1252
  adapter: SmartlingAdapter
1474
- };
1475
- var defaultFieldLevelConfig = {
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
- }
1253
+ });
1504
1254
 
1505
- return exportForTranslation;
1506
- }(),
1507
- importTranslation: function importTranslation(id, localeId, document) {
1508
- var deserialized = sanityNaiveHtmlSerializer.BaseDocumentDeserializer.deserializeDocument(document);
1509
- fieldLevelPatch(id, deserialized, localeId);
1510
- },
1255
+ var defaultFieldLevelConfig = /*#__PURE__*/_extends({}, sanityTranslationsTab.baseFieldLevelConfig, {
1511
1256
  adapter: SmartlingAdapter
1512
- };
1257
+ });
1513
1258
 
1514
- Object.defineProperty(exports, 'TranslationsTab', {
1259
+ Object.defineProperty(exports, 'BaseDocumentDeserializer', {
1515
1260
  enumerable: true,
1516
1261
  get: function () {
1517
- return sanityTranslationsTab.TranslationsTab;
1262
+ return sanityTranslationsTab.BaseDocumentDeserializer;
1518
1263
  }
1519
1264
  });
1520
- Object.defineProperty(exports, 'BaseDocumentDeserializer', {
1265
+ Object.defineProperty(exports, 'BaseDocumentMerger', {
1521
1266
  enumerable: true,
1522
1267
  get: function () {
1523
- return sanityNaiveHtmlSerializer.BaseDocumentDeserializer;
1268
+ return sanityTranslationsTab.BaseDocumentMerger;
1524
1269
  }
1525
1270
  });
1526
- Object.defineProperty(exports, 'BaseDocumentMerger', {
1271
+ Object.defineProperty(exports, 'BaseDocumentSerializer', {
1527
1272
  enumerable: true,
1528
1273
  get: function () {
1529
- return sanityNaiveHtmlSerializer.BaseDocumentMerger;
1274
+ return sanityTranslationsTab.BaseDocumentSerializer;
1530
1275
  }
1531
1276
  });
1532
- Object.defineProperty(exports, 'BaseDocumentSerializer', {
1277
+ Object.defineProperty(exports, 'TranslationsTab', {
1533
1278
  enumerable: true,
1534
1279
  get: function () {
1535
- return sanityNaiveHtmlSerializer.BaseDocumentSerializer;
1280
+ return sanityTranslationsTab.TranslationsTab;
1536
1281
  }
1537
1282
  });
1538
1283
  Object.defineProperty(exports, 'customSerializers', {
1539
1284
  enumerable: true,
1540
1285
  get: function () {
1541
- return sanityNaiveHtmlSerializer.customSerializers;
1286
+ return sanityTranslationsTab.customSerializers;
1542
1287
  }
1543
1288
  });
1544
1289
  Object.defineProperty(exports, 'defaultStopTypes', {
1545
1290
  enumerable: true,
1546
1291
  get: function () {
1547
- return sanityNaiveHtmlSerializer.defaultStopTypes;
1292
+ return sanityTranslationsTab.defaultStopTypes;
1293
+ }
1294
+ });
1295
+ Object.defineProperty(exports, 'findLatestDraft', {
1296
+ enumerable: true,
1297
+ get: function () {
1298
+ return sanityTranslationsTab.findLatestDraft;
1548
1299
  }
1549
1300
  });
1550
1301
  exports.SmartlingAdapter = SmartlingAdapter;