@translated/lara 2.0.0-beta.5 → 2.0.0-beta.7

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/lib/Documents.js CHANGED
@@ -3,7 +3,7 @@ import { DocumentStatus, } from './models';
3
3
  import toFormData from './utils/formdata';
4
4
  import toSnakeCase from './utils/toSnakeCase';
5
5
  import sleep from './utils/sleep';
6
- import { LaraApiError, TimeoutError } from './utils/errors';
6
+ import { LaraApiError, LaraTimeoutError } from './utils/errors';
7
7
  import toQueryString from './utils/toQueryString';
8
8
  import isNode from './utils/isNode';
9
9
  export default class Documents {
@@ -75,8 +75,8 @@ export default class Documents {
75
75
  if (status === DocumentStatus.TRANSLATED)
76
76
  return await this.download(id, downloadOptions);
77
77
  if (status === DocumentStatus.ERROR)
78
- throw new LaraApiError(500, 'DocumentError', errorReason);
78
+ throw new LaraApiError(500, errorReason, 'unknown');
79
79
  }
80
- throw new TimeoutError();
80
+ throw new LaraTimeoutError();
81
81
  }
82
82
  }
package/lib/Glossaries.js CHANGED
@@ -1,4 +1,4 @@
1
- import { TimeoutError } from './utils/errors';
1
+ import { LaraTimeoutError } from './utils/errors';
2
2
  import toFormData from './utils/formdata';
3
3
  import sleep from './utils/sleep';
4
4
  import toQueryString from './utils/toQueryString';
