druxt-router 0.20.0 → 0.24.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/CHANGELOG.md +69 -0
- package/README.md +2 -8
- package/dist/components/DruxtRouter.stories.mjs +16 -0
- package/dist/components/DruxtRouter.vue +204 -29
- package/dist/druxt-router.esm.js +13 -55
- package/dist/druxt-router.ssr.js +13 -55
- package/nuxt/index.d.ts +30 -0
- package/nuxt/index.js +57 -0
- package/package.json +24 -13
- package/{nuxt → templates}/component.js +0 -0
- package/{nuxt → templates}/plugin.js +8 -0
- package/{nuxt → templates}/store.js +0 -0
- package/dist/components/DruxtRoute.vue +0 -73
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,74 @@
|
|
|
1
1
|
# druxt-router
|
|
2
2
|
|
|
3
|
+
## 0.24.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 9161b38: Added Storybook integration for the DruxtRouter component.
|
|
8
|
+
- 9161b38: Added ability to render a specific route using the Path property.
|
|
9
|
+
|
|
10
|
+
```jsx
|
|
11
|
+
<DruxtRouter path="/node/1" />
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
- 9161b38: Added fetch hook to DruxtRouter component.
|
|
15
|
+
- 9161b38: Added option to disable the DruxtRouter page middleware.
|
|
16
|
+
|
|
17
|
+
```js
|
|
18
|
+
export default {
|
|
19
|
+
druxt: {
|
|
20
|
+
router: {
|
|
21
|
+
middleware: false
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
Note: This is experimental and your results may vary.
|
|
28
|
+
|
|
29
|
+
## 0.23.0
|
|
30
|
+
|
|
31
|
+
### Minor Changes
|
|
32
|
+
|
|
33
|
+
- 77ab204: Added support for API Proxy
|
|
34
|
+
|
|
35
|
+
### Patch Changes
|
|
36
|
+
|
|
37
|
+
- Updated dependencies [77ab204]
|
|
38
|
+
- druxt@0.13.0
|
|
39
|
+
|
|
40
|
+
## 0.22.0
|
|
41
|
+
|
|
42
|
+
### Minor Changes
|
|
43
|
+
|
|
44
|
+
- c7b267a: Throw Error on router errors
|
|
45
|
+
|
|
46
|
+
### Patch Changes
|
|
47
|
+
|
|
48
|
+
- Updated dependencies [4504a2f]
|
|
49
|
+
- druxt@0.12.0
|
|
50
|
+
|
|
51
|
+
## 0.21.0
|
|
52
|
+
|
|
53
|
+
### Minor Changes
|
|
54
|
+
|
|
55
|
+
- 9d905e8: Set router `pages` option default based on presence of `pages/` directory.
|
|
56
|
+
- 9d905e8: Moved Nuxt module to `druxt-router/nuxt`
|
|
57
|
+
|
|
58
|
+
⚠ Potential breaking change
|
|
59
|
+
|
|
60
|
+
```diff
|
|
61
|
+
-modules: ['druxt-router']
|
|
62
|
+
+modules: ['druxt-router/nuxt']
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
- dae345e: Added module level options
|
|
66
|
+
|
|
67
|
+
### Patch Changes
|
|
68
|
+
|
|
69
|
+
- Updated dependencies [dae345e]
|
|
70
|
+
- druxt@0.11.0
|
|
71
|
+
|
|
3
72
|
## 0.20.0
|
|
4
73
|
|
|
5
74
|
### Minor Changes
|
package/README.md
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
## Links
|
|
10
10
|
|
|
11
11
|
- DruxtJS: https://druxtjs.org
|
|
12
|
-
- Documentation: https://
|
|
12
|
+
- Documentation: https://druxtjs.org/modules/router
|
|
13
13
|
- Community Discord server: https://discord.druxtjs.org
|
|
14
14
|
|
|
15
15
|
## Features
|
|
@@ -29,19 +29,13 @@ Add module to `nuxt.config.js`
|
|
|
29
29
|
|
|
30
30
|
```js
|
|
31
31
|
module.exports = {
|
|
32
|
-
modules: [
|
|
33
|
-
...
|
|
34
|
-
'druxt-router'
|
|
35
|
-
],
|
|
36
|
-
|
|
32
|
+
modules: ['druxt-router/nuxt'],
|
|
37
33
|
druxt: {
|
|
38
34
|
baseUrl: 'https://demo-api.druxtjs.org'
|
|
39
35
|
}
|
|
40
36
|
}
|
|
41
37
|
```
|
|
42
38
|
|
|
43
|
-
Ensure you have activated the [Nuxt Vuex store](https://nuxtjs.org/guide/vuex-store/).
|
|
44
|
-
|
|
45
39
|
## Options
|
|
46
40
|
|
|
47
41
|
### Druxt Router options
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import DruxtRouter from './DruxtRouter.vue'
|
|
2
|
+
|
|
3
|
+
export default {
|
|
4
|
+
title: 'Druxt/Router/DruxtRouter',
|
|
5
|
+
component: DruxtRouter
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
const Template = (args, { argTypes }) => ({
|
|
9
|
+
components: { DruxtRouter },
|
|
10
|
+
props: Object.keys(argTypes),
|
|
11
|
+
template: `<DruxtRouter v-bind="$props" />`
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
export const Default = Template.bind({})
|
|
15
|
+
Default.storyName = 'DruxtRouter'
|
|
16
|
+
Default.args = { path: '/' }
|
|
@@ -1,21 +1,61 @@
|
|
|
1
1
|
<script>
|
|
2
2
|
|
|
3
3
|
import DruxtModule from 'druxt/dist/components/DruxtModule.vue'
|
|
4
|
-
import { mapState } from 'vuex'
|
|
5
4
|
|
|
6
5
|
/**
|
|
7
|
-
*
|
|
8
|
-
*
|
|
6
|
+
* The DruxtRouter component renders a Drupal decoupled route, or path, using
|
|
7
|
+
* the appropriate Druxt component.
|
|
9
8
|
*
|
|
10
|
-
*
|
|
9
|
+
* `<DruxtRouter path="/" />`
|
|
10
|
+
*
|
|
11
|
+
* If no Path is specified, the Vue router path will be used by default.
|
|
12
|
+
*
|
|
13
|
+
* For instance, using the `/node/1` route would result in a DruxtRouter
|
|
14
|
+
* component for the content in Drupal with the Node ID of 1.
|
|
15
|
+
*
|
|
16
|
+
* @example <caption>Render using the Vue router path</caption> @lang vue
|
|
11
17
|
* <DruxtRouter />
|
|
18
|
+
*
|
|
19
|
+
* @example <caption>Render a specified path</caption> @lang vue
|
|
20
|
+
* <DruxtRouter path="/node/1" />
|
|
21
|
+
*
|
|
22
|
+
* @example <caption>Render the result of the model, bypasses Drupal backend</caption> @lang vue
|
|
23
|
+
* <DruxtRouter v-model="route" />
|
|
24
|
+
*
|
|
25
|
+
* @example <caption>Render via a template</caption> @lang vue
|
|
26
|
+
* <DruxtRouter>
|
|
27
|
+
* <template #default="{ route }">
|
|
28
|
+
* <DruxtRouter v-bind="route.props" />
|
|
29
|
+
* </template>
|
|
30
|
+
* </DruxtRouter>
|
|
12
31
|
*/
|
|
13
32
|
export default {
|
|
14
33
|
name: 'DruxtRouter',
|
|
15
34
|
|
|
16
35
|
extends: DruxtModule,
|
|
17
36
|
|
|
18
|
-
|
|
37
|
+
/**
|
|
38
|
+
* Nuxt middleware; gets and sets the current route, and processes redirects.
|
|
39
|
+
*
|
|
40
|
+
* This can be disabled by setting the `druxt.router.middleware` option to
|
|
41
|
+
* `false` in `nuxt.config.js`
|
|
42
|
+
*
|
|
43
|
+
* @example @lang js
|
|
44
|
+
* export default {
|
|
45
|
+
* druxt: {
|
|
46
|
+
* router: {
|
|
47
|
+
* middleware: false
|
|
48
|
+
* }
|
|
49
|
+
* }
|
|
50
|
+
* }
|
|
51
|
+
*/
|
|
52
|
+
async middleware ({ $druxt, redirect, route, store }) {
|
|
53
|
+
// Ensure Router middleware is enabled.
|
|
54
|
+
if (typeof ($druxt.settings.router || {}).middleware !== 'undefined' && !$druxt.settings.router.middleware) {
|
|
55
|
+
return
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// Get and set the current route and redirect information.
|
|
19
59
|
const result = await store.dispatch('druxtRouter/get', route.fullPath)
|
|
20
60
|
|
|
21
61
|
// Process redirect.
|
|
@@ -24,6 +64,44 @@ export default {
|
|
|
24
64
|
}
|
|
25
65
|
},
|
|
26
66
|
|
|
67
|
+
/** */
|
|
68
|
+
props: {
|
|
69
|
+
/**
|
|
70
|
+
* The Decoupled router path.
|
|
71
|
+
*
|
|
72
|
+
* If not set, the Vue router value will be used instead.
|
|
73
|
+
*
|
|
74
|
+
* @type {string}
|
|
75
|
+
*
|
|
76
|
+
* @example @lang vue
|
|
77
|
+
* <DruxtRouter path="/node/1" />
|
|
78
|
+
*/
|
|
79
|
+
path: {
|
|
80
|
+
type: String,
|
|
81
|
+
default: undefined,
|
|
82
|
+
},
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* The Router object, used to determine the resolved route component.
|
|
86
|
+
*
|
|
87
|
+
* Setting this value will bypass the JSON:API.
|
|
88
|
+
*
|
|
89
|
+
* @type {object}
|
|
90
|
+
* @model
|
|
91
|
+
*/
|
|
92
|
+
value: {
|
|
93
|
+
type: Object,
|
|
94
|
+
default: () => undefined
|
|
95
|
+
}
|
|
96
|
+
},
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* @property {object} model - The model object.
|
|
100
|
+
*/
|
|
101
|
+
data: ({ value }) => ({
|
|
102
|
+
model: value,
|
|
103
|
+
}),
|
|
104
|
+
|
|
27
105
|
/**
|
|
28
106
|
* Nuxt head method.
|
|
29
107
|
*
|
|
@@ -39,7 +117,7 @@ export default {
|
|
|
39
117
|
{
|
|
40
118
|
hid: 'canonical',
|
|
41
119
|
rel: 'canonical',
|
|
42
|
-
href: this.route.canonical
|
|
120
|
+
href: this.canonical || (this.route || {}).canonical
|
|
43
121
|
}
|
|
44
122
|
]
|
|
45
123
|
}
|
|
@@ -51,45 +129,142 @@ export default {
|
|
|
51
129
|
return head
|
|
52
130
|
},
|
|
53
131
|
|
|
54
|
-
/**
|
|
55
|
-
* Vue.js Computed properties.
|
|
56
|
-
*
|
|
57
|
-
* @vue-computed {object} redirect The current Redirect, if applicable.
|
|
58
|
-
* @vue-computed {object} route The current Route.
|
|
59
|
-
*/
|
|
132
|
+
/** */
|
|
60
133
|
computed: {
|
|
134
|
+
/**
|
|
135
|
+
* The route module.
|
|
136
|
+
*
|
|
137
|
+
* @type {string}
|
|
138
|
+
*/
|
|
139
|
+
module: ({ route }) =>
|
|
140
|
+
(route || {}).component && route.component.startsWith('druxt-') ? route.component.substring(6) : false,
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* The route object.
|
|
144
|
+
*
|
|
145
|
+
* @type {object}
|
|
146
|
+
*/
|
|
147
|
+
route: ({ model }) => model,
|
|
148
|
+
|
|
61
149
|
/**
|
|
62
150
|
* Route title.
|
|
63
151
|
* @type {boolean|string}
|
|
64
152
|
* @default false
|
|
65
153
|
*/
|
|
66
|
-
title: ({ route }) => route.label || false,
|
|
154
|
+
title: ({ route }) => (route || {}).label || false,
|
|
67
155
|
|
|
68
156
|
/**
|
|
69
157
|
* Route component property data.
|
|
70
158
|
* @type {object|string}
|
|
71
159
|
* @default false
|
|
72
160
|
*/
|
|
73
|
-
props: ({ route }) => route.props || false,
|
|
161
|
+
props: ({ route }) => (route || {}).props || false,
|
|
162
|
+
},
|
|
74
163
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
164
|
+
/** */
|
|
165
|
+
watch: {
|
|
166
|
+
/**
|
|
167
|
+
* Re-fetch on update to Path prop.
|
|
168
|
+
*/
|
|
169
|
+
async path() {
|
|
170
|
+
await this.$fetch()
|
|
171
|
+
}
|
|
79
172
|
},
|
|
80
173
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
174
|
+
/** DruxtModule settings */
|
|
175
|
+
druxt: {
|
|
176
|
+
/**
|
|
177
|
+
* Provides the available component naming options for the Druxt Wrapper.
|
|
178
|
+
*
|
|
179
|
+
* @param {object} context - The module component ViewModel.
|
|
180
|
+
* @returns {ComponentOptions}
|
|
181
|
+
*/
|
|
182
|
+
componentOptions: ({ module, route }) => [
|
|
183
|
+
// @TODO - Add Path options.
|
|
184
|
+
[module || 'error', route.isHomePath ? 'front' : 'not-front'],
|
|
185
|
+
['default']
|
|
186
|
+
],
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Fetch the decoupled route.
|
|
190
|
+
*/
|
|
191
|
+
async fetchConfig() {
|
|
192
|
+
// Use the v-model value if provided.
|
|
193
|
+
if (this.value) {
|
|
194
|
+
return
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
// Get the route from the Drupal decoupled router module via the
|
|
198
|
+
// druxtRouter store.
|
|
199
|
+
// Use the Path prop or the Vue Router as the route to lookup.
|
|
200
|
+
const path = this.path || this.$route.fullPath
|
|
201
|
+
const route = await this.$store.dispatch('druxtRouter/getRoute', path)
|
|
202
|
+
this.model = route
|
|
203
|
+
|
|
204
|
+
// If this the path is the active Vue route, set the active route in the
|
|
205
|
+
// druxtRouter store for other modules to use.
|
|
206
|
+
// This is also done when via the middleware if in use.
|
|
207
|
+
const setActiveRoute = path === this.$route.fullPath
|
|
208
|
+
if (setActiveRoute) {
|
|
209
|
+
this.$store.commit('druxtRouter/setRoute', path)
|
|
91
210
|
}
|
|
92
|
-
|
|
211
|
+
},
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* Provides propsData for the DruxtWrapper.
|
|
215
|
+
*
|
|
216
|
+
* @param {object} context - The module component ViewModel.
|
|
217
|
+
* @returns {PropsData}
|
|
218
|
+
*/
|
|
219
|
+
propsData: ({ $route, path, model }) => ({
|
|
220
|
+
path: path || $route.fullPath,
|
|
221
|
+
route: model
|
|
222
|
+
})
|
|
93
223
|
}
|
|
94
224
|
}
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* Provides the available naming options for the Wrapper component.
|
|
228
|
+
*
|
|
229
|
+
* @typedef {array[]} ComponentOptions
|
|
230
|
+
*
|
|
231
|
+
* @example @lang js
|
|
232
|
+
* [
|
|
233
|
+
* 'DruxtRouter[Module][IsFront?]',
|
|
234
|
+
* 'DruxtRouterDefault',
|
|
235
|
+
* ]
|
|
236
|
+
*
|
|
237
|
+
* @example <caption>Entity route</caption> @lang js
|
|
238
|
+
* [
|
|
239
|
+
* 'DruxtRouterEntityFront',
|
|
240
|
+
* 'DruxtRouterEntity',
|
|
241
|
+
* 'DruxtRouterDefault',
|
|
242
|
+
* '',
|
|
243
|
+
* ]
|
|
244
|
+
*/
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* Provides property data for use in the Wrapper component.
|
|
248
|
+
*
|
|
249
|
+
* @typedef {object} PropsData
|
|
250
|
+
* @param {string} path - The route path.
|
|
251
|
+
* @param {object} route - The Decoupled Router object.
|
|
252
|
+
*
|
|
253
|
+
* @example @lang js
|
|
254
|
+
* {
|
|
255
|
+
* path: '/',
|
|
256
|
+
* route: {
|
|
257
|
+
* canonical: 'https://demo-api.druxtjs.org/en/node',
|
|
258
|
+
* component: 'druxt-view',
|
|
259
|
+
* error: false,
|
|
260
|
+
* isHomePath: true,
|
|
261
|
+
* jsonapi: {},
|
|
262
|
+
* label: 'Home',
|
|
263
|
+
* props: {},
|
|
264
|
+
* redirect: false,
|
|
265
|
+
* resolvedPath: '/en/node',
|
|
266
|
+
* type: 'views',
|
|
267
|
+
* }
|
|
268
|
+
* }
|
|
269
|
+
*/
|
|
95
270
|
</script>
|
package/dist/druxt-router.esm.js
CHANGED
|
@@ -1,52 +1,7 @@
|
|
|
1
|
-
import { join, resolve } from 'path';
|
|
2
1
|
import { DruxtClient } from 'druxt';
|
|
3
2
|
import Url from 'url-parse';
|
|
4
3
|
import { mapState, mapActions } from 'vuex';
|
|
5
4
|
|
|
6
|
-
const DruxtRouterNuxtModule = function() {
|
|
7
|
-
if (typeof this.options === "undefined" || !this.options.druxt) {
|
|
8
|
-
throw new TypeError("Druxt settings missing.");
|
|
9
|
-
}
|
|
10
|
-
const options = this.options.druxt;
|
|
11
|
-
options.router = {
|
|
12
|
-
pages: true,
|
|
13
|
-
wildcard: true,
|
|
14
|
-
...options.router
|
|
15
|
-
};
|
|
16
|
-
this.nuxt.hook("components:dirs", (dirs) => {
|
|
17
|
-
dirs.push({ path: join(__dirname, "components") });
|
|
18
|
-
});
|
|
19
|
-
if (options.router.wildcard) {
|
|
20
|
-
this.addTemplate({
|
|
21
|
-
src: resolve(__dirname, "../nuxt/component.js"),
|
|
22
|
-
fileName: "components/druxt-router.js",
|
|
23
|
-
options
|
|
24
|
-
});
|
|
25
|
-
this.extendRoutes((routes, resolve2) => {
|
|
26
|
-
routes.push({
|
|
27
|
-
name: "druxt-router",
|
|
28
|
-
path: "*",
|
|
29
|
-
component: resolve2(this.options.buildDir, "components/druxt-router.js"),
|
|
30
|
-
chunkName: "druxt-router"
|
|
31
|
-
});
|
|
32
|
-
});
|
|
33
|
-
}
|
|
34
|
-
if (options.router.wildcard && !options.router.pages) {
|
|
35
|
-
this.nuxt.hook("build:before", () => this.nuxt.options.build.createRoutes = () => []);
|
|
36
|
-
}
|
|
37
|
-
this.addPlugin({
|
|
38
|
-
src: resolve(__dirname, "../nuxt/plugin.js"),
|
|
39
|
-
fileName: "druxt-router.js",
|
|
40
|
-
options
|
|
41
|
-
});
|
|
42
|
-
this.options.store = true;
|
|
43
|
-
this.addPlugin({
|
|
44
|
-
src: resolve(__dirname, "../nuxt/store.js"),
|
|
45
|
-
fileName: "store/druxt-router.js",
|
|
46
|
-
options
|
|
47
|
-
});
|
|
48
|
-
};
|
|
49
|
-
|
|
50
5
|
class DruxtRouter {
|
|
51
6
|
constructor(baseUrl, options = {}) {
|
|
52
7
|
this.options = {
|
|
@@ -96,9 +51,6 @@ class DruxtRouter {
|
|
|
96
51
|
}
|
|
97
52
|
async get(path) {
|
|
98
53
|
const route = await this.getRoute(path);
|
|
99
|
-
if (route.error) {
|
|
100
|
-
return { route };
|
|
101
|
-
}
|
|
102
54
|
const redirect = this.getRedirect(path, route);
|
|
103
55
|
return { redirect, route };
|
|
104
56
|
}
|
|
@@ -195,11 +147,9 @@ class DruxtRouter {
|
|
|
195
147
|
break;
|
|
196
148
|
}
|
|
197
149
|
if (!(response.status >= 200 && response.status < 300)) {
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
};
|
|
202
|
-
route.component = "error";
|
|
150
|
+
const error = new Error();
|
|
151
|
+
error.response = response;
|
|
152
|
+
throw error;
|
|
203
153
|
}
|
|
204
154
|
return route;
|
|
205
155
|
}
|
|
@@ -271,7 +221,12 @@ const DruxtRouterStore = ({ store }) => {
|
|
|
271
221
|
if (typeof state.routes[path] !== "undefined") {
|
|
272
222
|
return state.routes[path];
|
|
273
223
|
}
|
|
274
|
-
|
|
224
|
+
let route;
|
|
225
|
+
try {
|
|
226
|
+
route = await this.$druxtRouter().getRoute(path);
|
|
227
|
+
} catch (err) {
|
|
228
|
+
route = { error: { statusCode: err.response.status, message: err.response.data.message } };
|
|
229
|
+
}
|
|
275
230
|
commit("addRoute", { path, route });
|
|
276
231
|
return route;
|
|
277
232
|
}
|
|
@@ -327,6 +282,10 @@ const DruxtRouterEntityMixin = {
|
|
|
327
282
|
|
|
328
283
|
const DruxtRouterMixin = {
|
|
329
284
|
props: {
|
|
285
|
+
path: {
|
|
286
|
+
type: String,
|
|
287
|
+
default: void 0
|
|
288
|
+
},
|
|
330
289
|
route: {
|
|
331
290
|
type: Object,
|
|
332
291
|
required: true
|
|
@@ -334,5 +293,4 @@ const DruxtRouterMixin = {
|
|
|
334
293
|
}
|
|
335
294
|
};
|
|
336
295
|
|
|
337
|
-
export default DruxtRouterNuxtModule;
|
|
338
296
|
export { DruxtRouter, DruxtRouterEntityMixin, DruxtRouterMixin, DruxtRouterStore };
|
package/dist/druxt-router.ssr.js
CHANGED
|
@@ -2,7 +2,6 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
-
const path = require('path');
|
|
6
5
|
const druxt = require('druxt');
|
|
7
6
|
const Url = require('url-parse');
|
|
8
7
|
const vuex = require('vuex');
|
|
@@ -11,50 +10,6 @@ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'defau
|
|
|
11
10
|
|
|
12
11
|
const Url__default = /*#__PURE__*/_interopDefaultLegacy(Url);
|
|
13
12
|
|
|
14
|
-
const DruxtRouterNuxtModule = function() {
|
|
15
|
-
if (typeof this.options === "undefined" || !this.options.druxt) {
|
|
16
|
-
throw new TypeError("Druxt settings missing.");
|
|
17
|
-
}
|
|
18
|
-
const options = this.options.druxt;
|
|
19
|
-
options.router = {
|
|
20
|
-
pages: true,
|
|
21
|
-
wildcard: true,
|
|
22
|
-
...options.router
|
|
23
|
-
};
|
|
24
|
-
this.nuxt.hook("components:dirs", (dirs) => {
|
|
25
|
-
dirs.push({ path: path.join(__dirname, "components") });
|
|
26
|
-
});
|
|
27
|
-
if (options.router.wildcard) {
|
|
28
|
-
this.addTemplate({
|
|
29
|
-
src: path.resolve(__dirname, "../nuxt/component.js"),
|
|
30
|
-
fileName: "components/druxt-router.js",
|
|
31
|
-
options
|
|
32
|
-
});
|
|
33
|
-
this.extendRoutes((routes, resolve2) => {
|
|
34
|
-
routes.push({
|
|
35
|
-
name: "druxt-router",
|
|
36
|
-
path: "*",
|
|
37
|
-
component: resolve2(this.options.buildDir, "components/druxt-router.js"),
|
|
38
|
-
chunkName: "druxt-router"
|
|
39
|
-
});
|
|
40
|
-
});
|
|
41
|
-
}
|
|
42
|
-
if (options.router.wildcard && !options.router.pages) {
|
|
43
|
-
this.nuxt.hook("build:before", () => this.nuxt.options.build.createRoutes = () => []);
|
|
44
|
-
}
|
|
45
|
-
this.addPlugin({
|
|
46
|
-
src: path.resolve(__dirname, "../nuxt/plugin.js"),
|
|
47
|
-
fileName: "druxt-router.js",
|
|
48
|
-
options
|
|
49
|
-
});
|
|
50
|
-
this.options.store = true;
|
|
51
|
-
this.addPlugin({
|
|
52
|
-
src: path.resolve(__dirname, "../nuxt/store.js"),
|
|
53
|
-
fileName: "store/druxt-router.js",
|
|
54
|
-
options
|
|
55
|
-
});
|
|
56
|
-
};
|
|
57
|
-
|
|
58
13
|
class DruxtRouter {
|
|
59
14
|
constructor(baseUrl, options = {}) {
|
|
60
15
|
this.options = {
|
|
@@ -104,9 +59,6 @@ class DruxtRouter {
|
|
|
104
59
|
}
|
|
105
60
|
async get(path) {
|
|
106
61
|
const route = await this.getRoute(path);
|
|
107
|
-
if (route.error) {
|
|
108
|
-
return { route };
|
|
109
|
-
}
|
|
110
62
|
const redirect = this.getRedirect(path, route);
|
|
111
63
|
return { redirect, route };
|
|
112
64
|
}
|
|
@@ -203,11 +155,9 @@ class DruxtRouter {
|
|
|
203
155
|
break;
|
|
204
156
|
}
|
|
205
157
|
if (!(response.status >= 200 && response.status < 300)) {
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
};
|
|
210
|
-
route.component = "error";
|
|
158
|
+
const error = new Error();
|
|
159
|
+
error.response = response;
|
|
160
|
+
throw error;
|
|
211
161
|
}
|
|
212
162
|
return route;
|
|
213
163
|
}
|
|
@@ -279,7 +229,12 @@ const DruxtRouterStore = ({ store }) => {
|
|
|
279
229
|
if (typeof state.routes[path] !== "undefined") {
|
|
280
230
|
return state.routes[path];
|
|
281
231
|
}
|
|
282
|
-
|
|
232
|
+
let route;
|
|
233
|
+
try {
|
|
234
|
+
route = await this.$druxtRouter().getRoute(path);
|
|
235
|
+
} catch (err) {
|
|
236
|
+
route = { error: { statusCode: err.response.status, message: err.response.data.message } };
|
|
237
|
+
}
|
|
283
238
|
commit("addRoute", { path, route });
|
|
284
239
|
return route;
|
|
285
240
|
}
|
|
@@ -335,6 +290,10 @@ const DruxtRouterEntityMixin = {
|
|
|
335
290
|
|
|
336
291
|
const DruxtRouterMixin = {
|
|
337
292
|
props: {
|
|
293
|
+
path: {
|
|
294
|
+
type: String,
|
|
295
|
+
default: void 0
|
|
296
|
+
},
|
|
338
297
|
route: {
|
|
339
298
|
type: Object,
|
|
340
299
|
required: true
|
|
@@ -346,4 +305,3 @@ exports.DruxtRouter = DruxtRouter;
|
|
|
346
305
|
exports.DruxtRouterEntityMixin = DruxtRouterEntityMixin;
|
|
347
306
|
exports.DruxtRouterMixin = DruxtRouterMixin;
|
|
348
307
|
exports.DruxtRouterStore = DruxtRouterStore;
|
|
349
|
-
exports.default = DruxtRouterNuxtModule;
|
package/nuxt/index.d.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Nuxt.js module function.
|
|
3
|
+
*
|
|
4
|
+
* - Extends the Vue router, adding the Druxt wildcard route.
|
|
5
|
+
* - Adds the Druxt router plugin.
|
|
6
|
+
* - Adds the Druxt router Vuex store.
|
|
7
|
+
*
|
|
8
|
+
* The module function should not be used directly, but rather installed via your Nuxt configuration file.
|
|
9
|
+
*
|
|
10
|
+
* Options are set on the root level `druxt` Nuxt config object.
|
|
11
|
+
*
|
|
12
|
+
* @example @lang js
|
|
13
|
+
* // `nuxt.config.js`
|
|
14
|
+
* module.exports = {
|
|
15
|
+
* buildModules: ['druxt-router/nuxt'],
|
|
16
|
+
* druxt: {
|
|
17
|
+
* baseUrl: 'https://demi-api.druxtjs.org'
|
|
18
|
+
* }
|
|
19
|
+
* }
|
|
20
|
+
*
|
|
21
|
+
* @property {object} options.druxt - Druxt root level options.
|
|
22
|
+
* @property {string} options.druxt.baseUrl - Base URL of Drupal JSON:API backend.
|
|
23
|
+
* @property {string} options.druxt.router.component - File to custom Router component.
|
|
24
|
+
*/
|
|
25
|
+
declare function DruxtRouterNuxtModule(moduleOptions?: {}): Promise<void>;
|
|
26
|
+
declare namespace DruxtRouterNuxtModule {
|
|
27
|
+
const meta: any;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export default DruxtRouterNuxtModule;
|
package/nuxt/index.js
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
const fs = require('fs');
|
|
6
|
+
const path = require('path');
|
|
7
|
+
|
|
8
|
+
const DruxtRouterNuxtModule = async function(moduleOptions = {}) {
|
|
9
|
+
const options = {
|
|
10
|
+
baseUrl: moduleOptions.baseUrl,
|
|
11
|
+
...(this.options || {}).druxt || {},
|
|
12
|
+
router: {
|
|
13
|
+
pages: await fs.existsSync(path.resolve(this.options.srcDir, this.options.dir.pages)),
|
|
14
|
+
wildcard: true,
|
|
15
|
+
...((this.options || {}).druxt || {}).router,
|
|
16
|
+
...moduleOptions
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
this.nuxt.hook("components:dirs", (dirs) => {
|
|
20
|
+
dirs.push({ path: path.join(__dirname, "../dist/components") });
|
|
21
|
+
});
|
|
22
|
+
if (options.router.wildcard) {
|
|
23
|
+
this.addTemplate({
|
|
24
|
+
src: path.resolve(__dirname, "../templates/component.js"),
|
|
25
|
+
fileName: "components/druxt-router.js",
|
|
26
|
+
options
|
|
27
|
+
});
|
|
28
|
+
this.extendRoutes((routes, resolve2) => {
|
|
29
|
+
routes.push({
|
|
30
|
+
name: "druxt-router",
|
|
31
|
+
path: "*",
|
|
32
|
+
component: resolve2(this.options.buildDir, "components/druxt-router.js"),
|
|
33
|
+
chunkName: "druxt-router"
|
|
34
|
+
});
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
if (options.router.wildcard && !options.router.pages) {
|
|
38
|
+
this.nuxt.hook("build:before", () => this.nuxt.options.build.createRoutes = () => []);
|
|
39
|
+
}
|
|
40
|
+
this.addPlugin({
|
|
41
|
+
src: path.resolve(__dirname, "../templates/plugin.js"),
|
|
42
|
+
fileName: "druxt-router.js",
|
|
43
|
+
options
|
|
44
|
+
});
|
|
45
|
+
this.options.store = true;
|
|
46
|
+
this.addPlugin({
|
|
47
|
+
src: path.resolve(__dirname, "../templates/store.js"),
|
|
48
|
+
fileName: "store/druxt-router.js",
|
|
49
|
+
options
|
|
50
|
+
});
|
|
51
|
+
this.nuxt.hook("storybook:config", async ({ stories }) => {
|
|
52
|
+
stories.push("druxt-router/dist/components/*.stories.mjs");
|
|
53
|
+
});
|
|
54
|
+
};
|
|
55
|
+
DruxtRouterNuxtModule.meta = require("../package.json");
|
|
56
|
+
|
|
57
|
+
exports.default = DruxtRouterNuxtModule;
|
package/package.json
CHANGED
|
@@ -1,37 +1,48 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "druxt-router",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.24.0",
|
|
4
4
|
"description": "Drupal router for Nuxt, powered by the Drupal Decoupled Router module.",
|
|
5
5
|
"keywords": [
|
|
6
|
+
"cms",
|
|
7
|
+
"decoupled",
|
|
6
8
|
"drupal",
|
|
7
9
|
"druxt",
|
|
8
|
-
"
|
|
9
|
-
"
|
|
10
|
+
"druxtjs",
|
|
11
|
+
"headless",
|
|
10
12
|
"jsonapi",
|
|
11
|
-
"json
|
|
12
|
-
"
|
|
13
|
+
"json:api",
|
|
14
|
+
"nuxt",
|
|
15
|
+
"nuxtjs",
|
|
16
|
+
"module",
|
|
17
|
+
"router",
|
|
18
|
+
"vue"
|
|
13
19
|
],
|
|
14
|
-
"
|
|
15
|
-
"
|
|
16
|
-
"
|
|
17
|
-
"email": "stuart@realityloop.com",
|
|
18
|
-
"url": "https://realityloop.com"
|
|
20
|
+
"homepage": "https://druxtjs.org",
|
|
21
|
+
"bugs": {
|
|
22
|
+
"url": "https://github.com/druxt/druxt.js/issues"
|
|
19
23
|
},
|
|
24
|
+
"repository": {
|
|
25
|
+
"type": "git",
|
|
26
|
+
"url": "git+https://github.com/druxt/druxt.js"
|
|
27
|
+
},
|
|
28
|
+
"license": "MIT",
|
|
20
29
|
"exports": {
|
|
21
30
|
".": {
|
|
22
31
|
"require": "./dist/druxt-router.ssr.js",
|
|
23
32
|
"import": "./dist/druxt-router.esm.js"
|
|
24
33
|
},
|
|
25
|
-
"./components/*": "./dist/components/*"
|
|
34
|
+
"./components/*": "./dist/components/*",
|
|
35
|
+
"./nuxt": "./nuxt/index.js"
|
|
26
36
|
},
|
|
27
37
|
"main": "dist/druxt-router.ssr.js",
|
|
28
38
|
"module": "dist/druxt-router.esm.js",
|
|
29
39
|
"files": [
|
|
30
40
|
"dist",
|
|
31
|
-
"nuxt"
|
|
41
|
+
"nuxt",
|
|
42
|
+
"templates"
|
|
32
43
|
],
|
|
33
44
|
"dependencies": {
|
|
34
|
-
"druxt": "^0.
|
|
45
|
+
"druxt": "^0.13.0",
|
|
35
46
|
"url-parse": "^1.4.7"
|
|
36
47
|
},
|
|
37
48
|
"optionalDependencies": {
|
|
File without changes
|
|
@@ -8,6 +8,14 @@ export default (context, inject) => {
|
|
|
8
8
|
options.endpoint = '<%= options.endpoint %>'
|
|
9
9
|
<% } %>
|
|
10
10
|
|
|
11
|
+
<% if ((options.proxy || {}).api) { %>
|
|
12
|
+
if (process.client) {
|
|
13
|
+
options.proxy = {
|
|
14
|
+
api: <%= options.proxy.api %>
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
<% } %>
|
|
18
|
+
|
|
11
19
|
<% if (options.router && options.router.render) { %>
|
|
12
20
|
// Render component.
|
|
13
21
|
options.render = '<%= options.router.render %>'
|
|
File without changes
|
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
<script>
|
|
2
|
-
|
|
3
|
-
import DruxtModule from 'druxt/dist/components/DruxtModule.vue'
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Renders a Druxt module router component based on the resolved route provided
|
|
7
|
-
* by the Drupal Decoupled Router module.
|
|
8
|
-
*
|
|
9
|
-
* @example @lang vue
|
|
10
|
-
* <DruxtRouter />
|
|
11
|
-
*/
|
|
12
|
-
export default {
|
|
13
|
-
name: 'DruxtRoute',
|
|
14
|
-
|
|
15
|
-
extends: DruxtModule,
|
|
16
|
-
|
|
17
|
-
props: {
|
|
18
|
-
path: {
|
|
19
|
-
type: String,
|
|
20
|
-
default() {
|
|
21
|
-
return this.$route.fullPath
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
},
|
|
25
|
-
|
|
26
|
-
async fetch() {
|
|
27
|
-
if (!this.value) {
|
|
28
|
-
this.model = await this.$store.dispatch('druxtRouter/get', this.path)
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
await DruxtModule.fetch.call(this)
|
|
32
|
-
},
|
|
33
|
-
|
|
34
|
-
fetchKey() {
|
|
35
|
-
return 'MOOOO'
|
|
36
|
-
},
|
|
37
|
-
|
|
38
|
-
data: ({ value }) => ({
|
|
39
|
-
model: {
|
|
40
|
-
redirect: false,
|
|
41
|
-
route: {},
|
|
42
|
-
...value
|
|
43
|
-
},
|
|
44
|
-
}),
|
|
45
|
-
|
|
46
|
-
/** */
|
|
47
|
-
computed: {
|
|
48
|
-
module: ({ route }) =>
|
|
49
|
-
(route || {}).component && route.component.startsWith('druxt-') ? route.component.substring(6) : false,
|
|
50
|
-
|
|
51
|
-
redirect: ({ model }) => (model || {}).redirect || false,
|
|
52
|
-
route: ({ model }) => (model || {}).route || {},
|
|
53
|
-
},
|
|
54
|
-
|
|
55
|
-
watch: {
|
|
56
|
-
async path() {
|
|
57
|
-
if (!this.value) {
|
|
58
|
-
await this.$fetch()
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
},
|
|
62
|
-
|
|
63
|
-
druxt: {
|
|
64
|
-
componentOptions: ({ module, route }) => [
|
|
65
|
-
// @TODO - Add Path options.
|
|
66
|
-
[module || 'error', (route || {}).isHomePath ? 'front' : 'not-front'],
|
|
67
|
-
['default']
|
|
68
|
-
],
|
|
69
|
-
|
|
70
|
-
propsData: ({ route }) => ({ route })
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
</script>
|