@swimlane/turbine-sdk 1.2.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (95) hide show
  1. package/LICENSE +5 -658
  2. package/README.md +784 -125
  3. package/bin/dev +17 -0
  4. package/bin/dev.cmd +3 -0
  5. package/bin/run +5 -0
  6. package/bin/run.cmd +3 -0
  7. package/lib/index.d.ts +1 -0
  8. package/lib/index.js +6 -0
  9. package/lib/index.js.map +1 -0
  10. package/oclif.manifest.json +1 -0
  11. package/package.json +98 -79
  12. package/dist/Sensor/BaseSensor.d.ts +0 -9
  13. package/dist/Sensor/BaseSensor.js +0 -10
  14. package/dist/Sensor/HttpSensorAgentInterface.d.ts +0 -21
  15. package/dist/Sensor/HttpSensorAgentInterface.js +0 -81
  16. package/dist/Sensor/MockSensorAgentInterface.d.ts +0 -22
  17. package/dist/Sensor/MockSensorAgentInterface.js +0 -76
  18. package/dist/Sensor/SensorAgentInterface.d.ts +0 -17
  19. package/dist/Sensor/SensorAgentInterface.js +0 -24
  20. package/dist/Sensor/SensorConfig.d.ts +0 -24
  21. package/dist/Sensor/SensorConfig.js +0 -3
  22. package/dist/Sensor/index.d.ts +0 -5
  23. package/dist/Sensor/index.js +0 -10
  24. package/dist/index.d.ts +0 -1
  25. package/dist/index.js +0 -7
  26. package/node_modules/axios/CHANGELOG.md +0 -348
  27. package/node_modules/axios/LICENSE +0 -19
  28. package/node_modules/axios/README.md +0 -684
  29. package/node_modules/axios/UPGRADE_GUIDE.md +0 -162
  30. package/node_modules/axios/dist/axios.js +0 -1668
  31. package/node_modules/axios/dist/axios.map +0 -1
  32. package/node_modules/axios/dist/axios.min.js +0 -9
  33. package/node_modules/axios/dist/axios.min.map +0 -1
  34. package/node_modules/axios/index.d.ts +0 -152
  35. package/node_modules/axios/index.js +0 -1
  36. package/node_modules/axios/lib/adapters/README.md +0 -37
  37. package/node_modules/axios/lib/adapters/http.js +0 -275
  38. package/node_modules/axios/lib/adapters/xhr.js +0 -174
  39. package/node_modules/axios/lib/axios.js +0 -53
  40. package/node_modules/axios/lib/cancel/Cancel.js +0 -19
  41. package/node_modules/axios/lib/cancel/CancelToken.js +0 -57
  42. package/node_modules/axios/lib/cancel/isCancel.js +0 -5
  43. package/node_modules/axios/lib/core/Axios.js +0 -86
  44. package/node_modules/axios/lib/core/InterceptorManager.js +0 -52
  45. package/node_modules/axios/lib/core/README.md +0 -7
  46. package/node_modules/axios/lib/core/createError.js +0 -18
  47. package/node_modules/axios/lib/core/dispatchRequest.js +0 -86
  48. package/node_modules/axios/lib/core/enhanceError.js +0 -42
  49. package/node_modules/axios/lib/core/mergeConfig.js +0 -51
  50. package/node_modules/axios/lib/core/settle.js +0 -25
  51. package/node_modules/axios/lib/core/transformData.js +0 -20
  52. package/node_modules/axios/lib/defaults.js +0 -98
  53. package/node_modules/axios/lib/helpers/README.md +0 -7
  54. package/node_modules/axios/lib/helpers/bind.js +0 -11
  55. package/node_modules/axios/lib/helpers/buildURL.js +0 -71
  56. package/node_modules/axios/lib/helpers/combineURLs.js +0 -14
  57. package/node_modules/axios/lib/helpers/cookies.js +0 -53
  58. package/node_modules/axios/lib/helpers/deprecatedMethod.js +0 -24
  59. package/node_modules/axios/lib/helpers/isAbsoluteURL.js +0 -14
  60. package/node_modules/axios/lib/helpers/isURLSameOrigin.js +0 -68
  61. package/node_modules/axios/lib/helpers/normalizeHeaderName.js +0 -12
  62. package/node_modules/axios/lib/helpers/parseHeaders.js +0 -53
  63. package/node_modules/axios/lib/helpers/spread.js +0 -27
  64. package/node_modules/axios/lib/utils.js +0 -334
  65. package/node_modules/axios/package.json +0 -115
  66. package/node_modules/debug/.coveralls.yml +0 -1
  67. package/node_modules/debug/.eslintrc +0 -14
  68. package/node_modules/debug/.npmignore +0 -9
  69. package/node_modules/debug/.travis.yml +0 -20
  70. package/node_modules/debug/CHANGELOG.md +0 -395
  71. package/node_modules/debug/LICENSE +0 -19
  72. package/node_modules/debug/Makefile +0 -58
  73. package/node_modules/debug/README.md +0 -368
  74. package/node_modules/debug/karma.conf.js +0 -70
  75. package/node_modules/debug/node.js +0 -1
  76. package/node_modules/debug/package.json +0 -87
  77. package/node_modules/debug/src/browser.js +0 -195
  78. package/node_modules/debug/src/debug.js +0 -225
  79. package/node_modules/debug/src/index.js +0 -10
  80. package/node_modules/debug/src/node.js +0 -186
  81. package/node_modules/follow-redirects/LICENSE +0 -18
  82. package/node_modules/follow-redirects/README.md +0 -155
  83. package/node_modules/follow-redirects/http.js +0 -1
  84. package/node_modules/follow-redirects/https.js +0 -1
  85. package/node_modules/follow-redirects/index.js +0 -322
  86. package/node_modules/follow-redirects/package.json +0 -99
  87. package/node_modules/is-buffer/LICENSE +0 -21
  88. package/node_modules/is-buffer/README.md +0 -56
  89. package/node_modules/is-buffer/index.d.ts +0 -2
  90. package/node_modules/is-buffer/index.js +0 -11
  91. package/node_modules/is-buffer/package.json +0 -81
  92. package/node_modules/ms/index.js +0 -152
  93. package/node_modules/ms/license.md +0 -21
  94. package/node_modules/ms/package.json +0 -73
  95. package/node_modules/ms/readme.md +0 -51
