UNPKG

4.81 kBMarkdownView Raw
1# Upgrade Guide
2
3### 0.15.x -> 0.16.0
4
5#### `Promise` Type Declarations
6
7The `Promise` type declarations have been removed from the axios typings in favor of the built-in type declarations. If you use axios in a TypeScript project that targets `ES5`, please make sure to include the `es2015.promise` lib. Please see [this post](https://blog.mariusschulz.com/2016/11/25/typescript-2-0-built-in-type-declarations) for details.
8
9### 0.13.x -> 0.14.0
10
11#### TypeScript Definitions
12
13The axios TypeScript definitions have been updated to match the axios API and use the ES2015 module syntax.
14
15Please use the following `import` statement to import axios in TypeScript:
16
17```typescript
18import axios from 'axios';
19
20axios.get('/foo')
21 .then(response => console.log(response))
22 .catch(error => console.log(error));
23```
24
25#### `agent` Config Option
26
27The `agent` config option has been replaced with two new options: `httpAgent` and `httpsAgent`. Please use them instead.
28
29```js
30{
31 // Define a custom agent for HTTP
32 httpAgent: new http.Agent({ keepAlive: true }),
33 // Define a custom agent for HTTPS
34 httpsAgent: new https.Agent({ keepAlive: true })
35}
36```
37
38#### `progress` Config Option
39
40The `progress` config option has been replaced with the `onUploadProgress` and `onDownloadProgress` options.
41
42```js
43{
44 // Define a handler for upload progress events
45 onUploadProgress: function (progressEvent) {
46 // ...
47 },
48
49 // Define a handler for download progress events
50 onDownloadProgress: function (progressEvent) {
51 // ...
52 }
53}
54```
55
56### 0.12.x -> 0.13.0
57
58The `0.13.0` release contains several changes to custom adapters and error handling.
59
60#### Error Handling
61
62Previous to this release an error could either be a server response with bad status code or an actual `Error`. With this release Promise will always reject with an `Error`. In the case that a response was received, the `Error` will also include the response.
63
64```js
65axios.get('/user/12345')
66 .catch((error) => {
67 console.log(error.message);
68 console.log(error.code); // Not always specified
69 console.log(error.config); // The config that was used to make the request
70 console.log(error.response); // Only available if response was received from the server
71 });
72```
73
74#### Request Adapters
75
76This release changes a few things about how request adapters work. Please take note if you are using your own custom adapter.
77
781. Response transformer is now called outside of adapter.
792. Request adapter returns a `Promise`.
80
81This means that you no longer need to invoke `transformData` on response data. You will also no longer receive `resolve` and `reject` as arguments in your adapter.
82
83Previous code:
84
85```js
86function myAdapter(resolve, reject, config) {
87 var response = {
88 data: transformData(
89 responseData,
90 responseHeaders,
91 config.transformResponse
92 ),
93 status: request.status,
94 statusText: request.statusText,
95 headers: responseHeaders
96 };
97 settle(resolve, reject, response);
98}
99```
100
101New code:
102
103```js
104function myAdapter(config) {
105 return new Promise(function (resolve, reject) {
106 var response = {
107 data: responseData,
108 status: request.status,
109 statusText: request.statusText,
110 headers: responseHeaders
111 };
112 settle(resolve, reject, response);
113 });
114}
115```
116
117See the related commits for more details:
118- [Response transformers](https://github.com/axios/axios/commit/10eb23865101f9347570552c04e9d6211376e25e)
119- [Request adapter Promise](https://github.com/axios/axios/commit/157efd5615890301824e3121cc6c9d2f9b21f94a)
120
121### 0.5.x -> 0.6.0
122
123The `0.6.0` release contains mostly bug fixes, but there are a couple things to be aware of when upgrading.
124
125#### ES6 Promise Polyfill
126
127Up until the `0.6.0` release ES6 `Promise` was being polyfilled using [es6-promise](https://github.com/jakearchibald/es6-promise). With this release, the polyfill has been removed, and you will need to supply it yourself if your environment needs it.
128
129```js
130require('es6-promise').polyfill();
131var axios = require('axios');
132```
133
134This will polyfill the global environment, and only needs to be done once.
135
136#### `axios.success`/`axios.error`
137
138The `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.
139
140```js
141axios.get('some/url')
142 .then(function (res) {
143 /* ... */
144 })
145 .catch(function (err) {
146 /* ... */
147 });
148```
149
150#### UMD
151
152Previous versions of axios shipped with an AMD, CommonJS, and Global build. This has all been rolled into a single UMD build.
153
154```js
155// AMD
156require(['bower_components/axios/dist/axios'], function (axios) {
157 /* ... */
158});
159
160// CommonJS
161var axios = require('axios/dist/axios');
162```