summaryrefslogtreecommitdiff
path: root/lib/core
diff options
context:
space:
mode:
Diffstat (limited to 'lib/core')
-rw-r--r--lib/core/Axios.js95
-rw-r--r--lib/core/InterceptorManager.js52
-rw-r--r--lib/core/README.md7
-rw-r--r--lib/core/buildFullPath.js20
-rw-r--r--lib/core/createError.js18
-rw-r--r--lib/core/dispatchRequest.js79
-rw-r--r--lib/core/enhanceError.js42
-rw-r--r--lib/core/mergeConfig.js87
-rw-r--r--lib/core/settle.js25
-rw-r--r--lib/core/transformData.js20
10 files changed, 445 insertions, 0 deletions
diff --git a/lib/core/Axios.js b/lib/core/Axios.js
new file mode 100644
index 0000000..c28c413
--- /dev/null
+++ b/lib/core/Axios.js
@@ -0,0 +1,95 @@
+'use strict';
+
+var utils = require('./../utils');
+var buildURL = require('../helpers/buildURL');
+var InterceptorManager = require('./InterceptorManager');
+var dispatchRequest = require('./dispatchRequest');
+var mergeConfig = require('./mergeConfig');
+
+/**
+ * Create a new instance of Axios
+ *
+ * @param {Object} instanceConfig The default config for the instance
+ */
+function Axios(instanceConfig) {
+ this.defaults = instanceConfig;
+ this.interceptors = {
+ request: new InterceptorManager(),
+ response: new InterceptorManager()
+ };
+}
+
+/**
+ * Dispatch a request
+ *
+ * @param {Object} config The config specific for this request (merged with this.defaults)
+ */
+Axios.prototype.request = function request(config) {
+ /*eslint no-param-reassign:0*/
+ // Allow for axios('example/url'[, config]) a la fetch API
+ if (typeof config === 'string') {
+ config = arguments[1] || {};
+ config.url = arguments[0];
+ } else {
+ config = config || {};
+ }
+
+ config = mergeConfig(this.defaults, config);
+
+ // Set config.method
+ if (config.method) {
+ config.method = config.method.toLowerCase();
+ } else if (this.defaults.method) {
+ config.method = this.defaults.method.toLowerCase();
+ } else {
+ config.method = 'get';
+ }
+
+ // Hook up interceptors middleware
+ var chain = [dispatchRequest, undefined];
+ var promise = Promise.resolve(config);
+
+ this.interceptors.request.forEach(function unshiftRequestInterceptors(interceptor) {
+ chain.unshift(interceptor.fulfilled, interceptor.rejected);
+ });
+
+ this.interceptors.response.forEach(function pushResponseInterceptors(interceptor) {
+ chain.push(interceptor.fulfilled, interceptor.rejected);
+ });
+
+ while (chain.length) {
+ promise = promise.then(chain.shift(), chain.shift());
+ }
+
+ return promise;
+};
+
+Axios.prototype.getUri = function getUri(config) {
+ config = mergeConfig(this.defaults, config);
+ return buildURL(config.url, config.params, config.paramsSerializer).replace(/^\?/, '');
+};
+
+// Provide aliases for supported request methods
+utils.forEach(['delete', 'get', 'head', 'options'], function forEachMethodNoData(method) {
+ /*eslint func-names:0*/
+ Axios.prototype[method] = function(url, config) {
+ return this.request(mergeConfig(config || {}, {
+ method: method,
+ url: url,
+ data: (config || {}).data
+ }));
+ };
+});
+
+utils.forEach(['post', 'put', 'patch'], function forEachMethodWithData(method) {
+ /*eslint func-names:0*/
+ Axios.prototype[method] = function(url, data, config) {
+ return this.request(mergeConfig(config || {}, {
+ method: method,
+ url: url,
+ data: data
+ }));
+ };
+});
+
+module.exports = Axios;
diff --git a/lib/core/InterceptorManager.js b/lib/core/InterceptorManager.js
new file mode 100644
index 0000000..50d667b
--- /dev/null
+++ b/lib/core/InterceptorManager.js
@@ -0,0 +1,52 @@
+'use strict';
+
+var utils = require('./../utils');
+
+function InterceptorManager() {
+ this.handlers = [];
+}
+
+/**
+ * Add a new interceptor to the stack
+ *
+ * @param {Function} fulfilled The function to handle `then` for a `Promise`
+ * @param {Function} rejected The function to handle `reject` for a `Promise`
+ *
+ * @return {Number} An ID used to remove interceptor later
+ */
+InterceptorManager.prototype.use = function use(fulfilled, rejected) {
+ this.handlers.push({
+ fulfilled: fulfilled,
+ rejected: rejected
+ });
+ return this.handlers.length - 1;
+};
+
+/**
+ * Remove an interceptor from the stack
+ *
+ * @param {Number} id The ID that was returned by `use`
+ */
+InterceptorManager.prototype.eject = function eject(id) {
+ if (this.handlers[id]) {
+ this.handlers[id] = null;
+ }
+};
+
+/**
+ * Iterate over all the registered interceptors
+ *
+ * This method is particularly useful for skipping over any
+ * interceptors that may have become `null` calling `eject`.
+ *
+ * @param {Function} fn The function to call for each interceptor
+ */
+InterceptorManager.prototype.forEach = function forEach(fn) {
+ utils.forEach(this.handlers, function forEachHandler(h) {
+ if (h !== null) {
+ fn(h);
+ }
+ });
+};
+
+module.exports = InterceptorManager;
diff --git a/lib/core/README.md b/lib/core/README.md
new file mode 100644
index 0000000..253bc48
--- /dev/null
+++ b/lib/core/README.md
@@ -0,0 +1,7 @@
+# axios // core
+
+The modules found in `core/` should be modules that are specific to the domain logic of axios. These modules would most likely not make sense to be consumed outside of the axios module, as their logic is too specific. Some examples of core modules are:
+
+- Dispatching requests
+- Managing interceptors
+- Handling config
diff --git a/lib/core/buildFullPath.js b/lib/core/buildFullPath.js
new file mode 100644
index 0000000..00b2b05
--- /dev/null
+++ b/lib/core/buildFullPath.js
@@ -0,0 +1,20 @@
+'use strict';
+
+var isAbsoluteURL = require('../helpers/isAbsoluteURL');
+var combineURLs = require('../helpers/combineURLs');
+
+/**
+ * Creates a new URL by combining the baseURL with the requestedURL,
+ * only when the requestedURL is not already an absolute URL.
+ * If the requestURL is absolute, this function returns the requestedURL untouched.
+ *
+ * @param {string} baseURL The base URL
+ * @param {string} requestedURL Absolute or relative URL to combine
+ * @returns {string} The combined full path
+ */
+module.exports = function buildFullPath(baseURL, requestedURL) {
+ if (baseURL && !isAbsoluteURL(requestedURL)) {
+ return combineURLs(baseURL, requestedURL);
+ }
+ return requestedURL;
+};
diff --git a/lib/core/createError.js b/lib/core/createError.js
new file mode 100644
index 0000000..933680f
--- /dev/null
+++ b/lib/core/createError.js
@@ -0,0 +1,18 @@
+'use strict';
+
+var enhanceError = require('./enhanceError');
+
+/**
+ * Create an Error with the specified message, config, error code, request and response.
+ *
+ * @param {string} message The error message.
+ * @param {Object} config The config.
+ * @param {string} [code] The error code (for example, 'ECONNABORTED').
+ * @param {Object} [request] The request.
+ * @param {Object} [response] The response.
+ * @returns {Error} The created error.
+ */
+module.exports = function createError(message, config, code, request, response) {
+ var error = new Error(message);
+ return enhanceError(error, config, code, request, response);
+};
diff --git a/lib/core/dispatchRequest.js b/lib/core/dispatchRequest.js
new file mode 100644
index 0000000..c8267ad
--- /dev/null
+++ b/lib/core/dispatchRequest.js
@@ -0,0 +1,79 @@
+'use strict';
+
+var utils = require('./../utils');
+var transformData = require('./transformData');
+var isCancel = require('../cancel/isCancel');
+var defaults = require('../defaults');
+
+/**
+ * Throws a `Cancel` if cancellation has been requested.
+ */
+function throwIfCancellationRequested(config) {
+ if (config.cancelToken) {
+ config.cancelToken.throwIfRequested();
+ }
+}
+
+/**
+ * Dispatch a request to the server using the configured adapter.
+ *
+ * @param {object} config The config that is to be used for the request
+ * @returns {Promise} The Promise to be fulfilled
+ */
+module.exports = function dispatchRequest(config) {
+ throwIfCancellationRequested(config);
+
+ // Ensure headers exist
+ config.headers = config.headers || {};
+
+ // Transform request data
+ config.data = transformData(
+ config.data,
+ config.headers,
+ config.transformRequest
+ );
+
+ // Flatten headers
+ config.headers = utils.merge(
+ config.headers.common || {},
+ config.headers[config.method] || {},
+ config.headers
+ );
+
+ utils.forEach(
+ ['delete', 'get', 'head', 'post', 'put', 'patch', 'common'],
+ function cleanHeaderConfig(method) {
+ delete config.headers[method];
+ }
+ );
+
+ var adapter = config.adapter || defaults.adapter;
+
+ return adapter(config).then(function onAdapterResolution(response) {
+ throwIfCancellationRequested(config);
+
+ // Transform response data
+ response.data = transformData(
+ response.data,
+ response.headers,
+ config.transformResponse
+ );
+
+ return response;
+ }, function onAdapterRejection(reason) {
+ if (!isCancel(reason)) {
+ throwIfCancellationRequested(config);
+
+ // Transform response data
+ if (reason && reason.response) {
+ reason.response.data = transformData(
+ reason.response.data,
+ reason.response.headers,
+ config.transformResponse
+ );
+ }
+ }
+
+ return Promise.reject(reason);
+ });
+};
diff --git a/lib/core/enhanceError.js b/lib/core/enhanceError.js
new file mode 100644
index 0000000..b6bc444
--- /dev/null
+++ b/lib/core/enhanceError.js
@@ -0,0 +1,42 @@
+'use strict';
+
+/**
+ * Update an Error with the specified config, error code, and response.
+ *
+ * @param {Error} error The error to update.
+ * @param {Object} config The config.
+ * @param {string} [code] The error code (for example, 'ECONNABORTED').
+ * @param {Object} [request] The request.
+ * @param {Object} [response] The response.
+ * @returns {Error} The error.
+ */
+module.exports = function enhanceError(error, config, code, request, response) {
+ error.config = config;
+ if (code) {
+ error.code = code;
+ }
+
+ error.request = request;
+ error.response = response;
+ error.isAxiosError = true;
+
+ error.toJSON = function toJSON() {
+ return {
+ // Standard
+ message: this.message,
+ name: this.name,
+ // Microsoft
+ description: this.description,
+ number: this.number,
+ // Mozilla
+ fileName: this.fileName,
+ lineNumber: this.lineNumber,
+ columnNumber: this.columnNumber,
+ stack: this.stack,
+ // Axios
+ config: this.config,
+ code: this.code
+ };
+ };
+ return error;
+};
diff --git a/lib/core/mergeConfig.js b/lib/core/mergeConfig.js
new file mode 100644
index 0000000..5a2c10c
--- /dev/null
+++ b/lib/core/mergeConfig.js
@@ -0,0 +1,87 @@
+'use strict';
+
+var utils = require('../utils');
+
+/**
+ * Config-specific merge-function which creates a new config-object
+ * by merging two configuration objects together.
+ *
+ * @param {Object} config1
+ * @param {Object} config2
+ * @returns {Object} New object resulting from merging config2 to config1
+ */
+module.exports = function mergeConfig(config1, config2) {
+ // eslint-disable-next-line no-param-reassign
+ config2 = config2 || {};
+ var config = {};
+
+ var valueFromConfig2Keys = ['url', 'method', 'data'];
+ var mergeDeepPropertiesKeys = ['headers', 'auth', 'proxy', 'params'];
+ var defaultToConfig2Keys = [
+ 'baseURL', 'transformRequest', 'transformResponse', 'paramsSerializer',
+ 'timeout', 'timeoutMessage', 'withCredentials', 'adapter', 'responseType', 'xsrfCookieName',
+ 'xsrfHeaderName', 'onUploadProgress', 'onDownloadProgress', 'decompress',
+ 'maxContentLength', 'maxBodyLength', 'maxRedirects', 'transport', 'httpAgent',
+ 'httpsAgent', 'cancelToken', 'socketPath', 'responseEncoding'
+ ];
+ var directMergeKeys = ['validateStatus'];
+
+ function getMergedValue(target, source) {
+ if (utils.isPlainObject(target) && utils.isPlainObject(source)) {
+ return utils.merge(target, source);
+ } else if (utils.isPlainObject(source)) {
+ return utils.merge({}, source);
+ } else if (utils.isArray(source)) {
+ return source.slice();
+ }
+ return source;
+ }
+
+ function mergeDeepProperties(prop) {
+ if (!utils.isUndefined(config2[prop])) {
+ config[prop] = getMergedValue(config1[prop], config2[prop]);
+ } else if (!utils.isUndefined(config1[prop])) {
+ config[prop] = getMergedValue(undefined, config1[prop]);
+ }
+ }
+
+ utils.forEach(valueFromConfig2Keys, function valueFromConfig2(prop) {
+ if (!utils.isUndefined(config2[prop])) {
+ config[prop] = getMergedValue(undefined, config2[prop]);
+ }
+ });
+
+ utils.forEach(mergeDeepPropertiesKeys, mergeDeepProperties);
+
+ utils.forEach(defaultToConfig2Keys, function defaultToConfig2(prop) {
+ if (!utils.isUndefined(config2[prop])) {
+ config[prop] = getMergedValue(undefined, config2[prop]);
+ } else if (!utils.isUndefined(config1[prop])) {
+ config[prop] = getMergedValue(undefined, config1[prop]);
+ }
+ });
+
+ utils.forEach(directMergeKeys, function merge(prop) {
+ if (prop in config2) {
+ config[prop] = getMergedValue(config1[prop], config2[prop]);
+ } else if (prop in config1) {
+ config[prop] = getMergedValue(undefined, config1[prop]);
+ }
+ });
+
+ var axiosKeys = valueFromConfig2Keys
+ .concat(mergeDeepPropertiesKeys)
+ .concat(defaultToConfig2Keys)
+ .concat(directMergeKeys);
+
+ var otherKeys = Object
+ .keys(config1)
+ .concat(Object.keys(config2))
+ .filter(function filterAxiosKeys(key) {
+ return axiosKeys.indexOf(key) === -1;
+ });
+
+ utils.forEach(otherKeys, mergeDeepProperties);
+
+ return config;
+};
diff --git a/lib/core/settle.js b/lib/core/settle.js
new file mode 100644
index 0000000..886adb0
--- /dev/null
+++ b/lib/core/settle.js
@@ -0,0 +1,25 @@
+'use strict';
+
+var createError = require('./createError');
+
+/**
+ * Resolve or reject a Promise based on response status.
+ *
+ * @param {Function} resolve A function that resolves the promise.
+ * @param {Function} reject A function that rejects the promise.
+ * @param {object} response The response.
+ */
+module.exports = function settle(resolve, reject, response) {
+ var validateStatus = response.config.validateStatus;
+ if (!response.status || !validateStatus || validateStatus(response.status)) {
+ resolve(response);
+ } else {
+ reject(createError(
+ 'Request failed with status code ' + response.status,
+ response.config,
+ null,
+ response.request,
+ response
+ ));
+ }
+};
diff --git a/lib/core/transformData.js b/lib/core/transformData.js
new file mode 100644
index 0000000..e065362
--- /dev/null
+++ b/lib/core/transformData.js
@@ -0,0 +1,20 @@
+'use strict';
+
+var utils = require('./../utils');
+
+/**
+ * Transform the data for a request or a response
+ *
+ * @param {Object|String} data The data to be transformed
+ * @param {Array} headers The headers for the request or response
+ * @param {Array|Function} fns A single function or Array of functions
+ * @returns {*} The resulting transformed data
+ */
+module.exports = function transformData(data, headers, fns) {
+ /*eslint no-param-reassign:0*/
+ utils.forEach(fns, function transform(fn) {
+ data = fn(data, headers);
+ });
+
+ return data;
+};