@@ -46,7 +46,7 @@ export default class Glossaries {
46
46
  const start = Date.now();
47
47
  while (gImport.progress < 1.0) {
48
48
  if (maxWaitTime && Date.now() - start > maxWaitTime)
49
- throw new TimeoutError();
49
+ throw new LaraTimeoutError();
50
50
  await sleep(this.pollingInterval);
51
51
  gImport = await this.getImportStatus(gImport.id);
52
52
  updateCallback && updateCallback(gImport);
package/lib/Memories.js CHANGED
@@ -1,4 +1,4 @@
1
- import { TimeoutError } from './utils/errors';
1
+ import { LaraTimeoutError } from './utils/errors';
2
2
  import toFormData from './utils/formdata';
3
3
  import sleep from './utils/sleep';
4
4
  export default class Memories {
@@ -79,7 +79,7 @@ export default class Memories {
79
79
  const start = Date.now();
80
80
  while (mImport.progress < 1.0) {
81
81
  if (maxWaitTime && Date.now() - start > maxWaitTime)
82
- throw new TimeoutError();
82
+ throw new LaraTimeoutError();
83
83
  await sleep(this.pollingInterval);
84
84
  mImport = await this.getImportStatus(mImport.id);
85
85
  updateCallback && updateCallback(mImport);
package/lib/Translator.js CHANGED
@@ -4,6 +4,7 @@ import { version } from './sdk-version';
4
4
  import Glossaries from './Glossaries';
5
5
  import Documents from './Documents';
6
6
  import toCamelCase from './utils/toCamelCase';
7
+ import { LaraApiError } from './utils/errors';
7
8
  const DEFAULT_BASE_URL = 'https://api.laratranslate.com';
8
9
  export class Translator {
9
10
  constructor(accessKey, credentials, authToken, options) {
@@ -35,17 +36,16 @@ export class Translator {
35
36
  }
36
37
  return response;
37
38
  }, async (error) => {
39
+ var _a, _b, _c, _d, _e, _f;
38
40
  const originalRequest = error.config;
39
- if (originalRequest.isRetry)
40
- return Promise.reject(error);
41
- if (error.response && error.response.status === 401 && error.response.data.message === 'jwt expired') {
41
+ if (!originalRequest.isRetry && error.response && error.response.status === 401 && error.response.data.message === 'jwt expired') {
42
42
  this.token = undefined;
43
43
  const token = await this.authenticate();
44
44
  originalRequest.isRetry = true;
45
45
  originalRequest.headers.Authorization = `Bearer ${token}`;
46
46
  return this.client(originalRequest);
47
47
  }
48
- return Promise.reject(error);
48
+ return Promise.reject(new LaraApiError((_b = (_a = error.response) === null || _a === void 0 ? void 0 : _a.status) !== null && _b !== void 0 ? _b : 500, (_e = (_d = (_c = error.response) === null || _c === void 0 ? void 0 : _c.data) === null || _d === void 0 ? void 0 : _d.message) !== null && _e !== void 0 ? _e : error.message, (_f = error.response) === null || _f === void 0 ? void 0 : _f.headers['x-transaction-id']));
49
49
  });
50
50
  this.memories = new Memories(this.client);
51
51
  this.glossaries = new Glossaries(this.client);
@@ -58,30 +58,30 @@ export class Translator {
58
58
  return this.token;
59
59
  // If we have a refresh token, use it to get a new token
60
60
  if (this.refreshToken) {
61
- const { data } = await axios.post(`${(_b = (_a = this.options) === null || _a === void 0 ? void 0 : _a.serverUrl) !== null && _b !== void 0 ? _b : DEFAULT_BASE_URL}/v2/auth/refresh`, null, {
61
+ const { data, headers } = await axios.post(`${(_b = (_a = this.options) === null || _a === void 0 ? void 0 : _a.serverUrl) !== null && _b !== void 0 ? _b : DEFAULT_BASE_URL}/v2/auth/refresh`, null, {
62
62
  headers: {
63
63
  authorization: `Bearer ${this.refreshToken}`,
64
64
  'X-Lara-Date': new Date().toUTCString(),
65
65
  },
66
66
  });
67
67
  this.token = data.token;
68
- this.refreshToken = data.refresh_token;
68
+ this.refreshToken = headers['x-lara-refresh-token'];
69
69
  return this.token;
70
70
  }
71
71
  else if (this.accessKey) {
72
72
  // Use access key to get a new token
73
- const { data } = await axios.post(`${(_d = (_c = this.options) === null || _c === void 0 ? void 0 : _c.serverUrl) !== null && _d !== void 0 ? _d : DEFAULT_BASE_URL}/v2/auth`, this.accessKey, {
73
+ const { data, headers } = await axios.post(`${(_d = (_c = this.options) === null || _c === void 0 ? void 0 : _c.serverUrl) !== null && _d !== void 0 ? _d : DEFAULT_BASE_URL}/v2/auth`, this.accessKey, {
74
74
  headers: {
75
75
  'X-Lara-Date': new Date().toUTCString(),
76
76
  },
77
77
  });
78
78
  this.token = data.token;
79
- this.refreshToken = data.refresh_token;
79
+ this.refreshToken = headers['x-lara-refresh-token'];
80
80
  return this.token;
81
81
  }
82
82
  else if (this.credentials) {
83
83
  // Use credentials to get a new token
84
- const { data } = await axios.post(
84
+ const { data, headers } = await axios.post(
85
85
  //FIXME: client id should be set
86
86
  `${(_f = (_e = this.options) === null || _e === void 0 ? void 0 : _e.serverUrl) !== null && _f !== void 0 ? _f : DEFAULT_BASE_URL}/v2/auth/Unknown/login`, this.credentials, {
87
87
  headers: {
@@ -89,7 +89,7 @@ export class Translator {
89
89
  },
90
90
  });
91
91
  this.token = data.token;
92
- this.refreshToken = data.refresh_token;
92
+ this.refreshToken = headers['x-lara-refresh-token'];
93
93
  return this.token;
94
94
  }
95
95
  else if (this.authToken) {
@@ -10,3 +10,6 @@ export type Credentials = {
10
10
  email: string;
11
11
  password: string;
12
12
  };
13
+ export type AuthenticationResponse = {
14
+ token: string;
15
+ };
@@ -1 +1 @@
1
- export declare const version = "2.0.0-beta.4";
1
+ export declare const version = "2.0.0-beta.6";
@@ -1 +1 @@
1
- export const version = '2.0.0-beta.4';
1
+ export const version = '2.0.0-beta.6';
@@ -1,10 +1,10 @@
1
1
  export declare class LaraError extends Error {
2
2
  }
3
- export declare class TimeoutError extends LaraError {
3
+ export declare class LaraTimeoutError extends LaraError {
4
4
  }
5
5
  export declare class LaraApiError extends LaraError {
6
6
  readonly statusCode: number;
7
- readonly type: string;
8
7
  readonly message: string;
9
- constructor(statusCode: number, type: string, message: string);
8
+ readonly idTransaction: string;
9
+ constructor(statusCode: number, message: string, idTransaction: string);
10
10
  }
@@ -1,12 +1,12 @@
1
1
  export class LaraError extends Error {
2
2
  }
3
- export class TimeoutError extends LaraError {
3
+ export class LaraTimeoutError extends LaraError {
4
4
  }
5
5
  export class LaraApiError extends LaraError {
6
- constructor(statusCode, type, message) {
6
+ constructor(statusCode, message, idTransaction) {
7
7
  super(message);
8
8
  this.statusCode = statusCode;
9
- this.type = type;
10
9
  this.message = message;
10
+ this.idTransaction = idTransaction;
11
11
  }
12
12
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@translated/lara",
3
- "version": "2.0.0-beta.5",
3
+ "version": "2.0.0-beta.7",
4
4
  "main": "./lib/index.js",
5
5
  "types": "./lib/index.d.ts",
6
6
  "engines": {