Skip to main content

Top-level API

createRouter

  • routes (required): Takes an object with your route names as keys, and their pattern as values.
  • options:
    • basePath: Prefix for the path (allowing your code to ignore it)

Returns a Router.

import { createRouter } from "@swan-io/chicane";

export const Router = createRouter({
Home: "/",
UserList: "/users",
UserDetail: "/users/:userId",
});

createGroup

Spread a createGroup in your routes if you want to avoid repetition with nested routes having the same prefix or search params.

  • routeName (required): string
  • routePath (required): string
  • subroutes (required): Takes an object with your route names as keys, and their pattern as values.
import { createRouter, createGroup } from "@swan-io/chicane";

export const Router = createRouter({
Home: "/",

...createGroup("User", "/users", {
Area: "/*", // UserArea: "/users/*"
List: "/", // UserList: "/users"
Detail: "/:userId", // UserDetail: "/users/:userId"
}),

...createGroup("Book", "/books?:isEditor", {
Area: "/*", // BookArea: "/books/*?:isEditor"
List: "/?:byAuthor", // BookList: "/books?:isEditor&:byAuthor"
Detail: "/:bookId", // BookDetail: "/books/:bookId?:isEditor"
}),
});