/
index.ts
474 lines (413 loc) · 14.9 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
import { AxiosError } from 'axios';
import { HttpClientError, toHttpClientError } from './http-client-error';
import { AxiosProvider } from './internal/axios/axiosProvider';
import { FetchClient } from './internal/fetch/fetchClient';
import { FetchClientJsonPayloadError } from './internal/fetch/fetchClientError';
import { RetryConfig, RetryService } from './internal/retry';
/**
* @internal
*/
export interface HttpClient {
get<T>(url: string, config?: { headers: Record<string, string> }): Promise<T>;
delete<T>(url: string, config?: { headers: Record<string, string> }): Promise<T>;
head<T>(url: string, config?: { headers: Record<string, string> }): Promise<T>;
post<T>(url: string, data?: unknown, config?: { headers: Record<string, string> }): Promise<T>;
put<T>(url: string, data?: unknown, config?: { headers: Record<string, string> }): Promise<T>;
patch<T>(url: string, data?: unknown, config?: { headers: Record<string, string> }): Promise<T>;
}
export type HttpClientType = 'axios' | 'fetch';
/**
* Authorization credentials
*/
export interface Credentials {
/** Personal Access Token */
token: string;
/** Yor Crowdin Enterprise organization name */
organization?: string;
/** API base URL */
baseUrl?: string;
}
/**
* Client Configuration
*/
export interface ClientConfig {
/** The type of HTTP client to be used for making requests */
httpClientType?: HttpClientType;
/** Instance of your HTTP client if needed */
httpClient?: HttpClient;
/** Custom User-Agent to be passed to the `User-Agent` header */
userAgent?: string;
/** Custom User-Agent to be passed to the `X-Crowdin-Integrations-User-Agent` header */
integrationUserAgent?: string;
/** Retry strategy configuration */
retryConfig?: RetryConfig;
/** Http request timeout in ms */
httpRequestTimeout?: number;
}
export interface ResponseList<T> {
data: ResponseObject<T>[];
pagination: Pagination;
}
export interface ResponseObject<T> {
data: T;
}
export interface Pagination {
offset: number;
limit: number;
}
export type PaginationOptions = Partial<Pagination>;
/**
* A JSON Patch document as defined by [RFC 6902](https://datatracker.ietf.org/doc/html/rfc6902#section-3)
*/
export interface PatchRequest {
/** Patch value */
value?: any;
/** Patch operation to perform */
op: PatchOperation;
/** A JSON Pointer as defined by [RFC 6901](https://datatracker.ietf.org/doc/html/rfc6901) */
path: string;
}
export type PatchOperation = 'add' | 'remove' | 'replace' | 'move' | 'copy' | 'test';
export interface DownloadLink {
url: string;
expireIn: string;
}
/**
* @internal
*/
export enum BooleanInt {
TRUE = 1,
FALSE = 0,
}
export interface Status<T> {
identifier: string;
status: string;
progress: number;
attributes: T;
createdAt: string;
updatedAt: string;
startedAt: string;
finishedAt: string;
eta: string;
}
export interface Attribute {
[key: string]: string;
}
/**
* @internal
*/
export class CrowdinError extends Error {
public code: number;
constructor(message: string, code: number) {
super(message);
this.code = code;
}
}
/**
* @internal
*/
export class CrowdinValidationError extends CrowdinError {
public validationCodes: { key: string; codes: string[] }[];
constructor(message: string, validationCodes: { key: string; codes: string[] }[]) {
super(message, 400);
this.validationCodes = validationCodes;
}
}
function isAxiosError(error: any): error is AxiosError {
return error instanceof AxiosError || !!error.response?.data;
}
/**
* @internal
*/
export function handleHttpClientError(error: HttpClientError): never {
let crowdinResponseErrors: any = null;
if (isAxiosError(error)) {
crowdinResponseErrors = (error.response?.data as any)?.errors || (error.response?.data as any)?.error;
} else if (error instanceof FetchClientJsonPayloadError) {
crowdinResponseErrors =
error.jsonPayload &&
typeof error.jsonPayload === 'object' &&
('errors' in error.jsonPayload || 'error' in error.jsonPayload)
? error.jsonPayload.errors || error.jsonPayload.error
: null;
}
if (Array.isArray(crowdinResponseErrors)) {
const validationCodes: { key: string; codes: string[] }[] = [];
const validationMessages: string[] = [];
crowdinResponseErrors.forEach((e: any) => {
if (typeof e.index === 'number' || typeof e.error?.key === 'number') {
throw new CrowdinValidationError(JSON.stringify(crowdinResponseErrors, null, 2), []);
}
if (e.error?.key && Array.isArray(e.error?.errors)) {
const codes: string[] = [];
e.error.errors.forEach((er: any) => {
if (er.message && er.code) {
codes.push(er.code);
validationMessages.push(er.message);
}
});
validationCodes.push({ key: e.error.key, codes });
}
});
const message = validationMessages.length === 0 ? 'Validation error' : validationMessages.join(', ');
throw new CrowdinValidationError(message, validationCodes);
} else if (crowdinResponseErrors?.message && crowdinResponseErrors?.code) {
throw new CrowdinError(crowdinResponseErrors.message, crowdinResponseErrors.code);
}
if (error instanceof Error) {
const code =
error instanceof AxiosError && error.response?.status
? error.response?.status
: error instanceof FetchClientJsonPayloadError
? error.statusCode
: 500;
throw new CrowdinError(error.message, code);
}
throw new CrowdinError(`unknown http error: ${String(error)}`, 500);
}
export abstract class CrowdinApi {
private static readonly CROWDIN_URL_SUFFIX: string = 'api.crowdin.com/api/v2';
private static readonly AXIOS_INSTANCE = new AxiosProvider().axios;
private static readonly FETCH_INSTANCE = new FetchClient();
/** @internal */
readonly token: string;
/** @internal */
readonly organization?: string;
/** @internal */
readonly url: string;
/** @internal */
readonly config: ClientConfig | undefined;
/** @internal */
readonly retryService: RetryService;
protected fetchAllFlag = false;
protected maxLimit: number | undefined;
/**
* @param credentials credentials
* @param config optional configuration of the client
*/
constructor(credentials: Credentials, config?: ClientConfig) {
this.token = credentials.token;
this.organization = credentials.organization;
if (credentials.baseUrl) {
this.url = credentials.baseUrl;
} else {
if (this.organization) {
this.url = `https://${this.organization}.${CrowdinApi.CROWDIN_URL_SUFFIX}`;
} else {
this.url = `https://${CrowdinApi.CROWDIN_URL_SUFFIX}`;
}
}
let retryConfig: RetryConfig;
if (config?.retryConfig) {
retryConfig = config.retryConfig;
} else {
retryConfig = {
waitInterval: 0,
retries: 0,
conditions: [],
};
}
this.retryService = new RetryService(retryConfig);
if (config?.httpRequestTimeout) {
CrowdinApi.FETCH_INSTANCE.withTimeout(config?.httpRequestTimeout);
CrowdinApi.AXIOS_INSTANCE.defaults.timeout = config?.httpRequestTimeout;
}
this.config = config;
}
graphql<T>(req: { query: string; operationName?: string; variables?: any }): Promise<ResponseObject<T>> {
let url;
if (this.organization) {
url = `https://${this.organization}.api.crowdin.com/api/graphql`;
} else {
url = 'https://api.crowdin.com/api/graphql';
}
return this.post<ResponseObject<T>>(url, req, this.defaultConfig());
}
protected addQueryParam(url: string, name: string, value?: string | number): string {
if (value) {
url += new RegExp(/\?.+=.*/g).test(url) ? '&' : '?';
url += `${name}=${this.encodeUrlParam(value)}`;
}
return url;
}
protected defaultConfig(): { headers: Record<string, string> } {
const config: {
headers: Record<string, string>;
} = {
headers: {
Authorization: `Bearer ${this.token}`,
},
};
if (this.config?.userAgent) {
config.headers['User-Agent'] = this.config.userAgent;
}
if (this.config?.integrationUserAgent) {
config.headers['X-Crowdin-Integrations-User-Agent'] = this.config.integrationUserAgent;
}
return config;
}
/** @internal */
get httpClient(): HttpClient {
if (this.config?.httpClient) {
return this.config.httpClient;
}
if (this.config?.httpClientType) {
switch (this.config.httpClientType) {
case 'axios':
return CrowdinApi.AXIOS_INSTANCE;
case 'fetch':
return CrowdinApi.FETCH_INSTANCE;
default:
return CrowdinApi.AXIOS_INSTANCE;
}
}
return CrowdinApi.AXIOS_INSTANCE;
}
public withFetchAll(maxLimit?: number): this {
this.fetchAllFlag = true;
this.maxLimit = maxLimit;
return this;
}
protected async getList<T = any>(
url: string,
limit?: number,
offset?: number,
config?: { headers: Record<string, string> },
): Promise<ResponseList<T>> {
const conf = config ?? this.defaultConfig();
if (this.fetchAllFlag) {
this.fetchAllFlag = false;
const maxAmount = this.maxLimit;
this.maxLimit = undefined;
return await this.fetchAll(url, conf, maxAmount);
} else {
url = this.addQueryParam(url, 'limit', limit);
url = this.addQueryParam(url, 'offset', offset);
return this.get(url, conf);
}
}
protected async fetchAll<T>(
url: string,
config: { headers: Record<string, string> },
maxAmount?: number,
): Promise<ResponseList<T>> {
let limit = 500;
if (maxAmount && maxAmount < limit) {
limit = maxAmount;
}
let offset = 0;
let resp: ResponseList<T> | undefined;
for (;;) {
let urlWithPagination = this.addQueryParam(url, 'limit', limit);
urlWithPagination = this.addQueryParam(urlWithPagination, 'offset', offset);
const e: ResponseList<T> = await this.get(urlWithPagination, config);
if (!resp) {
resp = e;
} else {
resp.data = resp.data.concat(e.data);
resp.pagination.limit += e.data.length;
}
if (e.data.length < limit || (maxAmount && resp.data.length >= maxAmount)) {
break;
} else {
offset += limit;
}
if (maxAmount && maxAmount < resp.data.length + limit) {
limit = maxAmount - resp.data.length;
}
}
return resp;
}
protected encodeUrlParam(param: string | number | boolean): string {
return encodeURIComponent(param);
}
//Http overrides
protected get<T>(url: string, config?: { headers: Record<string, string> }): Promise<T> {
return this.retryService
.executeAsyncFunc(() => this.httpClient.get<T>(url, config))
.catch((err: unknown) => handleHttpClientError(toHttpClientError(err)));
}
protected delete<T>(url: string, config?: { headers: Record<string, string> }): Promise<T> {
return this.retryService
.executeAsyncFunc(() => this.httpClient.delete<T>(url, config))
.catch((err: unknown) => handleHttpClientError(toHttpClientError(err)));
}
protected head<T>(url: string, config?: { headers: Record<string, string> }): Promise<T> {
return this.retryService
.executeAsyncFunc(() => this.httpClient.head<T>(url, config))
.catch((err: unknown) => handleHttpClientError(toHttpClientError(err)));
}
protected post<T>(url: string, data?: unknown, config?: { headers: Record<string, string> }): Promise<T> {
return this.retryService
.executeAsyncFunc(() => this.httpClient.post<T>(url, data, config))
.catch((err: unknown) => handleHttpClientError(toHttpClientError(err)));
}
protected put<T>(url: string, data?: unknown, config?: { headers: Record<string, string> }): Promise<T> {
return this.retryService
.executeAsyncFunc(() => this.httpClient.put<T>(url, data, config))
.catch((err: unknown) => handleHttpClientError(toHttpClientError(err)));
}
protected patch<T>(url: string, data?: unknown, config?: { headers: Record<string, string> }): Promise<T> {
return this.retryService
.executeAsyncFunc(() => this.httpClient.patch<T>(url, data, config))
.catch((err: unknown) => handleHttpClientError(toHttpClientError(err)));
}
}
let deprecationEmittedForOptionalParams = false;
function emitDeprecationWarning(): void {
if (!deprecationEmittedForOptionalParams) {
if (typeof process !== 'undefined' && typeof process.emitWarning === 'function') {
process.emitWarning(
'Passing optional parameters individually is deprecated. Pass a sole object instead',
'DeprecationWarning',
);
} else {
console.warn(
'DeprecationWarning: Passing optional parameters individually is deprecated. Pass a sole object instead',
);
}
deprecationEmittedForOptionalParams = true;
}
}
/**
* @internal
*/
export function isOptionalString(
parameter: string | unknown,
parameterInArgs: boolean,
): parameter is string | undefined {
if (typeof parameter === 'string' || typeof parameter === 'undefined') {
if (parameterInArgs) {
emitDeprecationWarning();
}
return true;
} else {
return false;
}
}
/**
* @internal
*/
export function isOptionalNumber(
parameter: number | unknown,
parameterInArgs: boolean,
): parameter is number | undefined {
if (typeof parameter === 'number' || typeof parameter === 'undefined') {
if (parameterInArgs) {
emitDeprecationWarning();
}
return true;
} else {
return false;
}
}
export interface ProjectRole {
name: string;
permissions: ProjectRolePermissions;
}
export interface ProjectRolePermissions {
allLanguages: boolean;
languagesAccess: {
[lang: string]: { allContent: boolean; workflowStepIds: number[] };
};
}