Skip to content

Commit

Permalink
Make responseHandlers as a helper & wrap response.
Browse files Browse the repository at this point in the history
  • Loading branch information
gillchristian committed Nov 11, 2016
1 parent 52f29e5 commit 3b19a69
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 22 deletions.
36 changes: 36 additions & 0 deletions lib/helpers/responseHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* Wrapp a response
*
* @param {Object} response response object
* @param {String} reader type of reader to use on response body
* @return {Promise} resolves to the wrapped read response
*/
function wrapResponse(response, reader) {
return response[reader]()
.then(data => ({
headers: response.headers,
status: response.status,
data
}));
}

/**
* Reads or rejects a fetch request response
*
* @param {Object} response response object
* @return {Promise} read or rejection promise
*/
function responseHandler(response) {
if (!response.ok) {
const err = new Error(response.statusText);
err.status = response.status;
err.headers = response.headers;
return Promise.reject(err);
}
if (response.headers.get('Content-Type') === 'application/json') {
return wrapResponse(response, 'json');
}
return wrapResponse(response, 'text');
}

module.exports = responseHandler;
21 changes: 5 additions & 16 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
require('whatwg-fetch');
const buildQuery = require('trae-query').buildQuery;

const Middleware = require('./middleware');
const Config = require('./config');
const skip = require('./utils').skip;
const Middleware = require('./middleware');
const Config = require('./config');
const skip = require('./utils').skip;
const responseHandler = require('./helpers/responseHandler');

class Trae {
constructor(config = {}) {
Expand Down Expand Up @@ -48,7 +49,7 @@ class Trae {
_fetch(url, config) {
return this._middleware.resolveRequests(config)
.then(config => fetch(url, config))
.then(res => this.constructor._responseHandler(res))
.then(res => responseHandler(res))
.then(res => this._middleware.resolveResponses(res));
}

Expand All @@ -74,18 +75,6 @@ class Trae {
});
}

static _responseHandler(response) {
if (!response.ok) {
const err = new Error(response.statusText);
err.status = response.status;
return Promise.reject(err);
}
if (response.headers.get('Content-Type') === 'application/json') {
return response.json();
}
return response.text();
}

}

module.exports = new Trae();
88 changes: 88 additions & 0 deletions test/__snapshots__/index.spec.js.snap
Original file line number Diff line number Diff line change
@@ -1,3 +1,91 @@
exports[`HTTP -> http del makes a DELETE request to baseURL + path 1`] = `
Object {
"data": "Deleted!",
"headers": Headers {
"_headers": Object {
"content-type": Array [
"application/text",
],
},
},
"status": 200,
}
`;

exports[`HTTP -> http get makes a GET request to baseURL + path 1`] = `
Object {
"data": Object {
"foo": "bar",
},
"headers": Headers {
"_headers": Object {
"content-type": Array [
"application/json",
],
},
},
"status": 200,
}
`;

exports[`HTTP -> http head makes a HEAD request to baseURL + path 1`] = `
Object {
"data": "",
"headers": Headers {
"_headers": Object {},
},
"status": 200,
}
`;

exports[`HTTP -> http patch makes a PATCH request to baseURL + path 1`] = `
Object {
"data": Object {
"foo": "bar",
},
"headers": Headers {
"_headers": Object {
"content-type": Array [
"application/json",
],
},
},
"status": 200,
}
`;

exports[`HTTP -> http post makes a POST request to baseURL + path 1`] = `
Object {
"data": Object {
"foo": "bar",
},
"headers": Headers {
"_headers": Object {
"content-type": Array [
"application/json",
],
},
},
"status": 200,
}
`;

exports[`HTTP -> http put makes a PUT request to baseURL + path 1`] = `
Object {
"data": Object {
"foo": "bar",
},
"headers": Headers {
"_headers": Object {
"content-type": Array [
"application/json",
],
},
},
"status": 200,
}
`;

exports[`HTTP -> http response not ok gets rejected 1`] = `[Error: Not Found]`;

exports[`trae defaults returns the current default config when no params are passed 1`] = `
Expand Down
12 changes: 6 additions & 6 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ describe('HTTP -> http', () => {

return trae.get(url)
.then((res) => {
expect(res).toEqual({ foo: 'bar' });
expect(res).toMatchSnapshot();
expect(fetchMock.called(url)).toBeTruthy();
expect(fetchMock.lastUrl()).toBe(url);
expect(fetchMock.lastOptions().method).toBe('get');
Expand All @@ -129,7 +129,7 @@ describe('HTTP -> http', () => {

return trae.delete(url)
.then((res) => {
expect(res).toEqual('Deleted!');
expect(res).toMatchSnapshot();
expect(fetchMock.called(url)).toBeTruthy();
expect(fetchMock.lastUrl()).toBe(url);
expect(fetchMock.lastOptions().method).toBe('delete');
Expand All @@ -149,7 +149,7 @@ describe('HTTP -> http', () => {

return trae.head(url)
.then((res) => {
expect(res).toEqual('');
expect(res).toMatchSnapshot();
expect(fetchMock.called(url)).toBeTruthy();
expect(fetchMock.lastUrl()).toBe(url);
expect(fetchMock.lastOptions().method).toBe('head');
Expand All @@ -173,7 +173,7 @@ describe('HTTP -> http', () => {

return trae.post(url, { foo: 'bar' })
.then((res) => {
expect(res).toEqual({ foo: 'bar' });
expect(res).toMatchSnapshot();
expect(fetchMock.called(url)).toBeTruthy();
expect(fetchMock.lastUrl()).toBe(url);
expect(fetchMock.lastOptions().method).toBe('post');
Expand All @@ -197,7 +197,7 @@ describe('HTTP -> http', () => {

return trae.put(url, { foo: 'bar' })
.then((res) => {
expect(res).toEqual({ foo: 'bar' });
expect(res).toMatchSnapshot();
expect(fetchMock.called(url)).toBeTruthy();
expect(fetchMock.lastUrl()).toBe(url);
expect(fetchMock.lastOptions().method).toBe('put');
Expand All @@ -221,7 +221,7 @@ describe('HTTP -> http', () => {

return trae.patch(url, { foo: 'bar' })
.then((res) => {
expect(res).toEqual({ foo: 'bar' });
expect(res).toMatchSnapshot();
expect(fetchMock.called(url)).toBeTruthy();
expect(fetchMock.lastUrl()).toBe(url);
expect(fetchMock.lastOptions().method).toBe('patch');
Expand Down

0 comments on commit 3b19a69

Please sign in to comment.