sanity-plugin-transifex 0.9.85 → 1.1.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.
@@ -7,7 +7,7 @@ jobs:
7
7
  runs-on: ${{ matrix.os }}
8
8
  strategy:
9
9
  matrix:
10
- node: ['10.x', '12.x', '14.x']
10
+ node: ['12.x', '14.x', '16.x']
11
11
  os: [ubuntu-latest, windows-latest, macOS-latest]
12
12
 
13
13
  steps:
@@ -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,16 +1,16 @@
1
- import { TranslationsTab as TranslationTab } from 'sanity-translations-tab';
2
- import { BaseDocumentDeserializer, BaseDocumentSerializer, BaseDocumentPatcher, defaultStopTypes, customSerializers } from 'sanity-naive-html-serializer';
1
+ import { TranslationsTab } from 'sanity-translations-tab';
2
+ import { BaseDocumentDeserializer, BaseDocumentSerializer, BaseDocumentMerger, defaultStopTypes, customSerializers } from 'sanity-naive-html-serializer';
3
3
  import { TransifexAdapter } from './transifexAdapter';
4
- export { TranslationTab, BaseDocumentDeserializer, BaseDocumentSerializer, BaseDocumentPatcher, defaultStopTypes, customSerializers, TransifexAdapter, };
5
- export declare const defaultDocumentLevelConfig: {
4
+ declare const defaultDocumentLevelConfig: {
6
5
  exportForTranslation: (id: string) => Promise<import("sanity-naive-html-serializer/dist/types").SerializedDocument>;
7
- importTranslation: (id: string, localeId: string, document: string) => any;
6
+ importTranslation: (id: string, localeId: string, document: string) => void;
8
7
  adapter: import("sanity-translations-tab").Adapter;
9
8
  secretsNamespace: string;
10
9
  };
11
- export declare const defaultFieldLevelConfig: {
10
+ declare const defaultFieldLevelConfig: {
12
11
  exportForTranslation: (id: string) => Promise<import("sanity-naive-html-serializer/dist/types").SerializedDocument>;
13
- importTranslation: (id: string, localeId: string, document: string) => any;
12
+ importTranslation: (id: string, localeId: string, document: string) => void;
14
13
  adapter: import("sanity-translations-tab").Adapter;
15
14
  secretsNamespace: string;
16
15
  };
16
+ export { TranslationsTab, BaseDocumentDeserializer, BaseDocumentSerializer, BaseDocumentMerger, defaultStopTypes, customSerializers, TransifexAdapter, defaultDocumentLevelConfig, defaultFieldLevelConfig, };
@@ -2,8 +2,11 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
+ function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
6
+
5
7
  var sanityTranslationsTab = require('sanity-translations-tab');
6
8
  var sanityNaiveHtmlSerializer = require('sanity-naive-html-serializer');
9
+ var sanityClient = _interopDefault(require('part:@sanity/base/client'));
7
10
 
8
11
  function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
9
12
  try {
@@ -856,9 +859,21 @@ var getTranslationTask = /*#__PURE__*/function () {
856
859
  while (1) {
857
860
  switch (_context2.prev = _context2.next) {
858
861
  case 0:
862
+ if (!(!documentId || !secrets)) {
863
+ _context2.next = 2;
864
+ break;
865
+ }
866
+
867
+ return _context2.abrupt("return", {
868
+ taskId: documentId,
869
+ documentId: documentId,
870
+ locales: []
871
+ });
872
+
873
+ case 2:
859
874
  projectFilter = "filter[project]=" + projOrgSlug(secrets);
860
875
  resourceFilter = "filter[resource]=" + projOrgSlug(secrets) + ":r:" + documentId;
861
- _context2.next = 4;
876
+ _context2.next = 6;
862
877
  return fetch(baseTransifexUrl + "/resource_language_stats?" + projectFilter + "&" + resourceFilter, {
863
878
  headers: getHeaders(secrets)
864
879
  }).then(function (res) {
@@ -880,18 +895,18 @@ var getTranslationTask = /*#__PURE__*/function () {
880
895
  locales: res.data.map(function (locale) {
881
896
  return {
882
897
  localeId: locale.relationships.language.data.id.split(':')[1],
883
- progress: Math.floor(100 * (locale.attributes.translated_strings / parseFloat(locale.attributes.total_strings)))
898
+ progress: Math.floor(100 * (locale.attributes.reviewed_strings / parseFloat(locale.attributes.total_strings)))
884
899
  };
885
900
  })
886
901
  };
887
902
  });
888
903
 
889
- case 4:
904
+ case 6:
890
905
  task = _context2.sent;
891
- _context2.next = 7;
906
+ _context2.next = 9;
892
907
  return getLocales(secrets);
893
908
 
894
- case 7:
909
+ case 9:
895
910
  locales = _context2.sent;
896
911
  localeIds = locales.map(function (l) {
897
912
  return l.localeId;
@@ -904,7 +919,7 @@ var getTranslationTask = /*#__PURE__*/function () {
904
919
  task.locales = validLocales;
905
920
  return _context2.abrupt("return", task);
906
921
 
907
- case 12:
922
+ case 14:
908
923
  case "end":
909
924
  return _context2.stop();
910
925
  }
@@ -1143,33 +1158,284 @@ var TransifexAdapter = {
1143
1158
  getTranslation: getTranslation
1144
1159
  };
1145
1160
 
1146
- //unfortunately this nomenclature was inconsistent between publishing this and publishing the agnostic tab
1161
+ var client = /*#__PURE__*/sanityClient.withConfig({
1162
+ apiVersion: '2021-03-25'
1163
+ }); //document fetch
1164
+
1165
+ var findLatestDraft = function findLatestDraft(documentId, ignoreI18n) {
1166
+ if (ignoreI18n === void 0) {
1167
+ ignoreI18n = true;
1168
+ }
1169
+
1170
+ //eliminates i18n versions
1171
+ var query = "*[_id match $id " + (ignoreI18n ? ' && (_id in path("drafts.*") || _id in path("*"))' : '') + "]";
1172
+ var params = {
1173
+ id: "*" + documentId
1174
+ };
1175
+ return client.fetch(query, params).then(function (docs) {
1176
+ var _docs$find;
1177
+
1178
+ return (_docs$find = docs.find(function (doc) {
1179
+ return doc._id.includes('draft');
1180
+ })) != null ? _docs$find : docs[0];
1181
+ });
1182
+ }; //revision fetch
1183
+
1184
+ var findDocumentAtRevision = /*#__PURE__*/function () {
1185
+ var _ref = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee(documentId, rev) {
1186
+ var dataset, baseUrl, url, revisionDoc;
1187
+ return runtime_1.wrap(function _callee$(_context) {
1188
+ while (1) {
1189
+ switch (_context.prev = _context.next) {
1190
+ case 0:
1191
+ dataset = client.config().dataset;
1192
+ baseUrl = "/data/history/" + dataset + "/documents/" + documentId + "?revision=" + rev;
1193
+ url = client.getUrl(baseUrl);
1194
+ _context.next = 5;
1195
+ return fetch(url, {
1196
+ credentials: 'include'
1197
+ }).then(function (req) {
1198
+ return req.json();
1199
+ }).then(function (req) {
1200
+ return req.documents[0];
1201
+ });
1202
+
1203
+ case 5:
1204
+ revisionDoc = _context.sent;
1205
+
1206
+ if (!(!revisionDoc || revisionDoc._rev !== rev)) {
1207
+ _context.next = 12;
1208
+ break;
1209
+ }
1210
+
1211
+ baseUrl = "/data/history/" + dataset + "/documents/drafts." + documentId + "?revision=" + rev;
1212
+ url = client.getUrl(baseUrl);
1213
+ _context.next = 11;
1214
+ return fetch(url, {
1215
+ credentials: 'include'
1216
+ }).then(function (req) {
1217
+ return req.json();
1218
+ }).then(function (req) {
1219
+ return req.documents[0];
1220
+ });
1221
+
1222
+ case 11:
1223
+ revisionDoc = _context.sent;
1224
+
1225
+ case 12:
1226
+ return _context.abrupt("return", revisionDoc);
1227
+
1228
+ case 13:
1229
+ case "end":
1230
+ return _context.stop();
1231
+ }
1232
+ }
1233
+ }, _callee);
1234
+ }));
1235
+
1236
+ return function findDocumentAtRevision(_x, _x2) {
1237
+ return _ref.apply(this, arguments);
1238
+ };
1239
+ }(); //document-level patch
1240
+
1241
+ var documentLevelPatch = /*#__PURE__*/function () {
1242
+ var _ref2 = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee2(documentId, translatedFields, localeId) {
1243
+ var baseDoc, merged, targetId, i18nDoc, cleanedMerge;
1244
+ return runtime_1.wrap(function _callee2$(_context2) {
1245
+ while (1) {
1246
+ switch (_context2.prev = _context2.next) {
1247
+ case 0:
1248
+ if (!translatedFields._rev) {
1249
+ _context2.next = 6;
1250
+ break;
1251
+ }
1252
+
1253
+ _context2.next = 3;
1254
+ return findDocumentAtRevision(documentId, translatedFields._rev);
1255
+
1256
+ case 3:
1257
+ baseDoc = _context2.sent;
1258
+ _context2.next = 9;
1259
+ break;
1260
+
1261
+ case 6:
1262
+ _context2.next = 8;
1263
+ return findLatestDraft(documentId);
1264
+
1265
+ case 8:
1266
+ baseDoc = _context2.sent;
1267
+
1268
+ case 9:
1269
+ merged = sanityNaiveHtmlSerializer.BaseDocumentMerger.documentLevelMerge(translatedFields, baseDoc);
1270
+ targetId = "i18n." + documentId + "." + localeId;
1271
+ _context2.next = 13;
1272
+ return findLatestDraft(targetId, false);
1273
+
1274
+ case 13:
1275
+ i18nDoc = _context2.sent;
1276
+
1277
+ if (i18nDoc) {
1278
+ cleanedMerge = {}; //don't overwrite any existing values on the i18n doc
1279
+
1280
+ Object.entries(merged).forEach(function (_ref3) {
1281
+ var key = _ref3[0],
1282
+ value = _ref3[1];
1283
+
1284
+ if (Object.keys(translatedFields).includes(key) && !['_id', '_rev', '_updatedAt'].includes(key)) {
1285
+ cleanedMerge[key] = value;
1286
+ }
1287
+ });
1288
+ client.transaction() //@ts-ignore
1289
+ .patch(i18nDoc._id, function (p) {
1290
+ return p.set(cleanedMerge);
1291
+ }).commit();
1292
+ } else {
1293
+ merged._id = "drafts." + targetId; //account for legacy implementations of i18n plugin lang
1294
+
1295
+ if (baseDoc._lang) {
1296
+ merged._lang = localeId;
1297
+ } else if (baseDoc.__i18n_lang) {
1298
+ merged.__i18n_lang = localeId;
1299
+ }
1300
+
1301
+ client.create(merged);
1302
+ }
1303
+
1304
+ case 15:
1305
+ case "end":
1306
+ return _context2.stop();
1307
+ }
1308
+ }
1309
+ }, _callee2);
1310
+ }));
1311
+
1312
+ return function documentLevelPatch(_x3, _x4, _x5) {
1313
+ return _ref2.apply(this, arguments);
1314
+ };
1315
+ }(); //field level patch
1316
+
1317
+ var fieldLevelPatch = /*#__PURE__*/function () {
1318
+ var _ref4 = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee3(documentId, translatedFields, localeId) {
1319
+ var baseDoc, merged;
1320
+ return runtime_1.wrap(function _callee3$(_context3) {
1321
+ while (1) {
1322
+ switch (_context3.prev = _context3.next) {
1323
+ case 0:
1324
+ if (!translatedFields._rev) {
1325
+ _context3.next = 6;
1326
+ break;
1327
+ }
1328
+
1329
+ _context3.next = 3;
1330
+ return findDocumentAtRevision(documentId, translatedFields._rev);
1331
+
1332
+ case 3:
1333
+ baseDoc = _context3.sent;
1334
+ _context3.next = 9;
1335
+ break;
1336
+
1337
+ case 6:
1338
+ _context3.next = 8;
1339
+ return findLatestDraft(documentId);
1340
+
1341
+ case 8:
1342
+ baseDoc = _context3.sent;
1343
+
1344
+ case 9:
1345
+ merged = sanityNaiveHtmlSerializer.BaseDocumentMerger.fieldLevelMerge(translatedFields, baseDoc, localeId, 'en');
1346
+ client.patch(baseDoc._id).set(merged).commit();
1347
+
1348
+ case 11:
1349
+ case "end":
1350
+ return _context3.stop();
1351
+ }
1352
+ }
1353
+ }, _callee3);
1354
+ }));
1355
+
1356
+ return function fieldLevelPatch(_x6, _x7, _x8) {
1357
+ return _ref4.apply(this, arguments);
1358
+ };
1359
+ }();
1360
+
1147
1361
  var defaultDocumentLevelConfig = {
1148
- exportForTranslation: function exportForTranslation(id) {
1149
- return sanityNaiveHtmlSerializer.BaseDocumentSerializer.serializeDocument(id, 'document', 'en', sanityNaiveHtmlSerializer.defaultStopTypes, sanityNaiveHtmlSerializer.customSerializers);
1150
- },
1362
+ exportForTranslation: /*#__PURE__*/function () {
1363
+ var _exportForTranslation = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee(id) {
1364
+ var doc, serialized;
1365
+ return runtime_1.wrap(function _callee$(_context) {
1366
+ while (1) {
1367
+ switch (_context.prev = _context.next) {
1368
+ case 0:
1369
+ _context.next = 2;
1370
+ return findLatestDraft(id);
1371
+
1372
+ case 2:
1373
+ doc = _context.sent;
1374
+ serialized = sanityNaiveHtmlSerializer.BaseDocumentSerializer.serializeDocument(doc, 'document');
1375
+ serialized.name = id;
1376
+ return _context.abrupt("return", serialized);
1377
+
1378
+ case 6:
1379
+ case "end":
1380
+ return _context.stop();
1381
+ }
1382
+ }
1383
+ }, _callee);
1384
+ }));
1385
+
1386
+ function exportForTranslation(_x) {
1387
+ return _exportForTranslation.apply(this, arguments);
1388
+ }
1389
+
1390
+ return exportForTranslation;
1391
+ }(),
1151
1392
  importTranslation: function importTranslation(id, localeId, document) {
1152
- return sanityNaiveHtmlSerializer.BaseDocumentDeserializer.deserializeDocument(id, document).then(function (deserialized) {
1153
- return sanityNaiveHtmlSerializer.BaseDocumentPatcher.documentLevelPatch(deserialized, id, localeId);
1154
- });
1393
+ var deserialized = sanityNaiveHtmlSerializer.BaseDocumentDeserializer.deserializeDocument(document);
1394
+ documentLevelPatch(id, deserialized, localeId);
1155
1395
  },
1156
1396
  adapter: TransifexAdapter,
1157
1397
  secretsNamespace: 'transifex'
1158
1398
  };
