Skip to content

Commit eea9a65

Browse files
committed
0.1.4
1 parent 0b38093 commit eea9a65

File tree

9 files changed

+476
-186
lines changed

9 files changed

+476
-186
lines changed

.babelrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"presets": [
3-
"latest"
3+
"env"
44
],
55
"plugins": [
66
"transform-class-properties"

dist/middleware.js

Lines changed: 4 additions & 5 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: 27 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ 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>
3840
</dl>
3941

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

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

104107
| Param | Type | Description |
105108
| --- | --- | --- |
106109
| target | <code>object</code> | The target object. |
107110
| ...middlewareObjects | <code>object</code> | Middleware objects. |
108111

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

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-
}
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.
120117

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

126-
Then we define a middleware function to print log.
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. |
127125

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-
}
126+
<a name="MiddlewareManager"></a>
135127

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

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);
131+
* [MiddlewareManager](#MiddlewareManager)
132+
* [new MiddlewareManager(target, ...middlewareObjects)](#new_MiddlewareManager_new)
133+
* [.use(methodName, ...middlewares)](#MiddlewareManager+use) ⇒ <code>object</code>
143134

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

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.
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. |
149144

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-
```
204145
<a name="MiddlewareManager+use"></a>
205146

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

0 commit comments

Comments
 (0)