low level base routing for nodejs, javascript and typescript
Bot releases are hidden (Show)
Published by willybrauner about 1 month ago
Published by willybrauner about 1 month ago
Published by willybrauner about 1 month ago
4e31d18: Low router preact wrapper first release.
(check the readme for more information)
main.tsx:
import { render } from "preact"
import { createBrowserHistory } from "@wbe/wbe-router"
import { Router } from "@wbe/low-router-preact"
// Prepare the routes list
const routes = [
{
name: "home",
path: "/",
action: () => Home,
},
{
name: "about",
path: "/about",
action: () => About,
},
]
// Pass the routes list as LowRouter param
const router = new LowRouter(routes)
// Prepare a browser history
const history = createBrowserHistory()
// Render the app wrapped by the Router
render(
<Router router={router} history={history}>
<App />
</Router>,
document.getElementById("root"),
)
App.tsx
import { Link, Stack } from "@wbe/low-router-preact"
export default function App() {
return (
<div>
<nav>
<Link to={{ name: "home" }}>{"home"}</Link>
<Link to={{ name: "about" }}>{"about"}</Link>
</nav>
{/* Render current route here */}
<Stack />
</div>
)
}
Home.tsx
import { useRef, useImperativeHandle } from "preact/hooks"
const Home = (props, ref) => {
const rootRef = useRef(null)
// Each route need to attached name, DOM root, playIn & playOut to the forwarded ref.
// The Stack use this forwarded ref in order to control the component.
useImperativeHandle(
ref,
() => ({
name: "Home",
root: rootRef.current,
playIn: () => customPlayIn(rootRef.current),
playOut: () => customPlayOut(rootRef.current),
}),
[],
)
return <div ref={rootRef}>Hello home!</div>
}
export default forwardRef(Home)
Published by willybrauner 2 months ago
8529535: Matcher returns params even if not matching
const path = "/base/:lang/a-propos/bar"
const matcher = createMatcher()
before:
expect(matcher(path, "/base/fr/a-propos/bar/b")).toEqual([false, null, null, null])
after:
expect(matcher(path, "/base/fr/a-propos/bar/b")).toEqual([false, { lang: "fr" }, {}, null])
7018e00: nomalize path
Create normalize path helper to get a formatted and constant path format.
path
// remove multiples slashes
?.replace(/(https?:\/\/)|(\/)+/g, "$1$2")
// remove trailing slash
.replace(/\/$/, "") ||
// add trailing slash if path is empty
"/"
normalizePath(path) => string
is available : import { normalizePath } from "@wbe/low-router
Published by willybrauner 2 months ago
2abc0f7: export compilePath
Add missing compilePath
export in index.ts
Published by willybrauner 2 months ago
e5eca20: extract compile path function
before:
LowRouter.compilePath(path)(params)
after:
compilePath(path)(params)
71279f3: extract compilePath static method as external function
Published by willybrauner 2 months ago
1cd1608: change local browserHistory listener params to object
Goal is to feat with the remix history lib api in order to switch to it easily, if needed.
before:
history.listen((location, action) => {})
after:
history.listen(({ location, action }) => {})
74f5122: Cleanup type generics
b2efc1d: expose options
options
are now a public value, in order to get them from the instance.
const router = new LowRouter(routes, options)
console.log(router.options) // {...}
Published by willybrauner 2 months ago
358bae8: RouteContext returns relativePathname
RouteContext returns relativePathname
. It's the compiled path of current router instance.
export interface RouteContext<A = any, C extends RouterContext = RouterContext> {
pathname: string
params: RouteParams
query: QueryParams
hash: Hash
base: string
route: Route<A, C>
parent: RouteContext<A, C> | null
+ relativePathname: string
}
Published by willybrauner 2 months ago
5035b82: Externalize compile path as static method.
ex:
LowRouter.compilePath("/foo/:id")({ id: "bar" }) // "/zoo/bar"
Published by willybrauner 6 months ago
4ce916c: Install @wbe/debug dependency
browser debug:
localStorage.debug = "low-router:*"
Node debug:
DEBUG=low-router:*
Published by willybrauner 7 months ago
e02a7f3: Create resolveSync method
Resolve a route synchronously. It returns response and context without promise.
const { response, context } = router.resolveSync("/foo")
Published by willybrauner 9 months ago
c98cc80: ## breaking change: Improve resolve
and onResolve
params.
router.resolve()
and onResolve()
option return now {response, context}
object of the matching route.
before:
router.resolve("/").then((res) => {
// res: "Hello home!"
})
after:
router.resolve("/").then(({ response, context }) => {
// response: "Hello home!"
// context: RouteContext interface
})
Published by willybrauner 11 months ago
Published by willybrauner about 1 year ago