dnote/browser/src/scripts/utils/fetch.js
Sung Won Cho 2758923c34
Implement personal knowedge base version (#254)
* Allow to add and edit notes

* Implement search

* Implement settings

* Implement checkout page

* Implement paywall

* Fix inconsistent margin

* Render mobile menu

* Allow to logout

* emails

* Implement user migration

* Always build standalone

* Embed digest in email

* Move browser extension

* Fix test

* Use system font

* Add favicon and app icons

* Make tabbar smaller

* Initialize focus on editor

* Fix various UI audit issues

* Simplify asset serving

* Register sw

* Upgrade deps
2019-09-30 11:02:09 +08:00

48 lines
922 B
JavaScript

import qs from 'qs';
function checkStatus(response) {
if (response.status >= 200 && response.status < 300) {
return response;
}
return response.text().then((body) => {
const error = new Error(body);
error.response = response;
throw error;
});
}
function parseJSON(response) {
if (response.headers.get('Content-Type') === 'application/json') {
return response.json();
}
return Promise.resolve();
}
function request(url, options) {
return fetch(url, options)
.then(checkStatus)
.then(parseJSON);
}
export function post(url, data, options = {}) {
return request(url, {
method: 'POST',
body: JSON.stringify(data),
...options,
});
}
export function get(url, options = {}) {
let endpoint = url;
if (options.params) {
endpoint = `${endpoint}?${qs.stringify(options.params)}`;
}
return request(endpoint, {
method: 'GET',
...options,
});
}