Skip to content

Commit b44eb57

Browse files
authored
setup docs (#20)
* improvement(opts-helpers): add function to add a property to middleware context * fix(opts-helpers): $addMiddlewareContext, override internal props only * improve(opts-helpers): remove $addMiddlewareContext * chore: setup docs * docs: add documentations * update docs * update docs * update docs * update docs * chore(tslint): change rules * ci: update build branch * refactor: install function * add docs
1 parent 52dc1b1 commit b44eb57

17 files changed

+11377
-5290
lines changed

.markdownlintrc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"MD036": false,
3+
"MD001": false
4+
}

.travis.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ cache:
66
branches:
77
only:
88
- master
9-
- develop
109
notifications:
1110
email: false
1211
node_js:

docs/.gitkeep

Whitespace-only changes.

docs/.vuepress/config.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
module.exports = {
2+
title: 'Vue Router Middleware Plugin',
3+
description: 'The vue router middleware pipeline you deserve',
4+
themeConfig: {
5+
nav: [
6+
{ text: 'Guide', link: '/guide/' },
7+
{ text: 'API', link: '/api/' },
8+
{ text: 'GitHub', link: 'https://github.com/dsfx3d/vue-router-middleware-plugin'}
9+
],
10+
sidebar: {
11+
'/guide/': [
12+
{
13+
title: 'Getting Started',
14+
collapsable: false,
15+
children: [
16+
'',
17+
'quickstart'
18+
]
19+
},
20+
{
21+
title: 'Advanced',
22+
collapsable: false,
23+
children: [
24+
'configurations',
25+
'context',
26+
'middlewares'
27+
]
28+
}
29+
],
30+
'/api/': [
31+
{
32+
title: 'API Reference',
33+
collapsable: false,
34+
children: [
35+
'',
36+
'context',
37+
'helpers'
38+
]
39+
}
40+
]
41+
}
42+
}
43+
}

docs/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
home: true
3+
heroText: Middleware Plugin
4+
tagline: The middleware pipeline you deserve
5+
actionText: Get Started →
6+
actionLink: /guide/
7+
features:
8+
- title: Performant
9+
details: Inspired from Nuxt Middlewares and built on top of vue router's Navigation Guards to offer the best of performance.
10+
- title: Light-Weight
11+
details: A light weight but powerful middleware pipeline to control logic between your routes.
12+
- title: Extensible
13+
details: The plugin API is inherently extensible to use within your applications or in integration with third-party plugins.
14+
footer: MIT Licensed | Copyright © 2020 @dsfx3d
15+
---

docs/api/README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Options
2+
3+
These plugin options can be used to configure the plugin on registration.
4+
5+
```javascript
6+
Vue.use(MiddlewarePlugin, {
7+
router,
8+
middleware,
9+
context
10+
})
11+
```
12+
13+
## Options Properties
14+
15+
### `router: VueRouter`
16+
17+
The vue router instance of your app.
18+
19+
### `middleware?: function | Array<function>`
20+
21+
The middleware property is used to register global middlewares, it can be set to either a single middleware function or an array of middleware functions. _(optional)_
22+
23+
### `context?: object`
24+
25+
This option is used to add custom properties to the default middleware context. The context object is merged with the middleware context on plugin registration. _(optional)_

