Skip to content

Commit

Permalink
refactor: move parseResponse to http module
Browse files Browse the repository at this point in the history
  • Loading branch information
pablopalacios committed Dec 27, 2021
1 parent 1bbdec6 commit a10b02c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 19 deletions.
13 changes: 1 addition & 12 deletions libs/fetcher.client.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,6 @@ var DEFAULT_TIMEOUT = 3000;
var MAX_URI_LEN = 2048;
var OP_READ = 'read';

function parseResponse(response) {
if (response && response.responseText) {
try {
return JSON.parse(response.responseText);
} catch (e) {
return null;
}
}
return null;
}

/**
* A RequestClient instance represents a single fetcher request.
* The constructor requires `operation` (CRUD) and `resource`.
Expand Down Expand Up @@ -188,7 +177,7 @@ function executeRequest(request, resolve, reject) {
if (err) {
return reject(err);
}
resolve(parseResponse(response));
resolve(response);
};

var config = Object.assign(
Expand Down
13 changes: 12 additions & 1 deletion libs/util/http.client.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@ var DEFAULT_CONFIG = {

var INITIAL_ATTEMPT = 0;

function parseResponse(response) {
if (response && response.responseText) {
try {
return JSON.parse(response.responseText);
} catch (e) {
return null;
}
}
return null;
}

function normalizeHeaders(rawHeaders, method, isCors) {
var headers = Object.assign({}, rawHeaders);

Expand Down Expand Up @@ -113,7 +124,7 @@ function doRequest(method, url, headers, data, config, attempt, callback) {
withCredentials: config.withCredentials,
on: {
success: function (err, response) {
callback(null, response);
callback(null, parseResponse(response));
},
failure: function (err, response) {
if (!shouldRetry(method, config, response.status, attempt)) {
Expand Down
10 changes: 4 additions & 6 deletions tests/unit/libs/util/http.client.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('Client HTTP', function () {
describe('#Successful requests', function () {
beforeEach(function () {
responseStatus = 200;
mockBody = 'BODY';
mockBody = { data: 'BODY' };
});

it('GET', function (done) {
Expand All @@ -45,8 +45,7 @@ describe('Client HTTP', function () {
expect(options.headers.get('X-Foo')).to.equal('foo');
expect(options.method).to.equal('GET');
expect(err).to.equal(null);
expect(response.statusCode).to.equal(200);
expect(response.responseText).to.equal('BODY');
expect(response).to.deep.equal(mockBody);
done();
});
});
Expand Down Expand Up @@ -81,7 +80,7 @@ describe('Client HTTP', function () {
describe('#Successful CORS requests', function () {
beforeEach(function () {
responseStatus = 200;
mockBody = 'BODY';
mockBody = { data: 'BODY' };
sinon.spy(global, 'Request');
});

Expand Down Expand Up @@ -109,8 +108,7 @@ describe('Client HTTP', function () {
expect(options.headers.get('X-Foo')).to.equal('foo');
expect(options.method).to.equal('GET');
expect(err).to.equal(null);
expect(response.statusCode).to.equal(200);
expect(response.responseText).to.equal('BODY');
expect(response).to.deep.equal(mockBody);

sinon.assert.calledWith(
Request,
Expand Down

0 comments on commit a10b02c

Please sign in to comment.