sanity-plugin-transifex 1.1.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.
@@ -1,7 +1,5 @@
1
- export { TranslationsTab } from 'sanity-translations-tab';
2
- import { BaseDocumentMerger, BaseDocumentDeserializer, BaseDocumentSerializer } from 'sanity-naive-html-serializer';
3
- export { BaseDocumentDeserializer, BaseDocumentMerger, BaseDocumentSerializer, customSerializers, defaultStopTypes } from 'sanity-naive-html-serializer';
4
- import sanityClient from 'part:@sanity/base/client';
1
+ import { baseFieldLevelConfig, baseDocumentLevelConfig } from 'sanity-translations-tab';
2
+ export { BaseDocumentDeserializer, BaseDocumentMerger, BaseDocumentSerializer, TranslationsTab, customSerializers, defaultStopTypes, findLatestDraft } from 'sanity-translations-tab';
5
3
 
6
4
  function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
7
5
  try {
@@ -39,6 +37,24 @@ function _asyncToGenerator(fn) {
39
37
  };
40
38
  }
41
39
 
40
+ function _extends() {
41
+ _extends = Object.assign || function (target) {
42
+ for (var i = 1; i < arguments.length; i++) {
43
+ var source = arguments[i];
44
+
45
+ for (var key in source) {
46
+ if (Object.prototype.hasOwnProperty.call(source, key)) {
47
+ target[key] = source[key];
48
+ }
49
+ }
50
+ }
51
+
52
+ return target;
53
+ };
54
+
55
+ return _extends.apply(this, arguments);
56
+ }
57
+
42
58
  function createCommonjsModule(fn, module) {
43
59
  return module = { exports: {} }, fn(module, module.exports), module.exports;
44
60
  }
