Skip to content

Commit 0b38093

Browse files
committed
0.1.3
1 parent a79f74e commit 0b38093

File tree

8 files changed

+185
-474
lines changed

8 files changed

+185
-474
lines changed

dist/middleware.js

Lines changed: 5 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/middleware.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/API.md

Lines changed: 86 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ the target function will be left hanging.</p>
3535
}
3636
</code></pre><p>Function&#39;s name start or end with &quot;_&quot; will not be able to apply middleware.</p>
3737
</dd>
38-
<dt><a href="#MiddlewareManager">MiddlewareManager</a></dt>
39-
<dd></dd>
4038
</dl>
4139

4240
## Functions
@@ -102,46 +100,107 @@ Function's name start or end with "_" will not be able to apply middleware.
102100
<a name="new_MiddlewareManager_new"></a>
103101

104102
### new MiddlewareManager(target, ...middlewareObjects)
105-
**Returns**: <code>object</code> - this
106103

107104
| Param | Type | Description |
108105
| --- | --- | --- |
109106
| target | <code>object</code> | The target object. |
110107
| ...middlewareObjects | <code>object</code> | Middleware objects. |
111108

112-
<a name="MiddlewareManager+use"></a>
109+
**Example**
110+
```js
111+
## Basic
113112

114-
### middlewareManager.use(methodName, ...middlewares) ⇒ <code>object</code>
115-
Apply (register) middleware functions to the target function or apply (register) middleware objects.
116-
If the first argument is a middleware object, the rest arguments must be middleware objects.
113+
We define a Person class.
114+
// the target object
115+
class Person {
116+
// the target function
117+
walk(step) {
118+
this.step = step;
119+
}
117120

118-
**Kind**: instance method of <code>[MiddlewareManager](#MiddlewareManager)</code>
119-
**Returns**: <code>object</code> - this
121+
speak(word) {
122+
this.word = word;
123+
}
124+
}
120125

121-
| Param | Type | Description |
122-
| --- | --- | --- |
123-
| methodName | <code>string</code> &#124; <code>object</code> | String for target function name, object for a middleware object. |
124-
| ...middlewares | <code>function</code> &#124; <code>object</code> | The middleware chain to be applied. |
126+
Then we define a middleware function to print log.
125127

126-
<a name="MiddlewareManager"></a>
128+
// middleware for walk function
129+
const logger = target => next => (...args) => {
130+
console.log(`walk start, steps: ${args[0]}.`);
131+
const result = next(...args);
132+
console.log(`walk end.`);
133+
return result;
134+
}
127135

128-
## MiddlewareManager
129-
**Kind**: global class
136+
Now we apply the log function as a middleware to a Person instance.
130137

131-
* [MiddlewareManager](#MiddlewareManager)
132-
* [new MiddlewareManager(target, ...middlewareObjects)](#new_MiddlewareManager_new)
133-
* [.use(methodName, ...middlewares)](#MiddlewareManager+use) ⇒ <code>object</code>
138+
// apply middleware to target object
139+
const p = new Person();
140+
const middlewareManager = new MiddlewareManager(p);
141+
middlewareManager.use('walk', walk);
142+
p.walk(3);
134143

135-
<a name="new_MiddlewareManager_new"></a>
144+
Whenever a Person instance call it's walk method, we'll see logs from the looger middleware.
136145

137-
### new MiddlewareManager(target, ...middlewareObjects)
138-
**Returns**: <code>object</code> - this
139-
140-
| Param | Type | Description |
141-
| --- | --- | --- |
142-
| target | <code>object</code> | The target object. |
143-
| ...middlewareObjects | <code>object</code> | Middleware objects. |
146+
## Middleware object
147+
We can also apply a middleware object to a target object.
148+
Middleware object is an object that contains function's name as same as the target object's function name.
144149

150+
const PersonMiddleware {
151+
walk: target => next => step => {
152+
console.log(`walk start, steps: step.`);
153+
const result = next(step);
154+
console.log(`walk end.`);
155+
return result;
156+
},
157+
speak: target => next => word => {
158+
word = 'this is a middleware trying to say: ' + word;
159+
return next(word);
160+
}
161+
}
162+
163+
// apply middleware to target object
164+
const p = new Person();
165+
const middlewareManager = new MiddlewareManager(p);
166+
middlewareManager.use(PersonMiddleware);
167+
p.walk(3);
168+
p.speak('hi');
169+
170+
## middlewareMethods
171+
Or we can use `middlewareMethods` to define function names for middleware target within a class.
172+
173+
class PersonMiddleware {
174+
constructor() {
175+
//Define function names for middleware target.
176+
this.middlewareMethods = ['walk', 'speak'];
177+
}
178+
log(text) {
179+
console.log('Middleware log: ' + text);
180+
}
181+
walk(target) {
182+
return next => step => {
183+
this.log(`walk start, steps: step.`);
184+
const result = next(step);
185+
this.log(`walk end.`);
186+
return result;
187+
}
188+
}
189+
speak(target) {
190+
return next => word => {
191+
this.log('this is a middleware trying to say: ' + word);
192+
return next(word);
193+
}
194+
}
195+
}
196+
197+
// apply middleware to target object
198+
const p = new Person();
199+
const middlewareManager = new MiddlewareManager(p);
200+
middlewareManager.use(new PersonMiddleware())
201+
p.walk(3);
202+
p.speak('hi');
203+
```
145204
<a name="MiddlewareManager+use"></a>
146205

147206
### middlewareManager.use(methodName, ...middlewares) ⇒ <code>object</code>

0 commit comments

Comments
 (0)