1
1
import { middlewarePipeline } from './helpers/middlewarePipeline'
2
2
import { retuenMiddlewareArray } from './helpers/returnMiddlewareArray'
3
+ import { BasePluginError } from './lib/Exceptions/BasePluginError'
3
4
import { OptionsMissingPluginError } from './lib/Exceptions/OptionsMissingPluginError'
4
5
import { Middleware } from './types/MiddlewareTypes'
5
6
import { Install , PluginOptions } from './types/PluginTypes'
@@ -18,15 +19,29 @@ export const install: Install<Router | PluginOptions> = (
18
19
) => {
19
20
let router : Router
20
21
let middlewares : Middleware [ ] = [ ]
22
+ let context : RouteContext = { }
21
23
22
24
if ( options && ( options as PluginOptions ) . router ) {
23
- const { router : _router , middleware } = options as PluginOptions
25
+ const {
26
+ router : _router ,
27
+ middleware,
28
+ context : _context
29
+ } = options as PluginOptions
24
30
router = _router
25
31
26
32
/* istanbul ignore if */
27
33
if ( middleware !== undefined ) {
28
34
middlewares = retuenMiddlewareArray ( middleware )
29
35
}
36
+
37
+ if ( _context !== undefined ) {
38
+ /* istanbul ignore if */
39
+ if ( typeof _context === 'object' ) {
40
+ context = { ..._context }
41
+ } else {
42
+ throw new BasePluginError ( 'invalid context provided in plugin options' )
43
+ }
44
+ }
30
45
} else {
31
46
router = options as Router
32
47
}
@@ -61,7 +76,7 @@ export const install: Install<Router | PluginOptions> = (
61
76
}
62
77
}
63
78
if ( middlewares . length ) {
64
- const context : RouteContext = { to, from, next }
79
+ context = { ... context , to, from, next }
65
80
const routeResolver = middlewarePipeline (
66
81
context ,
67
82
middlewares
0 commit comments