sanity-plugin-transifex 0.9.84 → 1.1.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 {
@@ -133,9 +136,9 @@ var runtime = (function (exports) {
133
136
  // This is a polyfill for %IteratorPrototype% for environments that
134
137
  // don't natively support it.
135
138
  var IteratorPrototype = {};
136
- define(IteratorPrototype, iteratorSymbol, function () {
139
+ IteratorPrototype[iteratorSymbol] = function () {
137
140
  return this;
138
- });
141
+ };
139
142
 
140
143
  var getProto = Object.getPrototypeOf;
141
144
  var NativeIteratorPrototype = getProto && getProto(getProto(values([])));
@@ -149,9 +152,8 @@ var runtime = (function (exports) {
149
152
 
150
153
  var Gp = GeneratorFunctionPrototype.prototype =
151
154
  Generator.prototype = Object.create(IteratorPrototype);
152
- GeneratorFunction.prototype = GeneratorFunctionPrototype;
153
- define(Gp, "constructor", GeneratorFunctionPrototype);
154
- define(GeneratorFunctionPrototype, "constructor", GeneratorFunction);
155
+ GeneratorFunction.prototype = Gp.constructor = GeneratorFunctionPrototype;
156
+ GeneratorFunctionPrototype.constructor = GeneratorFunction;
155
157
  GeneratorFunction.displayName = define(
156
158
  GeneratorFunctionPrototype,
157
159
  toStringTagSymbol,
@@ -265,9 +267,9 @@ var runtime = (function (exports) {
265
267
  }
266
268
 
267
269
  defineIteratorMethods(AsyncIterator.prototype);
268
- define(AsyncIterator.prototype, asyncIteratorSymbol, function () {
270
+ AsyncIterator.prototype[asyncIteratorSymbol] = function () {
269
271
  return this;
270
- });
272
+ };
271
273
  exports.AsyncIterator = AsyncIterator;
272
274
 
273
275
  // Note that simple async functions are implemented on top of
@@ -460,13 +462,13 @@ var runtime = (function (exports) {
460
462
  // iterator prototype chain incorrectly implement this, causing the Generator
461
463
  // object to not be returned from this call. This ensures that doesn't happen.
462
464
  // See https://github.com/facebook/regenerator/issues/274 for more details.
463
- define(Gp, iteratorSymbol, function() {
465
+ Gp[iteratorSymbol] = function() {
464
466
  return this;
465
- });
467
+ };
466
468
 
467
- define(Gp, "toString", function() {
469
+ Gp.toString = function() {
468
470
  return "[object Generator]";
469
- });
471
+ };
470
472
 
471
473
  function pushTryEntry(locs) {
472
474
  var entry = { tryLoc: locs[0] };
@@ -785,19 +787,14 @@ try {
785
787
  } catch (accidentalStrictMode) {
786
788
  // This module should not be running in strict mode, so the above
787
789
  // assignment should always work unless something is misconfigured. Just
788
- // in case runtime.js accidentally runs in strict mode, in modern engines
789
- // we can explicitly access globalThis. In older engines we can escape
790
+ // in case runtime.js accidentally runs in strict mode, we can escape
790
791
  // strict mode using a global Function call. This could conceivably fail
791
792
  // if a Content Security Policy forbids using Function, but in that case
792
793
  // the proper solution is to fix the accidental strict mode problem. If
793
794
  // you've misconfigured your bundler to force strict mode and applied a
794
795
  // CSP to forbid Function, and you're not willing to fix either of those
795
796
  // problems, please detail your unique predicament in a GitHub issue.
796
- if (typeof globalThis === "object") {
797
- globalThis.regeneratorRuntime = runtime;
798
- } else {
799
- Function("r", "regeneratorRuntime = r")(runtime);
800
- }
797
+ Function("r", "regeneratorRuntime = r")(runtime);
801
798
  }
802
799
  });
803
800
 
@@ -871,13 +868,14 @@ var getTranslationTask = /*#__PURE__*/function () {
871
868
  if (res.ok) {
872
869
  return res.json();
873
870
  } //normal -- just means that this task doesn't exist yet.
874
- else if (res.status === 404) {
875
- return {
876
- data: []
877
- };
878
- } else {
879
- throw Error("Failed to retrieve tasks from Transifex. Status: " + res.status);
880
- }
871
+ else //normal -- just means that this task doesn't exist yet.
872
+ if (res.status === 404) {
873
+ return {
874
+ data: []
875
+ };
876
+ } else {
877
+ throw Error("Failed to retrieve tasks from Transifex. Status: " + res.status);
878
+ }
881
879
  }).then(function (res) {
882
880
  return {
883
881
  taskId: projOrgSlug(secrets) + ":r:" + documentId,
@@ -885,7 +883,7 @@ var getTranslationTask = /*#__PURE__*/function () {
885
883
  locales: res.data.map(function (locale) {
886
884
  return {
887
885
  localeId: locale.relationships.language.data.id.split(':')[1],
888
- 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)))
889
887
  };
890
888
  })
891
889
  };
@@ -1148,33 +1146,284 @@ var TransifexAdapter = {
1148
1146
  getTranslation: getTranslation
1149
1147
  };
1150
1148
 
1151
- //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
+
1152
1349
  var defaultDocumentLevelConfig = {
1153
- exportForTranslation: function exportForTranslation(id) {
1154
- return sanityNaiveHtmlSerializer.BaseDocumentSerializer.serializeDocument(id, 'document', 'en', sanityNaiveHtmlSerializer.defaultStopTypes, sanityNaiveHtmlSerializer.customSerializers);
1155
- },
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
+ }(),
1156
1380
  importTranslation: function importTranslation(id, localeId, document) {
1157
- return sanityNaiveHtmlSerializer.BaseDocumentDeserializer.deserializeDocument(id, document).then(function (deserialized) {
1158
- return sanityNaiveHtmlSerializer.BaseDocumentPatcher.documentLevelPatch(deserialized, id, localeId);
1159
- });
1381
+ var deserialized = sanityNaiveHtmlSerializer.BaseDocumentDeserializer.deserializeDocument(document);
1382
+ documentLevelPatch(id, deserialized, localeId);
1160
1383
  },
1161
1384
  adapter: TransifexAdapter,
1162
1385
  secretsNamespace: 'transifex'
1163
1386
  };
1164
1387
  var defaultFieldLevelConfig = {
1165
- exportForTranslation: function exportForTranslation(id) {
1166
- return sanityNaiveHtmlSerializer.BaseDocumentSerializer.serializeDocument(id, 'field', 'en', sanityNaiveHtmlSerializer.defaultStopTypes, sanityNaiveHtmlSerializer.customSerializers);
1167
- },
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
+ }(),
1168
1418
  importTranslation: function importTranslation(id, localeId, document) {
1169
- return sanityNaiveHtmlSerializer.BaseDocumentDeserializer.deserializeDocument(id, document).then(function (deserialized) {
1170
- return sanityNaiveHtmlSerializer.BaseDocumentPatcher.fieldLevelPatch(deserialized, id, localeId, 'en');
1171
- });
1419
+ var deserialized = sanityNaiveHtmlSerializer.BaseDocumentDeserializer.deserializeDocument(document);
1420
+ fieldLevelPatch(id, deserialized, localeId);
1172
1421
  },
1173
1422
  adapter: TransifexAdapter,
1174
1423
  secretsNamespace: 'transifex'
1175
1424
  };
1176
1425
 
1177
- Object.defineProperty(exports, 'TranslationTab', {
1426
+ Object.defineProperty(exports, 'TranslationsTab', {
1178
1427
  enumerable: true,
1179
1428
  get: function () {
1180
1429
  return sanityTranslationsTab.TranslationsTab;
@@ -1186,10 +1435,10 @@ Object.defineProperty(exports, 'BaseDocumentDeserializer', {
1186
1435
  return sanityNaiveHtmlSerializer.BaseDocumentDeserializer;
1187
1436
  }
1188
1437
  });
1189
- Object.defineProperty(exports, 'BaseDocumentPatcher', {
1438
+ Object.defineProperty(exports, 'BaseDocumentMerger', {
1190
1439
  enumerable: true,
1191
1440
  get: function () {
1192
- return sanityNaiveHtmlSerializer.BaseDocumentPatcher;
1441
+ return sanityNaiveHtmlSerializer.BaseDocumentMerger;
1193
1442
  }
1194
1443
  });
1195
1444
  Object.defineProperty(exports, 'BaseDocumentSerializer', {