Created
July 12, 2019 20:55
-
-
Save olegpolyakov/8e7729c58757bfbe106a07133a17fd9f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { stringify } from "query-string"; | |
export const HTTP_HEADER_TYPES = { | |
json: "application/json", | |
text: "application/text", | |
form: "application/x-www-form-urlencoded", | |
multipart: "multipart/form-data" | |
}; | |
const status = response => { | |
if (response.ok) { | |
return Promise.resolve(response); | |
} | |
return Promise.reject(new Error(response.statusText)); | |
}; | |
const deserializeResponse = response => response.json(); | |
const encodeRequests = (params, contentType) => { | |
switch (contentType) { | |
case HTTP_HEADER_TYPES.form: { | |
return stringify(params); | |
} | |
case HTTP_HEADER_TYPES.multipart: { | |
var formData = new FormData(); | |
for (var k in params) { | |
formData.append(k, params[k]); | |
} | |
return formData; | |
} | |
default: | |
return JSON.stringify(params); | |
} | |
} | |
export const get = (url, params) => { | |
const prefix = url.endsWith('/') ? url : `${url}/`; | |
const queryString = params ? `?${stringify(params)}/` : ''; | |
return fetch(`${url}${queryString}`) | |
.then(status) | |
.then(deserializeResponse) | |
.catch(error => Promise.reject(new Error(error))); | |
}; | |
export const post = (url, params, options = {}) => request(url, params, options, 'post'); | |
export const patch = (url, params, options = {}) => request(url, params, options, 'patch'); | |
export const put = (url, params, options = {}) => request(url, params, options, 'put'); | |
const request = (url, params, options={}, method="post") => { | |
const {includeCsrf, contentType} = options; | |
const headers = new Headers(); | |
headers.append("Content-Type", contentType || HTTP_HEADER_TYPES.json); | |
if (includeCsrf) { | |
headers.append("X-CSRF-Token", getCSRFToken()); | |
} | |
return fetch(url, { | |
headers, | |
method, | |
body: encodeRequests(params, contentType) | |
}).then(status) | |
.then(deserializeResponse) | |
.catch(error => Promise.reject(new Error(error))); | |
}; | |
export const getCSRFToken = () => { | |
return "CSRF"; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment