|
2 | 2 | import Badge from "$lib/components/badge.svelte";
|
3 | 3 | import Container from "$lib/components/container.svelte";
|
4 | 4 | import RouteWrapper from "$lib/components/routes/route-wrapper.svelte";
|
5 |
| - import { type Route } from "@mateothegreat/svelte5-router"; |
| 5 | + import { getStatusByValue, StatusCode, type Route } from "@mateothegreat/svelte5-router"; |
6 | 6 | import Router from "@mateothegreat/svelte5-router/router.svelte";
|
| 7 | + import CustomNotFound from "./custom-not-found.svelte"; |
| 8 | + import DisplayParamsAnother from "./display-params-another.svelte"; |
7 | 9 | import DisplayParams from "./display-params.svelte";
|
| 10 | + import QueryMatch_1 from "./query-match-1.svelte"; |
| 11 | + import QueryMatch_2 from "./query-match-2.svelte"; |
8 | 12 |
|
9 | 13 | const routes: Route[] = [
|
10 | 14 | {
|
11 | 15 | // This route will be used if there is no match above.
|
12 | 16 | component: snippet
|
13 | 17 | },
|
| 18 | + { |
| 19 | + name: "props-query-match-2", |
| 20 | + path: "query-matcher", |
| 21 | + query: { |
| 22 | + value: 2 |
| 23 | + }, |
| 24 | + component: QueryMatch_2 |
| 25 | + }, |
| 26 | + { |
| 27 | + /** |
| 28 | + * /props/query-matcher?pagination=2,23 |
| 29 | + * /props/query-matcher?pagination=2,23&company=123 |
| 30 | + * /props/query-matcher?pagination=2,23&company=1234567 |
| 31 | + * /props/query-matcher?pagination=2,23&company=1 |
| 32 | + * /props/query-matcher?pagination=2,23&company=12 |
| 33 | + */ |
| 34 | + name: "props-query-matching", |
| 35 | + path: "/query-matcher", |
| 36 | + query: { |
| 37 | + // The "pagination" query param: |
| 38 | + // * must be present |
| 39 | + // * must be a number |
| 40 | + // + and then be followed by an optional "cursor" |
| 41 | + // * if present, it must have a comma delimiter |
| 42 | + // * and then be a string of alphanumeric characters: |
| 43 | + pagination: /^(?<page>\d+)(,(?<cursor>[a-z0-9]+))?$/, |
| 44 | + // The "company" query param is optional, and if present: |
| 45 | + // * can be empty |
| 46 | + // * must be a single number |
| 47 | + company: /^(\d+)?$/ |
| 48 | + }, |
| 49 | + component: QueryMatch_1, |
| 50 | + props: { |
| 51 | + metadata: { |
| 52 | + src: "props.svelte", |
| 53 | + routeName: "props-query-matching" |
| 54 | + } |
| 55 | + } |
| 56 | + }, |
14 | 57 | {
|
15 | 58 | // This route will match any path and pass the pattern groups
|
16 | 59 | // as an object to the component that is passed in $props().
|
|
27 | 70 | userAgent: navigator.userAgent
|
28 | 71 | }
|
29 | 72 | }
|
| 73 | + }, |
| 74 | + { |
| 75 | + path: /another\/(?<child>.*)/, |
| 76 | + component: DisplayParamsAnother, |
| 77 | + props: { |
| 78 | + another: { |
| 79 | + one: "works!" |
| 80 | + } |
| 81 | + } |
30 | 82 | }
|
31 | 83 | ];
|
32 | 84 |
|
|
67 | 119 | href: "/props/foo",
|
68 | 120 | label: "/props/foo"
|
69 | 121 | },
|
| 122 | + { |
| 123 | + href: "/props/another/bar", |
| 124 | + label: "/props/another/bar" |
| 125 | + }, |
70 | 126 | {
|
71 | 127 | href: "/props/bar?someQueryParam=123",
|
72 | 128 | label: "/props/bar?someQueryParam=123"
|
| 129 | + }, |
| 130 | + { |
| 131 | + href: "/props/query-matcher?pagination=2,23&company=123", |
| 132 | + label: "/props/query-matcher?pagination=2,23&company=123" |
| 133 | + }, |
| 134 | + { |
| 135 | + href: "/props/query-matcher?value=2", |
| 136 | + label: "/props/query-matcher?value=2" |
73 | 137 | }
|
74 | 138 | ]}>
|
75 | 139 | <Router
|
76 | 140 | id="props-router"
|
77 | 141 | basePath="/props"
|
78 | 142 | {routes}
|
79 | 143 | hooks={{
|
80 |
| - // pre: globalAuthGuardHook |
| 144 | + // |
| 145 | + // You could use a global auth guard here to run before every route: |
| 146 | + // pre: (route: Routed) => { |
| 147 | + // if (!isAuthenticated()) { |
| 148 | + // console.warn("user is not authenticated, redirecting to login", route); |
| 149 | + // return { |
| 150 | + // component: NotGonnaMakeIt, |
| 151 | + // }; |
| 152 | + // } |
| 153 | + // } |
| 154 | + // |
| 155 | + // You could also use a global error handler here to run after every route: |
| 156 | + // post: [ |
| 157 | + // (route: Routed) => { |
| 158 | + // console.info("do some more work here", route); |
| 159 | + // return true; |
| 160 | + // }, |
| 161 | + // someLogMethod, |
| 162 | + // finalMethod, |
| 163 | + // ] |
| 164 | + // |
| 165 | + }} |
| 166 | + statuses={{ |
| 167 | + [StatusCode.NotFound]: (path: string) => { |
| 168 | + console.warn(`the path "${path}" could not be found :(`, { |
| 169 | + // You could use the status name to make something pretty: |
| 170 | + status: getStatusByValue(StatusCode.NotFound), |
| 171 | + // You could also use the status code to something more dynamic: |
| 172 | + code: StatusCode.NotFound |
| 173 | + }); |
| 174 | + // Now, we're going to return a new route that will be rendered by the router: |
| 175 | + return { |
| 176 | + component: CustomNotFound, |
| 177 | + // You can pass props to the component that is rendered if you need to |
| 178 | + // bubble up some extra information: |
| 179 | + props: { |
| 180 | + src: "props.svelte" |
| 181 | + } |
| 182 | + }; |
| 183 | + } |
81 | 184 | }} />
|
82 | 185 | </RouteWrapper>
|
0 commit comments