Skip to content

Commit 22691e0

Browse files
committed
feat(middleware-context): add plugin option context
user can add custom properties in context option which will be accessible to the middleware in context object
2 parents b5dae53 + 787877e commit 22691e0

File tree

7 files changed

+69
-15
lines changed

7 files changed

+69
-15
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ before_install:
1515
before_script:
1616
- npm prune
1717
script:
18+
- npm run test
1819
- npm run build
1920
after_success:
2021
- bash <(curl -s https://codecov.io/bash)

README.md

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,14 +156,42 @@ const routes = [
156156
}
157157
}
158158
]
159+
```
160+
161+
### New features
162+
163+
#### v1.2.0: Custom Middleware properties
164+
165+
You can now pass custom properties in plugin option `context` which will be accessible to the middleware.
159166

167+
```javascript
168+
import Vue from 'vue'
169+
import MiddlewarePlugin from 'vue-router-middleware-plugin'
170+
import router from '~/router'
171+
import store from '~/store'
172+
173+
Vue.use(MiddlewarePlugin, {
174+
router,
175+
// context must be an object
176+
context: { store }
177+
})
178+
```
179+
180+
In the middleware
181+
182+
```javascript
183+
export default ({ next, store }) => {
184+
store.commit('app/commit', true)
185+
next()
186+
}
160187
```
161188

162189
## Roadmap
163190

164-
- [x] **v1.0.0** - Route Middlewares
165-
- [x] **v1.1.0** - Global Middlewares
166-
- [ ] **v1.2.0** - Auto importing middlewares.
191+
- [x] **v1.0.0** - Route Middlewares.
192+
- [x] **v1.1.0** - Global Middlewares.
193+
- [x] **v1.2.0** - Middleware context - custom properties.
194+
- [ ] **TBD** - Auto importing middlewares.
167195

168196
## Contributing
169197

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vue-router-middleware-plugin",
3-
"version": "1.0.2",
3+
"version": "0.0.0-development",
44
"description": "A vue.js plugin to implement a middleware pipeline between your routes.",
55
"main": "build/index.js",
66
"types": "build/index.d.ts",
@@ -10,7 +10,7 @@
1010
"scripts": {
1111
"type-check": "tslint-config-prettier-check ./tslint.json",
1212
"type-check:watch": "tsc --noEmit --watch",
13-
"prebuild": "npm run precommit",
13+
"prebuild": "npm run lint",
1414
"build": "npm run build:types && npm run build:js",
1515
"build:types": "tsc --emitDeclarationOnly",
1616
"build:js": "babel src --out-dir build --extensions \".ts,.tsx\" --source-maps inline",

src/install.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { middlewarePipeline } from './helpers/middlewarePipeline'
22
import { retuenMiddlewareArray } from './helpers/returnMiddlewareArray'
3+
import { BasePluginError } from './lib/Exceptions/BasePluginError'
34
import { OptionsMissingPluginError } from './lib/Exceptions/OptionsMissingPluginError'
45
import { Middleware } from './types/MiddlewareTypes'
56
import { Install, PluginOptions } from './types/PluginTypes'
@@ -18,15 +19,29 @@ export const install: Install<Router | PluginOptions> = (
1819
) => {
1920
let router: Router
2021
let middlewares: Middleware[] = []
22+
let context: RouteContext = {}
2123

2224
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
2430
router = _router
2531

2632
/* istanbul ignore if */
2733
if (middleware !== undefined) {
2834
middlewares = retuenMiddlewareArray(middleware)
2935
}
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+
}
3045
} else {
3146
router = options as Router
3247
}
@@ -61,7 +76,7 @@ export const install: Install<Router | PluginOptions> = (
6176
}
6277
}
6378
if (middlewares.length) {
64-
const context: RouteContext = { to, from, next }
79+
context = { ...context, to, from, next }
6580
const routeResolver = middlewarePipeline(
6681
context,
6782
middlewares

src/types/PluginTypes.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ export type Install<T> = (vue: Vue, router?: T) => void
66
export interface PluginOptions {
77
router: Router
88
middleware?: Middleware[] | Middleware
9+
context?: { [key: string]: any }
910
}

src/types/VueTypes.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ export interface Router {
1818
// route types
1919
export type RouteResolver = (arg?: boolean | string | Route) => void
2020
export interface RouteContext {
21-
to: Route
22-
from: Route
23-
next: RouteResolver
21+
to?: Route
22+
from?: Route
23+
next?: RouteResolver
24+
[key: string]: any
2425
}
2526
export interface RouteMeta {
2627
middleware?: Middleware[] | Middleware | MiddlewarePipeline

tests/install.spec.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
//
22
import { install } from '../src/install'
3-
// exceptions
3+
import { BasePluginError } from '../src/lib/Exceptions/BasePluginError'
44
import { OptionsMissingPluginError } from '../src/lib/Exceptions/OptionsMissingPluginError'
55
import { PluginOptions } from '../src/types/PluginTypes'
6-
// types
76
import { Router, Vue } from '../src/types/VueTypes'
8-
// test utils
97
import { expectErrorClass, VueRouter } from '../src/utils/testUtils'
108

119
describe('installation checks', () => {
@@ -15,13 +13,23 @@ describe('installation checks', () => {
1513
expectErrorClass(() => install(dummyVue), OptionsMissingPluginError)
1614
})
1715

18-
const router: Router = new VueRouter()
1916
it('accepts router as plugin option', () => {
17+
const router: Router = new VueRouter()
2018
install(dummyVue, router)
2119
})
2220

23-
const pluginOptions: PluginOptions = { router }
2421
it('accepts options object as plugin option', () => {
22+
const router: Router = new VueRouter()
23+
const pluginOptions: PluginOptions = { router }
2524
install(dummyVue, pluginOptions)
2625
})
26+
27+
it('raises exception if invalid context provided in plugin options', () => {
28+
const router: Router = new VueRouter()
29+
const pluginOptions: PluginOptions = ({
30+
context: 1,
31+
router
32+
} as unknown) as PluginOptions
33+
expectErrorClass(() => install(dummyVue, pluginOptions), BasePluginError)
34+
})
2735
})

0 commit comments

Comments
 (0)