You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

162 lines
4.7 KiB

4 years ago
  1. # Upgrade Guide
  2. ### 0.15.x -> 0.16.0
  3. #### `Promise` Type Declarations
  4. The `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.
  5. ### 0.13.x -> 0.14.0
  6. #### TypeScript Definitions
  7. The axios TypeScript definitions have been updated to match the axios API and use the ES2015 module syntax.
  8. Please use the following `import` statement to import axios in TypeScript:
  9. ```typescript
  10. import axios from 'axios';
  11. axios.get('/foo')
  12. .then(response => console.log(response))
  13. .catch(error => console.log(error));
  14. ```
  15. #### `agent` Config Option
  16. The `agent` config option has been replaced with two new options: `httpAgent` and `httpsAgent`. Please use them instead.
  17. ```js
  18. {
  19. // Define a custom agent for HTTP
  20. httpAgent: new http.Agent({ keepAlive: true }),
  21. // Define a custom agent for HTTPS
  22. httpsAgent: new https.Agent({ keepAlive: true })
  23. }
  24. ```
  25. #### `progress` Config Option
  26. The `progress` config option has been replaced with the `onUploadProgress` and `onDownloadProgress` options.
  27. ```js
  28. {
  29. // Define a handler for upload progress events
  30. onUploadProgress: function (progressEvent) {
  31. // ...
  32. },
  33. // Define a handler for download progress events
  34. onDownloadProgress: function (progressEvent) {
  35. // ...
  36. }
  37. }
  38. ```
  39. ### 0.12.x -> 0.13.0
  40. The `0.13.0` release contains several changes to custom adapters and error handling.
  41. #### Error Handling
  42. Previous 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.
  43. ```js
  44. axios.get('/user/12345')
  45. .catch((error) => {
  46. console.log(error.message);
  47. console.log(error.code); // Not always specified
  48. console.log(error.config); // The config that was used to make the request
  49. console.log(error.response); // Only available if response was received from the server
  50. });
  51. ```
  52. #### Request Adapters
  53. This release changes a few things about how request adapters work. Please take note if you are using your own custom adapter.
  54. 1. Response transformer is now called outside of adapter.
  55. 2. Request adapter returns a `Promise`.
  56. This 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.
  57. Previous code:
  58. ```js
  59. function myAdapter(resolve, reject, config) {
  60. var response = {
  61. data: transformData(
  62. responseData,
  63. responseHeaders,
  64. config.transformResponse
  65. ),
  66. status: request.status,
  67. statusText: request.statusText,
  68. headers: responseHeaders
  69. };
  70. settle(resolve, reject, response);
  71. }
  72. ```
  73. New code:
  74. ```js
  75. function myAdapter(config) {
  76. return new Promise(function (resolve, reject) {
  77. var response = {
  78. data: responseData,
  79. status: request.status,
  80. statusText: request.statusText,
  81. headers: responseHeaders
  82. };
  83. settle(resolve, reject, response);
  84. });
  85. }
  86. ```
  87. See the related commits for more details:
  88. - [Response transformers](https://github.com/axios/axios/commit/10eb23865101f9347570552c04e9d6211376e25e)
  89. - [Request adapter Promise](https://github.com/axios/axios/commit/157efd5615890301824e3121cc6c9d2f9b21f94a)
  90. ### 0.5.x -> 0.6.0
  91. The `0.6.0` release contains mostly bug fixes, but there are a couple things to be aware of when upgrading.
  92. #### ES6 Promise Polyfill
  93. Up 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.
  94. ```js
  95. require('es6-promise').polyfill();
  96. var axios = require('axios');
  97. ```
  98. This will polyfill the global environment, and only needs to be done once.
  99. #### `axios.success`/`axios.error`
  100. The `success`, and `error` aliases were deprecated 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.
  101. ```js
  102. axios.get('some/url')
  103. .then(function (res) {
  104. /* ... */
  105. })
  106. .catch(function (err) {
  107. /* ... */
  108. });
  109. ```
  110. #### UMD
  111. Previous versions of axios shipped with an AMD, CommonJS, and Global build. This has all been rolled into a single UMD build.
  112. ```js
  113. // AMD
  114. require(['bower_components/axios/dist/axios'], function (axios) {
  115. /* ... */
  116. });
  117. // CommonJS
  118. var axios = require('axios/dist/axios');
  119. ```