Skip to content

Commit 81579b3

Browse files
authored
Updates documentation with clearer return requirements (#19)
* Updates documentation with clearer return requirements * Updates indentation
1 parent 04065a8 commit 81579b3

File tree

12 files changed

+197
-158
lines changed

12 files changed

+197
-158
lines changed

docs/README.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,18 @@ import { Service } from 'axios-middleware';
3939
const service = new Service(axios);
4040

4141
service.register({
42-
onRequest(config) {
43-
console.log('onRequest');
44-
return config;
45-
},
46-
onSync(promise) {
47-
console.log('onSync');
48-
return promise;
49-
},
50-
onResponse(response) {
51-
console.log('onResponse');
52-
return response;
53-
}
42+
onRequest(config) {
43+
console.log('onRequest');
44+
return config;
45+
},
46+
onSync(promise) {
47+
console.log('onSync');
48+
return promise;
49+
},
50+
onResponse(response) {
51+
console.log('onResponse');
52+
return response;
53+
}
5454
});
5555

5656
console.log('Ready to fetch.');

docs/_sidebar.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22
- [Getting started]()
33
- [Installation](installation.md)
44
- [Simplified syntax](simplified-syntax.md)
5-
5+
66
- API
77
- [Middleware methods](api/methods.md)
88
- [`Service` class](api/Service.md)
99

1010
- Examples
11+
- [Auth retry middleware](examples/auth-middleware.md)
1112
- [Locale middleware](examples/locale-middleware.md)
1213
- [Service module](examples/service.md)
1314
- [Returning promises](examples/promise.md)

docs/api/Service.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Middleware `Service` class
22

3-
This is the heart of this plugin module. It works by leveraging axios' adapter to call its middleware stack at each relevant steps of a request lifecycle.
3+
This is the heart of this plugin module. It works by leveraging axios' adapter to call the middleware stack at each relevant steps of a request lifecycle.
44

55
## `constructor(axios)`
66

@@ -41,4 +41,3 @@ Empty the middleware stack.
4141
## `adapter(config)`
4242

4343
The adapter function that replaces the default axios adapter. It calls the default implementation and passes the resulting promise to the middleware stack's `onSync` method.
44-

docs/api/methods.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ These will be called at different step of a request lifecycle. Each method can r
66

77
## `onRequest(config)`
88

9-
Receives the configuration objects before the request is made. Useful to add headers, query parameters, validate data, etc.
9+
Receives the configuration object before the request is made. Useful to add headers, query parameters, validate data, etc.
1010

1111
!> It must return the received `config` even if no changes were made to it. It can also return a promise that should resolve with the config to use for the request.
1212

@@ -16,6 +16,8 @@ No internet connection right now? You might end up in this function. Do what you
1616

1717
You can return a promise, or throw another error to keep the middleware chain going.
1818

19+
!> Failing to return a rejecting promise or throw an error would mean that the error was dealt with and the chain would continue on a success path.
20+
1921
## `onSync(promise)`
2022

2123
The request is being made and its promise is being passed. Do what you want with it but be sure to **return a promise**, be it the one just received or a new one.
@@ -28,7 +30,7 @@ Parsing the response can be done here. Say all responses from your API are retur
2830

2931
```javascript
3032
onResponse(response) {
31-
return response._data;
33+
return response._data;
3234
}
3335
```
3436

@@ -39,3 +41,5 @@ onResponse(response) {
3941
Not authenticated? Server problems? You might end up here. Do what you need with the error.
4042

4143
?> You can return a promise, which is useful when you want to retry failed requests.
44+
45+
!> Failing to return a rejecting promise or throw an error would mean that the error was dealt with and the chain would continue on a success path.

docs/examples/auth-middleware.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Unauthorized requests retry middleware
2+
3+
In a case where we'd like to retry a request if not authenticated, we could return a promise in the `onResponseError` method.
4+
5+
```javascript
6+
export default class AuthMiddleware {
7+
constructor(auth, http) {
8+
this.auth = auth;
9+
this.http = http;
10+
}
11+
12+
onResponseError(err) {
13+
if (err.response.status === 401 && err.config && !err.config.hasRetriedRequest) {
14+
return this.auth()
15+
// Retrying the request now that we're authenticated.
16+
.then((token) => this.http({
17+
...err.config
18+
hasRetriedRequest: true,
19+
headers: {
20+
...err.config.headers,
21+
Authorization: `Bearer ${token}`
22+
}
23+
})
24+
.catch(function (error) {
25+
console.log('Refresh login error: ', error);
26+
throw error;
27+
});
28+
}
29+
throw err;
30+
}
31+
}
32+
```

docs/examples/browser.md

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,20 @@ Just use the short middleware syntax to quickly define one-time use middleware.
1010
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
1111
<script src="https://unpkg.com/axios-middleware/dist/axios-middleware.min.js"></script>
1212
<script>
13-
// Create a new service instance
14-
var service = new HttpMiddlewareService(axios);
15-
16-
// Then register your middleware instances.
17-
service.register({
18-
onRequest: function(config) {
19-
// handle the request
20-
},
21-
onResponseError(error) {
22-
// handle the response error
23-
}
24-
});
13+
// Create a new service instance
14+
var service = new HttpMiddlewareService(axios);
15+
16+
// Then register your middleware instances.
17+
service.register({
18+
onRequest: function(config) {
19+
// handle the request
20+
return config;
21+
},
22+
onResponseError(error) {
23+
// handle the response error
24+
throw error;
25+
}
26+
});
2527
</script>
2628
```
2729

@@ -40,16 +42,16 @@ var app = app || {};
4042
* Custom Middleware class
4143
*/
4244
app.MyMiddleware = (function(){
43-
function MyMiddleware() {
44-
}
45-
46-
var proto = MyMiddleware.prototype = Object.create();
47-
48-
proto.constructor = MyMiddleware;
49-
proto.onRequest = function(config) {
50-
// handle the request
51-
};
52-
return MyMiddleware;
45+
function MyMiddleware() {}
46+
47+
var proto = MyMiddleware.prototype = Object.create();
48+
49+
proto.constructor = MyMiddleware;
50+
proto.onRequest = function(config) {
51+
// handle the request
52+
return config;
53+
};
54+
return MyMiddleware;
5355
})();
5456
```
5557

@@ -63,13 +65,13 @@ var app = app || {};
6365
* Middleware Service
6466
*/
6567
app.MiddlewareService = (function(MiddlewareService, MyMiddleware) {
66-
// Create a new service instance
67-
var service = new MiddlewareService(axios);
68-
69-
// Then register your middleware instances.
70-
service.register(new MyMiddleware());
71-
72-
return service;
68+
// Create a new service instance
69+
var service = new MiddlewareService(axios);
70+
71+
// Then register your middleware instances.
72+
service.register(new MyMiddleware());
73+
74+
return service;
7375
})(AxiosMiddleware.Service, app.MyMiddleware);
7476
```
7577

docs/examples/es5.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ Create a custom middleware to register.
88
var BaseMiddleware = require('./base-middleware');
99

1010
function MyMiddleware() {
11-
// call the parent constructor
12-
BaseMiddleware.apply(this, arguments);
11+
// call the parent constructor
12+
BaseMiddleware.apply(this, arguments);
1313
}
1414

1515
// Prototype wiring
@@ -19,6 +19,7 @@ proto.constructor = MyMiddleware;
1919
// Method overriding
2020
proto.onRequest = function(config) {
2121
// handle the request
22+
return config;
2223
};
2324

2425
module.exports = MyMiddleware;
@@ -39,4 +40,3 @@ service.register(new MyMiddleware());
3940

4041
module.exports = service;
4142
```
42-

docs/examples/locale-middleware.md

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
# Locale middleware
22

3-
Here's a simple example of a locale middleware who sets a language header on each AJAX request.
3+
Here's a simple example of a locale middleware who sets a language header on each request.
44

55
```javascript
66
export default class LocaleMiddleware {
7-
constructor(i18n) {
8-
this.i18n = i18n;
9-
}
10-
11-
onRequest(config) {
12-
config.headers = {
13-
locale: this.i18n.lang,
14-
...config.headers
15-
};
16-
return config;
17-
}
7+
constructor(i18n) {
8+
this.i18n = i18n;
9+
}
10+
11+
onRequest(config) {
12+
// returns a new Object to avoid changing the config object referenced.
13+
return {
14+
...config,
15+
headers: {
16+
// default `locale`, can still be overwritten by config.headers.locale
17+
locale: this.i18n.lang,
18+
...config.headers
19+
}
20+
};
21+
}
1822
}
1923
```

docs/examples/promise.md

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,26 @@
11
# Returning promises
22

3-
In a case where we'd like to retry a request if not authenticated, we could return a promise in the `onResponseError` method.
3+
Every method of our middleware are promise callback functions, meaning that they can return either a value, a new promise or throw an error and the middleware chain will react accordingly.
44

55
```javascript
6-
export default class AuthMiddleware {
7-
constructor(auth, http) {
8-
this.auth = auth;
9-
this.http = http;
10-
}
11-
12-
onResponseError(err) {
13-
if (err.response.status === 401 && err.config && !err.config.hasRetriedRequest) {
14-
return this.auth()
15-
.then(function (token) {
16-
err.config.hasRetriedRequest = true;
17-
err.config.headers.Authorization = `Bearer ${token}`;
18-
19-
// Retrying the request now that we're authenticated.
20-
return this.http(err.config);
21-
})
22-
.catch(function (error) {
23-
console.log('Refresh login error: ', error);
24-
throw error;
25-
});
26-
}
27-
throw err;
6+
export default class DemoPromiseMiddleware {
7+
onRequest(config) {
8+
return asyncChecks().then(() => config);
9+
}
10+
11+
onResponseError({ config } = {}) {
12+
if (config && !config.hasRetriedRequest) {
13+
// Retrying the request
14+
return this.http({
15+
...config,
16+
hasRetriedRequest: true,
17+
})
18+
.catch(function (error) {
19+
console.log('Retry failed:', error);
20+
throw error;
21+
});
2822
}
23+
throw err;
24+
}
2925
}
3026
```

docs/examples/service.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ const service = new Service(axios);
1111

1212
// Then register your middleware instances.
1313
service.register([
14-
new LocaleMiddleware(i18n),
15-
new OtherMiddleware()
14+
new LocaleMiddleware(i18n),
15+
new OtherMiddleware()
1616
]);
1717

1818
export default service;

0 commit comments

Comments
 (0)