API
Xi class
Section titled “Xi class”You can pass an object of configuration options when creating the new instance.
{ base_path: string; // the router's base path for all created routes}
import { Xi, type BaseStore, type StoreConstructor } from "@xinkjs/xi"
class Store { /* Define your store variables and methods. */}
export declare class Router extends Xi<Store> { // you must define this, named as-is, and return your store protected getStoreConstructor(): StoreConstructor<Store> { return Store }
// support users passing the config to your router constructor(options) { // perhaps some options would be saved in your router's config
super(options) // then pass config to Xi // alternatively, you can be specific // super({ base_path: options.base_path }) }}
Register a route.
const store = this.route('/user')
Parameters:
path
(string
): The URL path to register. Must start with a/
.
Returns: The route’s store.
Throws:
Error
ifpath
does not start with a/
.
Find a route.
const { store, params } = this.find('/user')
Parameters:
path
(string
): The URL path to find. Must start with a/
.
Returns an object of:
store
(Store|null
): The route’s store.params
(Params
): An object ofRecord<string, any>
, or{}
if there are none.
Throws:
Error
ifpath
does not start with a/
.
getConfig
Section titled “getConfig”Get the router’s configuration.
const config = this.getConfig() // use `super.getConfig()` if you have your own method.
Returns: { base_path: string }
.
matcher
Section titled “matcher”Register a custom matcher function.
xi.matcher( 'fruit', (param: string): boolean => new Set['apple','banana','orange'].has(param))