mirror of
https://github.com/tenrok/axios.git
synced 2026-06-11 18:02:32 +03:00
docs: es6ify the docs a little (#1461)
This commit is contained in:
+22
-23
@@ -11,12 +11,12 @@ $ npm install axios promise --save
|
||||
```
|
||||
|
||||
```js
|
||||
var axios = require('axios');
|
||||
const axios = require('axios');
|
||||
require('promise/polyfill-done');
|
||||
|
||||
axios
|
||||
.get('http://www.example.com/user')
|
||||
.then(function (response) {
|
||||
.then((response) => {
|
||||
console.log(response.data);
|
||||
return response;
|
||||
})
|
||||
@@ -30,16 +30,16 @@ $ npm install axios promise.prototype.finally --save
|
||||
```
|
||||
|
||||
```js
|
||||
var axios = require('axios');
|
||||
const axios = require('axios');
|
||||
require('promise.prototype.finally').shim();
|
||||
|
||||
axios
|
||||
.get('http://www.example.com/user')
|
||||
.then(function (response) {
|
||||
.then((response) => {
|
||||
console.log(response.data);
|
||||
return response;
|
||||
})
|
||||
.finally(function () {
|
||||
.finally(() => {
|
||||
console.log('this will always be called');
|
||||
});
|
||||
```
|
||||
@@ -52,19 +52,19 @@ $ npm install axios pako --save
|
||||
|
||||
```js
|
||||
// client.js
|
||||
var axios = require('axios');
|
||||
var pako = require('pako');
|
||||
const axios = require('axios');
|
||||
const pako = require('pako');
|
||||
|
||||
var user = {
|
||||
const user = {
|
||||
firstName: 'Fred',
|
||||
lastName: 'Flintstone'
|
||||
};
|
||||
|
||||
var data = pako.deflate(JSON.stringify(user), { to: 'string' });
|
||||
const data = pako.deflate(JSON.stringify(user), { to: 'string' });
|
||||
|
||||
axios
|
||||
.post('http://127.0.0.1:3333/user', data)
|
||||
.then(function (response) {
|
||||
.then((response) => {
|
||||
response.data = JSON.parse(pako.inflate(response.data, { to: 'string' }));
|
||||
console.log(response.data);
|
||||
return response;
|
||||
@@ -73,25 +73,24 @@ axios
|
||||
|
||||
```js
|
||||
// server.js
|
||||
var pako = require('pako');
|
||||
var http = require('http');
|
||||
var url = require('url');
|
||||
var server;
|
||||
const pako = require('pako');
|
||||
const http = require('http');
|
||||
const url = require('url');
|
||||
|
||||
server = http.createServer(function (req, res) {
|
||||
const server = http.createServer((req, res) => {
|
||||
req.setEncoding('utf8');
|
||||
|
||||
var parsed = url.parse(req.url, true);
|
||||
var pathname = parsed.pathname;
|
||||
const parsed = url.parse(req.url, true);
|
||||
const pathname = parsed.pathname;
|
||||
|
||||
if (pathname === '/user') {
|
||||
var data = '';
|
||||
req.on('data', function (chunk) {
|
||||
let data = '';
|
||||
req.on('data', (chunk) => {
|
||||
data += chunk;
|
||||
});
|
||||
|
||||
req.on('end', function () {
|
||||
var user = JSON.parse(pako.inflate(data, { to: 'string' }));
|
||||
req.on('end', () => {
|
||||
const user = JSON.parse(pako.inflate(data, { to: 'string' }));
|
||||
console.log(user);
|
||||
|
||||
res.writeHead(200, {
|
||||
@@ -115,9 +114,9 @@ $ npm install jsonp --save
|
||||
```
|
||||
|
||||
```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) {
|
||||
console.error(err.message);
|
||||
} else {
|
||||
|
||||
@@ -188,7 +188,7 @@ You can create a new instance of axios with a custom config.
|
||||
##### axios.create([config])
|
||||
|
||||
```js
|
||||
var instance = axios.create({
|
||||
const instance = axios.create({
|
||||
baseURL: 'https://some-domain.com/api/',
|
||||
timeout: 1000,
|
||||
headers: {'X-Custom-Header': 'foobar'}
|
||||
@@ -424,7 +424,7 @@ axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded
|
||||
|
||||
```js
|
||||
// Set config defaults when creating the instance
|
||||
var instance = axios.create({
|
||||
const instance = axios.create({
|
||||
baseURL: 'https://api.example.com'
|
||||
});
|
||||
|
||||
@@ -439,7 +439,7 @@ Config will be merged with an order of precedence. The order is library defaults
|
||||
```js
|
||||
// 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
|
||||
var instance = axios.create();
|
||||
const instance = axios.create();
|
||||
|
||||
// Override timeout default for the library
|
||||
// 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.
|
||||
|
||||
```js
|
||||
var myInterceptor = axios.interceptors.request.use(function () {/*...*/});
|
||||
const myInterceptor = axios.interceptors.request.use(function () {/*...*/});
|
||||
axios.interceptors.request.eject(myInterceptor);
|
||||
```
|
||||
|
||||
You can add interceptors to a custom instance of axios.
|
||||
|
||||
```js
|
||||
var instance = axios.create();
|
||||
const instance = axios.create();
|
||||
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:
|
||||
|
||||
```js
|
||||
var CancelToken = axios.CancelToken;
|
||||
var source = CancelToken.source();
|
||||
const CancelToken = axios.CancelToken;
|
||||
const source = CancelToken.source();
|
||||
|
||||
axios.get('/user/12345', {
|
||||
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:
|
||||
|
||||
```js
|
||||
var CancelToken = axios.CancelToken;
|
||||
var cancel;
|
||||
const CancelToken = axios.CancelToken;
|
||||
let cancel;
|
||||
|
||||
axios.get('/user/12345', {
|
||||
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:
|
||||
|
||||
```js
|
||||
var params = new URLSearchParams();
|
||||
const params = new URLSearchParams();
|
||||
params.append('param1', 'value1');
|
||||
params.append('param2', 'value2');
|
||||
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:
|
||||
|
||||
```js
|
||||
var qs = require('qs');
|
||||
const qs = require('qs');
|
||||
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:
|
||||
|
||||
```js
|
||||
var querystring = require('querystring');
|
||||
const querystring = require('querystring');
|
||||
axios.post('http://something.com/', querystring.stringify({ foo: 'bar' }));
|
||||
```
|
||||
|
||||
|
||||
Reference in New Issue
Block a user