sanity-plugin-transifex 0.9.85 → 1.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.
@@ -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 {
@@ -880,7 +883,7 @@ var getTranslationTask = /*#__PURE__*/function () {
880
883
  locales: res.data.map(function (locale) {
881
884
  return {
882
885
  localeId: locale.relationships.language.data.id.split(':')[1],
883
- progress: Math.floor(100 * (locale.attributes.translated_strings / parseFloat(locale.attributes.total_strings)))
886
+ progress: Math.floor(100 * (locale.attributes.reviewed_strings / parseFloat(locale.attributes.total_strings)))
884
887
  };
885
888
  })
886
889
  };
@@ -1143,33 +1146,284 @@ var TransifexAdapter = {
1143
1146
  getTranslation: getTranslation
1144
1147
  };
1145
1148
 
1146
- //unfortunately this nomenclature was inconsistent between publishing this and publishing the agnostic tab
1149
+ var client = /*#__PURE__*/sanityClient.withConfig({
1150
+ apiVersion: '2021-03-25'
1151
+ }); //document fetch
1152
+
1153
+ var findLatestDraft = function findLatestDraft(documentId, ignoreI18n) {
1154
+ if (ignoreI18n === void 0) {
1155
+ ignoreI18n = true;
1156
+ }
1157
+
1158
+ //eliminates i18n versions
1159
+ var query = "*[_id match $id " + (ignoreI18n ? ' && (_id in path("drafts.*") || _id in path("*"))' : '') + "]";
1160
+ var params = {
1161
+ id: "*" + documentId
1162
+ };
1163
+ return client.fetch(query, params).then(function (docs) {
1164
+ var _docs$find;
1165
+
1166
+ return (_docs$find = docs.find(function (doc) {
1167
+ return doc._id.includes('draft');
1168
+ })) != null ? _docs$find : docs[0];
1169
+ });
1170
+ }; //revision fetch
1171
+
1172
+ var findDocumentAtRevision = /*#__PURE__*/function () {
1173
+ var _ref = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee(documentId, rev) {
1174
+ var dataset, baseUrl, url, revisionDoc;
1175
+ return runtime_1.wrap(function _callee$(_context) {
1176
+ while (1) {
1177
+ switch (_context.prev = _context.next) {
1178
+ case 0:
1179
+ dataset = client.config().dataset;
1180
+ baseUrl = "/data/history/" + dataset + "/documents/" + documentId + "?revision=" + rev;
1181
+ url = client.getUrl(baseUrl);
1182
+ _context.next = 5;
1183
+ return fetch(url, {
1184
+ credentials: 'include'
1185
+ }).then(function (req) {
1186
+ return req.json();
1187
+ }).then(function (req) {
1188
+ return req.documents[0];
1189
+ });
1190
+
1191
+ case 5:
1192
+ revisionDoc = _context.sent;
1193
+
1194
+ if (!(!revisionDoc || revisionDoc._rev !== rev)) {
1195
+ _context.next = 12;
1196
+ break;
1197
+ }
1198
+
1199
+ baseUrl = "/data/history/" + dataset + "/documents/drafts." + documentId + "?revision=" + rev;
1200
+ url = client.getUrl(baseUrl);
1201
+ _context.next = 11;
1202
+ return fetch(url, {
1203
+ credentials: 'include'
1204
+ }).then(function (req) {
1205
+ return req.json();
1206
+ }).then(function (req) {
1207
+ return req.documents[0];
1208
+ });
1209
+
1210
+ case 11:
1211
+ revisionDoc = _context.sent;
1212
+
1213
+ case 12:
1214
+ return _context.abrupt("return", revisionDoc);
1215
+
1216
+ case 13:
1217
+ case "end":
1218
+ return _context.stop();
1219
+ }
1220
+ }
1221
+ }, _callee);
1222
+ }));
1223
+
1224
+ return function findDocumentAtRevision(_x, _x2) {
1225
+ return _ref.apply(this, arguments);
1226
+ };
1227
+ }(); //document-level patch
1228
+
1229
+ var documentLevelPatch = /*#__PURE__*/function () {
1230
+ var _ref2 = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee2(documentId, translatedFields, localeId) {
1231
+ var baseDoc, merged, targetId, i18nDoc, cleanedMerge;
1232
+ return runtime_1.wrap(function _callee2$(_context2) {
1233
+ while (1) {
1234
+ switch (_context2.prev = _context2.next) {
1235
+ case 0:
1236
+ if (!translatedFields._rev) {
1237
+ _context2.next = 6;
1238
+ break;
1239
+ }
1240
+
1241
+ _context2.next = 3;
1242
+ return findDocumentAtRevision(documentId, translatedFields._rev);
1243
+
1244
+ case 3:
1245
+ baseDoc = _context2.sent;
1246
+ _context2.next = 9;
1247
+ break;
1248
+
1249
+ case 6:
1250
+ _context2.next = 8;
1251
+ return findLatestDraft(documentId);
1252
+
1253
+ case 8:
1254
+ baseDoc = _context2.sent;
1255
+
1256
+ case 9:
1257
+ merged = sanityNaiveHtmlSerializer.BaseDocumentMerger.documentLevelMerge(translatedFields, baseDoc);
1258
+ targetId = "i18n." + documentId + "." + localeId;
1259
+ _context2.next = 13;
1260
+ return findLatestDraft(targetId, false);
1261
+
1262
+ case 13:
1263
+ i18nDoc = _context2.sent;
1264
+
1265
+ if (i18nDoc) {
1266
+ cleanedMerge = {}; //don't overwrite any existing values on the i18n doc
1267
+
1268
+ Object.entries(merged).forEach(function (_ref3) {
1269
+ var key = _ref3[0],
1270
+ value = _ref3[1];
1271
+
1272
+ if (Object.keys(translatedFields).includes(key) && !['_id', '_rev', '_updatedAt'].includes(key)) {
1273
+ cleanedMerge[key] = value;
1274
+ }
1275
+ });
1276
+ client.transaction() //@ts-ignore
1277
+ .patch(i18nDoc._id, function (p) {
1278
+ return p.set(cleanedMerge);
1279
+ }).commit();
1280
+ } else {
1281
+ merged._id = "drafts." + targetId; //account for legacy implementations of i18n plugin lang
1282
+
1283
+ if (baseDoc._lang) {
1284
+ merged._lang = localeId;
1285
+ } else if (baseDoc.__i18n_lang) {
1286
+ merged.__i18n_lang = localeId;
1287
+ }
1288
+
1289
+ client.create(merged);
1290
+ }
1291
+
1292
+ case 15:
1293
+ case "end":
1294
+ return _context2.stop();
1295
+ }
1296
+ }
1297
+ }, _callee2);
1298
+ }));
1299
+
1300
+ return function documentLevelPatch(_x3, _x4, _x5) {
1301
+ return _ref2.apply(this, arguments);
1302
+ };
1303
+ }(); //field level patch
1304
+
1305
+ var fieldLevelPatch = /*#__PURE__*/function () {
1306
+ var _ref4 = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee3(documentId, translatedFields, localeId) {
1307
+ var baseDoc, merged;
1308
+ return runtime_1.wrap(function _callee3$(_context3) {
1309
+ while (1) {
1310
+ switch (_context3.prev = _context3.next) {
1311
+ case 0:
1312
+ if (!translatedFields._rev) {
1313
+ _context3.next = 6;
1314
+ break;
1315
+ }
1316
+
1317
+ _context3.next = 3;
1318
+ return findDocumentAtRevision(documentId, translatedFields._rev);
1319
+
1320
+ case 3:
1321
+ baseDoc = _context3.sent;
1322
+ _context3.next = 9;
1323
+ break;
1324
+
1325
+ case 6:
1326
+ _context3.next = 8;
1327
+ return findLatestDraft(documentId);
1328
+
1329
+ case 8:
1330
+ baseDoc = _context3.sent;
1331
+
1332
+ case 9:
1333
+ merged = sanityNaiveHtmlSerializer.BaseDocumentMerger.fieldLevelMerge(translatedFields, baseDoc, localeId, 'en');
1334
+ client.patch(baseDoc._id).set(merged).commit();
1335
+
1336
+ case 11:
1337
+ case "end":
1338
+ return _context3.stop();
1339
+ }
1340
+ }
1341
+ }, _callee3);
1342
+ }));
1343
+
1344
+ return function fieldLevelPatch(_x6, _x7, _x8) {
1345
+ return _ref4.apply(this, arguments);
1346
+ };
1347
+ }();
1348
+
1147
1349
  var defaultDocumentLevelConfig = {
1148
- exportForTranslation: function exportForTranslation(id) {
1149
- return sanityNaiveHtmlSerializer.BaseDocumentSerializer.serializeDocument(id, 'document', 'en', sanityNaiveHtmlSerializer.defaultStopTypes, sanityNaiveHtmlSerializer.customSerializers);
1150
- },
1350
+ exportForTranslation: /*#__PURE__*/function () {
1351
+ var _exportForTranslation = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee(id) {
1352
+ var doc, serialized;
1353
+ return runtime_1.wrap(function _callee$(_context) {
1354
+ while (1) {
1355
+ switch (_context.prev = _context.next) {
1356
+ case 0:
1357
+ _context.next = 2;
1358
+ return findLatestDraft(id);
1359
+
1360
+ case 2:
1361
+ doc = _context.sent;
1362
+ serialized = sanityNaiveHtmlSerializer.BaseDocumentSerializer.serializeDocument(doc, 'document');
1363
+ serialized.name = id;
1364
+ return _context.abrupt("return", serialized);
1365
+
1366
+ case 6:
1367
+ case "end":
1368
+ return _context.stop();
1369
+ }
1370
+ }
1371
+ }, _callee);
1372
+ }));
1373
+
1374
+ function exportForTranslation(_x) {
1375
+ return _exportForTranslation.apply(this, arguments);
1376
+ }
1377
+
1378
+ return exportForTranslation;
1379
+ }(),
1151
1380
  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
