Skip to content

Commit

Permalink
Merge pull request #20 from transifex/retry_fetch
Browse files Browse the repository at this point in the history
Retry languages/translations if status is 202
  • Loading branch information
kbairak authored Sep 3, 2020
2 parents e7050b7 + 7a8476d commit 3557576
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 10 deletions.
34 changes: 24 additions & 10 deletions packages/native/src/TxNative.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,18 @@ export default class TxNative {
// contact CDS
try {
sendEvent(FETCHING_TRANSLATIONS, localeCode, this);
const response = await axios.get(`${this.cdsHost}/content/${localeCode}`, {
headers: {
Authorization: `Bearer ${this.token}`,
},
});
let response;
let lastResponseStatus = 202;
while (lastResponseStatus === 202) {
/* eslint-disable no-await-in-loop */
response = await axios.get(`${this.cdsHost}/content/${localeCode}`, {
headers: {
Authorization: `Bearer ${this.token}`,
},
});
/* eslint-enable no-await-in-loop */
lastResponseStatus = response.status;
}

const { data } = response;
if (data && data.data) {
Expand Down Expand Up @@ -191,11 +198,18 @@ export default class TxNative {
// contact CDS
try {
sendEvent(FETCHING_LOCALES, null, this);
const response = await axios.get(`${this.cdsHost}/languages`, {
headers: {
Authorization: `Bearer ${this.token}`,
},
});
let response;
let lastResponseStatus = 202;
while (lastResponseStatus === 202) {
/* eslint-disable no-await-in-loop */
response = await axios.get(`${this.cdsHost}/languages`, {
headers: {
Authorization: `Bearer ${this.token}`,
},
});
/* eslint-enable no-await-in-loop */
lastResponseStatus = response.status;
}

const { data } = response;
if (data && data.data) {
Expand Down
31 changes: 31 additions & 0 deletions packages/native/tests/tx.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,4 +258,35 @@ describe('tx instance', () => {
foo: 'bar',
});
});

it('retries fetching languages', async () => {
tx.init({ token: 'abcd' });
nock(tx.cdsHost)
.get('/languages')
.twice()
.reply(202)
.get('/languages')
.reply(200, {
data: [{
name: 'Greek',
code: 'el',
localized_name: 'Ελληνικά',
rtl: false,
}],
});
const locales = await tx.getRemoteLocales({ refresh: true });
expect(locales).to.deep.equal(['el']);
});

it('retries fetching translations', async () => {
tx.init({ token: 'abcd' });
nock(tx.cdsHost)
.get('/content/el')
.twice()
.reply(202)
.get('/content/el')
.reply(200, { data: { source: { string: 'translation' } } });
await tx.fetchTranslations('el');
expect(tx.cache.get('source', 'el')).to.equal('translation');
});
});

0 comments on commit 3557576

Please sign in to comment.