Skip to content

Commit

Permalink
feat: allow Route Functions to return a boolean and make returning `m…
Browse files Browse the repository at this point in the history
…atch` optional
  • Loading branch information
brillout committed Apr 22, 2021
1 parent 4a36ae7 commit 963e488
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1798,11 +1798,11 @@ Route Functions give you full programmatic flexibility to define your routing lo
export default ({ url, contextProps }) {
// Route Functions allow us to implement advanced routing such as route guards.
if (! contextProps.user.isAdmin) {
return { match: false }
return false
}
// We can use RegExp and any JavaScript tool we want.
if (! /\/film\/[0-9]+\/admin/.test(url)) {
return { match: false }
return { match: false } // equivalent to `return false`
}
filmId = url.split('/')[2]
return {
Expand All @@ -1813,8 +1813,8 @@ export default ({ url, contextProps }) {
}
```
The `match` value can be a (negative) number which enables you to resolve route conflicts.
The higher the number, the higher the priority.
The `match` value can be a (negative) number which enables you to resolve route conflicts;
the higher the number, the higher the priority.
<br/><br/>
Expand Down
2 changes: 1 addition & 1 deletion examples/vue/pages/hello/index.page.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// This is a route similar to `/hello/:name` but with details impossible to achieve with a route string.
export default ({ url }: { url: string }) => {
if (!url.startsWith('/hello')) {
return { match: false }
return false
}
const name = url.split('/')[2] || 'anonymous'
return { match: true, contextProps: { name } }
Expand Down
12 changes: 9 additions & 3 deletions src/route.shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,12 +203,18 @@ function resolveRouteFunction(
matchValue: boolean | number
routeParams: Record<string, unknown>
} {
const result = routeFunction({ url: urlPathname, contextProps })
let result = routeFunction({ url: urlPathname, contextProps })
if ([true, false].includes(result)) {
result = { match: result }
}
assertUsage(
typeof result === 'object' && result !== null && result.constructor === Object,
`The Route Function ${routeFilePath} should return a plain JavaScript object, e.g. \`{ match: true }\`.`
`The Route Function ${routeFilePath} should return a boolean or a plain JavaScript object, e.g. \`{ match: true }\`.`
)
assertUsage(hasProp(result, 'match'), `The Route Function ${routeFilePath} should return a \`{ match }\` value.`)
if (!hasProp(result, 'match')) {
result.match = true
}
assert(hasProp(result, 'match'))
assertUsage(
typeof result.match === 'boolean' || typeof result.match === 'number',
`The \`match\` value returned by the Route Function ${routeFilePath} should be a boolean or a number.`
Expand Down

0 comments on commit 963e488

Please sign in to comment.