1159
1399
  var defaultFieldLevelConfig = {
1160
- exportForTranslation: function exportForTranslation(id) {
1161
- return sanityNaiveHtmlSerializer.BaseDocumentSerializer.serializeDocument(id, 'field', 'en', sanityNaiveHtmlSerializer.defaultStopTypes, sanityNaiveHtmlSerializer.customSerializers);
1162
- },
1400
+ exportForTranslation: /*#__PURE__*/function () {
1401
+ var _exportForTranslation2 = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee2(id) {
1402
+ var doc, serialized;
1403
+ return runtime_1.wrap(function _callee2$(_context2) {
1404
+ while (1) {
1405
+ switch (_context2.prev = _context2.next) {
1406
+ case 0:
1407
+ _context2.next = 2;
1408
+ return findLatestDraft(id);
1409
+
1410
+ case 2:
1411
+ doc = _context2.sent;
1412
+ serialized = sanityNaiveHtmlSerializer.BaseDocumentSerializer.serializeDocument(doc, 'field');
1413
+ serialized.name = id;
1414
+ return _context2.abrupt("return", serialized);
1415
+
1416
+ case 6:
1417
+ case "end":
1418
+ return _context2.stop();
1419
+ }
1420
+ }
1421
+ }, _callee2);
1422
+ }));
1423
+
1424
+ function exportForTranslation(_x2) {
1425
+ return _exportForTranslation2.apply(this, arguments);
1426
+ }
1427
+
1428
+ return exportForTranslation;
1429
+ }(),
1163
1430
  importTranslation: function importTranslation(id, localeId, document) {
1164
- return sanityNaiveHtmlSerializer.BaseDocumentDeserializer.deserializeDocument(id, document).then(function (deserialized) {
1165
- return sanityNaiveHtmlSerializer.BaseDocumentPatcher.fieldLevelPatch(deserialized, id, localeId, 'en');
1166
- });
1431
+ var deserialized = sanityNaiveHtmlSerializer.BaseDocumentDeserializer.deserializeDocument(document);
1432
+ fieldLevelPatch(id, deserialized, localeId);
1167
1433
  },
1168
1434
  adapter: TransifexAdapter,
1169
1435
  secretsNamespace: 'transifex'
1170
1436
  };
1171
1437
 
1172
- Object.defineProperty(exports, 'TranslationTab', {
1438
+ Object.defineProperty(exports, 'TranslationsTab', {
1173
1439
  enumerable: true,
1174
1440
  get: function () {
1175
1441
  return sanityTranslationsTab.TranslationsTab;
@@ -1181,10 +1447,10 @@ Object.defineProperty(exports, 'BaseDocumentDeserializer', {
1181
1447
  return sanityNaiveHtmlSerializer.BaseDocumentDeserializer;
1182
1448
  }
1183
1449
  });
1184
- Object.defineProperty(exports, 'BaseDocumentPatcher', {
1450
+ Object.defineProperty(exports, 'BaseDocumentMerger', {
1185
1451
  enumerable: true,
1186
1452
  get: function () {
1187
- return sanityNaiveHtmlSerializer.BaseDocumentPatcher;
1453
+ return sanityNaiveHtmlSerializer.BaseDocumentMerger;
1188
1454
  }
1189
1455
  });
1190
1456
  Object.defineProperty(exports, 'BaseDocumentSerializer', {