- });
1381
+ var deserialized = sanityNaiveHtmlSerializer.BaseDocumentDeserializer.deserializeDocument(document);
1382
+ documentLevelPatch(id, deserialized, localeId);
1155
1383
  },
1156
1384
  adapter: TransifexAdapter,
1157
1385
  secretsNamespace: 'transifex'
1158
1386
  };
1159
1387
  var defaultFieldLevelConfig = {
1160
- exportForTranslation: function exportForTranslation(id) {
1161
- return sanityNaiveHtmlSerializer.BaseDocumentSerializer.serializeDocument(id, 'field', 'en', sanityNaiveHtmlSerializer.defaultStopTypes, sanityNaiveHtmlSerializer.customSerializers);
1162
- },
1388
+ exportForTranslation: /*#__PURE__*/function () {
1389
+ var _exportForTranslation2 = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee2(id) {
1390
+ var doc, serialized;
1391
+ return runtime_1.wrap(function _callee2$(_context2) {
1392
+ while (1) {
1393
+ switch (_context2.prev = _context2.next) {
1394
+ case 0:
1395
+ _context2.next = 2;
1396
+ return findLatestDraft(id);
1397
+
1398
+ case 2:
1399
+ doc = _context2.sent;
1400
+ serialized = sanityNaiveHtmlSerializer.BaseDocumentSerializer.serializeDocument(doc, 'field');
1401
+ serialized.name = id;
1402
+ return _context2.abrupt("return", serialized);
1403
+
1404
+ case 6:
1405
+ case "end":
1406
+ return _context2.stop();
1407
+ }
1408
+ }
1409
+ }, _callee2);
1410
+ }));
1411
+
1412
+ function exportForTranslation(_x2) {
1413
+ return _exportForTranslation2.apply(this, arguments);
1414
+ }
1415
+
1416
+ return exportForTranslation;
1417
+ }(),
1163
1418
  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
