Skip to content

Commit

Permalink
Merge branch 'release-1.4.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
Anshul Sharma committed Jun 4, 2020
2 parents bf99f2d + 912efa2 commit d222ef0
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 33 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
63 changes: 34 additions & 29 deletions src/core/fetchRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand All @@ -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;
};
Expand Down
28 changes: 26 additions & 2 deletions test/core/fetchRequest/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
});
});
Expand Down

0 comments on commit d222ef0

Please sign in to comment.