@transifex/native 6.0.2 → 7.0.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@transifex/native",
3
- "version": "6.0.2",
3
+ "version": "7.0.1",
4
4
  "description": "i18n framework using Transifex Native",
5
5
  "keywords": [
6
6
  "transifex",
@@ -31,19 +31,19 @@
31
31
  "url": "https://github.com/transifex/transifex-javascript/issues"
32
32
  },
33
33
  "engines": {
34
- "node": ">=14.0.0"
34
+ "node": ">=16.0.0"
35
35
  },
36
+ "browserslist": "> 0.5%, last 2 versions, Firefox ESR, not dead",
36
37
  "devDependencies": {
37
- "@babel/core": "^7.23.5",
38
- "@babel/plugin-transform-runtime": "^7.23.4",
39
- "@babel/preset-env": "^7.23.5",
40
- "@babel/runtime": "^7.23.5",
41
- "babel-eslint": "^10.1.0",
42
- "babel-loader": "^8.3.0",
43
- "chai": "^4.3.10",
44
- "eslint": "^7.28.0",
45
- "eslint-config-airbnb-base": "^14.2.1",
46
- "eslint-plugin-import": "^2.29.0",
38
+ "@babel/core": "^7.23.7",
39
+ "@babel/plugin-transform-runtime": "^7.23.7",
40
+ "@babel/preset-env": "^7.23.8",
41
+ "@babel/runtime": "^7.23.8",
42
+ "babel-loader": "^9.1.3",
43
+ "chai": "^5.0.0",
44
+ "eslint": "^8.56.0",
45
+ "eslint-config-airbnb-base": "^15.0.0",
46
+ "eslint-plugin-import": "^2.29.1",
47
47
  "glob": "^8.1.0",
48
48
  "mocha": "^10.2.0",
49
49
  "nock": "^13.4.0",
@@ -54,7 +54,7 @@
54
54
  },
55
55
  "dependencies": {
56
56
  "@messageformat/core": "^3.3.0",
57
- "axios": "^1.6.2",
57
+ "cross-fetch": "^4.0.0",
58
58
  "md5": "^2.3.0"
59
59
  }
60
60
  }
package/src/TxNative.js CHANGED
@@ -1,6 +1,6 @@
1
1
  /* globals __VERSION__, __PLATFORM__ */
2
2
 
3
- import axios from 'axios';
3
+ import fetch from 'cross-fetch';
4
4
 
5
5
  import MemoryCache from './cache/MemoryCache';
6
6
  import SourceErrorPolicy from './policies/SourceErrorPolicy';
@@ -157,8 +157,12 @@ export default class TxNative {
157
157
  if (!isString(translation)) translation = `${translation}`;
158
158
  return translation;
159
159
  } catch (err) {
160
- return this.errorPolicy.handle(err,
161
- `${sourceString}`, locale, params);
160
+ return this.errorPolicy.handle(
161
+ err,
162
+ `${sourceString}`,
163
+ locale,
164
+ params,
165
+ );
162
166
  }
163
167
  }
164
168
 
