2
0
mirror of https://github.com/tenrok/axios.git synced 2026-06-23 20:40:40 +03:00

docs: es6ify the docs a little (#1461)

This commit is contained in:
Justin Beckwith
2018-04-08 00:18:56 -07:00
committed by GitHub
parent 7b11cc7181
commit 8e3b50c564
2 changed files with 34 additions and 35 deletions
+22 -23
View File
@@ -11,12 +11,12 @@ $ npm install axios promise --save
``` ```
```js ```js
var axios = require('axios'); const axios = require('axios');
require('promise/polyfill-done'); require('promise/polyfill-done');
axios axios
.get('http://www.example.com/user') .get('http://www.example.com/user')
.then(function (response) { .then((response) => {
console.log(response.data); console.log(response.data);
return response; return response;
}) })
@@ -30,16 +30,16 @@ $ npm install axios promise.prototype.finally --save
``` ```
```js ```js
var axios = require('axios'); const axios = require('axios');
require('promise.prototype.finally').shim(); require('promise.prototype.finally').shim();
axios axios
.get('http://www.example.com/user') .get('http://www.example.com/user')
.then(function (response) { .then((response) => {
console.log(response.data); console.log(response.data);
return response; return response;
}) })
.finally(function () { .finally(() => {
console.log('this will always be called'); console.log('this will always be called');
}); });
``` ```
@@ -52,19 +52,19 @@ $ npm install axios pako --save
```js ```js
// client.js // client.js
var axios = require('axios'); const axios = require('axios');
var pako = require('pako'); const pako = require('pako');
var user = { const user = {
firstName: 'Fred', firstName: 'Fred',
lastName: 'Flintstone' lastName: 'Flintstone'
}; };
var data = pako.deflate(JSON.stringify(user), { to: 'string' }); const data = pako.deflate(JSON.stringify(user), { to: 'string' });
axios axios
.post('http://127.0.0.1:3333/user', data) .post('http://127.0.0.1:3333/user', data)
.then(function (response) { .then((response) => {
response.data = JSON.parse(pako.inflate(response.data, { to: 'string' })); response.data = JSON.parse(pako.inflate(response.data, { to: 'string' }));
console.log(response.data); console.log(response.data);
return response; return response;
@@ -73,25 +73,24 @@ axios
```js ```js
// server.js // server.js
var pako = require('pako'); const pako = require('pako');
var http = require('http'); const http = require('http');
var url = require('url'); const url = require('url');
var server;
server = http.createServer(function (req, res) { const server = http.createServer((req, res) => {
req.setEncoding('utf8'); req.setEncoding('utf8');
var parsed = url.parse(req.url, true); const parsed = url.parse(req.url, true);
var pathname = parsed.pathname; const pathname = parsed.pathname;
if (pathname === '/user') { if (pathname === '/user') {
var data = ''; let data = '';
req.on('data', function (chunk) { req.on('data', (chunk) => {
data += chunk; data += chunk;
}); });
req.on('end', function () { req.on('end', () => {
var user = JSON.parse(pako.inflate(data, { to: 'string' })); const user = JSON.parse(pako.inflate(data, { to: 'string' }));
console.log(user); console.log(user);
res.writeHead(200, { res.writeHead(200, {
@@ -115,9 +114,9 @@ $ npm install jsonp --save
``` ```
```js ```js
var jsonp = require('jsonp'); const jsonp = require('jsonp');
jsonp('http://www.example.com/foo', null, function (err, data) { jsonp('http://www.example.com/foo', null, (err, data) => {
if (err) { if (err) {
console.error(err.message); console.error(err.message);
} else { } else {
+12 -12
View File
@@ -188,7 +188,7 @@ You can create a new instance of axios with a custom config.
##### axios.create([config]) ##### axios.create([config])
```js ```js
var instance = axios.create({ const instance = axios.create({
baseURL: 'https://some-domain.com/api/', baseURL: 'https://some-domain.com/api/',
timeout: 1000, timeout: 1000,
headers: {'X-Custom-Header': 'foobar'} headers: {'X-Custom-Header': 'foobar'}
@@ -424,7 +424,7 @@ axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded
```js ```js
// Set config defaults when creating the instance // Set config defaults when creating the instance
var instance = axios.create({ const instance = axios.create({
baseURL: 'https://api.example.com' baseURL: 'https://api.example.com'
}); });
@@ -439,7 +439,7 @@ Config will be merged with an order of precedence. The order is library defaults
```js ```js
// Create an instance using the config defaults provided by the library // Create an instance using the config defaults provided by the library
// At this point the timeout config value is `0` as is the default for the library // At this point the timeout config value is `0` as is the default for the library
var instance = axios.create(); const instance = axios.create();
// Override timeout default for the library // Override timeout default for the library
// Now all requests using this instance will wait 2.5 seconds before timing out // Now all requests using this instance will wait 2.5 seconds before timing out
@@ -478,14 +478,14 @@ axios.interceptors.response.use(function (response) {
If you may need to remove an interceptor later you can. If you may need to remove an interceptor later you can.
```js ```js
var myInterceptor = axios.interceptors.request.use(function () {/*...*/}); const myInterceptor = axios.interceptors.request.use(function () {/*...*/});
axios.interceptors.request.eject(myInterceptor); axios.interceptors.request.eject(myInterceptor);
``` ```
You can add interceptors to a custom instance of axios. You can add interceptors to a custom instance of axios.
```js ```js
var instance = axios.create(); const instance = axios.create();
instance.interceptors.request.use(function () {/*...*/}); instance.interceptors.request.use(function () {/*...*/});
``` ```
@@ -532,8 +532,8 @@ You can cancel a request using a *cancel token*.
You can create a cancel token using the `CancelToken.source` factory as shown below: You can create a cancel token using the `CancelToken.source` factory as shown below:
```js ```js
var CancelToken = axios.CancelToken; const CancelToken = axios.CancelToken;
var source = CancelToken.source(); const source = CancelToken.source();
axios.get('/user/12345', { axios.get('/user/12345', {
cancelToken: source.token cancelToken: source.token
@@ -558,8 +558,8 @@ source.cancel('Operation canceled by the user.');
You can also create a cancel token by passing an executor function to the `CancelToken` constructor: You can also create a cancel token by passing an executor function to the `CancelToken` constructor:
```js ```js
var CancelToken = axios.CancelToken; const CancelToken = axios.CancelToken;
var cancel; let cancel;
axios.get('/user/12345', { axios.get('/user/12345', {
cancelToken: new CancelToken(function executor(c) { cancelToken: new CancelToken(function executor(c) {
@@ -583,7 +583,7 @@ By default, axios serializes JavaScript objects to `JSON`. To send data in the `
In a browser, you can use the [`URLSearchParams`](https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams) API as follows: In a browser, you can use the [`URLSearchParams`](https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams) API as follows:
```js ```js
var params = new URLSearchParams(); const params = new URLSearchParams();
params.append('param1', 'value1'); params.append('param1', 'value1');
params.append('param2', 'value2'); params.append('param2', 'value2');
axios.post('/foo', params); axios.post('/foo', params);
@@ -594,7 +594,7 @@ axios.post('/foo', params);
Alternatively, you can encode data using the [`qs`](https://github.com/ljharb/qs) library: Alternatively, you can encode data using the [`qs`](https://github.com/ljharb/qs) library:
```js ```js
var qs = require('qs'); const qs = require('qs');
axios.post('/foo', qs.stringify({ 'bar': 123 })); axios.post('/foo', qs.stringify({ 'bar': 123 }));
``` ```
@@ -617,7 +617,7 @@ axios(options);
In node.js, you can use the [`querystring`](https://nodejs.org/api/querystring.html) module as follows: In node.js, you can use the [`querystring`](https://nodejs.org/api/querystring.html) module as follows:
```js ```js
var querystring = require('querystring'); const querystring = require('querystring');
axios.post('http://something.com/', querystring.stringify({ foo: 'bar' })); axios.post('http://something.com/', querystring.stringify({ foo: 'bar' }));
``` ```