sanity-plugin-transifex 2.0.6 → 3.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.
Files changed (35) hide show
  1. package/LICENSE +2 -2
  2. package/README.md +84 -84
  3. package/dist/index.d.ts +52 -11
  4. package/dist/index.esm.js +192 -0
  5. package/dist/index.esm.js.map +1 -0
  6. package/dist/index.js +251 -7
  7. package/dist/index.js.map +1 -0
  8. package/package.json +87 -48
  9. package/sanity.json +8 -0
  10. package/src/index.ts +14 -19
  11. package/src/transifexAdapter/createTask.ts +12 -17
  12. package/src/transifexAdapter/getLocales.ts +10 -11
  13. package/src/transifexAdapter/getTranslation.ts +48 -60
  14. package/src/transifexAdapter/getTranslationTask.ts +12 -19
  15. package/src/transifexAdapter/helpers.ts +3 -3
  16. package/src/transifexAdapter/index.ts +5 -5
  17. package/v2-incompatible.js +11 -0
  18. package/.github/workflows/main.yml +0 -29
  19. package/.github/workflows/npm-publish.yml +0 -19
  20. package/dist/sanity-plugin-transifex.cjs.development.js +0 -903
  21. package/dist/sanity-plugin-transifex.cjs.development.js.map +0 -1
  22. package/dist/sanity-plugin-transifex.cjs.production.min.js +0 -2
  23. package/dist/sanity-plugin-transifex.cjs.production.min.js.map +0 -1
  24. package/dist/sanity-plugin-transifex.esm.js +0 -844
  25. package/dist/sanity-plugin-transifex.esm.js.map +0 -1
  26. package/dist/transifexAdapter/createTask.d.ts +0 -6
  27. package/dist/transifexAdapter/getLocales.d.ts +0 -2
  28. package/dist/transifexAdapter/getTranslation.d.ts +0 -2
  29. package/dist/transifexAdapter/getTranslationTask.d.ts +0 -6
  30. package/dist/transifexAdapter/helpers.d.ts +0 -7
  31. package/dist/transifexAdapter/index.d.ts +0 -2
  32. package/test/directives.test.ts +0 -155
  33. package/test/localeId.test.ts +0 -17
  34. package/test/mergeTranslation.test.ts +0 -113
  35. package/tsconfig.json +0 -35
