Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import fetch from 'isomorphic-fetch'
- const defaultHeaders = {
- Accept: 'application/json',
- 'Content-Type': 'application/json',
- }
- /**
- * @return {Object} headers with token
- */
- function buildHeaders() {
- //const authToken = localStorage.getItem('we.token')
- //return { ...defaultHeaders, Authorization: authToken }
- return defaultHeaders
- }
- /**
- * @param {Object} response - standart isomorphic-fetch response object
- * @return {Object} success isomorphic-fetch response
- */
- export function checkStatus(response) {
- let json = response.json() // http://stackoverflow.com/a/29475662/1916578
- if (response.status >= 200 && response.status < 300) {
- return json
- } else {
- return json.then(window.Promise.reject.bind(window.Promise))
- }
- }
- /**
- * @param {String} url - api request url
- * @return {Object} fetch response (promise)
- */
- export function httpGet(url) {
- return fetch(url, {
- headers: buildHeaders(),
- })
- .then(checkStatus)
- }
- /**
- * @param {String} url - api request url
- * @return {Object} fetch response (promise)
- */
- export function httpPost(url, data) {
- const body = JSON.stringify(data)
- return fetch(url, {
- method: 'post',
- headers: buildHeaders(),
- body: body,
- })
- .then(checkStatus)
- }
- /**
- * @param {String} url - api request url
- * @return {Object} fetch response (promise)
- */
- export function httpDelete(url) {
- return fetch(url, {
- method: 'delete',
- headers: buildHeaders(),
- })
- .then(checkStatus)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement