ovr 1.1.0 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/router/index.d.ts +7 -0
- package/dist/router/index.js +13 -1
- package/package.json +1 -1
package/dist/router/index.d.ts
CHANGED
|
@@ -39,6 +39,13 @@ export declare class Router<State = null> {
|
|
|
39
39
|
*/
|
|
40
40
|
start?: Start<State>;
|
|
41
41
|
});
|
|
42
|
+
/**
|
|
43
|
+
* Add global middleware.
|
|
44
|
+
*
|
|
45
|
+
* @param middleware
|
|
46
|
+
* @returns the router instance
|
|
47
|
+
*/
|
|
48
|
+
use(...middleware: Middleware<State, Params>[]): this;
|
|
42
49
|
/**
|
|
43
50
|
* @param method HTTP method
|
|
44
51
|
* @param pattern route pattern
|
package/dist/router/index.js
CHANGED
|
@@ -5,6 +5,8 @@ export class Router {
|
|
|
5
5
|
#trieMap = new Map();
|
|
6
6
|
/** Added routes per HTTP method */
|
|
7
7
|
#routesMap = new Map();
|
|
8
|
+
/** Global middleware */
|
|
9
|
+
#use = [];
|
|
8
10
|
#start;
|
|
9
11
|
#trailingSlash;
|
|
10
12
|
constructor(config = {}) {
|
|
@@ -12,6 +14,16 @@ export class Router {
|
|
|
12
14
|
this.#start = config.start;
|
|
13
15
|
this.fetch = this.fetch.bind(this);
|
|
14
16
|
}
|
|
17
|
+
/**
|
|
18
|
+
* Add global middleware.
|
|
19
|
+
*
|
|
20
|
+
* @param middleware
|
|
21
|
+
* @returns the router instance
|
|
22
|
+
*/
|
|
23
|
+
use(...middleware) {
|
|
24
|
+
this.#use.push(...middleware);
|
|
25
|
+
return this;
|
|
26
|
+
}
|
|
15
27
|
on(method, pattern, ...middleware) {
|
|
16
28
|
let patterns;
|
|
17
29
|
if (!Array.isArray(pattern))
|
|
@@ -78,7 +90,7 @@ export class Router {
|
|
|
78
90
|
const match = trie.find(c.url.pathname);
|
|
79
91
|
if (match) {
|
|
80
92
|
Object.assign(c, match);
|
|
81
|
-
await this.#compose(match.route.store)(c, () => Promise.resolve());
|
|
93
|
+
await this.#compose([...this.#use, ...match.route.store])(c, () => Promise.resolve());
|
|
82
94
|
}
|
|
83
95
|
}
|
|
84
96
|
}
|