@transifex/native 3.0.0 → 3.2.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": "3.0.0",
3
+ "version": "3.2.1",
4
4
  "description": "Transifex Native for Javascript",
5
5
  "keywords": [
6
6
  "transifex",
package/src/TxNative.js CHANGED
@@ -1,4 +1,5 @@
1
1
  /* globals __VERSION__, __PLATFORM__ */
2
+
2
3
  import axios from 'axios';
3
4
 
4
5
  import MemoryCache from './cache/MemoryCache';
@@ -6,7 +7,7 @@ import SourceErrorPolicy from './policies/SourceErrorPolicy';
6
7
  import SourceStringPolicy from './policies/SourceStringPolicy';
7
8
  import MessageFormatRenderer from './renderers/MessageFormatRenderer';
8
9
  import {
9
- generateKey, isString, isPluralized, escape, generateHashedKey,
10
+ generateKey, isString, isPluralized, escape, generateHashedKey, sleep,
10
11
  } from './utils';
11
12
  import {
12
13
  sendEvent,
@@ -223,6 +224,120 @@ export default class TxNative {
223
224
  }
224
225
  }
225
226
 
227
+ /**
228
+ * Invalidate CDS cache
229
+ *
230
+ * @param {Object} params
231
+ * @param {Boolean} params.purge
232
+ * @returns {Object} Data
233
+ * @returns {Number} Data.count
234
+ * @returns {Number} Data.status
235
+ * @returns {Number} Data.token
236
+ */
237
+ async invalidateCDS(params = {}) {
238
+ if (!this.token) throw new Error('token is not defined');
239
+ if (!this.secret) throw new Error('secret is not defined');
240
+
241
+ const action = params.purge ? 'purge' : 'invalidate';
242
+ const res = await axios.post(`${this.cdsHost}/${action}`, {
243
+ }, {
244
+ headers: {
245
+ Authorization: `Bearer ${this.token}:${this.secret}`,
246
+ 'Accept-version': 'v2',
247
+ 'Content-Type': 'application/json;charset=utf-8',
248
+ 'X-NATIVE-SDK': `txjs/${__PLATFORM__}/${__VERSION__}`,
249
+ },
250
+ });
251
+ return res.data;
252
+ }
253
+
254
+ /**
255
+ * Push source content to CDS.
256
+ *
257
+ * Payload is in the following format:
258
+ * {
259
+ * <key>: {
260
+ * string: <string>,
261
+ * meta: {
262
+ * context: <string>
263
+ * developer_comment: <string>,
264
+ * character_limit: <number>,
265
+ * tags: <array>,
266
+ * occurrences: <array>,
267
+ * }
268
+ * },
269
+ * <key>: { .. }
270
+ * }
271
+ *
272
+ * @param {Object} payload
273
+ * @param {Object} params
274
+ * @param {Boolean} params.purge
275
+ * @param {Boolean} params.overrideTags
276
+ * @param {Boolean} params.noWait - do not wait for upload results
277
+ * @returns {Object} Data
278
+ * @returns {String} Data.jobUrl
279
+ * @returns {Number} Data.created
280
+ * @returns {Number} Data.updated
281
+ * @returns {Number} Data.skipped
282
+ * @returns {Number} Data.deleted
283
+ * @returns {Number} Data.failed
284
+ * @returns {String[]} Data.errors
285
+ * @returns {String} Data.status
286
+ */
287
+ async pushSource(payload, params = {}) {
288
+ if (!this.token) throw new Error('token is not defined');
289
+ if (!this.secret) throw new Error('secret is not defined');
290
+
291
+ const headers = {
292
+ Authorization: `Bearer ${this.token}:${this.secret}`,
293
+ 'Accept-version': 'v2',
294
+ 'Content-Type': 'application/json;charset=utf-8',
295
+ 'X-NATIVE-SDK': `txjs/${__PLATFORM__}/${__VERSION__}`,
296
+ };
297
+
298
+ const res = await axios.post(`${this.cdsHost}/content`, {
299
+ data: payload,
300
+ meta: {
301
+ purge: !!params.purge,
302
+ override_tags: !!params.overrideTags,
303
+ },
304
+ }, {
305
+ headers,
306
+ });
307
+
308
+ const jobUrl = `${this.cdsHost}${res.data.data.links.job}`;
309
+
310
+ if (params.noWait) {
311
+ return {
312
+ jobUrl,
313
+ };
314
+ }
315
+
316
+ let pollStatus = {
317
+ status: '',
318
+ };
319
+
320
+ do {
321
+ // eslint-disable-next-line no-await-in-loop
322
+ await sleep(1500);
323
+ // eslint-disable-next-line no-await-in-loop
324
+ const pollRes = await axios.get(jobUrl, {
325
+ headers,
326
+ });
327
+ const { data } = pollRes.data;
328
+ pollStatus = {
329
+ ...(data.details || {}),
330
+ errors: data.errors || [],
331
+ status: data.status,
332
+ };
333
+ } while (pollStatus.status === 'pending' || pollStatus.status === 'processing');
334
+
335
+ return {
336
+ jobUrl,
337
+ ...pollStatus,
338
+ };
339
+ }
340
+
226
341
  /**
227
342
  * Get remote project locales from CDS
228
343
  *
package/src/index.d.ts CHANGED
@@ -7,4 +7,24 @@ declare module '@transifex/native' {
7
7
  export const ThrowErrorPolicy: any;
8
8
  export const MessageFormatRenderer: any;
9
9
  export const createNativeInstance: any;
10
+
11
+ // Events
12
+ export const FETCHING_TRANSLATIONS = 'FETCHING_TRANSLATIONS';
13
+ export const TRANSLATIONS_FETCHED = 'TRANSLATIONS_FETCHED';
14
+ export const TRANSLATIONS_FETCH_FAILED = 'TRANSLATIONS_FETCH_FAILED';
15
+ export const LOCALE_CHANGED = 'LOCALE_CHANGED';
16
+ export const FETCHING_LOCALES = 'FETCHING_LOCALES';
17
+ export const LOCALES_FETCHED = 'LOCALES_FETCHED';
18
+ export const LOCALES_FETCH_FAILED = 'LOCALES_FETCH_FAILED';
19
+ export const onEvent: any;
20
+ export const offEvent: any;
21
+ export const sendEvent: any;
22
+
23
+ // Utils
24
+ export const escape: any;
25
+ export const isString: any;
26
+ export const generateHashedKey: any;
27
+ export const generateKey: any;
28
+ export const isPluralized: any;
29
+ export const normalizeLocale: any;
10
30
  }
package/src/utils.js CHANGED
@@ -215,3 +215,17 @@ export function isPluralized(string) {
215
215
 
216
216
  return true;
217
217
  }
218
+
219
+ /**
220
+ * Sleep in msec
221
+ *
222
+ * @export
223
+ * @param {Number} msec
224
+ */
225
+ export function sleep(msec) {
226
+ return new Promise((resolve) => {
227
+ setTimeout(() => {
228
+ resolve();
229
+ }, msec);
230
+ });
231
+ }