Provide an official runtime route-existence primitive #151
Replies: 1 comment
-
|
I think The current An additive helper keeps the manifest shape private: export function routeExists(name: keyof RouteParameters): boolean {
return Object.prototype.hasOwnProperty.call(routes, name)
}For locale wrappers, that gives a stable contract: const localized = `${locale}.${name}` as keyof RouteParameters
if (routeExists(localized)) {
return route(localized, params)
}
return route(name, params)The main typing caveat is dynamic string input. If a wrapper receives arbitrary strings, it may need a looser overload: routeExists(name: string): name is keyof RouteParametersThat would be very useful because it narrows the type before calling So my vote would be option B: export |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi - I'm wrapping the generated
route()fromLaravelRouteTransformedProviderfor a locale-aware routing package and wanted to flag an API gap I think a few people will hit.Context
Locale-routing packages (
niels-numbers/laravel-localizer, the recommended successor tomcamara/laravel-localizationwith ~8M downloads, andcodezero-be/laravel-localized-routes) register a single user-facing name likeaboutas several internal route names. A JS wrapper around yourroute()needs to ask "which variant exists in the manifest?" - the equivalent ofRoute::has(...)server-side.Today
There's a workable workaround: since the generated
route()does'/' + routes[name]and falls through to'/undefined'when the name is missing, wrappers can detect existence by inspecting the return value:This is what I'm shipping for now. But it relies on the current body of the generated
route()- if you ever change the miss handling (throw, return'', etc.), every wrapper that depends on it breaks silently. The generator already has the right object to make this explicit:Only
route()is exported ([source][1]: 4thTransformedconstructor argument is the export flag,falsefor the first two).Proposal
Either, whichever fits the design:
(a) Flip the
$exportflag on$transformedRoutessoroutesbecomes a named export. Wrappers checkname in routesinstead of the string-marker hack.(b) Emit
export function routeExists(name: keyof RouteParameters): booleanalongsideroute(). Keeps the lookup object's shape an implementation detail but gives downstream code a contract to depend on.Both are purely additive: no change to the existing
route()signature, no impact on users who only doimport {route} from './helpers/route'. Happy to send a PR for either.Thanks!
Beta Was this translation helpful? Give feedback.
All reactions