Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(router): route-validators: types and comments #10462

Merged
merged 1 commit into from
Apr 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 19 additions & 31 deletions packages/router/src/route-validators.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,75 +9,62 @@ import type {
} from './Route'
import { Route } from './Route'

const isNodeTypeRoute = (
function isNodeTypeRoute(
node: ReactNode,
): node is ReactElement<InternalRouteProps> => {
): node is ReactElement<InternalRouteProps> {
return isValidElement(node) && node.type === Route
}

function isString(value: unknown): value is string {
return typeof value === 'string'
}

/**
* Narrows down the type of the Route element to RouteProps
*
* It means that it is not a notfound page or a redirected route
*
* @param node
* @returns boolean
*/

export function isStandardRoute(
node: ReactElement<InternalRouteProps>,
): node is ReactElement<RouteProps> {
return !node.props.notfound && !node.props.redirect
}
/**
*
* Checks if a Route element is a Redirect Route
*
* @param node
* @returns
*/

/** Checks if a Route element is a Redirect Route */
export function isRedirectRoute(
node: ReactElement<InternalRouteProps>,
): node is ReactElement<RedirectRouteProps> {
return !!node.props.redirect
}
/**
*
* Checks if a Route element is a Redirect Route
*
* @param node
* @returns
*/

/** Checks if a Route element is a NotFound Route */
export function isNotFoundRoute(
node: ReactElement<InternalRouteProps>,
): node is ReactElement<NotFoundRouteProps> {
return !!node.props.notfound
}

/**
* Check that the Route element is ok
* and that it could be one of the following:
* <Route redirect .../> (redirect Route)
* <Route notfound .../> (notfound Route)
* <Route .../> (standard Route)
*
* @param node
* @returns boolean
*/

export function isValidRoute(
node: ReactNode,
): node is ReactElement<InternalRouteProps> {
const isValidRouteElement = isNodeTypeRoute(node)

// Throw inside here, because we know it's a Route otherwise it could be a Set or Private
if (isValidRouteElement) {
const notFoundOrRedirect = node.props.notfound || node.props.redirect
const requiredKeys = [
!node.props.notfound && 'path',
!node.props.redirect && 'page', // redirects dont need an actual page, but notfound and standard do
!notFoundOrRedirect && 'name', // this not so sure about! Redirects should have names too, but maybe we don't need to throw an error for it
].filter(Boolean) as string[]
// redirects don't need an actual page, but notfound and standard do
!node.props.redirect && 'page',
// Redirects can have names, but aren't required to
!notFoundOrRedirect && 'name',
].filter(isString)

const missingKeys = requiredKeys.filter((key) => !(key in node.props))

Expand All @@ -86,10 +73,11 @@ export function isValidRoute(
node.props.name || node.props.path
? `for "${node.props.name || node.props.path}" `
: ''
// Throw inside here, because we know it's a Route otherwise it could be
// a Set or Private
throw new Error(
`Route element ${stringToHelpIdentify}is missing required props: ${missingKeys.join(
', ',
)}`,
`Route element ${stringToHelpIdentify}is missing required props: ` +
missingKeys.join(', '),
)
}
}
Expand Down
Loading