Path: blob/master/node_modules/axios/lib/core/Axios.js
1126 views
'use strict';12var utils = require('./../utils');3var buildURL = require('../helpers/buildURL');4var InterceptorManager = require('./InterceptorManager');5var dispatchRequest = require('./dispatchRequest');6var mergeConfig = require('./mergeConfig');7var validator = require('../helpers/validator');89var validators = validator.validators;10/**11* Create a new instance of Axios12*13* @param {Object} instanceConfig The default config for the instance14*/15function Axios(instanceConfig) {16this.defaults = instanceConfig;17this.interceptors = {18request: new InterceptorManager(),19response: new InterceptorManager()20};21}2223/**24* Dispatch a request25*26* @param {Object} config The config specific for this request (merged with this.defaults)27*/28Axios.prototype.request = function request(config) {29/*eslint no-param-reassign:0*/30// Allow for axios('example/url'[, config]) a la fetch API31if (typeof config === 'string') {32config = arguments[1] || {};33config.url = arguments[0];34} else {35config = config || {};36}3738config = mergeConfig(this.defaults, config);3940// Set config.method41if (config.method) {42config.method = config.method.toLowerCase();43} else if (this.defaults.method) {44config.method = this.defaults.method.toLowerCase();45} else {46config.method = 'get';47}4849var transitional = config.transitional;5051if (transitional !== undefined) {52validator.assertOptions(transitional, {53silentJSONParsing: validators.transitional(validators.boolean),54forcedJSONParsing: validators.transitional(validators.boolean),55clarifyTimeoutError: validators.transitional(validators.boolean)56}, false);57}5859// filter out skipped interceptors60var requestInterceptorChain = [];61var synchronousRequestInterceptors = true;62this.interceptors.request.forEach(function unshiftRequestInterceptors(interceptor) {63if (typeof interceptor.runWhen === 'function' && interceptor.runWhen(config) === false) {64return;65}6667synchronousRequestInterceptors = synchronousRequestInterceptors && interceptor.synchronous;6869requestInterceptorChain.unshift(interceptor.fulfilled, interceptor.rejected);70});7172var responseInterceptorChain = [];73this.interceptors.response.forEach(function pushResponseInterceptors(interceptor) {74responseInterceptorChain.push(interceptor.fulfilled, interceptor.rejected);75});7677var promise;7879if (!synchronousRequestInterceptors) {80var chain = [dispatchRequest, undefined];8182Array.prototype.unshift.apply(chain, requestInterceptorChain);83chain = chain.concat(responseInterceptorChain);8485promise = Promise.resolve(config);86while (chain.length) {87promise = promise.then(chain.shift(), chain.shift());88}8990return promise;91}929394var newConfig = config;95while (requestInterceptorChain.length) {96var onFulfilled = requestInterceptorChain.shift();97var onRejected = requestInterceptorChain.shift();98try {99newConfig = onFulfilled(newConfig);100} catch (error) {101onRejected(error);102break;103}104}105106try {107promise = dispatchRequest(newConfig);108} catch (error) {109return Promise.reject(error);110}111112while (responseInterceptorChain.length) {113promise = promise.then(responseInterceptorChain.shift(), responseInterceptorChain.shift());114}115116return promise;117};118119Axios.prototype.getUri = function getUri(config) {120config = mergeConfig(this.defaults, config);121return buildURL(config.url, config.params, config.paramsSerializer).replace(/^\?/, '');122};123124// Provide aliases for supported request methods125utils.forEach(['delete', 'get', 'head', 'options'], function forEachMethodNoData(method) {126/*eslint func-names:0*/127Axios.prototype[method] = function(url, config) {128return this.request(mergeConfig(config || {}, {129method: method,130url: url,131data: (config || {}).data132}));133};134});135136utils.forEach(['post', 'put', 'patch'], function forEachMethodWithData(method) {137/*eslint func-names:0*/138Axios.prototype[method] = function(url, data, config) {139return this.request(mergeConfig(config || {}, {140method: method,141url: url,142data: data143}));144};145});146147module.exports = Axios;148149150