import { App, InjectionKey, Ref } from 'vue'; import { RouterHistoryMode } from '../services/createRouterHistory'; import { RouterRoute } from './routerRoute'; import { AddBeforeEnterHook, AddBeforeUpdateHook, AddBeforeLeaveHook, AddAfterEnterHook, AddAfterUpdateHook, AddAfterLeaveHook, AddErrorHook, AddRejectionHook } from './hooks'; import { PrefetchConfig } from './prefetch'; import { ResolvedRoute } from './resolved'; import { Route, Routes } from './route'; import { RouterPush } from './routerPush'; import { RouterReplace } from './routerReplace'; import { RouterResolve, RouterResolveOptions } from './routerResolve'; import { RouterReject } from './routerReject'; import { RouterPlugin } from './routerPlugin'; import { RoutesName } from './routesMap'; import { ExtractRejections, ExtractRejectionTypes, Rejections, BuiltInRejectionType } from './rejection'; /** * Options to initialize a {@link Router} instance. */ export type RouterOptions = { /** * Initial URL for the router to use. Required if using Node environment. Defaults to window.location when using browser. * * @default window.location.toString() */ initialUrl?: string; /** * Specifies the history mode for the router, such as "browser", "memory", or "hash". * * @default "auto" */ historyMode?: RouterHistoryMode; /** * Base path to be prepended to any URL. Can be used for Vue applications that run in nested folder for domain. * For example having `base` of `/foo` would assume all routes should start with `your.domain.com/foo`. */ base?: string; /** * Determines what assets are prefetched when router-link is rendered for a specific route */ prefetch?: PrefetchConfig; /** * Components assigned to each type of rejection your router supports. */ rejections?: Rejections; /** * Removes trailing slashes from the URL before matching routes. The browser's url is updated to reflect using `router.replace`. * * @default true */ removeTrailingSlashes?: boolean; /** * When false, createRouterAssets must be used for component and hooks. Assets exported by the library * will not work with the created router instance. * * @default true */ isGlobalRouter?: boolean; }; export type Router = { /** * Installs the router into a Vue application instance. * @param app The Vue application instance to install the router into */ install: (app: App) => void; /** * Manages the current route state. */ route: RouterRouteUnion | RouterRouteUnion; /** * Creates a ResolvedRoute record for a given route name and params. */ resolve: RouterResolve; /** * Creates a ResolvedRoute record for a given URL. */ find: (url: string, options?: RouterResolveOptions) => ResolvedRoute | undefined; /** * Navigates to a specified path or route object in the history stack, adding a new entry. */ push: RouterPush; /** * Replaces the current entry in the history stack with a new one. */ replace: RouterReplace; /** * Handles route rejection based on a specified rejection type. */ reject: RouterReject<[...ExtractRejections, ...ExtractRejections]>; /** * Forces the router to re-evaluate the current route. */ refresh: () => void; /** * Navigates to the previous entry in the browser's history stack. */ back: () => void; /** * Navigates to the next entry in the browser's history stack. */ forward: () => void; /** * Moves the current history entry to a specific point in the history stack. */ go: (delta: number) => void; /** * Registers a hook to be called before a route is entered. */ onBeforeRouteEnter: AddBeforeEnterHook | ExtractRejections>; /** * Registers a hook to be called before a route is left. */ onBeforeRouteLeave: AddBeforeLeaveHook | ExtractRejections>; /** * Registers a hook to be called before a route is updated. */ onBeforeRouteUpdate: AddBeforeUpdateHook | ExtractRejections>; /** * Registers a hook to be called after a route is entered. */ onAfterRouteEnter: AddAfterEnterHook | ExtractRejections>; /** * Registers a hook to be called after a route is left. */ onAfterRouteLeave: AddAfterLeaveHook | ExtractRejections>; /** * Registers a hook to be called after a route is updated. */ onAfterRouteUpdate: AddAfterUpdateHook | ExtractRejections>; /** * Registers a hook to be called when an error occurs. * If the hook returns true, the error is considered handled and the other hooks are not run. If all hooks return false the error is rethrown */ onError: AddErrorHook | ExtractRejections>; /** * Registers a hook to be called when a rejection occurs. */ onRejection: AddRejectionHook> | ExtractRejectionTypes> | BuiltInRejectionType, TRoutes | TPlugin['routes']>; /** * Given a URL, returns true if host does not match host stored on router instance */ isExternal: (url: string) => boolean; /** * Determines what assets are prefetched. */ prefetch?: PrefetchConfig; /** * Initializes the router based on the initial route. Automatically called when the router is installed. Calling this more than once has no effect. */ start: () => Promise; /** * Returns true if the router has been started. */ started: Ref; /** * Stops the router and teardown any listeners. */ stop: () => void; /** * Returns the key of the router. * * @private */ key: InjectionKey>; /** * Returns true if the router's devtools plugin has been installed * @private */ hasDevtools: boolean; }; /** * This type is the same as `RouterRoute>` while remaining distributive. * Routes without a name (empty string) are excluded so that router.route.name is never ''. */ export type RouterRouteUnion = { [K in keyof TRoutes]: TRoutes[K]['name'] extends '' ? never : RouterRoute>; }[number]; export type RouterRoutes = TRouter extends Router ? TRoutes : Routes; export type RouterRejections = TRouter extends Router ? ExtractRejections | ExtractRejections : []; export type RouterRouteName = TRouter extends Router ? RoutesName : RoutesName;