UNPKG

@ima/core

Version:

IMA.js framework for isomorphic javascript application

265 lines 8.69 kB
import { AbstractRoute, AsyncRouteController, AsyncRouteView, RouteParams } from './AbstractRoute'; import { RouteFactory } from './RouteFactory'; import { Router, RouteOptions, RouterMiddleware, RouteAction, RouteLocals } from './Router'; import { RouterEvents } from './RouterEvents'; import { Settings } from '../boot'; import { IMAError } from '../error/Error'; import { Dispatcher } from '../event/Dispatcher'; import { PageManager } from '../page/manager/PageManager'; import { UnknownParameters } from '../types'; type BeforeHandleRouteEventData = { route: InstanceType<typeof AbstractRoute>; path: string; params?: RouteParams; options?: Partial<RouteOptions>; action?: RouteAction; }; type AfterHandleRouteEventData = BeforeHandleRouteEventData & { response?: any; }; export interface RouterDispatcherEvents { [RouterEvents.AFTER_HANDLE_ROUTE]: AfterHandleRouteEventData; [RouterEvents.BEFORE_HANDLE_ROUTE]: BeforeHandleRouteEventData; } /** * The basic implementation of the {@link Router} interface, providing the * common or default functionality for parts of the API. */ export declare abstract class AbstractRouter extends Router { #private; /** * The page manager handling UI rendering, and transitions between * pages if at the client side. */ protected _pageManager: PageManager; /** * Factory for routes. */ protected _factory: RouteFactory; /** * Dispatcher fires events to app. */ protected _dispatcher: Dispatcher; /** * The current protocol used to access the application, terminated by a * colon (for example `https:`). */ protected _protocol: string; /** * The application's host. */ protected _host: string; /** * The URL path pointing to the application's root. */ protected _root: string; /** * The URL path fragment used as a suffix to the `_root` field * that specifies the current language. */ protected _languagePartPath: string; /** * Storage of all known routes and middlewares. The key are their names. */ protected _routeHandlers: Map<string, InstanceType<typeof AbstractRoute> | RouterMiddleware>; /** * Middleware ID counter which is used to auto-generate unique middleware * names when adding them to routeHandlers map. */ protected _currentMiddlewareId: number; protected _currentlyRoutedPath: string; protected _middlewareTimeout: number; protected _isSPARouted: NonNullable<Settings['$Router']>['isSPARouted'] | undefined; /** * Initializes the router. * * @param pageManager The page manager handling UI rendering, * and transitions between pages if at the client side. * @param factory Factory for routes. * @param dispatcher Dispatcher fires events to app. * @example * router.link('article', {articleId: 1}); * @example * router.redirect('http://www.example.com/web'); * @example * router.add( * 'home', * '/', * ns.app.page.home.Controller, * ns.app.page.home.View, * { * onlyUpdate: false, * autoScroll: true, * documentView: null, * managedRootView: null, * viewAdapter: null * } * ); */ constructor(pageManager: PageManager, factory: RouteFactory, dispatcher: Dispatcher, settings: Settings['$Router'] | number); /** * @inheritDoc */ init(config: { $Protocol?: string; $Root?: string; $LanguagePartPath?: string; $Host: string; }): void; /** * @inheritDoc */ add(name: string, pathExpression: string, controller: AsyncRouteController, view: AsyncRouteView, options?: Partial<RouteOptions>): this; /** * @inheritDoc */ use(middleware: RouterMiddleware): this; /** * @inheritDoc */ remove(name: string): this; /** * @inheritDoc */ getRouteHandler(name: string): AbstractRoute<string | import("./DynamicRoute").RoutePathExpression> | RouterMiddleware | undefined; /** * @inheritDoc */ getPath(): string; /** * @inheritDoc */ getUrl(): string; /** * @inheritDoc */ getBaseUrl(): string; /** * @inheritDoc */ getDomain(): string; /** * @inheritDoc */ getHost(): string; /** * @inheritDoc */ getProtocol(): string; /** * @inheritDoc */ getCurrentRouteInfo(): { route: AbstractRoute<string | import("./DynamicRoute").RoutePathExpression>; params: RouteParams<{}>; path: string; }; /** * @inheritDoc */ getRouteHandlers(): Map<string, AbstractRoute<string | import("./DynamicRoute").RoutePathExpression> | RouterMiddleware>; /** * @inheritDoc * @abstract */ listen(): this; /** * @inheritDoc */ unlisten(): this; /** * @inheritDoc */ unlistenAll(): this; /** * @inheritDoc */ redirect(url: string, options?: Partial<RouteOptions>, action?: RouteAction, locals?: RouteLocals): void; /** * @inheritDoc */ link(routeName: string, params: RouteParams): string; /** * @inheritDoc */ route(path: string, options?: Partial<RouteOptions>, action?: RouteAction, locals?: RouteLocals): Promise<void | UnknownParameters>; /** * @inheritDoc */ handleError(params: RouteParams, options?: Partial<RouteOptions>, locals?: RouteLocals): Promise<void | UnknownParameters>; /** * @inheritDoc */ handleNotFound(params: RouteParams, options?: Partial<RouteOptions>, locals?: RouteLocals): Promise<void | UnknownParameters>; /** * @inheritDoc */ isClientError(reason: IMAError | Error): boolean; /** * @inheritDoc */ isRedirection(reason: IMAError | Error): boolean; /** * Strips the URL path part that points to the application's root (base * URL) from the provided path. * * @protected * @param path Relative or absolute URL path. * @return URL path relative to the application's base URL. */ _extractRoutePath(path: string): string; /** * Handles the provided route and parameters by initializing the route's * controller and rendering its state via the route's view. * * The result is then sent to the client if used at the server side, or * displayed if used as the client side. * * @param route The route that should have its * associated controller rendered via the associated view. * @param params Parameters extracted from * the URL path and query. * @param options The options overrides route options defined in the * `routes.js` configuration file. * @param action An action * object describing what triggered this routing. * @return A promise that resolves when the * page is rendered and the result is sent to the client, or * displayed if used at the client side. */ _handle(route: InstanceType<typeof AbstractRoute>, params: RouteParams, options?: Partial<RouteOptions>, action?: RouteAction): Promise<void | UnknownParameters>; /** * Returns the route matching the provided URL path part (the path may * contain a query) and all middlewares preceding this route definition. * * @param path The URL path. * @return The route * matching the path and middlewares preceding it or `{}` * (empty object) if no such route exists. */ getRouteHandlersByPath(path: string): { route?: InstanceType<typeof AbstractRoute>; middlewares: RouterMiddleware[]; }; /** * Returns middlewares preceding given route name. */ _getMiddlewaresForRoute(routeName: string): RouterMiddleware[]; /** * Returns path that is stored in private property when a `route` * method is called. */ _getCurrentlyRoutedPath(): string; /** * Runs provided middlewares in sequence. * * @param middlewares Array of middlewares. * @param params Router params that can be * mutated by middlewares. * @param locals The locals param is used to pass local data * between middlewares. */ _runMiddlewares(middlewares: RouterMiddleware[] | undefined, params: RouteParams, locals: RouteLocals): Promise<void>; } export {}; //# sourceMappingURL=AbstractRouter.d.ts.map