docs/api/context.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Context
2+
3+
```javascript
4+
export default ({ app, to, from, redirect }) => {
5+
if (to.path === '/protected') {
6+
redirect(from)
7+
} else {
8+
const { visits } = app.$getMiddlewareContext()
9+
app.$updateMiddlewareContext('visits', (visits || 0) + 1)
10+
}
11+
}
12+
```
13+
14+
## Built-In Properties
15+
16+
The context has the following built-in properties:
17+
18+
### `app: VueConstructor`
19+
20+
A middleware app object with injected [helpers](helpers/).
21+
22+
### `to: Route`
23+
24+
The target [Route Object](https://router.vuejs.org/api/#the-route-object) being navigated to.
25+
26+
*An argument from [Navigation Guard](https://router.vuejs.org/guide/advanced/navigation-guards.html#global-before-guards)*
27+
28+
### `from: Route`
29+
30+
The target [Route Object](https://router.vuejs.org/api/#the-route-object) being navigated from.
31+
32+
*An argument from [Navigation Guard](https://router.vuejs.org/guide/advanced/navigation-guards.html#global-before-guards)*
33+
34+
### `redirect: function`
35+
36+
A function to redirect to any route. Accepts same arguments as the `next` function in a [Navigation Guard](https://router.vuejs.org/guide/advanced/navigation-guards.html#global-before-guards) but unlike a navigation guard, it's not required to be called in each middleware in order to resolve it.
37+
38+
## Adding custom properties
39+
40+
In the above example, we appearently add a property `visits` to the middleware context. Goto [Helper Functions](helpers.html#functions) for more.

docs/api/helpers.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Helpers
2+
3+
These are the helper functions of the `app` object in the context.
4+
5+
## Properties
6+
7+
### `$MiddlewarePlugin: boolean`
8+
9+
A flag for other plugins to identify `vue-router-middleware-plugin` is installed in the app. This will always be `true`
10+
11+
## Functions
12+
13+
> __Note:__ [Built-In middleware context properties](context.html#built-in-properties) cannot be mutated from theses helper functions. If a built-in property is passed in the arguments it will get overriden by the internal value.
14+
15+
### `$getMiddlewareContext: () => object`
16+
17+
returns the custom middleware context.
18+
19+
```javascript
20+
const context = app.$getMiddlewareContext()
21+
```
22+
23+
### `$setMiddlewareContext: (context: object) => object`
24+
25+
set custom middleware context. This will overide all existing custom properties.
26+
27+
```javascript
28+
const updatedContext = app.$setMiddlewareContext({ foo: 'baz' })
29+
```
30+
31+
### `$updateMiddlewareContext: (key: string, value: any) => void`
32+
33+
add or update a custom middleware context property.
34+
35+
```javascript
36+
app.$updateMiddlewareContext('foo', 'baz')
37+
```

docs/guide/README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Introduction
2+
3+
A middleware is a block of code that will run when navigating
4+
from one route to another.
5+
6+
The Vue Router offers [navigation guard API](https://router.vuejs.org/guide/advanced/navigation-guards.html)
7+
which can be used to add control logic between your routes but they can easily make
8+
your route objects messy and difficult to read and there's no elegant way of reusing
9+
same logic in multiple routes.
10+
11+
The plugin utilizes navigation guards to implement easy to use, readable and more
12+
organized middlewares for your routes.
13+
14+
### Installation
15+
16+
```bash
17+
npm i -S vue-router-middleware-plugin
18+
```
19+
20+
Get started with the quickstart guide.

docs/guide/configurations.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Configuration
2+
3+
The plugin can be configured in multiple ways with the help of plugin options.
4+
5+
#### 1. Simple Configuration
6+
7+
```javascript
8+
import Vue from 'vue'
9+
import MiddlewarePlugin from 'vue-router-middleware-plugin'
10+
import router from '@/path-to-router'
11+
12+
Vue.use(MiddlewarePlugin, router)
13+
```
14+
15+
#### 2. Custom [Middleware Context](context)
16+
17+
```javascript
18+
import Vue from 'vue'
19+
import MiddlewarePlugin from 'vue-router-middleware-plugin'
20+
import router from '@/path-to-router'
21+
import store from '@/path-to-store'
22+
23+
Vue.use(MiddlewarePlugin, {
24+
router,
25+
context: { store }
26+
})
27+
```
28+
29+
Now, `store` will be injected as a property in all middleware contexts.
30+
31+
```javascript
32+
export default async ({ store }) => {
33+
await store.dispatch('app/getData')
34+
}
35+
```
36+
37+
#### 3. Single [Global Middleware](middlewares.html#global-middleware)
38+
39+
```javascript
40+
import Vue from 'vue'
41+
import MiddlewarePlugin from 'vue-router-middleware-plugin'
42+
import router from '@/path-to-router'
43+
import AuthMiddleware from '@/path-to-auth-middleware'
44+
45+
Vue.use(MiddlewarePlugin, {
46+
router,
47+
middleware: AuthMiddleware
48+
})
49+
```
50+
51+
If you are wondering, it is possible to ignore a global middleware for individual
52+
routes.
53+
54+
#### 4. Multiple [Global Middlewares](middlewares.html#global-middleware)
55+
56+
```javascript
57+
import Vue from 'vue'
58+
import MiddlewarePlugin from 'vue-router-middleware-plugin'
59+
import router from '@/path-to-router'
60+
import AuthMiddleware from '@/path-to-auth-middleware'
61+
import LoggerMiddleware from '@/path-to-logger-middleware'
62+
63+
Vue.use(MiddlewarePlugin, {
64+
router,
65+
middleware: [AuthMiddleware, LoggerMiddleware]
66+
})
67+
```

docs/guide/context.md

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# Context
2+
3+
The `context` or middleware context is the only argument of a middleware function.
4+
5+
```javascript
6+
export default context => {
7+
const { app, to, from, redirect } = context
8+
9+
if (to.path === '/protected') {
10+
redirect(from)
11+
}
12+
}
13+
```
14+
15+
## Custom Context
16+
17+
The context can be extend by adding custom properties in plugin options.
18+
19+
```javascript
20+
import Vue from 'vue'
21+
import MiddlewarePlugin from 'vue-router-middleware-plugin'
22+
import router from '@/path-to-router'
23+
import store from '@/path-to-store'
24+
25+
Vue.use(MiddlewarePlugin, {
26+
router,
27+
context: { store }
28+
})
29+
```
30+
31+
Now, `store` will be injected as a property in all middleware contexts.
32+
33+
```javascript
34+
export default async ({ store }) => {
35+
await store.dispatch('app/getData')
36+
}
37+
```
38+
39+
In this case, you could've imported the store in the middleware module instead of
40+
adding it as a custom context property.
41+
42+
```javascript
43+
import store from '@/path-to-store'
44+
45+
export default async () => {
46+
await store.dispatch('app/getData')
47+
}
48+
```
49+
50+
If you are thinking what's the point of adding custom context. It will depend on
51+
the use case and personal preferences of a developer. One such case might be,
52+
if you are lazy and don't want to import the same module in multiple middlewares.
53+
54+
The idea behind adding custom context was so that other plugins can leverage it
55+
to inject functionalities which can used inside a middleware, but there's more than
56+
that. With the context, you can carry state in the middleware pipeline.
57+
58+
### Dynamic Custom Context
59+
60+
The custom context properties can be mutated with the helper functions exposed to
61+
the middleware via `app` context. The ability to mutate the custom context properties
62+
makes it possible to use these properties as middleware state.
63+
64+
Initialize the state in plugin options.
65+
66+
```javascript
67+
import Vue from 'vue'
68+
import MiddlewarePlugin from 'vue-router-middleware-plugin'
69+
import router from '@/path-to-router'
70+
71+
Vue.use(MiddlewarePlugin, {
72+
router,
73+
context: { dashboardVisits: 0 }
74+
})
75+
```
76+
77+
Mutate `dashboardVisits` in any middleware function.
78+
79+
```javascript
80+
export default ({ app, to, dashboardVisits }) => {
81+
if (to.path === '/dashboard') {
82+
app.$setMiddlewareContext({ dashboardVisits: dashboardVisits + 1 })
83+
}
84+
}
85+
```
86+
87+
> You cannot mutate built-in context properties. Like `redirect`, `from` etc.
88+
89+
Goto [API References](/api/context) to find out more about helper functions and
90+
`app` context.

0 commit comments

Comments
 (0)