fetch-fetchy
Advanced tools
Comparing version 1.0.3 to 1.0.4
@@ -70,16 +70,12 @@ ; | ||
*/ | ||
const request = (method, route, data = null, requestOptions = {}) => { | ||
const options = Object.assign({}, defaultOptions, requestOptions); | ||
let body; | ||
if (data) { | ||
body = options.json ? JSON.stringify(data) : body; | ||
} else { | ||
body = {}; | ||
} | ||
return new Request(getURL(route), { | ||
const request = (method, route, data = null, definedOptions = {}) => { | ||
const options = Object.assign({}, defaultOptions, validateOptions(definedOptions)); | ||
const body = () => data ? { body: options.json ? JSON.stringify(data) : data } : {}; | ||
const baseOptions = { | ||
method: method.toUpperCase(), | ||
mode: options.mode, | ||
headers: new Headers(headers(options.headers)), | ||
body, | ||
}); | ||
}; | ||
const requestOptions = Object.assign({}, baseOptions, body()); | ||
return new Request(getURL(route), requestOptions); | ||
}; | ||
@@ -103,18 +99,12 @@ | ||
/** Exports */ | ||
/** | ||
* Update the default configuration for all requests | ||
* Validate options for configure() or a single request | ||
* @param {Object} options | ||
* @param {Boolean} [json] | ||
* @param {String} [url] | ||
* @param {Object} [headers] | ||
* @param {String} [mode] | ||
* @returns Object | ||
* @returns {Object} | ||
*/ | ||
const configure = (options) => { | ||
const validateOptions = (options) => { | ||
const configurableOptions = ['json', 'url', 'headers', 'mode']; | ||
const definedOptions = Object.keys(options).reduce((acc, k) => { | ||
const validation = Object.keys(options).reduce((acc, k) => { | ||
if (configurableOptions.includes(k)) { | ||
acc.update[k] = options[k]; | ||
acc.options[k] = options[k]; | ||
} else { | ||
@@ -124,4 +114,4 @@ acc.errors.push(k) | ||
return acc; | ||
}, { update: {}, errors: [] }); | ||
const { update, errors } = definedOptions; | ||
}, { options: {}, errors: [] }); | ||
const { errors } = validation; | ||
if (errors.length) { | ||
@@ -131,3 +121,18 @@ const warning = `ƒetch ƒetchy: ${errors.join(', ')} ${errors.length > 1 ? 'are' : 'is'} not configurable.` | ||
}; | ||
const updatedOptions = Object.assign({}, defaultOptions, update); | ||
return validation.options; | ||
}; | ||
/** Exports */ | ||
/** | ||
* Update the default configuration for all requests | ||
* @param {Object} options | ||
* @param {Boolean} [json] | ||
* @param {String} [url] | ||
* @param {Object} [headers] | ||
* @param {String} [mode] | ||
* @returns Object | ||
*/ | ||
const configure = (options) => { | ||
const updatedOptions = Object.assign({}, defaultOptions, validateOptions(options)); | ||
defaultOptions = updatedOptions; | ||
@@ -134,0 +139,0 @@ return updatedOptions; |
{ | ||
"name": "fetch-fetchy", | ||
"version": "1.0.3", | ||
"version": "1.0.4", | ||
"description": "A simplified fetch api", | ||
@@ -5,0 +5,0 @@ "main": "fetch-fetchy.js", |
34006
198