Skip to content

Commit

Permalink
merge mzabriskie/master
Browse files Browse the repository at this point in the history
  • Loading branch information
yinhangfeng committed Oct 23, 2017
2 parents 60434dc + 2c0e318 commit 9a470fc
Show file tree
Hide file tree
Showing 32 changed files with 374 additions and 196 deletions.
6 changes: 3 additions & 3 deletions .github/ISSUE_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

Please read and follow this instructions before submitting an issue:

- Read all our documentation, specially the [README](https://github.com/mzabriskie/axios/blob/master/README.md). It may contain information that helps you solve your issue.
- Ensure your issue isn't already [reported](https://github.com/mzabriskie/axios/issues?utf8=%E2%9C%93&q=is%3Aissue).
- Read all our documentation, specially the [README](https://github.com/axios/axios/blob/master/README.md). It may contain information that helps you solve your issue.
- Ensure your issue isn't already [reported](https://github.com/axios/axios/issues?utf8=%E2%9C%93&q=is%3Aissue).
- If you aren't sure that the issue is caused by axios or you just need for help, please use [Stack Overflow](https://stackoverflow.com/questions/tagged/axios) or [our chat](https://gitter.im/mzabriskie/axios).
- If you're reporting a bug, ensure it isn't already fixed in the latest axios version.
- If you need a new feature there's a chance it's already implemented in a [library](https://github.com/mzabriskie/axios/blob/master/ECOSYSTEM.md) or you can implement it using [interceptors](https://github.com/mzabriskie/axios#interceptors).
- If you need a new feature there's a chance it's already implemented in a [library](https://github.com/axios/axios/blob/master/ECOSYSTEM.md) or you can implement it using [interceptors](https://github.com/axios/axios#interceptors).

*^^^ Delete the instructions before submitting the issue ^^^*

Expand Down
2 changes: 1 addition & 1 deletion .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Please read and follow this instructions before creating and submitting a pull r

- Create an issue explaining the feature. It could save you some effort in case we don't consider it should be included in axios.
- If you're fixing a bug, try to commit the failing test/s and the code fixing it in different commits.
- Ensure you're following our [contributing guide](https://github.com/mzabriskie/axios/blob/master/CONTRIBUTING.md).
- Ensure you're following our [contributing guide](https://github.com/axios/axios/blob/master/CONTRIBUTING.md).

*^^^ Delete the instructions before submitting the pull request ^^^*

Expand Down
225 changes: 119 additions & 106 deletions CHANGELOG.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion COLLABORATOR_GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

As a collaborator you will be involved with axios with some administrative responsibilities. This guide will help you understand your role and the responsibilities that come with being a collaborator.

1. __Adhere to and help enforce the Code of Conduct.__ It is expected that you have read the [code of conduct](https://github.com/mzabriskie/axios/blob/master/CODE_OF_CONDUCT.md) and that you agree to live by it. This community should be friendly and welcoming.
1. __Adhere to and help enforce the Code of Conduct.__ It is expected that you have read the [code of conduct](https://github.com/axios/axios/blob/master/CODE_OF_CONDUCT.md) and that you agree to live by it. This community should be friendly and welcoming.

1. __Triage issues.__ As a collaborator you may help sort through the issues that are reported. Issues vary from bugs, regressions, feature requests, questions, etc. Apply the appropriate label(s) and respond as needed. If it is a legitimate request please address it, otherwise feel free to close the issue and include a comment with a suggestion on where to find support. If an issue has been inactive for more than a week (i.e, the owner of the issue hasn’t responded to you), close the issue with a note indicating stales issues are closed; it can always be reopened if needed. In the case of issues that require a code change encourage the owner to submit a PR. For less complex code changes, add a very simple and detailed checklist, apply the “first-timers-only” label, and encourage a newcomer to open source to get involved.

Expand Down
4 changes: 2 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Contributing

We are open to, and grateful for, any contributions made by the community. By contributing to axios, you agree to abide by the [code of conduct](https://github.com/mzabriskie/axios/blob/master/CODE_OF_CONDUCT.md).
We are open to, and grateful for, any contributions made by the community. By contributing to axios, you agree to abide by the [code of conduct](https://github.com/axios/axios/blob/master/CODE_OF_CONDUCT.md).

### Code Style

Expand Down Expand Up @@ -35,7 +35,7 @@ Please don't include changes to `dist/` in your pull request. This should only b

### Releasing

Releasing a new version is mostly automated. For now the [CHANGELOG](https://github.com/mzabriskie/axios/blob/master/CHANGELOG.md) requires being updated manually. Once this has been done run the commands below. Versions should follow [semantic versioning](http://semver.org/).
Releasing a new version is mostly automated. For now the [CHANGELOG](https://github.com/axios/axios/blob/master/CHANGELOG.md) requires being updated manually. Once this has been done run the commands below. Versions should follow [semantic versioning](http://semver.org/).

- `npm version <newversion> -m "Releasing %s"`
- `npm publish`
Expand Down
6 changes: 5 additions & 1 deletion ECOSYSTEM.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ This is a list of axios related libraries and resources. If you have a suggestio

## Libraries

* [moxios](https://github.com/mzabriskie/moxios) - Mock axios requests for testing
* [moxios](https://github.com/axios/moxios) - Mock axios requests for testing
* [axios-response-logger](https://github.com/srph/axios-response-logger) - Axios interceptor which logs responses
* [axios-mock-adapter](https://github.com/ctimmerm/axios-mock-adapter) — Axios adapter that allows to easily mock requests
* [redux-axios-middleware](https://github.com/svrcekmichal/redux-axios-middleware) - Redux middleware for fetching data with axios HTTP client
* [axios-vcr](https://github.com/nettofarah/axios-vcr) - 📼 Record and Replay Axios requests
* [@3846masa/axios-cookiejar-support](https://github.com/3846masa/axios-cookiejar-support) - Add tough-cookie support to axios
* [axios-debug-log](https://github.com/Gerhut/axios-debug-log) - Axios interceptor of logging requests & responses by debug.
* [axios-method-override](https://github.com/jacobbuck/axios-method-override) - Axios http request method override plugin
* [mocha-axios](https://github.com/jdrydn/mocha-axios) - Streamlined integration testing with Mocha & Axios
* [axiosist](https://github.com/Gerhut/axiosist) - Axios based supertest: convert node.js request handler to axios adapter, used for node.js server unit test.
* [axios-cache-plugin](https://github.com/jin5354/axios-cache-plugin) - Help you cache GET request when using axios.
* [axios-extensions](https://github.com/kuitos/axios-extensions) - A collection of axios extensions, including throttle and cache GET request plugin.
17 changes: 12 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,8 @@ These are the available config options for making requests. Only the `url` is re
// This is only applicable for request methods 'PUT', 'POST', and 'PATCH'
// The last function in the array must return a string or an instance of Buffer, ArrayBuffer,
// FormData or Stream
transformRequest: [function (data) {
// You may modify the headers object.
transformRequest: [function (data, headers) {
// Do whatever you want to transform the data

return data;
Expand Down Expand Up @@ -308,6 +309,7 @@ These are the available config options for making requests. Only the `url` is re
httpsAgent: new https.Agent({ keepAlive: true }),

// 'proxy' defines the hostname and port of the proxy server
// Use `false` to disable proxies, ignoring environment variables.
// `auth` indicates that HTTP Basic auth should be used to connect to the proxy, and
// supplies credentials.
// This will set an `Proxy-Authorization` header, overwriting any existing
Expand Down Expand Up @@ -460,11 +462,16 @@ instance.interceptors.request.use(function () {/*...*/});
axios.get('/user/12345')
.catch(function (error) {
if (error.response) {
// The request was made, but the server responded with a status code
// The request was made and the server responded with a status code
// that falls out of the range of 2xx
console.log(error.response.data);
console.log(error.response.status);
console.log(error.response.headers);
} else if (error.request) {
// The request was made but no response was received
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of
// http.ClientRequest in node.js
console.log(error.request);
} else {
// Something happened in setting up the request that triggered an Error
console.log('Error', error.message);
Expand Down Expand Up @@ -545,7 +552,7 @@ params.append('param2', 'value2');
axios.post('/foo', params);
```

> Note that `URLSearchParams` is not supported by all browsers, but there is a [polyfill](https://github.com/WebReflection/url-search-params) available (make sure to polyfill the global environment).
> Note that `URLSearchParams` is not supported by all browsers (see [caniuse.com](http://www.caniuse.com/#feat=urlsearchparams)), but there is a [polyfill](https://github.com/WebReflection/url-search-params) available (make sure to polyfill the global environment).
Alternatively, you can encode data using the [`qs`](https://github.com/ljharb/qs) library:

Expand All @@ -565,15 +572,15 @@ var querystring = require('querystring');
axios.post('http://something.com/', querystring.stringify({ foo: 'bar' }));
```

You can also use the `qs` library.
You can also use the [`qs`](https://github.com/ljharb/qs) library.

## Semver

Until axios reaches a `1.0` release, breaking changes will be released with a new minor version. For example `0.5.1`, and `0.5.4` will have the same API, but `0.6.0` will have breaking changes.

## Promises

默认使用native Promise, 可通过设置axios.Promise进行配置
config Promise
```js
var axios = require('ex-axios');
var Promise = require('bluebird');
Expand Down
6 changes: 3 additions & 3 deletions UPGRADE_GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ function myAdapter(config) {
```

See the related commits for more details:
- [Response transformers](https://github.com/mzabriskie/axios/commit/10eb23865101f9347570552c04e9d6211376e25e)
- [Request adapter Promise](https://github.com/mzabriskie/axios/commit/157efd5615890301824e3121cc6c9d2f9b21f94a)
- [Response transformers](https://github.com/axios/axios/commit/10eb23865101f9347570552c04e9d6211376e25e)
- [Request adapter Promise](https://github.com/axios/axios/commit/157efd5615890301824e3121cc6c9d2f9b21f94a)

### 0.5.x -> 0.6.0

Expand All @@ -135,7 +135,7 @@ This will polyfill the global environment, and only needs to be done once.

#### `axios.success`/`axios.error`

The `success`, and `error` aliases were deprectated in [0.4.0](https://github.com/mzabriskie/axios/blob/master/CHANGELOG.md#040-oct-03-2014). As of this release they have been removed entirely. Instead please use `axios.then`, and `axios.catch` respectively.
The `success`, and `error` aliases were deprectated in [0.4.0](https://github.com/axios/axios/blob/master/CHANGELOG.md#040-oct-03-2014). As of this release they have been removed entirely. Instead please use `axios.then`, and `axios.catch` respectively.

```js
axios.get('some/url')
Expand Down
4 changes: 2 additions & 2 deletions bower.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "axios",
"main": "./dist/axios.js",
"version": "0.16.1",
"homepage": "https://github.com/mzabriskie/axios",
"version": "0.17.0",
"homepage": "https://github.com/axios/axios",
"authors": [
"Matt Zabriskie"
],
Expand Down
2 changes: 1 addition & 1 deletion examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

To run the examples:

1. `git clone https://github.com/mzabriskie/axios.git`
1. `git clone https://github.com/axios/axios.git`
2. `cd axios`
3. `npm install`
4. `grunt build`
Expand Down
19 changes: 10 additions & 9 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export interface AxiosTransformer {
}

export interface AxiosAdapter {
(config: AxiosRequestConfig): AxiosPromise;
(config: AxiosRequestConfig): AxiosPromise<any>;
}

export interface AxiosBasicCredentials {
Expand Down Expand Up @@ -45,8 +45,8 @@ export interface AxiosRequestConfig {
cancelToken?: CancelToken;
}

export interface AxiosResponse {
data: any;
export interface AxiosResponse<T = any> {
data: T;
status: number;
statusText: string;
headers: any;
Expand All @@ -56,10 +56,11 @@ export interface AxiosResponse {
export interface AxiosError extends Error {
config: AxiosRequestConfig;
code?: string;
request?: any;
response?: AxiosResponse;
}

export interface AxiosPromise extends Promise<AxiosResponse> {
export interface AxiosPromise<T = any> extends Promise<AxiosResponse<T>> {
}

export interface CancelStatic {
Expand Down Expand Up @@ -101,13 +102,13 @@ export interface AxiosInstance {
request: AxiosInterceptorManager<AxiosRequestConfig>;
response: AxiosInterceptorManager<AxiosResponse>;
};
request(config: AxiosRequestConfig): AxiosPromise;
get(url: string, config?: AxiosRequestConfig): AxiosPromise;
request<T = any>(config: AxiosRequestConfig): AxiosPromise<T>;
get<T = any>(url: string, config?: AxiosRequestConfig): AxiosPromise<T>;
delete(url: string, config?: AxiosRequestConfig): AxiosPromise;
head(url: string, config?: AxiosRequestConfig): AxiosPromise;
post(url: string, config?: AxiosRequestConfig): AxiosPromise;
put(url: string, config?: AxiosRequestConfig): AxiosPromise;
patch(url: string, config?: AxiosRequestConfig): AxiosPromise;
post<T = any>(url: string, config?: AxiosRequestConfig): AxiosPromise<T>;
put<T = any>(url: string, config?: AxiosRequestConfig): AxiosPromise<T>;
patch<T = any>(url: string, config?: AxiosRequestConfig): AxiosPromise<T>;
}

export interface AxiosStatic extends AxiosInstance {
Expand Down
2 changes: 1 addition & 1 deletion karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ module.exports = function(config) {
sauceLabs: {
recordScreenshots: false,
connectOptions: {
port: 5757,
// port: 5757,
logfile: 'sauce_connect.log'
},
public: 'public'
Expand Down
29 changes: 12 additions & 17 deletions lib/adapters/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ var httpsFollow = require('follow-redirects').https;
var url = require('url');
var zlib = require('zlib');
var pkg = require('./../../package.json');
var Buffer = require('buffer').Buffer;
var createError = require('../core/createError');
var enhanceError = require('../core/enhanceError');

Expand All @@ -21,17 +20,16 @@ module.exports = function httpAdapter(config) {
var data = config.data;
var headers = config.headers;
var timer;
var aborted = false;

// Set User-Agent (required by some servers)
// Only set header if it hasn't been set in config
// See https://github.com/mzabriskie/axios/issues/69
// See https://github.com/axios/axios/issues/69
if (!headers['User-Agent'] && !headers['user-agent']) {
headers['User-Agent'] = 'axios/' + pkg.version;
}

if (data && !utils.isStream(data)) {
if (utils.isBuffer(data)) {
if (Buffer.isBuffer(data)) {
// Nothing to do...
} else if (utils.isArrayBuffer(data)) {
data = new Buffer(new Uint8Array(data));
Expand Down Expand Up @@ -85,7 +83,7 @@ module.exports = function httpAdapter(config) {
};

var proxy = config.proxy;
if (!proxy) {
if (!proxy && proxy !== false) {
var proxyEnv = protocol.slice(0, -1) + '_proxy';
var proxyUrl = process.env[proxyEnv] || process.env[proxyEnv.toUpperCase()];
if (proxyUrl) {
Expand Down Expand Up @@ -131,7 +129,7 @@ module.exports = function httpAdapter(config) {

// Create the request
var req = transport.request(options, function handleResponse(res) {
if (aborted) return;
if (req.aborted) return;

// Response has been received so kill timer that handles request timeout
clearTimeout(timer);
Expand Down Expand Up @@ -173,13 +171,14 @@ module.exports = function httpAdapter(config) {

// make sure the content length is not over the maxContentLength if specified
if (config.maxContentLength > -1 && Buffer.concat(responseBuffer).length > config.maxContentLength) {
reject(createError('maxContentLength size of ' + config.maxContentLength + ' exceeded', config));
reject(createError('maxContentLength size of ' + config.maxContentLength + ' exceeded',
config, null, lastRequest));
}
});

stream.on('error', function handleStreamError(err) {
if (aborted) return;
reject(enhanceError(err, config));
if (req.aborted) return;
reject(enhanceError(err, config, null, lastRequest));
});

stream.on('end', function handleStreamEnd() {
Expand All @@ -196,29 +195,25 @@ module.exports = function httpAdapter(config) {

// Handle errors
req.on('error', function handleRequestError(err) {
if (aborted) return;
reject(enhanceError(err, config));
if (req.aborted) return;
reject(enhanceError(err, config, null, req));
});

// Handle request timeout
if (config.timeout && !timer) {
timer = setTimeout(function handleRequestTimeout() {
req.abort();
reject(createError('timeout of ' + config.timeout + 'ms exceeded', config, 'ECONNABORTED'));
aborted = true;
reject(createError('timeout of ' + config.timeout + 'ms exceeded', config, 'ECONNABORTED', req));
}, config.timeout);
}

if (config.cancelToken) {
// Handle cancellation
config.cancelToken.promise.then(function onCanceled(cancel) {
if (aborted) {
return;
}
if (req.aborted) return;

req.abort();
reject(cancel);
aborted = true;
});
}

Expand Down
11 changes: 7 additions & 4 deletions lib/adapters/xhr.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ module.exports = function xhrAdapter(config) {
// For IE 8/9 CORS support
// Only supports POST and GET calls and doesn't returns the response headers.
// DON'T do this for testing b/c XMLHttpRequest is mocked, not XDomainRequest.
// if (process.env.NODE_ENV !== 'test' &&
// 不支持 XDomainRequest
// if (!window.XMLHttpRequest &&
// process.env.NODE_ENV !== 'test' &&
// typeof window !== 'undefined' &&
// window.XDomainRequest && !('withCredentials' in request) &&
// !isURLSameOrigin(config.url)) {
Expand Down Expand Up @@ -61,7 +63,7 @@ module.exports = function xhrAdapter(config) {
var responseData = !config.responseType || config.responseType === 'text' ? request.responseText : request.response;
var response = {
data: responseData,
// IE sends 1223 instead of 204 (https://github.com/mzabriskie/axios/issues/201)
// IE sends 1223 instead of 204 (https://github.com/axios/axios/issues/201)
status: request.status === 1223 ? 204 : request.status,
statusText: request.status === 1223 ? 'No Content' : request.statusText,
get headers() {
Expand All @@ -85,15 +87,16 @@ module.exports = function xhrAdapter(config) {
request.onerror = function handleError() {
// Real errors are hidden from us by the browser
// onerror should only fire if it's a network error
reject(createError('Network Error', config));
reject(createError('Network Error', config, null, request));

// Clean up request
request = null;
};

// Handle timeout
request.ontimeout = function handleTimeout() {
reject(createError('timeout of ' + config.timeout + 'ms exceeded', config, 'ECONNABORTED'));
reject(createError('timeout of ' + config.timeout + 'ms exceeded', config, 'ECONNABORTED',
request));

// Clean up request
request = null;
Expand Down
7 changes: 0 additions & 7 deletions lib/core/Axios.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ var defaults = require('./../defaults');
var utils = require('./../utils');
var InterceptorManager = require('./InterceptorManager');
var dispatchRequest = require('./dispatchRequest');
var isAbsoluteURL = require('./../helpers/isAbsoluteURL');
var combineURLs = require('./../helpers/combineURLs');
var promiseProvider = require('../promiseProvider');
var Headers = require('../fetch/Headers');

Expand Down Expand Up @@ -86,11 +84,6 @@ Axios.prototype.request = function request(config) {
config.method = 'get';
}

// Support baseURL config
if (config.baseURL && !isAbsoluteURL(config.url)) {
config.url = combineURLs(config.baseURL, config.url);
}

if (!config.headers || config.headers === this.defaults.headers || config.headers === oriHeaders) {
config.headers = new Headers(config.headers);
}
Expand Down
Loading

0 comments on commit 9a470fc

Please sign in to comment.