Skip to content

Commit 0ee5fcf

Browse files
committed
docs: add documentations
1 parent 3a1448a commit 0ee5fcf

File tree

10 files changed

+174
-13
lines changed

10 files changed

+174
-13
lines changed

docs/.vuepress/config.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ module.exports = {
1313
title: 'Guide',
1414
collapsable: false,
1515
children: [
16-
''
16+
'',
17+
'installation',
18+
'usage',
19+
'context'
1720
]
1821
}
1922
],
@@ -24,7 +27,7 @@ module.exports = {
2427
children: [
2528
'',
2629
'context',
27-
'options'
30+
'helpers'
2831
]
2932
}
3033
]

docs/api/README.md

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
1-
# Configuration
1+
# Options
22

3-
Pass the configuration object
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: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# Context
22

3-
The `context` or middleware context is the only argument in all middleware functions. It's an object which provide properties which can be used to control the behaviour of a middleware
4-
53
```javascript
64
export default ({ app, to, from, redirect }) => {
75
if (to.path === '/protected') {
@@ -19,7 +17,7 @@ The context has the following built-in properties:
1917

2018
### `app: VueConstructor`
2119

22-
A regular vue constructor with injected plugin [options](options/).
20+
A middleware app object with injected [helpers](helpers/).
2321

2422
### `to: Route`
2523

@@ -39,4 +37,4 @@ A function to redirect to any route. Accepts same arguments as the `next` functi
3937

4038
## Adding custom properties
4139

42-
From the example above, we are appearently adding a property `visits` to the middleware context. Goto [options](options/) for more.
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/api/options.md

Lines changed: 0 additions & 1 deletion
This file was deleted.

docs/guide/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
# Introduction
1+
# Getting Started

docs/guide/context.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
It always contain a few built-in properties but can also be extended to include [custom properties](/api/context.html#adding-custom-properties) as per use case.

docs/guide/installation.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Installation
2+
3+
```bash
4+
npm install --save vue-router-middleware-plugin
5+
```

docs/guide/usage.md

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# Usage
2+
3+
## Concept
4+
5+
## Types of middlewares
6+
7+
### Global Middlewares
8+
9+
```javascript
10+
Vue.use(MiddlewarePlugin, {
11+
router,
12+
middleware: AuthMiddleware
13+
})
14+
15+
// or
16+
17+
Vue.use(MiddlewarePlugin, {
18+
router,
19+
middleware: [AuthMiddleware, AnalyticsMiddleware]
20+
})
21+
```
22+
23+
### Route Middlewares
24+
25+
```javascript
26+
const routes = [
27+
{
28+
path: '/',
29+
name: 'home',
30+
meta: {
31+
middleware: ({ redirect }) => {
32+
redirect('/dashboard')
33+
}
34+
}
35+
}
36+
```
37+
38+
```javascript
39+
{
40+
path: '/dashboard',
41+
name: 'dashboard',
42+
meta: {
43+
middleware: DashboardMiddleware
44+
}
45+
},
46+
{
47+
path: '/profile',
48+
name: 'profile',
49+
meta: {
50+
middleware: [ProfileMiddleware, AnalyticsMiddleware]
51+
}
52+
},
53+
```
54+
55+
```javascript
56+
{
57+
path: '/about',
58+
name: 'about',
59+
meta: {
60+
middleware: {
61+
ignore: [AuthMiddleware],
62+
attach: [AnalyticsMiddleware]
63+
}
64+
}
65+
},
66+
]
67+
```
68+
69+
## Use Cases
70+
71+
### Route Protection
72+
73+
```javascript
74+
```
75+
76+
### Long Running Tasks
77+
78+
```javascript
79+
```

src/install.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,18 @@ export const install: Install<Router | PluginOptions> = (
6060

6161
// ==== helpers ============
6262
vue.$MiddlewarePlugin = true
63-
vue.$getMiddlewareContext = () => context
63+
vue.$getMiddlewareContext = () => {
64+
const { app, ..._context } = context
65+
return _context
66+
}
6467
vue.$setMiddlewareContext = (_context: any): any => {
6568
const { app } = context
6669
context = { ..._context, app }
6770
return context
6871
}
6972
vue.$updateMiddlewareContext = (key: string, value: any) => {
70-
const { to, from, redirect, app } = context
71-
context = { [key]: value, to, from, redirect, app }
73+
const { app } = context
74+
context = { [key]: value, app }
7275
}
7376

7477
/* istanbul ignore next */

0 commit comments

Comments
 (0)