diff --git a/.eslintrc b/.eslintrc.js similarity index 99% rename from .eslintrc rename to .eslintrc.js index 22ae77fde2..7f9e73ed1c 100644 --- a/.eslintrc +++ b/.eslintrc.js @@ -1,4 +1,4 @@ -{ +module.exports = { "globals": { "console": true, "module": true, diff --git a/.github/ISSUE_TEMPLATE/---bug-report.md b/.github/ISSUE_TEMPLATE/---bug-report.md new file mode 100644 index 0000000000..a9f1fc9bb4 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/---bug-report.md @@ -0,0 +1,43 @@ +--- +name: "\U0001F41E Bug Report" +about: Report a reproducible bug +title: '' +labels: bug +assignees: '' + +--- + + + +**Describe the bug** +A clear and concise description of what the bug is. **If your problem is not a bug, please file under Support or Usage Question** + +**To Reproduce** +Code snippet to reproduce, ideally that will work by pasting into something like https://npm.runkit.com/axios, a hosted solution, or a repository that illustrates the issue. **If your problem is not reproducible, please file under Support or Usage Question** + +```js +// Example code here +``` + +**Expected behavior** +A clear and concise description of what you expected to happen. + +**Environment:** + - Axios Version [e.g. 0.18.0] + - OS: [e.g. iOS 12.1.0, OSX 10.13.4] + - Browser [e.g. Chrome, Safari] + - Browser Version [e.g. 22] + - Additional Library Versions [e.g. React 16.7, React Native 0.58.0] + +**Additional context/Screenshots** +Add any other context about the problem here. If applicable, add screenshots to help explain. diff --git a/.github/ISSUE_TEMPLATE/---documentation.md b/.github/ISSUE_TEMPLATE/---documentation.md new file mode 100644 index 0000000000..0ce50ef5e8 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/---documentation.md @@ -0,0 +1,23 @@ +--- +name: "\U0001F4DA Documentation" +about: Report an error or area that needs clarification +title: '' +labels: documentation +assignees: '' + +--- + + + +**Section/Content To Improve** +Quote or link to section + +**Suggested Improvement** +Identify what is confusing or incorrect and what could make it better + +**Relevant File(s)**: [e.g. README.md] diff --git a/.github/ISSUE_TEMPLATE/---support-or-usage-question.md b/.github/ISSUE_TEMPLATE/---support-or-usage-question.md new file mode 100644 index 0000000000..bba2dad340 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/---support-or-usage-question.md @@ -0,0 +1,43 @@ +--- +name: "\U0001F914 Support or Usage Question" +about: Get help using Axios +title: '' +labels: question +assignees: '' + +--- + + + +**Describe the issue** +A clear and concise description of what the issue is. + +**Example Code** +Code snippet to illustrate your question + +```js +// Example code here +``` + +**Expected behavior, if applicable** +A clear and concise description of what you expected to happen. + +**Environment:** + - Axios Version [e.g. 0.18.0] + - OS: [e.g. iOS 12.1.0, OSX 10.13.4] + - Browser [e.g. Chrome, Safari] + - Browser Version [e.g. 22] + - Additional Library Versions [e.g. React 16.7, React Native 0.58.0] + +**Additional context/Screenshots** +Add any other context about the problem here. If applicable, add screenshots to help explain. diff --git a/.github/ISSUE_TEMPLATE/--feature-request.md b/.github/ISSUE_TEMPLATE/--feature-request.md new file mode 100644 index 0000000000..ad49bc0712 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/--feature-request.md @@ -0,0 +1,20 @@ +--- +name: "✨ Feature Request" +about: Suggest an idea or feature +title: '' +labels: feature +assignees: '' + +--- + +**Is your feature request related to a problem? Please describe.** +A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] + +**Describe the solution you'd like** +A clear and concise description of what you want to happen. + +**Describe alternatives you've considered** +A clear and concise description of any alternative solutions or features you've considered. + +**Additional context** +Add any other context or screenshots about the feature request here. diff --git a/ECOSYSTEM.md b/ECOSYSTEM.md index a2b8cc7a3b..41461ec31f 100644 --- a/ECOSYSTEM.md +++ b/ECOSYSTEM.md @@ -4,6 +4,7 @@ This is a list of axios related libraries and resources. If you have a suggestio ## Libraries +* [react-hooks-axios](https://github.com/use-hooks/react-hooks-axios) - Custom React Hooks for Axios.js * [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 @@ -20,3 +21,5 @@ This is a list of axios related libraries and resources. If you have a suggestio * [axios-fetch](https://github.com/lifeomic/axios-fetch) - A WebAPI Fetch implementation backed by an Axios client * [axios-curlirize](https://www.npmjs.com/package/axios-curlirize) - Logs axios requests as curl commands, also adds a property to the response object with the curl command as value. * [axios-actions](https://github.com/davestewart/axios-actions) - Bundle endpoints as callable, reusable services +* [axios-api-versioning](https://weffe.github.io/axios-api-versioning) - Add easy to manage api versioning to axios +* [r2curl](https://github.com/uyu423/r2curl) - Extracts the cURL command string from the Axios object. (AxiosResponse, AxiosRequestConfig) diff --git a/README.md b/README.md index 6c0bc57cdf..45583c94cc 100755 --- a/README.md +++ b/README.md @@ -43,6 +43,12 @@ Using bower: $ bower install axios ``` +Using yarn: + +```bash +$ yarn add axios +``` + Using cdn: ```html @@ -66,7 +72,7 @@ axios.get('/user?ID=12345') // handle error console.log(error); }) - .then(function () { + .finally(function () { // always executed }); @@ -153,11 +159,11 @@ axios({ ```js // GET request for remote image axios({ - method:'get', - url:'http://bit.ly/2mTM3nY', - responseType:'stream' + method: 'get', + url: 'http://bit.ly/2mTM3nY', + responseType: 'stream' }) - .then(function(response) { + .then(function (response) { response.data.pipe(fs.createWriteStream('ada_lovelace.jpg')) }); ``` @@ -267,7 +273,7 @@ These are the available config options for making requests. Only the `url` is re // `paramsSerializer` is an optional function in charge of serializing `params` // (e.g. https://www.npmjs.com/package/qs, http://api.jquery.com/jquery.param/) - paramsSerializer: function(params) { + paramsSerializer: function (params) { return Qs.stringify(params, {arrayFormat: 'brackets'}) }, @@ -304,7 +310,8 @@ These are the available config options for making requests. Only the `url` is re }, // `responseType` indicates the type of data that the server will respond with - // options are 'arraybuffer', 'blob', 'document', 'json', 'text', 'stream' + // options are: 'arraybuffer', 'document', 'json', 'text', 'stream' + // browser only: 'blob' responseType: 'json', // default // `responseEncoding` indicates encoding to use for decoding responses @@ -413,7 +420,7 @@ When using `then`, you will receive the response as follows: ```js axios.get('/user/12345') - .then(function(response) { + .then(function (response) { console.log(response.data); console.log(response.status); console.log(response.statusText); @@ -553,7 +560,7 @@ const source = CancelToken.source(); axios.get('/user/12345', { cancelToken: source.token -}).catch(function(thrown) { +}).catch(function (thrown) { if (axios.isCancel(thrown)) { console.log('Request canceled', thrown.message); } else { @@ -639,6 +646,9 @@ axios.post('http://something.com/', querystring.stringify({ foo: 'bar' })); You can also use the [`qs`](https://github.com/ljharb/qs) library. +###### NOTE +The `qs` library is preferable if you need to stringify nested objects, as the `querystring` method has known issues with that use case (https://github.com/nodejs/node-v0.x-archive/issues/1665). + ## 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. diff --git a/index.d.ts b/index.d.ts index b60e43607c..f934886659 100644 --- a/index.d.ts +++ b/index.d.ts @@ -21,14 +21,14 @@ export interface AxiosProxyConfig { protocol?: string; } -export type Method = - | 'get' - | 'delete' - | 'head' - | 'options' - | 'post' - | 'put' - | 'patch' +export type Method = + | 'get' | 'GET' + | 'delete' | 'DELETE' + | 'head' | 'HEAD' + | 'options' | 'OPTIONS' + | 'post' | 'POST' + | 'put' | 'PUT' + | 'patch' | 'PATCH' export type ResponseType = | 'arraybuffer' @@ -76,11 +76,11 @@ export interface AxiosResponse { request?: any; } -export interface AxiosError extends Error { +export interface AxiosError extends Error { config: AxiosRequestConfig; code?: string; request?: any; - response?: AxiosResponse; + response?: AxiosResponse; isAxiosError: boolean; } diff --git a/lib/adapters/http.js b/lib/adapters/http.js index d39a573a2a..06169ff2cb 100755 --- a/lib/adapters/http.js +++ b/lib/adapters/http.js @@ -188,7 +188,7 @@ module.exports = function httpAdapter(config) { case 'compress': case 'deflate': // add the unzipper to the body stream processing pipeline - stream = stream.pipe(zlib.createUnzip()); + stream = (res.statusCode === 204) ? stream : stream.pipe(zlib.createUnzip()); // remove the content-encoding in order to not confuse downstream operations delete res.headers['content-encoding']; @@ -216,6 +216,7 @@ 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) { + stream.destroy(); reject(createError('maxContentLength size of ' + config.maxContentLength + ' exceeded', config, null, lastRequest)); } diff --git a/lib/helpers/buildURL.js b/lib/helpers/buildURL.js index be83cd0029..8c40e4096a 100644 --- a/lib/helpers/buildURL.js +++ b/lib/helpers/buildURL.js @@ -59,6 +59,11 @@ module.exports = function buildURL(url, params, paramsSerializer) { } if (serializedParams) { + var hashmarkIndex = url.indexOf('#'); + if (hashmarkIndex !== -1) { + url = url.slice(0, hashmarkIndex); + } + url += (url.indexOf('?') === -1 ? '?' : '&') + serializedParams; } diff --git a/package.json b/package.json index 2229ce19f8..1289bb0a10 100644 --- a/package.json +++ b/package.json @@ -73,7 +73,7 @@ }, "typings": "./index.d.ts", "dependencies": { - "follow-redirects": "^1.4.1", + "follow-redirects": "1.5.10", "is-buffer": "^2.0.2" }, "bundlesize": [ diff --git a/test/specs/helpers/buildURL.spec.js b/test/specs/helpers/buildURL.spec.js index 83d7ba1640..b819f9d2eb 100644 --- a/test/specs/helpers/buildURL.spec.js +++ b/test/specs/helpers/buildURL.spec.js @@ -54,6 +54,12 @@ describe('helpers::buildURL', function () { })).toEqual('/foo?query=bar&start=0&length=5'); }); + it('should correct discard url hash mark', function () { + expect(buildURL('/foo?foo=bar#hash', { + query: 'baz' + })).toEqual('/foo?foo=bar&query=baz'); + }); + it('should use serializer if provided', function () { serializer = sinon.stub(); params = {foo: 'bar'};