Skip to content

Commit

Permalink
docs: es6ify the docs a little (axios#1461)
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinBeckwith authored Apr 8, 2018
1 parent 7b11cc7 commit 8e3b50c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 35 deletions.
45 changes: 22 additions & 23 deletions COOKBOOK.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
})
Expand All @@ -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');
});
```
Expand All @@ -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;
Expand All @@ -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, {
Expand All @@ -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 {
Expand Down
24 changes: 12 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'}
Expand Down Expand Up @@ -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'
});

Expand All @@ -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
Expand Down Expand Up @@ -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 () {/*...*/});
```

Expand Down Expand Up @@ -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
Expand All @@ -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) {
Expand All @@ -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);
Expand All @@ -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 }));
```

Expand All @@ -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' }));
```

Expand Down

0 comments on commit 8e3b50c

Please sign in to comment.