@@ -1,844 +0,0 @@
1
- import { baseFieldLevelConfig, baseDocumentLevelConfig } from 'sanity-translations-tab';
2
- export { BaseDocumentDeserializer, BaseDocumentMerger, BaseDocumentSerializer, TranslationsTab, customSerializers, defaultStopTypes, documentLevelPatch, fieldLevelPatch, findLatestDraft } from 'sanity-translations-tab';
3
-
4
- function _regeneratorRuntime() {
5
- /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */
6
-
7
- _regeneratorRuntime = function () {
8
- return exports;
9
- };
10
-
11
- var exports = {},
12
- Op = Object.prototype,
13
- hasOwn = Op.hasOwnProperty,
14
- $Symbol = "function" == typeof Symbol ? Symbol : {},
15
- iteratorSymbol = $Symbol.iterator || "@@iterator",
16
- asyncIteratorSymbol = $Symbol.asyncIterator || "@@asyncIterator",
17
- toStringTagSymbol = $Symbol.toStringTag || "@@toStringTag";
18
-
19
- function define(obj, key, value) {
20
- return Object.defineProperty(obj, key, {
21
- value: value,
22
- enumerable: !0,
23
- configurable: !0,
24
- writable: !0
25
- }), obj[key];
26
- }
27
-
28
- try {
29
- define({}, "");
30
- } catch (err) {
31
- define = function (obj, key, value) {
32
- return obj[key] = value;
33
- };
34
- }
35
-
36
- function wrap(innerFn, outerFn, self, tryLocsList) {
37
- var protoGenerator = outerFn && outerFn.prototype instanceof Generator ? outerFn : Generator,
38
- generator = Object.create(protoGenerator.prototype),
39
- context = new Context(tryLocsList || []);
40
- return generator._invoke = function (innerFn, self, context) {
41
- var state = "suspendedStart";
42
- return function (method, arg) {
43
- if ("executing" === state) throw new Error("Generator is already running");
44
-
45
- if ("completed" === state) {
46
- if ("throw" === method) throw arg;
47
- return doneResult();
48
- }
49
-
50
- for (context.method = method, context.arg = arg;;) {
51
- var delegate = context.delegate;
52
-
53
- if (delegate) {
54
- var delegateResult = maybeInvokeDelegate(delegate, context);
55
-
56
- if (delegateResult) {
57
- if (delegateResult === ContinueSentinel) continue;
58
- return delegateResult;
59
- }
60
- }
61
-
62
- if ("next" === context.method) context.sent = context._sent = context.arg;else if ("throw" === context.method) {
63
- if ("suspendedStart" === state) throw state = "completed", context.arg;
64
- context.dispatchException(context.arg);
65
- } else "return" === context.method && context.abrupt("return", context.arg);
66
- state = "executing";
67
- var record = tryCatch(innerFn, self, context);
68
-
69
- if ("normal" === record.type) {
70
- if (state = context.done ? "completed" : "suspendedYield", record.arg === ContinueSentinel) continue;
71
- return {
72
- value: record.arg,
73
- done: context.done
74
- };
75
- }
76
-
77
- "throw" === record.type && (state = "completed", context.method = "throw", context.arg = record.arg);
78
- }
79
- };
80
- }(innerFn, self, context), generator;
81
- }
82
-
83
- function tryCatch(fn, obj, arg) {
84
- try {
85
- return {
86
- type: "normal",
87
- arg: fn.call(obj, arg)
88
- };
89
- } catch (err) {
90
- return {
91
- type: "throw",
92
- arg: err
93
- };
94
- }
95
- }
96
-
97
- exports.wrap = wrap;
98
- var ContinueSentinel = {};
99
-
100
- function Generator() {}
101
-
102
- function GeneratorFunction() {}
103
-
104
- function GeneratorFunctionPrototype() {}
105
-
106
- var IteratorPrototype = {};
107
- define(IteratorPrototype, iteratorSymbol, function () {
108
- return this;
109
- });
110
- var getProto = Object.getPrototypeOf,
111
- NativeIteratorPrototype = getProto && getProto(getProto(values([])));
112
- NativeIteratorPrototype && NativeIteratorPrototype !== Op && hasOwn.call(NativeIteratorPrototype, iteratorSymbol) && (IteratorPrototype = NativeIteratorPrototype);
113
- var Gp = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(IteratorPrototype);
114
-
115
- function defineIteratorMethods(prototype) {
116
- ["next", "throw", "return"].forEach(function (method) {
117
- define(prototype, method, function (arg) {
118
- return this._invoke(method, arg);
119
- });
120
- });
121
- }
122
-
123
- function AsyncIterator(generator, PromiseImpl) {
124
- function invoke(method, arg, resolve, reject) {
125
- var record = tryCatch(generator[method], generator, arg);
126
-
127
- if ("throw" !== record.type) {
128
- var result = record.arg,
129
- value = result.value;
130
- return value && "object" == typeof value && hasOwn.call(value, "__await") ? PromiseImpl.resolve(value.__await).then(function (value) {
131
- invoke("next", value, resolve, reject);
132
- }, function (err) {
133
- invoke("throw", err, resolve, reject);
134
- }) : PromiseImpl.resolve(value).then(function (unwrapped) {
135
- result.value = unwrapped, resolve(result);
136
- }, function (error) {
137
- return invoke("throw", error, resolve, reject);
138
- });
139
- }
140
-
141
- reject(record.arg);
142
- }
143
-
144
- var previousPromise;
145
-
146
- this._invoke = function (method, arg) {
147
- function callInvokeWithMethodAndArg() {
148
- return new PromiseImpl(function (resolve, reject) {
149
- invoke(method, arg, resolve, reject);
150
- });
151
- }
152
-
153
- return previousPromise = previousPromise ? previousPromise.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg();
154
- };
155
- }
156
-
157
- function maybeInvokeDelegate(delegate, context) {
158
- var method = delegate.iterator[context.method];
159
-
160
- if (undefined === method) {
161
- if (context.delegate = null, "throw" === context.method) {
162
- if (delegate.iterator.return && (context.method = "return", context.arg = undefined, maybeInvokeDelegate(delegate, context), "throw" === context.method)) return ContinueSentinel;
163
- context.method = "throw", context.arg = new TypeError("The iterator does not provide a 'throw' method");
164
- }
165
-
166
- return ContinueSentinel;
167
- }
168
-
169
- var record = tryCatch(method, delegate.iterator, context.arg);
170
- if ("throw" === record.type) return context.method = "throw", context.arg = record.arg, context.delegate = null, ContinueSentinel;
171
- var info = record.arg;
172
- return info ? info.done ? (context[delegate.resultName] = info.value, context.next = delegate.nextLoc, "return" !== context.method && (context.method = "next", context.arg = undefined), context.delegate = null, ContinueSentinel) : info : (context.method = "throw", context.arg = new TypeError("iterator result is not an object"), context.delegate = null, ContinueSentinel);
173
- }
174
-
175
- function pushTryEntry(locs) {
176
- var entry = {
177
- tryLoc: locs[0]
178
- };
179
- 1 in locs && (entry.catchLoc = locs[1]), 2 in locs && (entry.finallyLoc = locs[2], entry.afterLoc = locs[3]), this.tryEntries.push(entry);
180
- }
181
-
182
- function resetTryEntry(entry) {
183
- var record = entry.completion || {};
184
- record.type = "normal", delete record.arg, entry.completion = record;
185
- }
186
-
187
- function Context(tryLocsList) {
188
- this.tryEntries = [{
189
- tryLoc: "root"
190
- }], tryLocsList.forEach(pushTryEntry, this), this.reset(!0);
191
- }
192
-
193
- function values(iterable) {
194
- if (iterable) {
195
- var iteratorMethod = iterable[iteratorSymbol];
196
- if (iteratorMethod) return iteratorMethod.call(iterable);
197
- if ("function" == typeof iterable.next) return iterable;
198
-
199
- if (!isNaN(iterable.length)) {
200
- var i = -1,
201
- next = function next() {
202
- for (; ++i < iterable.length;) if (hasOwn.call(iterable, i)) return next.value = iterable[i], next.done = !1, next;
203
-
204
- return next.value = undefined, next.done = !0, next;
205
- };
206
-
207
- return next.next = next;
208
- }
209
- }
210
-
211
- return {
212
- next: doneResult
213
- };
214
- }
215
-
216
- function doneResult() {
217
- return {
218
- value: undefined,
219
- done: !0
220
- };
221
- }
222
-
223
- return GeneratorFunction.prototype = GeneratorFunctionPrototype, define(Gp, "constructor", GeneratorFunctionPrototype), define(GeneratorFunctionPrototype, "constructor", GeneratorFunction), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, toStringTagSymbol, "GeneratorFunction"), exports.isGeneratorFunction = function (genFun) {
224
- var ctor = "function" == typeof genFun && genFun.constructor;
225
- return !!ctor && (ctor === GeneratorFunction || "GeneratorFunction" === (ctor.displayName || ctor.name));
226
- }, exports.mark = function (genFun) {
227
- return Object.setPrototypeOf ? Object.setPrototypeOf(genFun, GeneratorFunctionPrototype) : (genFun.__proto__ = GeneratorFunctionPrototype, define(genFun, toStringTagSymbol, "GeneratorFunction")), genFun.prototype = Object.create(Gp), genFun;
228
- }, exports.awrap = function (arg) {
229
- return {
230
- __await: arg
231
- };
232
- }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, asyncIteratorSymbol, function () {
233
- return this;
234
- }), exports.AsyncIterator = AsyncIterator, exports.async = function (innerFn, outerFn, self, tryLocsList, PromiseImpl) {
235
- void 0 === PromiseImpl && (PromiseImpl = Promise);
236
- var iter = new AsyncIterator(wrap(innerFn, outerFn, self, tryLocsList), PromiseImpl);
237
- return exports.isGeneratorFunction(outerFn) ? iter : iter.next().then(function (result) {
238
- return result.done ? result.value : iter.next();
239
- });
240
- }, defineIteratorMethods(Gp), define(Gp, toStringTagSymbol, "Generator"), define(Gp, iteratorSymbol, function () {
241
- return this;
242
- }), define(Gp, "toString", function () {
243
- return "[object Generator]";
244
- }), exports.keys = function (object) {
245
- var keys = [];
246
-
247
- for (var key in object) keys.push(key);
248
-
249
- return keys.reverse(), function next() {
250
- for (; keys.length;) {
251
- var key = keys.pop();
252
- if (key in object) return next.value = key, next.done = !1, next;
253
- }
254
-
255
- return next.done = !0, next;
256
- };
257
- }, exports.values = values, Context.prototype = {
258
- constructor: Context,
259
- reset: function (skipTempReset) {
260
- if (this.prev = 0, this.next = 0, this.sent = this._sent = undefined, this.done = !1, this.delegate = null, this.method = "next", this.arg = undefined, this.tryEntries.forEach(resetTryEntry), !skipTempReset) for (var name in this) "t" === name.charAt(0) && hasOwn.call(this, name) && !isNaN(+name.slice(1)) && (this[name] = undefined);
261
- },
262
- stop: function () {
263
- this.done = !0;
264
- var rootRecord = this.tryEntries[0].completion;
265
- if ("throw" === rootRecord.type) throw rootRecord.arg;
266
- return this.rval;
267
- },
268
- dispatchException: function (exception) {
269
- if (this.done) throw exception;
270
- var context = this;
271
-
272
- function handle(loc, caught) {
273
- return record.type = "throw", record.arg = exception, context.next = loc, caught && (context.method = "next", context.arg = undefined), !!caught;
274
- }
275
-
276
- for (var i = this.tryEntries.length - 1; i >= 0; --i) {
277
- var entry = this.tryEntries[i],
278
- record = entry.completion;
279
- if ("root" === entry.tryLoc) return handle("end");
280
-
281
- if (entry.tryLoc <= this.prev) {
282
- var hasCatch = hasOwn.call(entry, "catchLoc"),
283
- hasFinally = hasOwn.call(entry, "finallyLoc");
284
-
285
- if (hasCatch && hasFinally) {
286
- if (this.prev < entry.catchLoc) return handle(entry.catchLoc, !0);
287
- if (this.prev < entry.finallyLoc) return handle(entry.finallyLoc);
288
- } else if (hasCatch) {
289
- if (this.prev < entry.catchLoc) return handle(entry.catchLoc, !0);
290
- } else {
291
- if (!hasFinally) throw new Error("try statement without catch or finally");
292
- if (this.prev < entry.finallyLoc) return handle(entry.finallyLoc);
293
- }
294
- }
295
- }
296
- },
297
- abrupt: function (type, arg) {
298
- for (var i = this.tryEntries.length - 1; i >= 0; --i) {
299
- var entry = this.tryEntries[i];
300
-
301
- if (entry.tryLoc <= this.prev && hasOwn.call(entry, "finallyLoc") && this.prev < entry.finallyLoc) {
302
- var finallyEntry = entry;
303
- break;
304
- }
305
- }
306
-
307
- finallyEntry && ("break" === type || "continue" === type) && finallyEntry.tryLoc <= arg && arg <= finallyEntry.finallyLoc && (finallyEntry = null);
308
- var record = finallyEntry ? finallyEntry.completion : {};
309
- return record.type = type, record.arg = arg, finallyEntry ? (this.method = "next", this.next = finallyEntry.finallyLoc, ContinueSentinel) : this.complete(record);
310
- },
311
- complete: function (record, afterLoc) {
312
- if ("throw" === record.type) throw record.arg;
313
- return "break" === record.type || "continue" === record.type ? this.next = record.arg : "return" === record.type ? (this.rval = this.arg = record.arg, this.method = "return", this.next = "end") : "normal" === record.type && afterLoc && (this.next = afterLoc), ContinueSentinel;
314
- },
315
- finish: function (finallyLoc) {
316
- for (var i = this.tryEntries.length - 1; i >= 0; --i) {
317
- var entry = this.tryEntries[i];
318
- if (entry.finallyLoc === finallyLoc) return this.complete(entry.completion, entry.afterLoc), resetTryEntry(entry), ContinueSentinel;
319
- }
320
- },
321
- catch: function (tryLoc) {
322
- for (var i = this.tryEntries.length - 1; i >= 0; --i) {
323
- var entry = this.tryEntries[i];
324
-
325
- if (entry.tryLoc === tryLoc) {
326
- var record = entry.completion;
327
-
328
- if ("throw" === record.type) {
329
- var thrown = record.arg;
330
- resetTryEntry(entry);
331
- }
332
-
333
- return thrown;
334
- }
335
- }
336
-
337
- throw new Error("illegal catch attempt");
338
- },
339
- delegateYield: function (iterable, resultName, nextLoc) {
340
- return this.delegate = {
341
- iterator: values(iterable),
342
- resultName: resultName,
343
- nextLoc: nextLoc
344
- }, "next" === this.method && (this.arg = undefined), ContinueSentinel;
345
- }
346
- }, exports;
347
- }
348
-
349
- function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
350
- try {
351
- var info = gen[key](arg);
352
- var value = info.value;
353
- } catch (error) {
354
- reject(error);
355
- return;
356
- }
357
-
358
- if (info.done) {
359
- resolve(value);
360
- } else {
361
- Promise.resolve(value).then(_next, _throw);
362
- }
363
- }
364
-
365
- function _asyncToGenerator(fn) {
366
- return function () {
367
- var self = this,
368
- args = arguments;
369
- return new Promise(function (resolve, reject) {
370
- var gen = fn.apply(self, args);
371
-
372
- function _next(value) {
373
- asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
374
- }
375
-
376
- function _throw(err) {
377
- asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
378
- }
379
-
380
- _next(undefined);
381
- });
382
- };
383
- }
384
-
385
- function _extends() {
386
- _extends = Object.assign ? Object.assign.bind() : function (target) {
387
- for (var i = 1; i < arguments.length; i++) {
388
- var source = arguments[i];
389
-
390
- for (var key in source) {
391
- if (Object.prototype.hasOwnProperty.call(source, key)) {
392
- target[key] = source[key];
393
- }
394
- }
395
- }
396
-
397
- return target;
398
- };
399
- return _extends.apply(this, arguments);
400
- }
401
-
402
- var baseTransifexUrl = 'https://rest.api.transifex.com';
403
- var getHeaders = function getHeaders(secrets) {
404
- return {
405
- Authorization: "Bearer " + (secrets == null ? void 0 : secrets.token),
406
- 'Content-Type': 'application/vnd.api+json'
407
- };
408
- };
409
- var projOrgSlug = function projOrgSlug(secrets) {
410
- return "o:" + (secrets == null ? void 0 : secrets.organization) + ":p:" + (secrets == null ? void 0 : secrets.project);
411
- };
412
-
413
- function getLocales(_x) {
414
- return _getLocales.apply(this, arguments);
415
- }
416
-
417
- function _getLocales() {
418
- _getLocales = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(secrets) {
419
- return _regeneratorRuntime().wrap(function _callee$(_context) {
420
- while (1) {
421
- switch (_context.prev = _context.next) {
422
- case 0:
423
- if (!secrets) {
424
- _context.next = 4;
425
- break;
426
- }
427
-
428
- return _context.abrupt("return", fetch(baseTransifexUrl + "/projects/" + projOrgSlug(secrets) + "/languages", {
429
- headers: getHeaders(secrets)
430
- }).then(function (res) {
431
- return res.json();
432
- }).then(function (res) {
433
- return res.data.map(function (lang) {
434
- return {
435
- enabled: true,
436
- description: lang.attributes.name,
437
- localeId: lang.attributes.code
438
- };
439
- });
440
- }));
441
-
442
- case 4:
443
- return _context.abrupt("return", []);
444
-
445
- case 5:
446
- case "end":
447
- return _context.stop();
448
- }
449
- }
450
- }, _callee);
451
- }));
452
- return _getLocales.apply(this, arguments);
453
- }
454
-
455
- function getTranslationTask(_x, _x2) {
456
- return _getTranslationTask.apply(this, arguments);
457
- }
458
-
459
- function _getTranslationTask() {
460
- _getTranslationTask = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(documentId, secrets) {
461
- var projectFilter, resourceFilter, task, locales, localeIds, validLocales;
462
- return _regeneratorRuntime().wrap(function _callee$(_context) {
463
- while (1) {
464
- switch (_context.prev = _context.next) {
465
- case 0:
466
- if (!(!documentId || !secrets)) {
467
- _context.next = 2;
468
- break;
469
- }
470
-
471
- return _context.abrupt("return", {
472
- taskId: documentId,
473
- documentId: documentId,
474
- locales: []
475
- });
476
-
477
- case 2:
478
- projectFilter = "filter[project]=" + projOrgSlug(secrets);
479
- resourceFilter = "filter[resource]=" + projOrgSlug(secrets) + ":r:" + documentId;
480
- _context.next = 6;
481
- return fetch(baseTransifexUrl + "/resource_language_stats?" + projectFilter + "&" + resourceFilter, {
482
- headers: getHeaders(secrets)
483
- }).then(function (res) {
484
- if (res.ok) {
485
- return res.json();
486
- } //normal -- just means that this task doesn't exist yet.
487
- else if (res.status === 404) {
488
- return {
489
- data: []
490
- };
491
- } else {
492
- throw Error("Failed to retrieve tasks from Transifex. Status: " + res.status);
493
- }
494
- }).then(function (res) {
495
- return {
496
- taskId: projOrgSlug(secrets) + ":r:" + documentId,
497
- documentId: documentId,
498
- locales: res.data.map(function (locale) {
499
- return {
500
- localeId: locale.relationships.language.data.id.split(':')[1],
501
- progress: Math.floor(100 * (locale.attributes.reviewed_strings / parseFloat(locale.attributes.total_strings)))
502
- };
503
- })
504
- };
505
- });
506
-
507
- case 6:
508
- task = _context.sent;
509
- _context.next = 9;
510
- return getLocales(secrets);
511
-
512
- case 9:
513
- locales = _context.sent;
514
- localeIds = locales.map(function (l) {
515
- return l.localeId;
516
- });
517
- validLocales = task.locales.filter(function (locale) {
518
- return localeIds.find(function (id) {
519
- return id === locale.localeId;
520
- });
521
- });
522
- task.locales = validLocales;
523
- return _context.abrupt("return", task);
524
-
525
- case 14:
526
- case "end":
527
- return _context.stop();
528
- }
529
- }
530
- }, _callee);
531
- }));
532
- return _getTranslationTask.apply(this, arguments);
533
- }
534
-
535
- function getTranslation(_x, _x2, _x3) {
536
- return _getTranslation.apply(this, arguments);
537
- }
538
-
539
- function _getTranslation() {
540
- _getTranslation = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3(taskId, localeId, secrets) {
541
- var resourceDownloadBody, resourceDownloadUrl, translationDownloadId, headers, location;
542
- return _regeneratorRuntime().wrap(function _callee3$(_context3) {
543
- while (1) {
544
- switch (_context3.prev = _context3.next) {
545
- case 0:
546
- resourceDownloadBody = {
547
- data: {
548
- attributes: {
549
- content_encoding: 'text'
550
- },
551
- relationships: {
552
- language: {
553
- data: {
554
- id: "l:" + localeId,
555
- type: 'languages'
556
- }
557
- },
558
- resource: {
559
- data: {
560
- id: taskId,
561
- type: 'resources'
562
- }
563
- }
564
- },
565
- type: 'resource_translations_async_downloads'
566
- }
567
- };
568
- resourceDownloadUrl = baseTransifexUrl + "/resource_translations_async_downloads";
569
- _context3.next = 4;
570
- return fetch(resourceDownloadUrl, {
571
- headers: getHeaders(secrets),
572
- method: 'POST',
573
- body: JSON.stringify(resourceDownloadBody)
574
- }).then(function (res) {
575
- return res.json();
576
- }).then(function (res) {
577
- return res.data.id;
578
- });
579
-
580
- case 4:
581
- translationDownloadId = _context3.sent;
582
- headers = getHeaders(secrets);
583
- _context3.next = 8;
584
- return pollForFileDownloadLocation(resourceDownloadUrl, translationDownloadId, headers);
585
-
586
- case 8:
587
- location = _context3.sent;
588
- return _context3.abrupt("return", handleFileDownload(location));
589
-
590
- case 10:
591
- case "end":
592
- return _context3.stop();
593
- }
594
- }
595
- }, _callee3);
596
- }));
597
- return _getTranslation.apply(this, arguments);
598
- }
599
-
600
- var pollForFileDownloadLocation = /*#__PURE__*/function () {
601
- var _ref = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(resourceDownloadUrl, translationDownloadId, headers) {
602
- var response;
603
- return _regeneratorRuntime().wrap(function _callee$(_context) {
604
- while (1) {
605
- switch (_context.prev = _context.next) {
606
- case 0:
607
- _context.next = 2;
608
- return fetch(resourceDownloadUrl + "/" + translationDownloadId, {
609
- headers: headers
610
- });
611
-
612
- case 2:
613
- response = _context.sent;
614
-
615
- if (!(response.status === 500)) {
616
- _context.next = 10;
617
- break;
618
- }
619
-
620
- console.info("Transifex plugin message: Received 500 for translation download ID " + translationDownloadId + ". Trying to reconnect...");
621
- _context.next = 7;
622
- return new Promise(function (resolve) {
623
- return setTimeout(resolve, 3000);
624
- });
625
-
626
- case 7:
627
- return _context.abrupt("return", pollForFileDownloadLocation(resourceDownloadUrl, translationDownloadId, headers));
628
-
629
- case 10:
630
- if (!response.redirected) {
631
- _context.next = 15;
632
- break;
633
- }
634
-
635
- console.info("Transifex plugin message: Received redirect for translation download ID " + translationDownloadId + ". Following redirect now for file download.");
636
- return _context.abrupt("return", response.url);
637
-
638
- case 15:
639
- if (!(response.status === 200)) {
640
- _context.next = 22;
641
- break;
642
- }
643
-
644
- console.info("Transifex plugin message: Requested download location for translation download ID " + translationDownloadId + ". Location is still pending, trying again.");
645
- _context.next = 19;
646
- return new Promise(function (resolve) {
647
- return setTimeout(resolve, 3000);
648
- });
649
-
650
- case 19:
651
- return _context.abrupt("return", pollForFileDownloadLocation(resourceDownloadUrl, translationDownloadId, headers));
652
-
653
- case 22:
654
- console.info("Transifex plugin message: Requested download location for translation download ID " + translationDownloadId + " but received error code " + response.status + ". Waiting and trying again.");
655
- _context.next = 25;
656
- return new Promise(function (resolve) {
657
- return setTimeout(resolve, 3000);
658
- });
659
-
660
- case 25:
661
- return _context.abrupt("return", pollForFileDownloadLocation(resourceDownloadUrl, translationDownloadId, headers));
662
-
663
- case 26:
664
- case "end":
665
- return _context.stop();
666
- }
667
- }
668
- }, _callee);
669
- }));
670
-
671
- return function pollForFileDownloadLocation(_x4, _x5, _x6) {
672
- return _ref.apply(this, arguments);
673
- };
674
- }();
675
-
676
- var handleFileDownload = /*#__PURE__*/function () {
677
- var _ref2 = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(url) {
678
- return _regeneratorRuntime().wrap(function _callee2$(_context2) {
679
- while (1) {
680
- switch (_context2.prev = _context2.next) {
681
- case 0:
682
- return _context2.abrupt("return", fetch(url).then(function (res) {
683
- return res.text();
684
- }));
685
-
686
- case 1:
687
- case "end":
688
- return _context2.stop();
689
- }
690
- }
691
- }, _callee2);
692
- }));
693
-
694
- return function handleFileDownload(_x7) {
695
- return _ref2.apply(this, arguments);
696
- };
697
- }();
698
-
699
- var createResource = /*#__PURE__*/function () {
700
- var _ref = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(doc, documentId, secrets) {
701
- var resourceCreateBody;
702
- return _regeneratorRuntime().wrap(function _callee$(_context) {
703
- while (1) {
704
- switch (_context.prev = _context.next) {
705
- case 0:
706
- resourceCreateBody = {
707
- data: {
708
- attributes: {
709
- accept_translations: true,
710
- name: doc.name,
711
- slug: documentId
712
- },
713
- relationships: {
714
- i18n_format: {
715
- data: {
716
- id: 'HTML_FRAGMENT',
717
- type: 'i18n_formats'
718
- }
719
- },
720
- project: {
721
- data: {
722
- id: projOrgSlug(secrets),
723
- type: 'projects'
724
- }
725
- }
726
- },
727
- type: 'resources'
728
- }
729
- };
730
- return _context.abrupt("return", fetch(baseTransifexUrl + "/resources", {
731
- headers: getHeaders(secrets),
732
- method: 'POST',
733
- body: JSON.stringify(resourceCreateBody)
734
- }).then(function (res) {
735
- return res.json();
736
- }).then(function (res) {
737
- return res.data.id;
738
- }));
739
-
740
- case 2:
741
- case "end":
742
- return _context.stop();
743
- }
744
- }
745
- }, _callee);
746
- }));
747
-
748
- return function createResource(_x, _x2, _x3) {
749
- return _ref.apply(this, arguments);
750
- };
751
- }();
752
-
753
- function createTask(_x4, _x5, _x6, _x7) {
754
- return _createTask.apply(this, arguments);
755
- }
756
-
757
- function _createTask() {
758
- _createTask = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(documentId, document, //unfortunately Transifex doesn't let you specify locales on creating a task
759
- //@ts-ignore
760
- localeIds, secrets) {
761
- var resourceId, resourceUploadUrl, resourceUploadBody;
762
- return _regeneratorRuntime().wrap(function _callee2$(_context2) {
763
- while (1) {
764
- switch (_context2.prev = _context2.next) {
765
- case 0:
766
- _context2.next = 2;
767
- return fetch(baseTransifexUrl + "/resources/" + projOrgSlug(secrets) + ":r:" + documentId, {
768
- headers: getHeaders(secrets)
769
- }).then(function (res) {
770
- return res.json();
771
- }).then(function (res) {
772
- return res.data ? res.data.id : null;
773
- });
774
-
775
- case 2:
776
- resourceId = _context2.sent;
777
-
778
- if (resourceId) {
779
- _context2.next = 7;
780
- break;
781
- }
782
-
783
- _context2.next = 6;
784
- return createResource(document, documentId, secrets);
785
-
786
- case 6:
787
- resourceId = _context2.sent;
788
-
789
- case 7:
790
- resourceUploadUrl = baseTransifexUrl + "/resource_strings_async_uploads";
791
- resourceUploadBody = {
792
- data: {
793
- attributes: {
794
- content: document.content,
795
- content_encoding: 'text'
796
- },
797
- relationships: {
798
- resource: {
799
- data: {
800
- id: resourceId,
801
- type: 'resources'
802
- }
803
- }
804
- },
805
- type: 'resource_strings_async_uploads'
806
- }
807
- };
808
- return _context2.abrupt("return", fetch(resourceUploadUrl, {
809
- method: 'POST',
810
- body: JSON.stringify(resourceUploadBody),
811
- headers: getHeaders(secrets)
812
- }).then(function () {
813
- return getTranslationTask(documentId, secrets);
814
- }));
815
-
816
- case 10:
817
- case "end":
818
- return _context2.stop();
819
- }
820
- }
821
- }, _callee2);
822
- }));
823
- return _createTask.apply(this, arguments);
824
- }
825
-
826
- var TransifexAdapter = {
827
- getLocales: getLocales,
828
- getTranslationTask: getTranslationTask,
829
- createTask: createTask,
830
- getTranslation: getTranslation
831
- };
832
-
833
- var defaultDocumentLevelConfig = /*#__PURE__*/_extends({}, baseDocumentLevelConfig, {
834
- adapter: TransifexAdapter,
835
- secretsNamespace: 'transifex'
836
- });
837
-
838
- var defaultFieldLevelConfig = /*#__PURE__*/_extends({}, baseFieldLevelConfig, {
839
- adapter: TransifexAdapter,
840
- secretsNamespace: 'transifex'
841
- });
842
-
843
- export { TransifexAdapter, defaultDocumentLevelConfig, defaultFieldLevelConfig };
844
- //# sourceMappingURL=sanity-plugin-transifex.esm.js.map