Skip to content

Commit

Permalink
utils: import dataFetch
Browse files Browse the repository at this point in the history
  • Loading branch information
lohitha02 committed Dec 13, 2019
1 parent 406696f commit 1ebd60f
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/utils/dataFetch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import fetch from 'isomorphic-fetch';

const API_URL = 'https://api.amfoss.in/';

export default ({ query, variables }) => {
const body = {
query,
variables,
};

const apiConfig = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(body),
};

return fetch(API_URL, apiConfig).then(function(response) {
const contentType = response.headers.get('content-type');
if (response.ok) {
if (contentType && contentType.indexOf('application/json') !== -1) {
return response.json().then(json => json);
}
if (contentType && contentType.indexOf('text') !== -1) {
return response.text().then(text => text);
}
return response;
}
console.error(`Response status ${response.status} during dataFetch for url ${response.url}.`);
throw response;
});
};

0 comments on commit 1ebd60f

Please sign in to comment.