forked from cypress-io/cypress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter.ts
72 lines (61 loc) · 1.99 KB
/
router.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import { createRouter as _createRouter, createWebHashHistory } from 'vue-router'
import generatedRoutes from 'virtual:generated-pages'
import { setupLayouts } from 'virtual:generated-layouts'
/**
* Generated Routes are created via https://github.com/hannoeru/vite-plugin-pages
* The generates are based on the files contained in src/pages.
* See README.md this package for more details
*/
export const createRouter = () => {
const routes = setupLayouts(generatedRoutes)
const defaultRoute = generatedRoutes.find((route) => route.meta?.default)
if (defaultRoute) {
routes.push({
path: '/',
redirect: defaultRoute.path,
})
}
/**
* Redirect route useful for passing page params when routing to a particular page
*
* @param name route name
* @param params url encoded JSON object of page component params
*
* @example
* // redirects to the Debug page passing a parameter of `from` set to `notification`
* "/redirect?name=Debug¶ms=%7B%22from%22%3A%22notification%22%7D"
*
* @see changeUrlToDebug in packages/server/lib/open_project.ts
*/
routes.push({
path: '/redirect',
redirect: (from) => {
if (from.query.name) {
if (typeof from.query.name !== 'string') {
throw new Error(`name should be a single string but got: ${from.query.name}`)
}
let params = {}
if (from.query.params) {
if (typeof from.query.params !== 'string') {
throw new Error(`params should be a string but got: ${from.query.params}`)
}
try {
params = JSON.parse(from.query.params)
} catch {
throw new Error(`params was not valid JSON: ${from.query.params}`)
}
}
return {
name: from.query.name,
params,
query: {}, //reset query params so they do not get passed on
}
}
return { path: '/' }
},
})
return _createRouter({
history: createWebHashHistory(),
routes,
})
}