1
0
mirror of https://github.com/bigchaindb/js-bigchaindb-driver.git synced 2024-11-29 07:16:25 +01:00
js-bigchaindb-driver/src/baseRequest.js

88 lines
3.8 KiB
JavaScript
Raw Normal View History

2017-06-12 16:57:29 +02:00
import { Promise } from 'es6-promise'
import fetchPonyfill from 'fetch-ponyfill'
import { vsprintf } from 'sprintf-js'
2017-04-26 15:58:19 +02:00
2017-06-12 16:57:29 +02:00
import formatText from './format_text'
import stringifyAsQueryParam from './stringify_as_query_param'
2017-04-26 15:58:19 +02:00
2017-06-12 16:57:29 +02:00
const fetch = fetchPonyfill(Promise)
2017-04-26 15:58:19 +02:00
/**
* @private
2017-04-26 15:58:19 +02:00
* imported from https://github.com/bigchaindb/js-utility-belt/
*
* Global fetch wrapper that adds some basic error handling and ease of use enhancements.
* Considers any non-2xx response as an error.
*
* For more information on fetch, see https://developer.mozilla.org/en-US/docs/Web/API/GlobalFetch/fetch.
*
* Expects fetch to already be available (either in a ES6 environment, bundled through webpack, or
* injected through a polyfill).
*
* @param {string} url Url to request. Can be specified as a sprintf format string (see
* https://github.com/alexei/sprintf.js) that will be resolved using
* `config.urlTemplateSpec`.
* @param {Object} config Additional configuration, mostly passed to fetch as its 'init' config
2017-04-26 15:58:19 +02:00
* (see https://developer.mozilla.org/en-US/docs/Web/API/GlobalFetch/fetch#Parameters).
* @param {*} config.jsonBody Json payload to the request. Will automatically be
* JSON.stringify()-ed and override `config.body`.
* @param {string|Object} config.query Query parameter to append to the end of the url.
2017-04-26 15:58:19 +02:00
* If specified as an object, keys will be
* decamelized into snake case first.
* @param {*[]|Object} config.urlTemplateSpec Format spec to use to expand the url (see sprintf).
2017-04-26 15:58:19 +02:00
* @param {*} config.* All other options are passed through to fetch.
*
* @return {Promise} Promise that will resolve with the response if its status was 2xx;
* otherwise rejects with the response
*/
2018-01-04 10:15:45 +01:00
export default function baseRequest(url, {
jsonBody, query, urlTemplateSpec, ...fetchConfig
} = {}) {
2017-06-12 16:57:29 +02:00
let expandedUrl = url
2017-04-26 15:58:19 +02:00
if (urlTemplateSpec != null) {
if (Array.isArray(urlTemplateSpec) && urlTemplateSpec.length) {
// Use vsprintf for the array call signature
2017-06-12 16:57:29 +02:00
expandedUrl = vsprintf(url, urlTemplateSpec)
2017-04-26 15:58:19 +02:00
} else if (urlTemplateSpec &&
typeof urlTemplateSpec === 'object' &&
Object.keys(urlTemplateSpec).length) {
2017-06-12 16:57:29 +02:00
expandedUrl = formatText(url, urlTemplateSpec)
2017-04-26 15:58:19 +02:00
} else if (process.env.NODE_ENV !== 'production') {
// eslint-disable-next-line no-console
2017-06-12 16:57:29 +02:00
console.warn('Supplied urlTemplateSpec was not an array or object. Ignoring...')
2017-04-26 15:58:19 +02:00
}
}
if (query != null) {
if (typeof query === 'string') {
2017-06-12 16:57:29 +02:00
expandedUrl += query
2017-04-26 15:58:19 +02:00
} else if (query && typeof query === 'object') {
2017-06-12 16:57:29 +02:00
expandedUrl += stringifyAsQueryParam(query)
2017-04-26 15:58:19 +02:00
} else if (process.env.NODE_ENV !== 'production') {
// eslint-disable-next-line no-console
2017-06-12 16:57:29 +02:00
console.warn('Supplied query was not a string or object. Ignoring...')
2017-04-26 15:58:19 +02:00
}
}
if (jsonBody != null) {
2017-06-12 16:57:29 +02:00
fetchConfig.body = JSON.stringify(jsonBody)
2017-04-26 15:58:19 +02:00
}
return fetch.fetch(expandedUrl, fetchConfig)
.then((res) => {
// If status is not a 2xx (based on Response.ok), assume it's an error
// See https://developer.mozilla.org/en-US/docs/Web/API/GlobalFetch/fetch
if (!(res && res.ok)) {
2017-09-26 16:25:07 +02:00
const errorObject = {
message: 'HTTP Error: Requested page not reachable',
2017-09-26 16:29:52 +02:00
status: `${res.status} ${res.statusText}`,
requestURI: res.url
}
throw errorObject
2017-04-26 15:58:19 +02:00
}
2017-06-12 16:57:29 +02:00
return res
})
}