Skip to content

Commit f46dd4e

Browse files
committed
Removes HttpMiddleware base class
1 parent 679302c commit f46dd4e

File tree

15 files changed

+59
-115
lines changed

15 files changed

+59
-115
lines changed

docs/README.md

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
# Axios HTTP middleware service
22

3-
Simple [axios](https://github.com/axios/axios) HTTP middleware service to simplify hooking to HTTP requests made through Axios.
3+
Simple [axios](https://github.com/axios/axios) HTTP middleware service to simplify hooking (and testing of hooks) to HTTP requests made through Axios.
44

55
## What's this?
66

7-
There are two classes exposed in this module:
7+
A [`HttpMiddlewareService`](api/Service.md) which manages a middleware stack and hooking itself to an axios instance.
88

9-
- [`HttpMiddleware`](api/HttpMiddleware.md): the base class to extend from when creating your own middleware.
10-
- [`HttpMiddlewareService`](api/HttpMiddlewareService.md) which manages the middleware stack and the hooking into the passed axios.
9+
Middlewares are just objects or classes composed of simple methods for different points in a request lifecycle.
1110

1211
It works with either the global axios or a local instance.
1312

@@ -17,11 +16,11 @@ Using axios interceptors makes the code tightly coupled to axios and harder to t
1716

1817
This middleware service module:
1918

20-
- offers more functionalities (e.g. see [`onSync`](api/HttpMiddleware?id=onsyncpromise))
19+
- offers more functionalities (e.g. see [`onSync`](api/methods?id=onsyncpromise))
2120
- looser coupling to axios
2221
- really easy to test middleware classes
2322

24-
It improves readability and reusability.
23+
It improves readability and reusability in a centralized hooking strategy.
2524

2625
## Examples
2726

@@ -37,26 +36,38 @@ The following example is using the [simplified syntax](simplified-syntax.md).
3736
import axios from 'axios';
3837
import { HttpMiddlewareService } from 'axios-middleware';
3938

40-
// Create a new service instance
4139
const service = new HttpMiddlewareService(axios);
4240

43-
// Then register your middleware instances.
4441
service.register({
4542
onRequest(config) {
46-
// handle the request config
43+
console.log('onRequest');
4744
return config;
4845
},
4946
onSync(promise) {
50-
// handle the promsie
47+
console.log('onSync');
5148
return promise;
5249
},
5350
onResponse(response) {
54-
// handle the response
51+
console.log('onResponse');
5552
return response;
5653
}
5754
});
5855

59-
// We're good to go!
60-
export default service;
56+
console.log('Ready to fetch.');
57+
58+
// Just use axios like you would normally.
59+
axios('https://jsonplaceholder.typicode.com/posts/1')
60+
.then(({ data }) => console.log('Received:', data));
61+
```
62+
63+
It should output:
64+
65+
```
66+
Ready to fetch.
67+
onRequest
68+
onSync
69+
onResponse
70+
Received: {userId: 1, id: 1, title: "sunt aut facere repellat provident occaecati excepturi optio reprehenderit", body: "quia et suscipit↵suscipit recusandae consequuntur …strum rerum est autem sunt rem eveniet architecto"}
6171
```
6272

73+
[**Demo snippet**](https://jsfiddle.net/emileber/sfqo0rt1/)

docs/_sidebar.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
- [Simplified syntax](simplified-syntax.md)
55

66
- API
7-
- [`HttpMiddleware`](api/HttpMiddleware.md)
8-
- [`HttpMiddlewareService`](api/HttpMiddlewareService.md)
7+
- [Middleware methods](api/methods.md)
8+
- [`Service` class](api/Service.md)
99

1010
- Examples
1111
- [Locale middleware](examples/locale-middleware.md)

docs/api/HttpMiddlewareService.md renamed to docs/api/Service.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# HttpMiddlewareService
1+
# 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.
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.
44

55
## `constructor(axios)`
66

@@ -26,7 +26,7 @@ Returns `true` if the passed `middleware` instance is within the stack.
2626

2727
Adds a middleware instance or an array of middlewares to the stack.
2828

29-
You can pass an `HttpMiddleware` instance or a simple object implementing only the functions you need (see the [simplified syntax](simplified-syntax.md)).
29+
You can pass a class instance or a simple object implementing only the functions you need (see the [simplified syntax](simplified-syntax.md)).
3030

3131
!> Throws an error if a middleware instance is already within the stack.
3232

docs/api/HttpMiddleware.md renamed to docs/api/methods.md

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
1-
# HttpMiddleware
1+
# The middleware methods
22

3-
?> This class is optional if you opt to use the [simplified syntax](simplified-syntax.md).
3+
These will be called at different step of a request lifecycle. Each method can return a promise which will be resolved or reject before continuing through the middleware stack.
44

5-
The base implementation to inherit when creating your custom implementation. **Any function is optional** and should be provided within a custom middleware only if needed.
6-
7-
## `constructor`
8-
9-
The constructor isn't used in the default middleware, leaving it totally available to the child classes. Very useful to pass any relevant services your middleware might need, like [the `i18n` service in our example](examples/locale-middleware.md).
5+
?> **Any function is optional** and should be provided within a custom middleware only if needed.
106

117
## `onRequest(config)`
128

@@ -36,7 +32,7 @@ onResponse(response) {
3632
}
3733
```
3834

39-
!> The original `response` object, _a new one_, or a promise should be returned.
35+
!> The original `response` object, or _a new/modified one_, or a promise should be returned.
4036

4137
## `onResponseError(error)`
4238

docs/examples/browser.md

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,18 @@ var app = app || {};
3939
/**
4040
* Custom Middleware class
4141
*/
42-
app.MyMiddleware = (function(HttpMiddleware){
42+
app.MyMiddleware = (function(){
4343
function MyMiddleware() {
44-
// call the parent constructor
45-
HttpMiddleware.apply(this, arguments);
4644
}
4745

48-
var proto = MyMiddleware.prototype = Object.create(HttpMiddleware.prototype);
46+
var proto = MyMiddleware.prototype = Object.create();
4947

5048
proto.constructor = MyMiddleware;
5149
proto.onRequest = function(config) {
5250
// handle the request
5351
};
5452
return MyMiddleware;
55-
})(AxiosMiddleware.HttpMiddleware);
53+
})();
5654
```
5755

5856
Then register these middlewares with a newly created `HttpMiddlewareService` instance.
@@ -64,15 +62,15 @@ var app = app || {};
6462
/**
6563
* Middleware Service
6664
*/
67-
app.MiddlewareService = (function(HttpMiddlewareService, MyMiddleware) {
65+
app.MiddlewareService = (function(MiddlewareService, MyMiddleware) {
6866
// Create a new service instance
69-
var service = new HttpMiddlewareService(axios);
67+
var service = new MiddlewareService(axios);
7068

7169
// Then register your middleware instances.
7270
service.register(new MyMiddleware());
7371

7472
return service;
75-
})(AxiosMiddleware.HttpMiddlewareService, app.MyMiddleware);
73+
})(AxiosMiddleware.Service, app.MyMiddleware);
7674
```
7775

7876
In this case, the order in which to import the JS files is important.

docs/examples/es5.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
# Usage with ES5 in Node
22

3-
ES5 doesn't have [classes](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes) so you can't use the easy extend syntax sugar. Creating a new middleware from the base `HttpMiddleware` class can still be done with a typical prototype based inheritance pattern.
3+
ES5 doesn't have [classes](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes) so you can't use the easy extend syntax sugar. Creating a new middleware from a base middleware class can still be done with a typical prototype based inheritance pattern.
44

55
Create a custom middleware to register.
66

77
```javascript
8-
var HttpMiddleware = require('axios-middleware').HttpMiddleware;
8+
var BaseMiddleware = require('./base-middleware');
99

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

1515
// Prototype wiring
16-
var proto = MyMiddleware.prototype = Object.create(HttpMiddleware.prototype);
16+
var proto = MyMiddleware.prototype = Object.create(BaseMiddleware.prototype);
1717
proto.constructor = MyMiddleware;
1818

1919
// Method overriding
@@ -28,11 +28,11 @@ Then export the service.
2828

2929
```javascript
3030
var axios = require('axios'),
31-
HttpMiddlewareService = require('axios-middleware').HttpMiddlewareService,
31+
Service = require('axios-middleware').Service,
3232
MyMiddleware = require('./MyMiddleware');
3333

3434
// Create a new service instance
35-
var service = new HttpMiddlewareService(axios);
35+
var service = new Service(axios);
3636

3737
// Then register your middleware instances.
3838
service.register(new MyMiddleware());

docs/examples/locale-middleware.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,8 @@
33
Here's a simple example of a locale middleware who sets a language header on each AJAX request.
44

55
```javascript
6-
import { HttpMiddleware } from 'axios-middleware';
7-
8-
export default class LocaleMiddleware extends HttpMiddleware {
6+
export default class LocaleMiddleware {
97
constructor(i18n) {
10-
super();
118
this.i18n = i18n;
129
}
1310

docs/examples/promise.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,8 @@
33
In a case where we'd like to retry a request if not authenticated, we could return a promise in the `onResponseError` method.
44

55
```javascript
6-
import { HttpMiddleware } from 'axios-middleware';
7-
8-
export default class AuthMiddleware extends HttpMiddleware {
6+
export default class AuthMiddleware {
97
constructor(auth, http) {
10-
super();
118
this.auth = auth;
129
this.http = http;
1310
}

docs/examples/service.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
```javascript
44
import axios from 'axios';
5-
import { HttpMiddlewareService } from 'axios-middleware';
5+
import { Service } from 'axios-middleware';
66
import i18n from './i18n';
77
import { LocaleMiddleware, OtherMiddleware } from './middlewares';
88

99
// Create a new service instance
10-
const service = new HttpMiddlewareService(axios);
10+
const service = new Service(axios);
1111

1212
// Then register your middleware instances.
1313
service.register([

docs/simplified-syntax.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Simplified syntax for middlewares
22

3-
Instead of creating a class from the [`HttpMiddleware`](api/HttpMiddleware.md) base class, you can use a simple object literal only implementing the functions you need.
3+
Instead of creating a class, you can use a simple object literal only implementing the [methods](api/methods.md) you need.
44

55
```javascript
66
service.register({

0 commit comments

Comments
 (0)