low-router

low level base routing for nodejs, javascript and typescript

Downloads
371
Stars
7

Bot releases are visible (Hide)

low-router - @wbe/[email protected] Latest Release

Published by willybrauner about 1 month ago

Minor Changes

  • cfb9bf4: Allow augmented Route & RouteContext interfaces
low-router - @wbe/[email protected]

Published by willybrauner about 1 month ago

Minor Changes

  • 193e647: Prevent hash pathname on server to avoid '/undefined' as matching route
low-router - @wbe/[email protected]

Published by willybrauner about 1 month ago

Minor Changes

  • cd68512: Internalize preact dependencies in bundle
low-router - @wbe/[email protected]

Published by willybrauner about 1 month ago

Minor Changes

  • 4e31d18: Low router preact wrapper first release.

    Usage example

    (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)
    
low-router - @wbe/[email protected]

Published by willybrauner 2 months ago

Minor Changes

  • 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

low-router - @wbe/[email protected]

Published by willybrauner 2 months ago

Patch Changes

  • 2abc0f7: export compilePath

    Add missing compilePath export in index.ts

low-router - @wbe/[email protected]

Published by willybrauner 2 months ago

Minor Changes

  • e5eca20: extract compile path function

    before:

    LowRouter.compilePath(path)(params)
    

    after:

    compilePath(path)(params)
    
    • compile base if it contains params in createUrl
  • 71279f3: extract compilePath static method as external function

low-router - @wbe/[email protected]

Published by willybrauner 2 months ago

Minor Changes

  • 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

Patch Changes

  • 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) // {...}
    
low-router - @wbe/[email protected]

Published by willybrauner 2 months ago

Minor Changes

  • 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
    }
    
low-router - @wbe/[email protected]

Published by willybrauner 2 months ago

Minor Changes

  • 5035b82: Externalize compile path as static method.

    ex:

    LowRouter.compilePath("/foo/:id")({ id: "bar" }) // "/zoo/bar"
    
low-router - @wbe/[email protected]

Published by willybrauner 6 months ago

Minor Changes

  • 4ce916c: Install @wbe/debug dependency

    • browser debug:

      localStorage.debug = "low-router:*"
      
    • Node debug:

      DEBUG=low-router:*
      
low-router - @wbe/[email protected]

Published by willybrauner 7 months ago

Minor Changes

  • e02a7f3: Create resolveSync method

    Resolve a route synchronously. It returns response and context without promise.

    const { response, context } = router.resolveSync("/foo")
    
low-router - @wbe/[email protected]

Published by willybrauner 9 months ago

Minor Changes

  • 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
    })
    
low-router - @wbe/[email protected]

Published by willybrauner 11 months ago

Patch Changes

  • 7b8d0d6: Rework types
low-router - @wbe/[email protected]

Published by willybrauner about 1 year ago

Minor Changes

  • a6f0d1f: first release