@@ -1,684 +0,0 @@
1
- # axios
2
-
3
- [![npm version](https://img.shields.io/npm/v/axios.svg?style=flat-square)](https://www.npmjs.org/package/axios)
4
- [![build status](https://img.shields.io/travis/axios/axios.svg?style=flat-square)](https://travis-ci.org/axios/axios)
5
- [![code coverage](https://img.shields.io/coveralls/mzabriskie/axios.svg?style=flat-square)](https://coveralls.io/r/mzabriskie/axios)
6
- [![install size](https://packagephobia.now.sh/badge?p=axios)](https://packagephobia.now.sh/result?p=axios)
7
- [![npm downloads](https://img.shields.io/npm/dm/axios.svg?style=flat-square)](http://npm-stat.com/charts.html?package=axios)
8
- [![gitter chat](https://img.shields.io/gitter/room/mzabriskie/axios.svg?style=flat-square)](https://gitter.im/mzabriskie/axios)
9
- [![code helpers](https://www.codetriage.com/axios/axios/badges/users.svg)](https://www.codetriage.com/axios/axios)
10
-
11
- Promise based HTTP client for the browser and node.js
12
-
13
- ## Features
14
-
15
- - Make [XMLHttpRequests](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest) from the browser
16
- - Make [http](http://nodejs.org/api/http.html) requests from node.js
17
- - Supports the [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) API
18
- - Intercept request and response
19
- - Transform request and response data
20
- - Cancel requests
21
- - Automatic transforms for JSON data
22
- - Client side support for protecting against [XSRF](http://en.wikipedia.org/wiki/Cross-site_request_forgery)
23
-
24
- ## Browser Support
25
-
26
- ![Chrome](https://raw.github.com/alrra/browser-logos/master/src/chrome/chrome_48x48.png) | ![Firefox](https://raw.github.com/alrra/browser-logos/master/src/firefox/firefox_48x48.png) | ![Safari](https://raw.github.com/alrra/browser-logos/master/src/safari/safari_48x48.png) | ![Opera](https://raw.github.com/alrra/browser-logos/master/src/opera/opera_48x48.png) | ![Edge](https://raw.github.com/alrra/browser-logos/master/src/edge/edge_48x48.png) | ![IE](https://raw.github.com/alrra/browser-logos/master/src/archive/internet-explorer_9-11/internet-explorer_9-11_48x48.png) |
27
- --- | --- | --- | --- | --- | --- |
28
- Latest ✔ | Latest ✔ | Latest ✔ | Latest ✔ | Latest ✔ | 11 ✔ |
29
-
30
- [![Browser Matrix](https://saucelabs.com/open_sauce/build_matrix/axios.svg)](https://saucelabs.com/u/axios)
31
-
32
- ## Installing
33
-
34
- Using npm:
35
-
36
- ```bash
37
- $ npm install axios
38
- ```
39
-
40
- Using bower:
41
-
42
- ```bash
43
- $ bower install axios
44
- ```
45
-
46
- Using yarn:
47
-
48
- ```bash
49
- $ yarn add axios
50
- ```
51
-
52
- Using cdn:
53
-
54
- ```html
55
- <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
56
- ```
57
-
58
- ## Example
59
-
60
- Performing a `GET` request
61
-
62
- ```js
63
- const axios = require('axios');
64
-
65
- // Make a request for a user with a given ID
66
- axios.get('/user?ID=12345')
67
- .then(function (response) {
68
- // handle success
69
- console.log(response);
70
- })
71
- .catch(function (error) {
72
- // handle error
73
- console.log(error);
74
- })
75
- .finally(function () {
76
- // always executed
77
- });
78
-
79
- // Optionally the request above could also be done as
80
- axios.get('/user', {
81
- params: {
82
- ID: 12345
83
- }
84
- })
85
- .then(function (response) {
86
- console.log(response);
87
- })
88
- .catch(function (error) {
89
- console.log(error);
90
- })
91
- .then(function () {
92
- // always executed
93
- });
94
-
95
- // Want to use async/await? Add the `async` keyword to your outer function/method.
96
- async function getUser() {
97
- try {
98
- const response = await axios.get('/user?ID=12345');
99
- console.log(response);
100
- } catch (error) {
101
- console.error(error);
102
- }
103
- }
104
- ```
105
-
106
- > **NOTE:** `async/await` is part of ECMAScript 2017 and is not supported in Internet
107
- > Explorer and older browsers, so use with caution.
108
-
109
- Performing a `POST` request
110
-
111
- ```js
112
- axios.post('/user', {
113
- firstName: 'Fred',
114
- lastName: 'Flintstone'
115
- })
116
- .then(function (response) {
117
- console.log(response);
118
- })
119
- .catch(function (error) {
120
- console.log(error);
121
- });
122
- ```
123
-
124
- Performing multiple concurrent requests
125
-
126
- ```js
127
- function getUserAccount() {
128
- return axios.get('/user/12345');
129
- }
130
-
131
- function getUserPermissions() {
132
- return axios.get('/user/12345/permissions');
133
- }
134
-
135
- axios.all([getUserAccount(), getUserPermissions()])
136
- .then(axios.spread(function (acct, perms) {
137
- // Both requests are now complete
138
- }));
139
- ```
140
-
141
- ## axios API
142
-
143
- Requests can be made by passing the relevant config to `axios`.
144
-
145
- ##### axios(config)
146
-
147
- ```js
148
- // Send a POST request
149
- axios({
150
- method: 'post',
151
- url: '/user/12345',
152
- data: {
153
- firstName: 'Fred',
154
- lastName: 'Flintstone'
155
- }
156
- });
157
- ```
158
-
159
- ```js
160
- // GET request for remote image
161
- axios({
162
- method: 'get',
163
- url: 'http://bit.ly/2mTM3nY',
164
- responseType: 'stream'
165
- })
166
- .then(function (response) {
167
- response.data.pipe(fs.createWriteStream('ada_lovelace.jpg'))
168
- });
169
- ```
170
-
171
- ##### axios(url[, config])
172
-
173
- ```js
174
- // Send a GET request (default method)
175
- axios('/user/12345');
176
- ```
177
-
178
- ### Request method aliases
179
-
180
- For convenience aliases have been provided for all supported request methods.
181
-
182
- ##### axios.request(config)
183
- ##### axios.get(url[, config])
184
- ##### axios.delete(url[, config])
185
- ##### axios.head(url[, config])
186
- ##### axios.options(url[, config])
187
- ##### axios.post(url[, data[, config]])
188
- ##### axios.put(url[, data[, config]])
189
- ##### axios.patch(url[, data[, config]])
190
-
191
- ###### NOTE
192
- When using the alias methods `url`, `method`, and `data` properties don't need to be specified in config.
193
-
194
- ### Concurrency
195
-
196
- Helper functions for dealing with concurrent requests.
197
-
198
- ##### axios.all(iterable)
199
- ##### axios.spread(callback)
200
-
201
- ### Creating an instance
202
-
203
- You can create a new instance of axios with a custom config.
204
-
205
- ##### axios.create([config])
206
-
207
- ```js
208
- const instance = axios.create({
209
- baseURL: 'https://some-domain.com/api/',
210
- timeout: 1000,
211
- headers: {'X-Custom-Header': 'foobar'}
212
- });
213
- ```
214
-
215
- ### Instance methods
216
-
217
- The available instance methods are listed below. The specified config will be merged with the instance config.
218
-
219
- ##### axios#request(config)
220
- ##### axios#get(url[, config])
221
- ##### axios#delete(url[, config])
222
- ##### axios#head(url[, config])
223
- ##### axios#options(url[, config])
224
- ##### axios#post(url[, data[, config]])
225
- ##### axios#put(url[, data[, config]])
226
- ##### axios#patch(url[, data[, config]])
227
- ##### axios#getUri([config])
228
-
229
- ## Request Config
230
-
231
- These are the available config options for making requests. Only the `url` is required. Requests will default to `GET` if `method` is not specified.
232
-
233
- ```js
234
- {
235
- // `url` is the server URL that will be used for the request
236
- url: '/user',
237
-
238
- // `method` is the request method to be used when making the request
239
- method: 'get', // default
240
-
241
- // `baseURL` will be prepended to `url` unless `url` is absolute.
242
- // It can be convenient to set `baseURL` for an instance of axios to pass relative URLs
243
- // to methods of that instance.
244
- baseURL: 'https://some-domain.com/api/',
245
-
246
- // `transformRequest` allows changes to the request data before it is sent to the server
247
- // This is only applicable for request methods 'PUT', 'POST', 'PATCH' and 'DELETE'
248
- // The last function in the array must return a string or an instance of Buffer, ArrayBuffer,
249
- // FormData or Stream
250
- // You may modify the headers object.
251
- transformRequest: [function (data, headers) {
252
- // Do whatever you want to transform the data
253
-
254
- return data;
255
- }],
256
-
257
- // `transformResponse` allows changes to the response data to be made before
258
- // it is passed to then/catch
259
- transformResponse: [function (data) {
260
- // Do whatever you want to transform the data
261
-
262
- return data;
263
- }],
264
-
265
- // `headers` are custom headers to be sent
266
- headers: {'X-Requested-With': 'XMLHttpRequest'},
267
-
268
- // `params` are the URL parameters to be sent with the request
269
- // Must be a plain object or a URLSearchParams object
270
- params: {
271
- ID: 12345
272
- },
273
-
274
- // `paramsSerializer` is an optional function in charge of serializing `params`
275
- // (e.g. https://www.npmjs.com/package/qs, http://api.jquery.com/jquery.param/)
276
- paramsSerializer: function (params) {
277
- return Qs.stringify(params, {arrayFormat: 'brackets'})
278
- },
279
-
280
- // `data` is the data to be sent as the request body
281
- // Only applicable for request methods 'PUT', 'POST', and 'PATCH'
282
- // When no `transformRequest` is set, must be of one of the following types:
283
- // - string, plain object, ArrayBuffer, ArrayBufferView, URLSearchParams
284
- // - Browser only: FormData, File, Blob
285
- // - Node only: Stream, Buffer
286
- data: {
287
- firstName: 'Fred'
288
- },
289
-
290
- // `timeout` specifies the number of milliseconds before the request times out.
291
- // If the request takes longer than `timeout`, the request will be aborted.
292
- timeout: 1000, // default is `0` (no timeout)
293
-
294
- // `withCredentials` indicates whether or not cross-site Access-Control requests
295
- // should be made using credentials
296
- withCredentials: false, // default
297
-
298
- // `adapter` allows custom handling of requests which makes testing easier.
299
- // Return a promise and supply a valid response (see lib/adapters/README.md).
300
- adapter: function (config) {
301
- /* ... */
302
- },
303
-
304
- // `auth` indicates that HTTP Basic auth should be used, and supplies credentials.
305
- // This will set an `Authorization` header, overwriting any existing
306
- // `Authorization` custom headers you have set using `headers`.
307
- // Please note that only HTTP Basic auth is configurable through this parameter.
308
- // For Bearer tokens and such, use `Authorization` custom headers instead.
309
- auth: {
310
- username: 'janedoe',
311
- password: 's00pers3cret'
312
- },
313
-
314
- // `responseType` indicates the type of data that the server will respond with
315
- // options are: 'arraybuffer', 'document', 'json', 'text', 'stream'
316
- // browser only: 'blob'
317
- responseType: 'json', // default
318
-
319
- // `responseEncoding` indicates encoding to use for decoding responses
320
- // Note: Ignored for `responseType` of 'stream' or client-side requests
321
- responseEncoding: 'utf8', // default
322
-
323
- // `xsrfCookieName` is the name of the cookie to use as a value for xsrf token
324
- xsrfCookieName: 'XSRF-TOKEN', // default
325
-
326
- // `xsrfHeaderName` is the name of the http header that carries the xsrf token value
327
- xsrfHeaderName: 'X-XSRF-TOKEN', // default
328
-
329
- // `onUploadProgress` allows handling of progress events for uploads
330
- onUploadProgress: function (progressEvent) {
331
- // Do whatever you want with the native progress event
332
- },
333
-
334
- // `onDownloadProgress` allows handling of progress events for downloads
335
- onDownloadProgress: function (progressEvent) {
336
- // Do whatever you want with the native progress event
337
- },
338
-
339
- // `maxContentLength` defines the max size of the http response content in bytes allowed
340
- maxContentLength: 2000,
341
-
342
- // `validateStatus` defines whether to resolve or reject the promise for a given
343
- // HTTP response status code. If `validateStatus` returns `true` (or is set to `null`
344
- // or `undefined`), the promise will be resolved; otherwise, the promise will be
345
- // rejected.
346
- validateStatus: function (status) {
347
- return status >= 200 && status < 300; // default
348
- },
349
-
350
- // `maxRedirects` defines the maximum number of redirects to follow in node.js.
351
- // If set to 0, no redirects will be followed.
352
- maxRedirects: 5, // default
353
-
354
- // `socketPath` defines a UNIX Socket to be used in node.js.
355
- // e.g. '/var/run/docker.sock' to send requests to the docker daemon.
356
- // Only either `socketPath` or `proxy` can be specified.
357
- // If both are specified, `socketPath` is used.
358
- socketPath: null, // default
359
-
360
- // `httpAgent` and `httpsAgent` define a custom agent to be used when performing http
361
- // and https requests, respectively, in node.js. This allows options to be added like
362
- // `keepAlive` that are not enabled by default.
363
- httpAgent: new http.Agent({ keepAlive: true }),
364
- httpsAgent: new https.Agent({ keepAlive: true }),
365
-
366
- // 'proxy' defines the hostname and port of the proxy server.
367
- // You can also define your proxy using the conventional `http_proxy` and
368
- // `https_proxy` environment variables. If you are using environment variables
369
- // for your proxy configuration, you can also define a `no_proxy` environment
370
- // variable as a comma-separated list of domains that should not be proxied.
371
- // Use `false` to disable proxies, ignoring environment variables.
372
- // `auth` indicates that HTTP Basic auth should be used to connect to the proxy, and
373
- // supplies credentials.
374
- // This will set an `Proxy-Authorization` header, overwriting any existing
375
- // `Proxy-Authorization` custom headers you have set using `headers`.
376
- proxy: {
377
- host: '127.0.0.1',
378
- port: 9000,
379
- auth: {
380
- username: 'mikeymike',
381
- password: 'rapunz3l'
382
- }
383
- },
384
-
385
- // `cancelToken` specifies a cancel token that can be used to cancel the request
386
- // (see Cancellation section below for details)
387
- cancelToken: new CancelToken(function (cancel) {
388
- })
389
- }
390
- ```
391
-
392
- ## Response Schema
393
-
394
- The response for a request contains the following information.
395
-
396
- ```js
397
- {
398
- // `data` is the response that was provided by the server
399
- data: {},
400
-
401
- // `status` is the HTTP status code from the server response
402
- status: 200,
403
-
404
- // `statusText` is the HTTP status message from the server response
405
- statusText: 'OK',
406
-
407
- // `headers` the headers that the server responded with
408
- // All header names are lower cased
409
- headers: {},
410
-
411
- // `config` is the config that was provided to `axios` for the request
412
- config: {},
413
-
414
- // `request` is the request that generated this response
415
- // It is the last ClientRequest instance in node.js (in redirects)
416
- // and an XMLHttpRequest instance the browser
417
- request: {}
418
- }
419
- ```
420
-
421
- When using `then`, you will receive the response as follows:
422
-
423
- ```js
424
- axios.get('/user/12345')
425
- .then(function (response) {
426
- console.log(response.data);
427
- console.log(response.status);
428
- console.log(response.statusText);
429
- console.log(response.headers);
430
- console.log(response.config);
431
- });
432
- ```
433
-
434
- When using `catch`, or passing a [rejection callback](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then) as second parameter of `then`, the response will be available through the `error` object as explained in the [Handling Errors](#handling-errors) section.
435
-
436
- ## Config Defaults
437
-
438
- You can specify config defaults that will be applied to every request.
439
-
440
- ### Global axios defaults
441
-
442
- ```js
443
- axios.defaults.baseURL = 'https://api.example.com';
444
- axios.defaults.headers.common['Authorization'] = AUTH_TOKEN;
445
- axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
446
- ```
447
-
448
- ### Custom instance defaults
449
-
450
- ```js
451
- // Set config defaults when creating the instance
452
- const instance = axios.create({
453
- baseURL: 'https://api.example.com'
454
- });
455
-
456
- // Alter defaults after instance has been created
457
- instance.defaults.headers.common['Authorization'] = AUTH_TOKEN;
458
- ```
459
-
460
- ### Config order of precedence
461
-
462
- Config will be merged with an order of precedence. The order is library defaults found in [lib/defaults.js](https://github.com/axios/axios/blob/master/lib/defaults.js#L28), then `defaults` property of the instance, and finally `config` argument for the request. The latter will take precedence over the former. Here's an example.
463
-
464
- ```js
465
- // Create an instance using the config defaults provided by the library
466
- // At this point the timeout config value is `0` as is the default for the library
467
- const instance = axios.create();
468
-
469
- // Override timeout default for the library
470
- // Now all requests using this instance will wait 2.5 seconds before timing out
471
- instance.defaults.timeout = 2500;
472
-
473
- // Override timeout for this request as it's known to take a long time
474
- instance.get('/longRequest', {
475
- timeout: 5000
476
- });
477
- ```
478
-
479
- ## Interceptors
480
-
481
- You can intercept requests or responses before they are handled by `then` or `catch`.
482
-
483
- ```js
484
- // Add a request interceptor
485
- axios.interceptors.request.use(function (config) {
486
- // Do something before request is sent
487
- return config;
488
- }, function (error) {
489
- // Do something with request error
490
- return Promise.reject(error);
491
- });
492
-
493
- // Add a response interceptor
494
- axios.interceptors.response.use(function (response) {
495
- // Do something with response data
496
- return response;
497
- }, function (error) {
498
- // Do something with response error
499
- return Promise.reject(error);
500
- });
501
- ```
502
-
503
- If you may need to remove an interceptor later you can.
504
-
505
- ```js
506
- const myInterceptor = axios.interceptors.request.use(function () {/*...*/});
507
- axios.interceptors.request.eject(myInterceptor);
508
- ```
509
-
510
- You can add interceptors to a custom instance of axios.
511
-
512
- ```js
513
- const instance = axios.create();
514
- instance.interceptors.request.use(function () {/*...*/});
515
- ```
516
-
517
- ## Handling Errors
518
-
519
- ```js
520
- axios.get('/user/12345')
521
- .catch(function (error) {
522
- if (error.response) {
523
- // The request was made and the server responded with a status code
524
- // that falls out of the range of 2xx
525
- console.log(error.response.data);
526
- console.log(error.response.status);
527
- console.log(error.response.headers);
528
- } else if (error.request) {
529
- // The request was made but no response was received
530
- // `error.request` is an instance of XMLHttpRequest in the browser and an instance of
531
- // http.ClientRequest in node.js
532
- console.log(error.request);
533
- } else {
534
- // Something happened in setting up the request that triggered an Error
535
- console.log('Error', error.message);
536
- }
537
- console.log(error.config);
538
- });
539
- ```
540
-
541
- You can define a custom HTTP status code error range using the `validateStatus` config option.
542
-
543
- ```js
544
- axios.get('/user/12345', {
545
- validateStatus: function (status) {
546
- return status < 500; // Reject only if the status code is greater than or equal to 500
547
- }
548
- })
549
- ```
550
-
551
- ## Cancellation
552
-
553
- You can cancel a request using a *cancel token*.
554
-
555
- > The axios cancel token API is based on the withdrawn [cancelable promises proposal](https://github.com/tc39/proposal-cancelable-promises).
556
-
557
- You can create a cancel token using the `CancelToken.source` factory as shown below:
558
-
559
- ```js
560
- const CancelToken = axios.CancelToken;
561
- const source = CancelToken.source();
562
-
563
- axios.get('/user/12345', {
564
- cancelToken: source.token
565
- }).catch(function (thrown) {
566
- if (axios.isCancel(thrown)) {
567
- console.log('Request canceled', thrown.message);
568
- } else {
569
- // handle error
570
- }
571
- });
572
-
573
- axios.post('/user/12345', {
574
- name: 'new name'
575
- }, {
576
- cancelToken: source.token
577
- })
578
-
579
- // cancel the request (the message parameter is optional)
580
- source.cancel('Operation canceled by the user.');
581
- ```
582
-
583
- You can also create a cancel token by passing an executor function to the `CancelToken` constructor:
584
-
585
- ```js
586
- const CancelToken = axios.CancelToken;
587
- let cancel;
588
-
589
- axios.get('/user/12345', {
590
- cancelToken: new CancelToken(function executor(c) {
591
- // An executor function receives a cancel function as a parameter
592
- cancel = c;
593
- })
594
- });
595
-
596
- // cancel the request
597
- cancel();
598
- ```
599
-
600
- > Note: you can cancel several requests with the same cancel token.
601
-
602
- ## Using application/x-www-form-urlencoded format
603
-
604
- By default, axios serializes JavaScript objects to `JSON`. To send data in the `application/x-www-form-urlencoded` format instead, you can use one of the following options.
605
-
606
- ### Browser
607
-
608
- In a browser, you can use the [`URLSearchParams`](https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams) API as follows:
609
-
610
- ```js
611
- const params = new URLSearchParams();
612
- params.append('param1', 'value1');
613
- params.append('param2', 'value2');
614
- axios.post('/foo', params);
615
- ```
616
-
617
- > 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).
618
-
619
- Alternatively, you can encode data using the [`qs`](https://github.com/ljharb/qs) library:
620
-
621
- ```js
622
- const qs = require('qs');
623
- axios.post('/foo', qs.stringify({ 'bar': 123 }));
624
- ```
625
-
626
- Or in another way (ES6),
627
-
628
- ```js
629
- import qs from 'qs';
630
- const data = { 'bar': 123 };
631
- const options = {
632
- method: 'POST',
633
- headers: { 'content-type': 'application/x-www-form-urlencoded' },
634
- data: qs.stringify(data),
635
- url,
636
- };
637
- axios(options);
638
- ```
639
-
640
- ### Node.js
641
-
642
- In node.js, you can use the [`querystring`](https://nodejs.org/api/querystring.html) module as follows:
643
-
644
- ```js
645
- const querystring = require('querystring');
646
- axios.post('http://something.com/', querystring.stringify({ foo: 'bar' }));
647
- ```
648
-
649
- You can also use the [`qs`](https://github.com/ljharb/qs) library.
650
-
651
- ###### NOTE
652
- 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).
653
-
654
- ## Semver
655
-
656
- 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.
657
-
658
- ## Promises
659
-
660
- axios depends on a native ES6 Promise implementation to be [supported](http://caniuse.com/promises).
661
- If your environment doesn't support ES6 Promises, you can [polyfill](https://github.com/jakearchibald/es6-promise).
662
-
663
- ## TypeScript
664
- axios includes [TypeScript](http://typescriptlang.org) definitions.
665
- ```typescript
666
- import axios from 'axios';
667
- axios.get('/user?ID=12345');
668
- ```
669
-
670
- ## Resources
671
-
672
- * [Changelog](https://github.com/axios/axios/blob/master/CHANGELOG.md)
673
- * [Upgrade Guide](https://github.com/axios/axios/blob/master/UPGRADE_GUIDE.md)
674
- * [Ecosystem](https://github.com/axios/axios/blob/master/ECOSYSTEM.md)
675
- * [Contributing Guide](https://github.com/axios/axios/blob/master/CONTRIBUTING.md)
676
- * [Code of Conduct](https://github.com/axios/axios/blob/master/CODE_OF_CONDUCT.md)
677
-
678
- ## Credits
679
-
680
- axios is heavily inspired by the [$http service](https://docs.angularjs.org/api/ng/service/$http) provided in [Angular](https://angularjs.org/). Ultimately axios is an effort to provide a standalone `$http`-like service for use outside of Angular.
681
-
682
- ## License
683
-
684
- MIT