1
0
mirror of https://github.com/bigchaindb/js-bigchaindb-driver.git synced 2024-06-28 00:27:44 +02:00
js-bigchaindb-driver/src/request.js
vrde 7c4d66bbb7
Do not include credentials in XHR
BigchainDB API doesn't use cookies, we can remove credentials from the
headers. It also create problems for CORS, since the server must
implement this.
2017-06-12 14:01:09 +02:00

44 lines
1.2 KiB
JavaScript

import baseRequest from './baseRequest';
import sanitize from './sanitize';
const DEFAULT_REQUEST_CONFIG = {
headers: {
'Accept': 'application/json'
}
};
/**
* Small wrapper around js-utility-belt's request that provides url resolving,
* default settings, and response handling.
*/
export default function request(url, config = {}, onlyJsonResponse=true) {
// Load default fetch configuration and remove any falsy query parameters
const requestConfig = Object.assign({}, DEFAULT_REQUEST_CONFIG, config, {
query: config.query && sanitize(config.query)
});
let apiUrl = url;
if (requestConfig.jsonBody) {
requestConfig.headers = Object.assign({}, requestConfig.headers, {
'Content-Type': 'application/json'
});
}
if (!url) {
return Promise.reject(new Error('Request was not given a url.'));
}
return baseRequest(apiUrl, requestConfig)
.then((res) => {
return onlyJsonResponse ? res.json() :
{
json: res.json(),
url: res.url
};
})
.catch((err) => {
console.error(err);
throw err;
});
}