- });
1419
+ var deserialized = sanityNaiveHtmlSerializer.BaseDocumentDeserializer.deserializeDocument(document);
1420
+ fieldLevelPatch(id, deserialized, localeId);
1167
1421
  },
1168
1422
  adapter: TransifexAdapter,
1169
1423
  secretsNamespace: 'transifex'
1170
1424
  };
1171
1425
 
1172
- Object.defineProperty(exports, 'TranslationTab', {
1426
+ Object.defineProperty(exports, 'TranslationsTab', {
1173
1427
  enumerable: true,
1174
1428
  get: function () {
1175
1429
  return sanityTranslationsTab.TranslationsTab;
@@ -1181,10 +1435,10 @@ Object.defineProperty(exports, 'BaseDocumentDeserializer', {
1181
1435
  return sanityNaiveHtmlSerializer.BaseDocumentDeserializer;
1182
1436
  }
1183
1437
  });
1184
- Object.defineProperty(exports, 'BaseDocumentPatcher', {
1438
+ Object.defineProperty(exports, 'BaseDocumentMerger', {
1185
1439
  enumerable: true,
1186
1440
  get: function () {
1187
- return sanityNaiveHtmlSerializer.BaseDocumentPatcher;
1441
+ return sanityNaiveHtmlSerializer.BaseDocumentMerger;
1188
1442
  }
1189
1443
  });
1190
1444
  Object.defineProperty(exports, 'BaseDocumentSerializer', {