@@ -204,7 +208,6 @@ export default class TxNative {
204
208
  let lastResponseStatus = 202;
205
209
  const tsNow = Date.now();
206
210
  while (lastResponseStatus === 202) {
207
- /* eslint-disable no-await-in-loop */
208
211
  let url = `${this.cdsHost}/content/${localeCode}`;
209
212
  const getOptions = [];
210
213
  if (filterTags) {
@@ -216,13 +219,19 @@ export default class TxNative {
216
219
  if (getOptions.length) {
217
220
  url = `${url}?${getOptions.join('&')}`;
218
221
  }
219
- response = await axios.get(url, {
222
+ response = await fetch(url, {
223
+ method: 'GET',
220
224
  headers: {
221
225
  Authorization: `Bearer ${this.token}`,
222
226
  'Accept-version': 'v2',
223
227
  'X-NATIVE-SDK': `txjs/${__PLATFORM__}/${__VERSION__}`,
224
228
  },
229
+ signal: this.fetchTimeout > 0 ? AbortSignal.timeout(this.fetchTimeout) : undefined,
225
230
  });
231
+ if (!response.ok) {
232
+ throw (await this._fetchError(response));
233
+ }
234
+
226
235
  lastResponseStatus = response.status;
227
236
  if (this.fetchTimeout > 0 && (Date.now() - tsNow) >= this.fetchTimeout) {
228
237
  throw handleError(new Error('Fetch translations timeout'));
@@ -230,10 +239,9 @@ export default class TxNative {
230
239
  if (lastResponseStatus === 202 && this.fetchInterval > 0) {
231
240
  await sleep(this.fetchInterval);
232
241
  }
233
- /* eslint-enable no-await-in-loop */
234
242
  }
235
243
 
236
- const { data } = response;
244
+ const data = await response.json();
237
245
  if (data && data.data) {
238
246
  const hashmap = {};
239
247
  Object.keys(data.data).forEach((key) => {
@@ -266,8 +274,8 @@ export default class TxNative {
266
274
  if (!this.secret) throw new Error('secret is not defined');
267
275
 
268
276
  const action = params.purge ? 'purge' : 'invalidate';
269
- const res = await axios.post(`${this.cdsHost}/${action}`, {
270
- }, {
277
+ const response = await fetch(`${this.cdsHost}/${action}`, {
278
+ method: 'POST',
271
279
  headers: {
272
280
  Authorization: `Bearer ${this.token}:${this.secret}`,
273
281
  'Accept-version': 'v2',
@@ -275,7 +283,11 @@ export default class TxNative {
275
283
  'X-NATIVE-SDK': `txjs/${__PLATFORM__}/${__VERSION__}`,
276
284
  },
277
285
  });
278
- return res.data;
286
+ if (!response.ok) {
287
+ throw (await this._fetchError(response));
288
+ }
289
+ const data = await response.json();
290
+ return data;
279
291
  }
280
292
 
281
293
  /**
@@ -323,18 +335,25 @@ export default class TxNative {
323
335
  'X-NATIVE-SDK': `txjs/${__PLATFORM__}/${__VERSION__}`,
324
336
  };
325
337
 
326
- const res = await axios.post(`${this.cdsHost}/content`, {
327
- data: payload,
328
- meta: {
329
- purge: !!params.purge,
330
- override_tags: !!params.overrideTags,
331
- override_occurrences: !!params.overrideOccurrences,
332
- },
333
- }, {
338
+ const response = await fetch(`${this.cdsHost}/content`, {
339
+ method: 'POST',
334
340
  headers,
341
+ body: JSON.stringify({
342
+ data: payload,
343
+ meta: {
344
+ purge: !!params.purge,
345
+ override_tags: !!params.overrideTags,
346
+ override_occurrences: !!params.overrideOccurrences,
347
+ },
348
+ }),
335
349
  });
350
+ if (!response.ok) {
351
+ throw (await this._fetchError(response));
352
+ }
353
+
354
+ const postResData = await response.json();
336
355
 
337
- const jobUrl = `${this.cdsHost}${res.data.data.links.job}`;
356
+ const jobUrl = `${this.cdsHost}${postResData.data.links.job}`;
338
357
 
339
358
  if (params.noWait) {
340
359
  return {
@@ -347,13 +366,16 @@ export default class TxNative {
347
366
  };
348
367
 
349
368
  do {
350
- // eslint-disable-next-line no-await-in-loop
351
369
  await sleep(1500);
352
- // eslint-disable-next-line no-await-in-loop
353
- const pollRes = await axios.get(jobUrl, {
370
+ const pollRes = await fetch(jobUrl, {
371
+ method: 'GET',
354
372
  headers,
355
373
  });
356
- const { data } = pollRes.data;
374
+ if (!pollRes.ok) {
375
+ throw (await this._fetchError(pollRes));
376
+ }
377
+ const pollResData = await pollRes.json();
378
+ const { data } = pollResData;
357
379
  pollStatus = {
358
380
  ...(data.details || {}),
359
381
  errors: data.errors || [],
@@ -395,14 +417,18 @@ export default class TxNative {
395
417
  let lastResponseStatus = 202;
396
418
  const tsNow = Date.now();
397
419
  while (lastResponseStatus === 202) {
398
- /* eslint-disable no-await-in-loop */
399
- response = await axios.get(`${this.cdsHost}/languages`, {
420
+ response = await fetch(`${this.cdsHost}/languages`, {
421
+ method: 'GET',
400
422
  headers: {
401
423
  Authorization: `Bearer ${this.token}`,
402
424
  'Accept-version': 'v2',
403
425
  'X-NATIVE-SDK': `txjs/${__PLATFORM__}/${__VERSION__}`,
404
426
  },
427
+ signal: this.fetchTimeout > 0 ? AbortSignal.timeout(this.fetchTimeout) : undefined,
405
428
  });
429
+ if (!response.ok) {
430
+ throw (await this._fetchError(response));
431
+ }
406
432
  lastResponseStatus = response.status;
407
433
  if (this.fetchTimeout > 0 && (Date.now() - tsNow) >= this.fetchTimeout) {
408
434
  throw handleError(new Error('Get locales timeout'));
@@ -410,10 +436,9 @@ export default class TxNative {
410
436
  if (lastResponseStatus === 202 && this.fetchInterval > 0) {
411
437
  await sleep(this.fetchInterval);
412
438
  }
413
- /* eslint-enable no-await-in-loop */
414
439
  }
415
440
 
416
- const { data } = response;
441
+ const data = await response.json();
417
442
  if (data && data.data) {
418
443
  this.languages = data.data;
419
444
  this.locales = this.languages.map((entry) => entry.code);
@@ -536,7 +561,6 @@ export default class TxNative {
536
561
  // Fetch translations for additional instance without blocking
537
562
  // anything else in case of missing language
538
563
  try {
539
- // eslint-disable-next-line no-await-in-loop
540
564
  await instances[i].fetchTranslations(localeCode);
541
565
  } catch (e) {
542
566
  // no-op
@@ -553,4 +577,20 @@ export default class TxNative {
553
577
  }
554
578
  });
555
579
  }
580
+
581
+ /**
582
+ * Return a new fetch error
583
+ *
584
+ * @param {*} response
585
+ * @memberof TxNative
586
+ */
587
+ // eslint-disable-next-line class-methods-use-this
588
+ async _fetchError(response) {
589
+ try {
590
+ const text = await response.text();
591
+ return new Error(`HTTP ${response.status}: ${text}`);
592
+ } catch (err) {
593
+ return new Error(`HTTP error ${response.status}`);
594
+ }
595
+ }
556
596
  }
package/src/index.d.ts CHANGED
@@ -115,6 +115,8 @@ declare module '@transifex/native' {
115
115
  translate(sourceString: string, params?: ITranslateParams): string;
116
116
 
117
117
  translateLocale(localeCode: string, sourceString: string, params?: ITranslateParams): string;
118
+
119
+ t(sourceString: string, params?: ITranslateParams): string;
118
120
  }
119
121
 
120
122
  export class MessageFormatRenderer implements IMessageFormatRenderer {
@@ -165,7 +167,7 @@ declare module '@transifex/native' {
165
167
  localeCode: string;
166
168
  }
167
169
 
168
- export function createNativeInstance(initOptions: ITranslationConfig): TxNative;
170
+ export function createNativeInstance(initOptions?: ITranslationConfig): TxNative;
169
171
 
170
172
  export function escape(unsafe: string): string;
171
173