@@ -131,9 +147,9 @@ var runtime = (function (exports) {
131
147
  // This is a polyfill for %IteratorPrototype% for environments that
132
148
  // don't natively support it.
133
149
  var IteratorPrototype = {};
134
- IteratorPrototype[iteratorSymbol] = function () {
150
+ define(IteratorPrototype, iteratorSymbol, function () {
135
151
  return this;
136
- };
152
+ });
137
153
 
138
154
  var getProto = Object.getPrototypeOf;
139
155
  var NativeIteratorPrototype = getProto && getProto(getProto(values([])));
@@ -147,8 +163,9 @@ var runtime = (function (exports) {
147
163
 
148
164
  var Gp = GeneratorFunctionPrototype.prototype =
149
165
  Generator.prototype = Object.create(IteratorPrototype);
150
- GeneratorFunction.prototype = Gp.constructor = GeneratorFunctionPrototype;
151
- GeneratorFunctionPrototype.constructor = GeneratorFunction;
166
+ GeneratorFunction.prototype = GeneratorFunctionPrototype;
167
+ define(Gp, "constructor", GeneratorFunctionPrototype);
168
+ define(GeneratorFunctionPrototype, "constructor", GeneratorFunction);
152
169
  GeneratorFunction.displayName = define(
153
170
  GeneratorFunctionPrototype,
154
171
  toStringTagSymbol,
@@ -262,9 +279,9 @@ var runtime = (function (exports) {
262
279
  }
263
280
 
264
281
  defineIteratorMethods(AsyncIterator.prototype);
265
- AsyncIterator.prototype[asyncIteratorSymbol] = function () {
282
+ define(AsyncIterator.prototype, asyncIteratorSymbol, function () {
266
283
  return this;
267
- };
284
+ });
268
285
  exports.AsyncIterator = AsyncIterator;
269
286
 
270
287
  // Note that simple async functions are implemented on top of
@@ -457,13 +474,13 @@ var runtime = (function (exports) {
457
474
  // iterator prototype chain incorrectly implement this, causing the Generator
458
475
  // object to not be returned from this call. This ensures that doesn't happen.
459
476
  // See https://github.com/facebook/regenerator/issues/274 for more details.
460
- Gp[iteratorSymbol] = function() {
477
+ define(Gp, iteratorSymbol, function() {
461
478
  return this;
462
- };
479
+ });
463
480
 
464
- Gp.toString = function() {
481
+ define(Gp, "toString", function() {
465
482
  return "[object Generator]";
466
- };
483
+ });
467
484
 
468
485
  function pushTryEntry(locs) {
469
486
  var entry = { tryLoc: locs[0] };
@@ -782,14 +799,19 @@ try {
782
799
  } catch (accidentalStrictMode) {
783
800
  // This module should not be running in strict mode, so the above
784
801
  // assignment should always work unless something is misconfigured. Just
785
- // in case runtime.js accidentally runs in strict mode, we can escape
802
+ // in case runtime.js accidentally runs in strict mode, in modern engines
803
+ // we can explicitly access globalThis. In older engines we can escape
786
804
  // strict mode using a global Function call. This could conceivably fail
787
805
  // if a Content Security Policy forbids using Function, but in that case
788
806
  // the proper solution is to fix the accidental strict mode problem. If
789
807
  // you've misconfigured your bundler to force strict mode and applied a
790
808
  // CSP to forbid Function, and you're not willing to fix either of those
791
809
  // problems, please detail your unique predicament in a GitHub issue.
792
- Function("r", "regeneratorRuntime = r")(runtime);
810
+ if (typeof globalThis === "object") {
811
+ globalThis.regeneratorRuntime = runtime;
812
+ } else {
813
+ Function("r", "regeneratorRuntime = r")(runtime);
814
+ }
793
815
  }
794
816
  });
795
817
 
@@ -854,23 +876,34 @@ var getTranslationTask = /*#__PURE__*/function () {
854
876
  while (1) {
855
877
  switch (_context2.prev = _context2.next) {
856
878
  case 0:
879
+ if (!(!documentId || !secrets)) {
880
+ _context2.next = 2;
881
+ break;
882
+ }
883
+
884
+ return _context2.abrupt("return", {
885
+ taskId: documentId,
886
+ documentId: documentId,
887
+ locales: []
888
+ });
889
+
890
+ case 2:
857
891
  projectFilter = "filter[project]=" + projOrgSlug(secrets);
858
892
  resourceFilter = "filter[resource]=" + projOrgSlug(secrets) + ":r:" + documentId;
859
- _context2.next = 4;
893
+ _context2.next = 6;
860
894
  return fetch(baseTransifexUrl + "/resource_language_stats?" + projectFilter + "&" + resourceFilter, {
861
895
  headers: getHeaders(secrets)
862
896
  }).then(function (res) {
863
897
  if (res.ok) {
864
898
  return res.json();
865
899
  } //normal -- just means that this task doesn't exist yet.
866
- else //normal -- just means that this task doesn't exist yet.
867
- if (res.status === 404) {
868
- return {
869
- data: []
870
- };
871
- } else {
872
- throw Error("Failed to retrieve tasks from Transifex. Status: " + res.status);
873
- }
900
+ else if (res.status === 404) {
901
+ return {
902
+ data: []
903
+ };
904
+ } else {
905
+ throw Error("Failed to retrieve tasks from Transifex. Status: " + res.status);
906
+ }
874
907
  }).then(function (res) {
875
908
  return {
876
909
  taskId: projOrgSlug(secrets) + ":r:" + documentId,
@@ -884,12 +917,12 @@ var getTranslationTask = /*#__PURE__*/function () {
884
917
  };
885
918
  });
886
919
 
887
- case 4:
920
+ case 6:
888
921
  task = _context2.sent;
889
- _context2.next = 7;
922
+ _context2.next = 9;
890
923
  return getLocales(secrets);
891
924
 
892
- case 7:
925
+ case 9:
893
926
  locales = _context2.sent;
894
927
  localeIds = locales.map(function (l) {
895
928
  return l.localeId;
@@ -902,7 +935,7 @@ var getTranslationTask = /*#__PURE__*/function () {
902
935
  task.locales = validLocales;
903
936
  return _context2.abrupt("return", task);
904
937
 
905
- case 12:
938
+ case 14:
906
939
  case "end":
907
940
  return _context2.stop();
908
941
  }
@@ -1092,6 +1125,7 @@ var getTranslation = /*#__PURE__*/function () {
1092
1125
  if (res.redirected) {
1093
1126
  return resolve(handleFileDownload(res.url));
1094
1127
  } else {
1128
+ console.log('no redirected, try again');
1095
1129
  return res.json();
1096
1130
  }
1097
1131
  });
@@ -1141,282 +1175,15 @@ var TransifexAdapter = {
1141
1175
  getTranslation: getTranslation
1142
1176
  };
1143
1177
 
1144
- var client = /*#__PURE__*/sanityClient.withConfig({
1145
- apiVersion: '2021-03-25'
1146
- }); //document fetch
1147
-
1148
- var findLatestDraft = function findLatestDraft(documentId, ignoreI18n) {
1149
- if (ignoreI18n === void 0) {
1150
- ignoreI18n = true;
1151
- }
1152
-
1153
- //eliminates i18n versions
1154
- var query = "*[_id match $id " + (ignoreI18n ? ' && (_id in path("drafts.*") || _id in path("*"))' : '') + "]";
1155
- var params = {
1156
- id: "*" + documentId
1157
- };
1158
- return client.fetch(query, params).then(function (docs) {
1159
- var _docs$find;
1160
-
1161
- return (_docs$find = docs.find(function (doc) {
1162
- return doc._id.includes('draft');
1163
- })) != null ? _docs$find : docs[0];
1164
- });
1165
- }; //revision fetch
1166
-
1167
- var findDocumentAtRevision = /*#__PURE__*/function () {
1168
- var _ref = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee(documentId, rev) {
1169
- var dataset, baseUrl, url, revisionDoc;
1170
- return runtime_1.wrap(function _callee$(_context) {
1171
- while (1) {
1172
- switch (_context.prev = _context.next) {
1173
- case 0:
1174
- dataset = client.config().dataset;
1175
- baseUrl = "/data/history/" + dataset + "/documents/" + documentId + "?revision=" + rev;
1176
- url = client.getUrl(baseUrl);
1177
- _context.next = 5;
1178
- return fetch(url, {
1179
- credentials: 'include'
1180
- }).then(function (req) {
1181
- return req.json();
1182
- }).then(function (req) {
1183
- return req.documents[0];
1184
- });
1185
-
1186
- case 5:
1187
- revisionDoc = _context.sent;
1188
-
1189
- if (!(!revisionDoc || revisionDoc._rev !== rev)) {
1190
- _context.next = 12;
1191
- break;
1192
- }
1193
-
1194
- baseUrl = "/data/history/" + dataset + "/documents/drafts." + documentId + "?revision=" + rev;
1195
- url = client.getUrl(baseUrl);
1196
- _context.next = 11;
1197
- return fetch(url, {
1198
- credentials: 'include'
1199
- }).then(function (req) {
1200
- return req.json();
1201
- }).then(function (req) {
1202
- return req.documents[0];
1203
- });
1204
-
1205
- case 11:
1206
- revisionDoc = _context.sent;
1207
-
1208
- case 12:
1209
- return _context.abrupt("return", revisionDoc);
1210
-
1211
- case 13:
1212
- case "end":
1213
- return _context.stop();
1214
- }
1215
- }
1216
- }, _callee);
1217
- }));
1218
-
1219
- return function findDocumentAtRevision(_x, _x2) {
1220
- return _ref.apply(this, arguments);
1221
- };
1222
- }(); //document-level patch
1223
-
1224
- var documentLevelPatch = /*#__PURE__*/function () {
1225
- var _ref2 = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee2(documentId, translatedFields, localeId) {
1226
- var baseDoc, merged, targetId, i18nDoc, cleanedMerge;
1227
- return runtime_1.wrap(function _callee2$(_context2) {
1228
- while (1) {
1229
- switch (_context2.prev = _context2.next) {
1230
- case 0:
1231
- if (!translatedFields._rev) {
1232
- _context2.next = 6;
1233
- break;
1234
- }
1235
-
1236
- _context2.next = 3;
1237
- return findDocumentAtRevision(documentId, translatedFields._rev);
1238
-
1239
- case 3:
1240
- baseDoc = _context2.sent;
1241
- _context2.next = 9;
1242
- break;
1243
-
1244
- case 6:
1245
- _context2.next = 8;
1246
- return findLatestDraft(documentId);
1247
-
1248
- case 8:
1249
- baseDoc = _context2.sent;
1250
-
1251
- case 9:
1252
- merged = BaseDocumentMerger.documentLevelMerge(translatedFields, baseDoc);
1253
- targetId = "i18n." + documentId + "." + localeId;
1254
- _context2.next = 13;
1255
- return findLatestDraft(targetId, false);
1256
-
1257
- case 13:
1258
- i18nDoc = _context2.sent;
1259
-
1260
- if (i18nDoc) {
1261
- cleanedMerge = {}; //don't overwrite any existing values on the i18n doc
1262
-
1263
- Object.entries(merged).forEach(function (_ref3) {
1264
- var key = _ref3[0],
1265
- value = _ref3[1];
1266
-
1267
- if (Object.keys(translatedFields).includes(key) && !['_id', '_rev', '_updatedAt'].includes(key)) {
1268
- cleanedMerge[key] = value;
1269
- }
1270
- });
1271
- client.transaction() //@ts-ignore
1272
- .patch(i18nDoc._id, function (p) {
1273
- return p.set(cleanedMerge);
1274
- }).commit();
1275
- } else {
1276
- merged._id = "drafts." + targetId; //account for legacy implementations of i18n plugin lang
1277
-
1278
- if (baseDoc._lang) {
1279
- merged._lang = localeId;
1280
- } else if (baseDoc.__i18n_lang) {
1281
- merged.__i18n_lang = localeId;
1282
- }
1283
-
1284
- client.create(merged);
1285
- }
1286
-
1287
- case 15:
1288
- case "end":
1289
- return _context2.stop();
1290
- }
1291
- }
1292
- }, _callee2);
1293
- }));
1294
-
1295
- return function documentLevelPatch(_x3, _x4, _x5) {
1296
- return _ref2.apply(this, arguments);
1297
- };
1298
- }(); //field level patch
1299
-
1300
- var fieldLevelPatch = /*#__PURE__*/function () {
1301
- var _ref4 = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee3(documentId, translatedFields, localeId) {
1302
- var baseDoc, merged;
1303
- return runtime_1.wrap(function _callee3$(_context3) {
1304
- while (1) {
1305
- switch (_context3.prev = _context3.next) {
1306
- case 0:
1307
- if (!translatedFields._rev) {
1308
- _context3.next = 6;
1309
- break;
1310
- }
1311
-
1312
- _context3.next = 3;
1313
- return findDocumentAtRevision(documentId, translatedFields._rev);
1314
-
1315
- case 3:
1316
- baseDoc = _context3.sent;
1317
- _context3.next = 9;
1318
- break;
1319
-
1320
- case 6:
1321
- _context3.next = 8;
1322
- return findLatestDraft(documentId);
1323
-
1324
- case 8:
1325
- baseDoc = _context3.sent;
1326
-
1327
- case 9:
1328
- merged = BaseDocumentMerger.fieldLevelMerge(translatedFields, baseDoc, localeId, 'en');
1329
- client.patch(baseDoc._id).set(merged).commit();
1330
-
1331
- case 11:
1332
- case "end":
1333
- return _context3.stop();
1334
- }
1335
- }
1336
- }, _callee3);
1337
- }));
1338
-
1339
- return function fieldLevelPatch(_x6, _x7, _x8) {
1340
- return _ref4.apply(this, arguments);
1341
- };
1342
- }();
1343
-
1344
- var defaultDocumentLevelConfig = {
1345
- exportForTranslation: /*#__PURE__*/function () {
1346
- var _exportForTranslation = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee(id) {
1347
- var doc, serialized;
1348
- return runtime_1.wrap(function _callee$(_context) {
1349
- while (1) {
1350
- switch (_context.prev = _context.next) {
1351
- case 0:
1352
- _context.next = 2;
1353
- return findLatestDraft(id);
1354
-
1355
- case 2:
1356
- doc = _context.sent;
1357
- serialized = BaseDocumentSerializer.serializeDocument(doc, 'document');
1358
- serialized.name = id;
1359
- return _context.abrupt("return", serialized);
1360
-
1361
- case 6:
1362
- case "end":
1363
- return _context.stop();
1364
- }
1365
- }
1366
- }, _callee);
1367
- }));
1368
-
1369
- function exportForTranslation(_x) {
1370
- return _exportForTranslation.apply(this, arguments);
1371
- }
1372
-
1373
- return exportForTranslation;
1374
- }(),
1375
- importTranslation: function importTranslation(id, localeId, document) {
1376
- var deserialized = BaseDocumentDeserializer.deserializeDocument(document);
1377
- documentLevelPatch(id, deserialized, localeId);
1378
- },
1178
+ var defaultDocumentLevelConfig = /*#__PURE__*/_extends({}, baseDocumentLevelConfig, {
1379
1179
  adapter: TransifexAdapter,
1380
1180
  secretsNamespace: 'transifex'
1381
- };
1382
- var defaultFieldLevelConfig = {
1383
- exportForTranslation: /*#__PURE__*/function () {
1384
- var _exportForTranslation2 = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee2(id) {
1385
- var doc, serialized;
1386
- return runtime_1.wrap(function _callee2$(_context2) {
1387
- while (1) {
1388
- switch (_context2.prev = _context2.next) {
1389
- case 0:
1390
- _context2.next = 2;
1391
- return findLatestDraft(id);
1392
-
1393
- case 2:
1394
- doc = _context2.sent;
1395
- serialized = BaseDocumentSerializer.serializeDocument(doc, 'field');
1396
- serialized.name = id;
1397
- return _context2.abrupt("return", serialized);
1398
-
1399
- case 6:
1400
- case "end":
1401
- return _context2.stop();
1402
- }
1403
- }
1404
- }, _callee2);
1405
- }));
1406
-
1407
- function exportForTranslation(_x2) {
1408
- return _exportForTranslation2.apply(this, arguments);
1409
- }
1181
+ });
1410
1182
 
1411
- return exportForTranslation;
1412
- }(),
1413
- importTranslation: function importTranslation(id, localeId, document) {
1414
- var deserialized = BaseDocumentDeserializer.deserializeDocument(document);
1415
- fieldLevelPatch(id, deserialized, localeId);
1416
- },
1183
+ var defaultFieldLevelConfig = /*#__PURE__*/_extends({}, baseFieldLevelConfig, {
1417
1184
  adapter: TransifexAdapter,
1418
1185
  secretsNamespace: 'transifex'
1419
- };
1186
+ });
1420
1187
 
1421
1188
  export { TransifexAdapter, defaultDocumentLevelConfig, defaultFieldLevelConfig };
1422
1189
  //# sourceMappingURL=sanity-plugin-transifex.esm.js.map