diff --git a/package-lock.json b/package-lock.json index 130352e..4aa944b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "@oat-sa/tao-core-sdk", - "version": "1.4.0", + "version": "1.4.1", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index be6233f..94489cc 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@oat-sa/tao-core-sdk", - "version": "1.4.0", + "version": "1.4.1", "displayName": "TAO Core SDK", "description": "Core libraries of TAO", "homepage": "https://github.com/oat-sa/tao-core-sdk-fe#readme", diff --git a/src/core/fetchRequest.js b/src/core/fetchRequest.js index d03a1af..31752ce 100644 --- a/src/core/fetchRequest.js +++ b/src/core/fetchRequest.js @@ -49,12 +49,14 @@ const requestFactory = (url, options) => { }); } - flow = flow.then(() => Promise.race([ - fetch(url, options), - new Promise((resolve, reject) => { - setTimeout(() => reject(new Error('Timeout')), options.timeout); - }) - ])); + flow = flow.then(() => + Promise.race([ + fetch(url, options), + new Promise((resolve, reject) => { + setTimeout(() => reject(new Error('Timeout')), options.timeout); + }) + ]) + ); if (options.jwtTokenHandler) { flow = flow.then(response => { @@ -81,31 +83,34 @@ const requestFactory = (url, options) => { */ let responseCode; - flow = flow.then(response => { - originalResponse = response; - responseCode = response.status; - return response.json().catch(() => ({})); - }) - .then(response => { - // successful request - if (responseCode === 200 || response.success === true) { - return response; - } + flow = flow + .then(response => { + originalResponse = response; + responseCode = response.status; + return response.json().catch(() => ({})); + }) + .then(response => { + if (responseCode === 204) { + return null; + } - if (responseCode === 204) { - return null; - } + // successful request + if ((responseCode >= 200 && responseCode < 300) || (response && response.success === true)) { + return response; + } - // create error - let err; - if (response.errorCode) { - err = new Error(`${response.errorCode} : ${response.errorMsg || response.errorMessage || response.error}`); - } else { - err = new Error(`${responseCode} : Request error`); - } - err.response = originalResponse; - throw err; - }); + // create error + let err; + if (response.errorCode) { + err = new Error( + `${response.errorCode} : ${response.errorMsg || response.errorMessage || response.error}` + ); + } else { + err = new Error(`${responseCode} : Request error`); + } + err.response = originalResponse; + throw err; + }); return flow; }; diff --git a/test/core/fetchRequest/test.js b/test/core/fetchRequest/test.js index a7ff03b..84b6cbb 100644 --- a/test/core/fetchRequest/test.js +++ b/test/core/fetchRequest/test.js @@ -73,6 +73,30 @@ define(['core/fetchRequest', 'core/jwt/jwtTokenHandler', 'fetch-mock'], ( }); }); + QUnit.test('request returns with correct response if status code is 2XX with response payload', assert => { + assert.expect(1); + const done = assert.async(); + + fetchMock.mock('/foo', new Response(JSON.stringify({ success: true, data: { ping: 123 } }), { status: 206 })); + + request('/foo').then(response => { + assert.deepEqual(response.data, { ping: 123 }); + done(); + }); + }); + + QUnit.test('request returns with correct response if status code is 2XX with empty response payload', assert => { + assert.expect(1); + const done = assert.async(); + + fetchMock.mock('/foo', new Response({}, { status: 202 })); + + request('/foo').then(response => { + assert.deepEqual(response, {}); + done(); + }); + }); + QUnit.test('request returns with a correct error response', assert => { assert.expect(2); const done = assert.async(); @@ -93,13 +117,13 @@ define(['core/fetchRequest', 'core/jwt/jwtTokenHandler', 'fetch-mock'], ( fetchMock.mock( '/foo', new Response(JSON.stringify({ success: false, errorCode: 'ABC123', errorMessage: 'Cannot trigger ABC' }), { - status: 201 + status: 406 }) ); request('/foo').catch(error => { assert.equal(error.message, 'ABC123 : Cannot trigger ABC'); - assert.equal(error.response.status, 201); + assert.equal(error.response.status, 406); done(); }); });