@transifex/api 2.4.1 → 3.2.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@transifex/api",
3
- "version": "2.4.1",
3
+ "version": "3.2.0",
4
4
  "description": "Transifex API SDK",
5
5
  "keywords": [
6
6
  "transifex",
@@ -29,9 +29,9 @@
29
29
  },
30
30
  "devDependencies": {
31
31
  "@babel/cli": "^7.15.4",
32
- "@babel/core": "^7.15.5",
32
+ "@babel/core": "^7.17.8",
33
33
  "@babel/preset-env": "^7.15.0",
34
- "@transifex/jsonapi": "^2.4.1",
34
+ "@transifex/jsonapi": "^3.2.0",
35
35
  "babel-eslint": "^10.1.0",
36
36
  "babel-loader": "^8.2.2",
37
37
  "eslint": "^7.32.0",
@@ -1,4 +1,31 @@
1
- import { JsonApi, Resource as JsonApiResource } from '@transifex/jsonapi'; /* eslint-disable-line max-classes-per-file */
1
+ /* eslint-disable no-await-in-loop, max-classes-per-file */
2
+ import { JsonApi, Resource as JsonApiResource } from '@transifex/jsonapi';
3
+
4
+ function sleep(sec) {
5
+ return new Promise((resolve) => {
6
+ setTimeout(() => {
7
+ resolve();
8
+ }, sec * 1000);
9
+ });
10
+ }
11
+
12
+ async function download({ interval = 5, ...data }) {
13
+ // 'this' is a Resource subclass
14
+ const resource = await this.create(data);
15
+ while (!resource.redirect) {
16
+ await sleep(interval);
17
+ await resource.reload();
18
+ }
19
+ return resource.redirect;
20
+ }
21
+
22
+ async function upload({ interval = 5, ...data }) {
23
+ const resource = await this.create(data);
24
+ while (resource.get('status') !== 'succeeded') {
25
+ await sleep(interval);
26
+ await resource.reload();
27
+ }
28
+ }
2
29
 
3
30
  export class TransifexApi extends JsonApi {
4
31
  static HOST = 'https://rest.api.transifex.com';
@@ -31,6 +58,28 @@ TransifexApi.register(ProjectWebhook, 'ProjectWebhook');
31
58
 
32
59
  class Resource extends JsonApiResource {
33
60
  static TYPE = 'resources';
61
+
62
+ async purge() {
63
+ let count = 0;
64
+ /* eslint-disable no-restricted-syntax */
65
+ for await (
66
+ const page of this.constructor.API.ResourceString
67
+ .filter({ resource: this })
68
+ .allPages()
69
+ ) {
70
+ count += page.data.length;
71
+ await this.constructor.API.ResourceString.bulkDelete(page.data);
72
+ }
73
+ /* eslint-enable */
74
+ return count;
75
+ }
76
+
77
+ async downloadSource(attributes = {}) {
78
+ return this.constructor.API.ResourceStringsAsyncDownload.download({
79
+ resource: this,
80
+ ...attributes,
81
+ });
82
+ }
34
83
  }
35
84
  TransifexApi.register(Resource, 'Resource');
36
85
 
@@ -39,15 +88,56 @@ class ResourceString extends JsonApiResource {
39
88
  }
40
89
  TransifexApi.register(ResourceString, 'ResourceString');
41
90
 
42
- class ResourceStringAsyncDownload extends JsonApiResource {
91
+ class ResourceStringsAsyncDownload extends JsonApiResource {
43
92
  static TYPE = 'resource_strings_async_downloads';
93
+
94
+ /**
95
+ * Download source content
96
+ *
97
+ * @static
98
+ * @param {object} data
99
+ * @param {Resource} data.resource
100
+ * @param {string} data.content_encoding - "text" (default) or "base64"
101
+ * @param {string} data.file_type - "default" (default) or "xliff"
102
+ * @param {boolean} data.pseudo - Generate mock string translations
103
+ * @param {string} data.callback_url - The URL that will be called when processing is complete
104
+ * @param {number} data.interval - Seconds to check status (5sec default)
105
+ * @memberof ResourceStringsAsyncDownload
106
+ * @returns - A URL to download the content
107
+ */
108
+ static async download(data) {
109
+ return download.call(this, {
110
+ content_encoding: 'text',
111
+ file_type: 'default',
112
+ ...data,
113
+ });
114
+ }
44
115
  }
45
- TransifexApi.register(ResourceStringAsyncDownload, 'ResourceStringAsyncDownload');
116
+ TransifexApi.register(ResourceStringsAsyncDownload, 'ResourceStringsAsyncDownload');
46
117
 
47
- class ResourceStringAsyncUpload extends JsonApiResource {
118
+ class ResourceStringsAsyncUpload extends JsonApiResource {
48
119
  static TYPE = 'resource_strings_async_uploads';
120
+
121
+ /**
122
+ * Upload source content
123
+ *
124
+ * Content is always in string format. To upload a binary file, convert it to
125
+ * Base64 and set the content_type='base64'.
126
+ *
127
+ * @static
128
+ * @param {object} data
129
+ * @param {Resource} data.resource
130
+ * @param {string} data.content
131
+ * @param {string} data.content_encoding - "text" (default) or "base64"
132
+ * @param {string} data.callback_url - The URL that will be called when processing is complete
133
+ * @param {number} data.interval - seconds to check status (5sec default)
134
+ * @memberof ResourceStringsAsyncUpload
135
+ */
136
+ static async upload(data) {
137
+ return upload.call(this, { content_encoding: 'text', ...data });
138
+ }
49
139
  }
50
- TransifexApi.register(ResourceStringAsyncUpload, 'ResourceStringAsyncUpload');
140
+ TransifexApi.register(ResourceStringsAsyncUpload, 'ResourceStringsAsyncUpload');
51
141
 
52
142
  class ResourceStringComment extends JsonApiResource {
53
143
  static TYPE = 'resource_string_comments';
@@ -71,43 +161,166 @@ TransifexApi.register(ContextScreenshot, 'ContextScreenshot');
71
161
 
72
162
  class OrganizationActivityReportsAsyncDownload extends JsonApiResource {
73
163
  static TYPE = 'organization_activity_reports_async_downloads';
164
+
165
+ /**
166
+ * Download organization activity report
167
+ *
168
+ * @static
169
+ * @param {object} data
170
+ * @param {Organization} data.organization
171
+ * @param {Language} data.language
172
+ * @param {User} data.user
173
+ * @param {string} data.date_from - Date, will be treated as UTC by the API
174
+ * @param {string} data.date_to - Date, will be treated as UTC by the API
175
+ * @param {number} data.interval - seconds to check status (5sec default)
176
+ * @memberof OrganizationActivityReportsAsyncDownload
177
+ * @returns - A URL to download the content
178
+ */
179
+ static async download(data) {
180
+ return download.call(this, data);
181
+ }
74
182
  }
75
183
  TransifexApi.register(OrganizationActivityReportsAsyncDownload, 'OrganizationActivityReportsAsyncDownload');
76
184
 
77
185
  class ProjectActivityReportsAsyncDownload extends JsonApiResource {
78
186
  static TYPE = 'project_activity_reports_async_downloads';
187
+
188
+ /**
189
+ * Download project activity report
190
+ *
191
+ * @static
192
+ * @param {object} data
193
+ * @param {Project} data.project
194
+ * @param {Language} data.language
195
+ * @param {User} data.user
196
+ * @param {string} data.date_from - Date, will be treated as UTC by the API
197
+ * @param {string} data.date_to - Date, will be treated as UTC by the API
198
+ * @param {number} data.interval - seconds to check status (5sec default)
199
+ * @memberof ProjectActivityReportsAsyncDownload
200
+ * @returns - A URL to download the content
201
+ */
202
+ static async download(data) {
203
+ return download.call(this, data);
204
+ }
79
205
  }
80
206
  TransifexApi.register(ProjectActivityReportsAsyncDownload, 'ProjectActivityReportsAsyncDownload');
81
207
 
82
208
  class ResourceActivityReportsAsyncDownload extends JsonApiResource {
83
209
  static TYPE = 'resource_activity_reports_async_downloads';
210
+
211
+ /**
212
+ * Download resource activity report
213
+ *
214
+ * @static
215
+ * @param {object} data
216
+ * @param {Resource} data.resource
217
+ * @param {Language} data.language
218
+ * @param {User} data.user
219
+ * @param {string} data.date_from - Date, will be treated as UTC by the API
220
+ * @param {string} data.date_to - Date, will be treated as UTC by the API
221
+ * @param {number} data.interval - seconds to check status (5sec default)
222
+ * @memberof ResourceActivityReportsAsyncDownload
223
+ * @returns - A URL to download the content
224
+ */
225
+ static async download(data) {
226
+ return download.call(this, data);
227
+ }
84
228
  }
85
229
  TransifexApi.register(ResourceActivityReportsAsyncDownload, 'ResourceActivityReportsAsyncDownload');
86
230
 
87
231
  class TeamActivityReportsAsyncDownload extends JsonApiResource {
88
232
  static TYPE = 'team_activity_reports_async_downloads';
233
+
234
+ /**
235
+ * Download team activity report
236
+ *
237
+ * @static
238
+ * @param {object} data
239
+ * @param {Team} data.team
240
+ * @param {Language} data.language
241
+ * @param {User} data.user
242
+ * @param {string} data.date_from - Date, will be treated as UTC by the API
243
+ * @param {string} data.date_to - Date, will be treated as UTC by the API
244
+ * @param {number} data.interval - seconds to check status (5sec default)
245
+ * @memberof TeamActivityReportsAsyncDownload
246
+ * @returns - A URL to download the content
247
+ */
248
+ static async download(data) {
249
+ return download.call(this, data);
250
+ }
89
251
  }
90
252
  TransifexApi.register(TeamActivityReportsAsyncDownload, 'TeamActivityReportsAsyncDownload');
91
253
 
92
- class ResourceLanguageStat extends JsonApiResource {
254
+ class ResourceLanguageStats extends JsonApiResource {
93
255
  static TYPE = 'resource_language_stats';
94
256
  }
95
- TransifexApi.register(ResourceLanguageStat, 'ResourceLanguageStat');
257
+ TransifexApi.register(ResourceLanguageStats, 'ResourceLanguageStats');
96
258
 
97
259
  class ResourceTranslation extends JsonApiResource {
98
260
  static TYPE = 'resource_translations';
99
261
  }
100
262
  TransifexApi.register(ResourceTranslation, 'ResourceTranslation');
101
263
 
102
- class ResourceTranslationAsyncDownload extends JsonApiResource {
264
+ class ResourceTranslationsAsyncDownload extends JsonApiResource {
103
265
  static TYPE = 'resource_translations_async_downloads';
266
+
267
+ /**
268
+ * Download translation file
269
+ *
270
+ * @static
271
+ * @param {object} data
272
+ * @param {Resource} data.resource
273
+ * @param {Language} data.language
274
+ * @param {string} data.content_encoding - "text" (default) or "base64"
275
+ * @param {string} data.file_type - "default" (default) or "xliff"
276
+ * @param {string} data.mode - "default" (default), "reviewed", "proofread",
277
+ * "translator", "untranslated", "onlytranslated", "onlyreviewed",
278
+ * "onlyproofread", "sourceastranslation"
279
+ * @param {string} data.callback_url - The URL that will be called when processing is complete
280
+ * @param {number} data.interval - seconds to check status (5sec default)
281
+ * @memberof ResourceTranslationsAsyncDownload
282
+ * @returns - A URL to download the content
283
+ */
284
+ static async download(data) {
285
+ return download.call(this, {
286
+ content_encoding: 'text',
287
+ file_type: 'default',
288
+ mode: 'default',
289
+ ...data,
290
+ });
291
+ }
104
292
  }
105
- TransifexApi.register(ResourceTranslationAsyncDownload, 'ResourceTranslationAsyncDownload');
293
+ TransifexApi.register(ResourceTranslationsAsyncDownload, 'ResourceTranslationsAsyncDownload');
106
294
 
107
- class ResourceTranslationAsyncUpload extends JsonApiResource {
295
+ class ResourceTranslationsAsyncUpload extends JsonApiResource {
108
296
  static TYPE = 'resource_translations_async_uploads';
297
+
298
+ /**
299
+ * Upload translations.
300
+ *
301
+ * Content is always in string format. To upload a binary file, convert it to
302
+ * Base64 and set the content_type='base64'.
303
+ *
304
+ * @static
305
+ * @param {object} data
306
+ * @param {Resource} data.resource
307
+ * @param {Language} data.language
308
+ * @param {string} data.content
309
+ * @param {string} data.content_encoding - "text" (default) or "base64"
310
+ * @param {string} file_type - "default" (default) or "xliff"
311
+ * @param {string} data.callback_url - The URL that will be called when processing is complete
312
+ * @param {number} data.interval - seconds to check status (5sec default)
313
+ * @memberof ResourceTranslationsAsyncUpload
314
+ */
315
+ static async upload(data) {
316
+ return upload.call(this, {
317
+ content_encoding: 'text',
318
+ file_type: 'default',
319
+ ...data,
320
+ });
321
+ }
109
322
  }
110
- TransifexApi.register(ResourceTranslationAsyncUpload, 'ResourceTranslationAsyncUpload');
323
+ TransifexApi.register(ResourceTranslationsAsyncUpload, 'ResourceTranslationsAsyncUpload');
111
324
 
112
325
  class TeamMembership extends JsonApiResource {
113
326
  static TYPE = 'team_memberships';
@@ -121,12 +334,54 @@ TransifexApi.register(Team, 'Team');
121
334
 
122
335
  class TmxAsyncDownload extends JsonApiResource {
123
336
  static TYPE = 'tmx_async_downloads';
337
+
338
+ /**
339
+ * Export project's TM as a TMX file
340
+ *
341
+ * @static
342
+ * @param {object} data
343
+ * @param {Project} data.project
344
+ * @param {Language} data.language
345
+ * @param {string} data.callback_url - The URL that will be called when processing is complete
346
+ * @param {number} data.interval - seconds to check status (5sec default)
347
+ * @memberof TmxAsyncDownload
348
+ * @returns - A URL to download the content
349
+ */
350
+ static async download(data) {
351
+ return download.call(this, { callback_url: null, ...data });
352
+ }
124
353
  }
125
354
  TransifexApi.register(TmxAsyncDownload, 'TmxAsyncDownload');
126
355
 
127
356
  class TmxAsyncUpload extends JsonApiResource {
128
357
  static TYPE = 'tmx_async_uploads';
358
+
359
+ /**
360
+ * Upload a TMX file
361
+ *
362
+ * Content is always in string format. To upload a binary file, convert it to
363
+ * Base64 and set the content_type='base64'.
364
+ *
365
+ * @static
366
+ * @param {object} data
367
+ * @param {Project} data.project
368
+ * @param {Language} data.language
369
+ * @param {string} data.content
370
+ * @param {string} data.content_encoding - "text" (default) or "base64"
371
+ * @param {boolean} data.override - Whether to override the TM
372
+ * @param {string} data.callback_url - The URL that will be called when processing is complete
373
+ * @param {number} data.interval - seconds to check status (5sec default)
374
+ * @memberof TmxAsyncUpload
375
+ */
376
+ static async upload(data) {
377
+ return upload.call(this, { content_encoding: 'text', ...data });
378
+ }
129
379
  }
130
380
  TransifexApi.register(TmxAsyncUpload, 'TmxAsyncUpload');
131
381
 
382
+ class ResourceStringsRevision extends JsonApiResource {
383
+ static TYPE = 'resource_strings_revisions';
384
+ }
385
+ TransifexApi.register(ResourceStringsRevision, 'ResourceStringsRevision');
386
+
132
387
  export const transifexApi = new TransifexApi();
@@ -1 +0,0 @@
1
- /*! For license information please see browser.jsonapi.js.LICENSE.txt */
@@ -1 +0,0 @@
1
- /*! For license information please see node.jsonapi.js.LICENSE.txt */