sanity-plugin-studio-smartling 1.5.0 → 2.0.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.
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) => Promise<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) => Promise<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 {
@@ -1243,369 +1239,54 @@ var SmartlingAdapter = {
1243
1239
  getTranslation: getTranslation
1244
1240
  };
1245
1241
 
1246
- var client = /*#__PURE__*/sanityClient.withConfig({
1247
- apiVersion: '2021-03-25'
1248
- }); //document fetch
1249
-
1250
- var findLatestDraft = function findLatestDraft(documentId, ignoreI18n) {
1251
- if (ignoreI18n === void 0) {
1252
- ignoreI18n = true;
1253
- }
1254
-
1255
- //eliminates i18n versions
1256
- var query = "*[_id match $id " + (ignoreI18n ? ' && (_id in path("drafts.*") || _id in path("*"))' : '') + "]";
1257
- var params = {
1258
- id: "*" + documentId
1259
- };
1260
- return client.fetch(query, params).then(function (docs) {
1261
- var _docs$find;
1262
-
1263
- return (_docs$find = docs.find(function (doc) {
1264
- return doc._id.includes('draft');
1265
- })) != null ? _docs$find : docs[0];
1266
- });
1267
- }; //revision fetch
1268
-
1269
- var findDocumentAtRevision = /*#__PURE__*/function () {
1270
- var _ref = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee(documentId, rev) {
1271
- var dataset, baseUrl, url, revisionDoc;
1272
- return runtime_1.wrap(function _callee$(_context) {
1273
- while (1) {
1274
- switch (_context.prev = _context.next) {
1275
- case 0:
1276
- dataset = client.config().dataset;
1277
- baseUrl = "/data/history/" + dataset + "/documents/" + documentId + "?revision=" + rev;
1278
- url = client.getUrl(baseUrl);
1279
- _context.next = 5;
1280
- return fetch(url, {
1281
- credentials: 'include'
1282
- }).then(function (req) {
1283
- return req.json();
1284
- }).then(function (req) {
1285
- return req.documents && req.documents[0];
1286
- });
1287
-
1288
- case 5:
1289
- revisionDoc = _context.sent;
1290
-
1291
- if (!(!revisionDoc || revisionDoc._rev !== rev)) {
1292
- _context.next = 12;
1293
- break;
1294
- }
1295
-
1296
- baseUrl = "/data/history/" + dataset + "/documents/drafts." + documentId + "?revision=" + rev;
1297
- url = client.getUrl(baseUrl);
1298
- _context.next = 11;
1299
- return fetch(url, {
1300
- credentials: 'include'
1301
- }).then(function (req) {
1302
- return req.json();
1303
- }).then(function (req) {
1304
- return req.documents[0];
1305
- });
1306
-
1307
- case 11:
1308
- revisionDoc = _context.sent;
1309
-
1310
- case 12:
1311
- return _context.abrupt("return", revisionDoc);
1312
-
1313
- case 13:
1314
- case "end":
1315
- return _context.stop();
1316
- }
1317
- }
1318
- }, _callee);
1319
- }));
1320
-
1321
- return function findDocumentAtRevision(_x, _x2) {
1322
- return _ref.apply(this, arguments);
1323
- };
1324
- }(); //document-level patch
1325
-
1326
- var documentLevelPatch = /*#__PURE__*/function () {
1327
- var _ref2 = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee2(documentId, translatedFields, localeId) {
1328
- var baseDoc, merged, targetId, i18nDoc, cleanedMerge;
1329
- return runtime_1.wrap(function _callee2$(_context2) {
1330
- while (1) {
1331
- switch (_context2.prev = _context2.next) {
1332
- case 0:
1333
- if (!translatedFields._rev) {
1334
- _context2.next = 6;
1335
- break;
1336
- }
1337
-
1338
- _context2.next = 3;
1339
- return findDocumentAtRevision(documentId, translatedFields._rev);
1340
-
1341
- case 3:
1342
- baseDoc = _context2.sent;
1343
- _context2.next = 9;
1344
- break;
1345
-
1346
- case 6:
1347
- _context2.next = 8;
1348
- return findLatestDraft(documentId);
1349
-
1350
- case 8:
1351
- baseDoc = _context2.sent;
1352
-
1353
- case 9:
1354
- merged = sanityNaiveHtmlSerializer.BaseDocumentMerger.documentLevelMerge(translatedFields, baseDoc);
1355
- targetId = "i18n." + documentId + "." + localeId;
1356
- _context2.next = 13;
1357
- return findLatestDraft(targetId, false);
1358
-
1359
- case 13:
1360
- i18nDoc = _context2.sent;
1361
-
1362
- if (!i18nDoc) {
1363
- _context2.next = 21;
1364
- break;
1365
- }
1366
-
1367
- cleanedMerge = {}; //don't overwrite any existing values on the i18n doc
1368
-
1369
- Object.entries(merged).forEach(function (_ref3) {
1370
- var key = _ref3[0],
1371
- value = _ref3[1];
1372
-
1373
- if (Object.keys(translatedFields).includes(key) && !['_id', '_rev', '_updatedAt'].includes(key)) {
1374
- cleanedMerge[key] = value;
1375
- }
1376
- });
1377
- _context2.next = 19;
1378
- return client.transaction() //@ts-ignore
1379
- .patch(i18nDoc._id, function (p) {
1380
- return p.set(cleanedMerge);
1381
- }).commit();
1382
-
1383
- case 19:
1384
- _context2.next = 24;
1385
- break;
1386
-
1387
- case 21:
1388
- merged._id = "drafts." + targetId; //account for legacy implementations of i18n plugin lang
1389
-
1390
- if (baseDoc._lang) {
1391
- merged._lang = localeId;
1392
- } else if (baseDoc.__i18n_lang) {
1393
- merged.__i18n_lang = localeId;
1394
- }
1395
-
1396
- client.create(merged);
1397
-
1398
- case 24:
1399
- case "end":
1400
- return _context2.stop();
1401
- }
1402
- }
1403
- }, _callee2);
1404
- }));
1405
-
1406
- return function documentLevelPatch(_x3, _x4, _x5) {
1407
- return _ref2.apply(this, arguments);
1408
- };
1409
- }(); //field level patch
1410
-
1411
- var fieldLevelPatch = /*#__PURE__*/function () {
1412
- var _ref4 = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee3(documentId, translatedFields, localeId) {
1413
- var baseDoc, merged;
1414
- return runtime_1.wrap(function _callee3$(_context3) {
1415
- while (1) {
1416
- switch (_context3.prev = _context3.next) {
1417
- case 0:
1418
- if (!translatedFields._rev) {
1419
- _context3.next = 6;
1420
- break;
1421
- }
1422
-
1423
- _context3.next = 3;
1424
- return findDocumentAtRevision(documentId, translatedFields._rev);
1425
-
1426
- case 3:
1427
- baseDoc = _context3.sent;
1428
- _context3.next = 9;
1429
- break;
1430
-
1431
- case 6:
1432
- _context3.next = 8;
1433
- return findLatestDraft(documentId);
1434
-
1435
- case 8:
1436
- baseDoc = _context3.sent;
1437
-
1438
- case 9:
1439
- merged = sanityNaiveHtmlSerializer.BaseDocumentMerger.fieldLevelMerge(translatedFields, baseDoc, localeId, 'en');
1440
- _context3.next = 12;
1441
- return client.patch(baseDoc._id).set(merged).commit();
1442
-
1443
- case 12:
1444
- case "end":
1445
- return _context3.stop();
1446
- }
1447
- }
1448
- }, _callee3);
1449
- }));
1450
-
1451
- return function fieldLevelPatch(_x6, _x7, _x8) {
1452
- return _ref4.apply(this, arguments);
1453
- };
1454
- }();
1455
-
1456
- var defaultDocumentLevelConfig = {
1457
- exportForTranslation: /*#__PURE__*/function () {
1458
- var _exportForTranslation = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee(id) {
1459
- var doc, serialized;
1460
- return runtime_1.wrap(function _callee$(_context) {
1461
- while (1) {
1462
- switch (_context.prev = _context.next) {
1463
- case 0:
1464
- _context.next = 2;
1465
- return findLatestDraft(id);
1466
-
1467
- case 2:
1468
- doc = _context.sent;
1469
- serialized = sanityNaiveHtmlSerializer.BaseDocumentSerializer.serializeDocument(doc, 'document'); //needed for lookup by translation tab
1470
-
1471
- serialized.name = id;
1472
- return _context.abrupt("return", serialized);
1473
-
1474
- case 6:
1475
- case "end":
1476
- return _context.stop();
1477
- }
1478
- }
1479
- }, _callee);
1480
- }));
1481
-
1482
- function exportForTranslation(_x) {
1483
- return _exportForTranslation.apply(this, arguments);
1484
- }
1485
-
1486
- return exportForTranslation;
1487
- }(),
1488
- importTranslation: /*#__PURE__*/function () {
1489
- var _importTranslation = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee2(id, localeId, document) {
1490
- var deserialized;
1491
- return runtime_1.wrap(function _callee2$(_context2) {
1492
- while (1) {
1493
- switch (_context2.prev = _context2.next) {
1494
- case 0:
1495
- deserialized = sanityNaiveHtmlSerializer.BaseDocumentDeserializer.deserializeDocument(document);
1496
- _context2.next = 3;
1497
- return documentLevelPatch(id, deserialized, localeId);
1498
-
1499
- case 3:
1500
- case "end":
1501
- return _context2.stop();
1502
- }
1503
- }
1504
- }, _callee2);
1505
- }));
1506
-
1507
- function importTranslation(_x2, _x3, _x4) {
1508
- return _importTranslation.apply(this, arguments);
1509
- }
1510
-
1511
- return importTranslation;
1512
- }(),
1242
+ var defaultDocumentLevelConfig = /*#__PURE__*/_extends({}, sanityTranslationsTab.baseDocumentLevelConfig, {
1513
1243
  adapter: SmartlingAdapter
1514
- };
1515
- var defaultFieldLevelConfig = {
1516
- exportForTranslation: /*#__PURE__*/function () {
1517
- var _exportForTranslation2 = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee3(id) {
1518
- var doc, serialized;
1519
- return runtime_1.wrap(function _callee3$(_context3) {
1520
- while (1) {
1521
- switch (_context3.prev = _context3.next) {
1522
- case 0:
1523
- _context3.next = 2;
1524
- return findLatestDraft(id);
1525
-
1526
- case 2:
1527
- doc = _context3.sent;
1528
- serialized = sanityNaiveHtmlSerializer.BaseDocumentSerializer.serializeDocument(doc, 'field'); //needed for lookup by translation tab
1529
-
1530
- serialized.name = id;
1531
- return _context3.abrupt("return", serialized);
1532
-
1533
- case 6:
1534
- case "end":
1535
- return _context3.stop();
1536
- }
1537
- }
1538
- }, _callee3);
1539
- }));
1540
-
1541
- function exportForTranslation(_x5) {
1542
- return _exportForTranslation2.apply(this, arguments);
1543
- }
1544
-
1545
- return exportForTranslation;
1546
- }(),
1547
- importTranslation: /*#__PURE__*/function () {
1548
- var _importTranslation2 = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee4(id, localeId, document) {
1549
- var deserialized;
1550
- return runtime_1.wrap(function _callee4$(_context4) {
1551
- while (1) {
1552
- switch (_context4.prev = _context4.next) {
1553
- case 0:
1554
- deserialized = sanityNaiveHtmlSerializer.BaseDocumentDeserializer.deserializeDocument(document);
1555
- _context4.next = 3;
1556
- return fieldLevelPatch(id, deserialized, localeId);
1557
-
1558
- case 3:
1559
- case "end":
1560
- return _context4.stop();
1561
- }
1562
- }
1563
- }, _callee4);
1564
- }));
1565
-
1566
- function importTranslation(_x6, _x7, _x8) {
1567
- return _importTranslation2.apply(this, arguments);
1568
- }
1244
+ });
1569
1245
 
