From e114cff32faa9645c32d98cebcfa12f10582b9ca Mon Sep 17 00:00:00 2001 From: KevinMidboe Date: Fri, 20 Oct 2017 18:11:34 +0200 Subject: [PATCH] This will be handling all http requests. --- client/app/components/http.jsx | 52 ++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 client/app/components/http.jsx diff --git a/client/app/components/http.jsx b/client/app/components/http.jsx new file mode 100644 index 0000000..63de148 --- /dev/null +++ b/client/app/components/http.jsx @@ -0,0 +1,52 @@ +import React from 'react'; + +import { getCookie } from './Cookie.jsx'; + +// class http { +// dispatch(obj) { +// console.log(obj); +// } + + function checkStatus(response) { + const hasError = (response.status < 200 || response.status >= 300) + if (hasError) { + throw response.text(); + } + return response; + } + + function parseJSON(response) { response.json(); } + + + +// * +// * Retrieve search results from tmdb with added seasoned information. +// * @param {String} uri query you want to search for +// * @param {Number} page representing pagination of results +// * @returns {Promise} succeeds if results were found + +// fetchSearch(uri) { +// fetch(uri, { +// method: 'GET', +// headers: { +// 'authorization': getCookie('token') +// }, +// }) +// .then(response => { + +// }); +// } +// } + +// export default http; + +export function fetchJSON(url, method, data) { + return fetch(url, { + method: method, + headers: new Headers({ + 'Content-Type': 'application/json', + 'authorization': getCookie('token'), + }), + body: JSON.stringify(data) + }).then(checkStatus).then(parseJSON); + } \ No newline at end of file