UNPKG

vasille-router

Version:

The first Developer eXperience Orientated front-end framework (router library).

66 lines (65 loc) 3.26 kB
import { Fragment } from "vasille"; import { Answer, Routing, RouteParameters, QueryParams, ScreenProps } from "./types.js"; export interface RouterInitialization<Node, Element, TagOptions extends object, Routes extends string, Extras extends object> { routes: { [K in Routes]: Answer<Node, Element, TagOptions, K, Extras>; }; getAccessLevel?(): Promise<number>; fallbackScreen?(arg: { cause: "not-found" | "no-access"; }, ctx: Fragment<Node, Element, TagOptions>): void; errorScreen?(data: { error: unknown; }, ctx: Fragment<Node, Element, TagOptions>): void; } export type RouteRenderScope = "found" | "not-found" | "fallback" | "error"; export declare function composeUrl<Route extends string>(route: Route, params: RouteParameters<Route>): string; export declare abstract class Router<Node, Element, TagOptions extends object, Routes extends string, Extras extends object, Args extends unknown[]> { protected root: Routing<Node, Element, TagOptions, Routes, Extras>; protected init: RouterInitialization<Node, Element, TagOptions, Routes, Extras>; constructor(init: RouterInitialization<Node, Element, TagOptions, Routes, Extras>); navigate<T extends Routes>(route: T, params: RouteParameters<T>, ...args: Args): void; protected createRouting(): Routing<Node, Element, TagOptions, Routes, Extras>; protected findTarget(routing: Routing<Node, Element, TagOptions, Routes, Extras>, path: string[], params: object): [Answer<Node, Element, TagOptions, Routes, Extras> | undefined, object]; protected targetByUrl(url: string): { url: string; path: string; query: QueryParams; hash: string; target: Answer<Node, Element, TagOptions, Routes, Extras>; params: object; }; protected prepareNavigation(url: string, canNavigate: boolean, async: boolean, ...args: Args): Promise<void>; /** * Must call prepareNavigation and handle promise error * @param url is URL to navigate to * @param canNavigate accept redirects when true * @param args extra args * @protected */ protected abstract doNavigate(url: string, canNavigate: boolean, ...args: Args): void; /** * Parse a url to path, query param and hash * @param url to be parsed * @returns [path, query params, hash] * @protected */ protected abstract parseUrl(url: string): [string, QueryParams, string]; /** * Load (Render) a target screen * @param target target screen to load * @param props props of the target screen * @param args extra args * @protected */ protected abstract loadTarget<Route extends string>(target: Answer<Node, Element, TagOptions, Route, Extras>, props: ScreenProps<Route>, ...args: Args): Promise<void>; /** * Render a screen * @param screen is screen to be rendered * @param props are props of the screen * @param scope is the cause of screen rendering * @param args are extra args * @protected */ protected abstract renderScreen<Props>(screen: (props: Props, ctx: Fragment<Node, Element, TagOptions>) => void | Promise<void>, props: Props, scope: RouteRenderScope, ...args: Args): Promise<void>; }