1570
- return importTranslation;
1571
- }(),
1246
+ var defaultFieldLevelConfig = /*#__PURE__*/_extends({}, sanityTranslationsTab.baseFieldLevelConfig, {
1572
1247
  adapter: SmartlingAdapter
1573
- };
1248
+ });
1574
1249
 
1575
- Object.defineProperty(exports, 'TranslationsTab', {
1250
+ Object.defineProperty(exports, 'BaseDocumentDeserializer', {
1576
1251
  enumerable: true,
1577
1252
  get: function () {
1578
- return sanityTranslationsTab.TranslationsTab;
1253
+ return sanityTranslationsTab.BaseDocumentDeserializer;
1579
1254
  }
1580
1255
  });
1581
- Object.defineProperty(exports, 'BaseDocumentDeserializer', {
1256
+ Object.defineProperty(exports, 'BaseDocumentMerger', {
1582
1257
  enumerable: true,
1583
1258
  get: function () {
1584
- return sanityNaiveHtmlSerializer.BaseDocumentDeserializer;
1259
+ return sanityTranslationsTab.BaseDocumentMerger;
1585
1260
  }
1586
1261
  });
1587
- Object.defineProperty(exports, 'BaseDocumentMerger', {
1262
+ Object.defineProperty(exports, 'BaseDocumentSerializer', {
1588
1263
  enumerable: true,
1589
1264
  get: function () {
1590
- return sanityNaiveHtmlSerializer.BaseDocumentMerger;
1265
+ return sanityTranslationsTab.BaseDocumentSerializer;
1591
1266
  }
1592
1267
  });
1593
- Object.defineProperty(exports, 'BaseDocumentSerializer', {
1268
+ Object.defineProperty(exports, 'TranslationsTab', {
1594
1269
  enumerable: true,
1595
1270
  get: function () {
1596
- return sanityNaiveHtmlSerializer.BaseDocumentSerializer;
1271
+ return sanityTranslationsTab.TranslationsTab;
1597
1272
  }
1598
1273
  });
1599
1274
  Object.defineProperty(exports, 'customSerializers', {
1600
1275
  enumerable: true,
1601
1276
  get: function () {
1602
- return sanityNaiveHtmlSerializer.customSerializers;
1277
+ return sanityTranslationsTab.customSerializers;
1603
1278
  }
1604
1279
  });
1605
1280
  Object.defineProperty(exports, 'defaultStopTypes', {
1606
1281
  enumerable: true,
1607
1282
  get: function () {
1608
- return sanityNaiveHtmlSerializer.defaultStopTypes;
1283
+ return sanityTranslationsTab.defaultStopTypes;
1284
+ }
1285
+ });
1286
+ Object.defineProperty(exports, 'findLatestDraft', {
1287
+ enumerable: true,
1288
+ get: function () {
1289
+ return sanityTranslationsTab.findLatestDraft;
1609
1290
  }
1610
1291
  });
1611
1292
  exports.SmartlingAdapter = SmartlingAdapter;