Skip to main content

Abstract Class: AbstractRouter

Defined in: packages/core/src/router/AbstractRouter.ts:51

The basic implementation of the Router interface, providing the common or default functionality for parts of the API.

Extends​

Extended by​

Constructors​

Constructor​

new AbstractRouter(pageManager, factory, dispatcher, settings): AbstractRouter

Defined in: packages/core/src/router/AbstractRouter.ts:127

Initializes the router.

Parameters​

pageManager​

PageManager

The page manager handling UI rendering, and transitions between pages if at the client side.

factory​

RouteFactory

Factory for routes.

dispatcher​

Dispatcher

Dispatcher fires events to app.

settings​

number |

{ isSPARouted?: (url, action?) => boolean; middlewareTimeout?: number; }

isSPARouted?​

(url, action?) => boolean

middlewareTimeout?​

number

Middleware execution timeout, see https://imajs.io/basic-features/routing/middlewares#execution-timeout for more information. [ms]

| undefined

Returns​

AbstractRouter

Examples​

router.link('article', {articleId: 1});
router.redirect('http://www.example.com/web');
router.add(
'home',
'/',
ns.app.page.home.Controller,
ns.app.page.home.View,
{
onlyUpdate: false,
autoScroll: true,
documentView: null,
managedRootView: null,
viewAdapter: null
}
);

Overrides​

Router.constructor

Properties​

_currentlyRoutedPath​

protected _currentlyRoutedPath: string = ''

Defined in: packages/core/src/router/AbstractRouter.ts:95


_currentMiddlewareId​

protected _currentMiddlewareId: number = 0

Defined in: packages/core/src/router/AbstractRouter.ts:94

Middleware ID counter which is used to auto-generate unique middleware names when adding them to routeHandlers map.


_dispatcher​

protected _dispatcher: Dispatcher

Defined in: packages/core/src/router/AbstractRouter.ts:64

Dispatcher fires events to app.


_factory​

protected _factory: RouteFactory

Defined in: packages/core/src/router/AbstractRouter.ts:60

Factory for routes.


_host​

protected _host: string = ''

Defined in: packages/core/src/router/AbstractRouter.ts:73

The application's host.


_isSPARouted​

protected _isSPARouted: (url, action?) => boolean | undefined

Defined in: packages/core/src/router/AbstractRouter.ts:97


_languagePartPath​

protected _languagePartPath: string = ''

Defined in: packages/core/src/router/AbstractRouter.ts:82

The URL path fragment used as a suffix to the _root field that specifies the current language.


_middlewareTimeout​

protected _middlewareTimeout: number

Defined in: packages/core/src/router/AbstractRouter.ts:96


_pageManager​

protected _pageManager: PageManager

Defined in: packages/core/src/router/AbstractRouter.ts:56

The page manager handling UI rendering, and transitions between pages if at the client side.


_protocol​

protected _protocol: string = ''

Defined in: packages/core/src/router/AbstractRouter.ts:69

The current protocol used to access the application, terminated by a colon (for example https:).


_root​

protected _root: string = ''

Defined in: packages/core/src/router/AbstractRouter.ts:77

The URL path pointing to the application's root.


_routeHandlers​

protected _routeHandlers: Map<string, AbstractRoute<string | RoutePathExpression> | RouterMiddleware>

Defined in: packages/core/src/router/AbstractRouter.ts:86

Storage of all known routes and middlewares. The key are their names.

Methods​

_extractRoutePath()​

protected _extractRoutePath(path): string

Defined in: packages/core/src/router/AbstractRouter.ts:546

Strips the URL path part that points to the application's root (base URL) from the provided path.

Parameters​

path​

string

Relative or absolute URL path.

Returns​

string

URL path relative to the application's base URL.


_getCurrentlyRoutedPath()​

_getCurrentlyRoutedPath(): string

Defined in: packages/core/src/router/AbstractRouter.ts:684

Returns path that is stored in private property when a route method is called.

Returns​

string


_getMiddlewaresForRoute()​

_getMiddlewaresForRoute(routeName): RouterMiddleware[]

Defined in: packages/core/src/router/AbstractRouter.ts:662

Returns middlewares preceding given route name.

Parameters​

routeName​

string

Returns​

RouterMiddleware[]


_handle()​

_handle(route, params, options?, action?): Promise<void | UnknownParameters>

Defined in: packages/core/src/router/AbstractRouter.ts:569

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.

Parameters​

route​

AbstractRoute<string | RoutePathExpression>

The route that should have its associated controller rendered via the associated view.

params​

RouteParams

Parameters extracted from the URL path and query.

options?​

Partial<RouteOptions>

The options overrides route options defined in the routes.js configuration file.

action?​

RouteAction

An action object describing what triggered this routing.

Returns​

Promise<void | UnknownParameters>

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.


_runMiddlewares()​

_runMiddlewares(middlewares, params, locals): Promise<void | typeof MIDDLEWARE_ABORT_ROUTE>

Defined in: packages/core/src/router/AbstractRouter.ts:700

Runs provided middlewares in sequence.

Parameters​

middlewares​

Array of middlewares.

RouterMiddleware[] | undefined

params​

RouteParams

Router params that can be mutated by middlewares.

locals​

RouteLocals

The locals param is used to pass local data between middlewares.

Returns​

Promise<void | typeof MIDDLEWARE_ABORT_ROUTE>

Promise resolving to MIDDLEWARE_ABORT_ROUTE if a middleware aborted the entire routing pipeline, or void if all middlewares completed normally (including early stop via MIDDLEWARE_STOP_PROPAGATION).


add()​

add(name, pathExpression, controller, view, options?): AbstractRouter

Defined in: packages/core/src/router/AbstractRouter.ts:169

Adds a new route to router.

Parameters​

name​

string

The unique name of this route, identifying it among the rest of the routes in the application.

pathExpression​

string

A path expression specifying the URL path part matching this route (must not contain a query string), optionally containing named parameter placeholders specified as :parameterName. The name of the parameter is terminated by a forward slash (/) or the end of the path expression string. The path expression may also contain optional parameters, which are specified as :?parameterName. It is recommended to specify the optional parameters at the end of the path expression.

controller​

AsyncRouteController

The full name of Object Container alias identifying the controller associated with this route.

view​

AsyncRouteView

The full name or Object Container alias identifying the view class associated with this route.

options?​

Partial<RouteOptions>

Additional route options, specified how the navigation to the route will be handled. The onlyUpdate can be either a flag signalling whether the current controller and view instances should be kept if they match the ones used by the previous route; or a callback function that will receive the previous controller and view identifiers used in the previously matching route, and returns a boolean representing the value of the flag. This flag is disabled by default. The autoScroll flag signals whether the page should be scrolled to the top when the navigation takes place. This flag is enabled by default.

Returns​

AbstractRouter

This router.

Throws​

Thrown if a route with the same name already exists.

Overrides​

Router.add


getBaseUrl()​

getBaseUrl(): string

Defined in: packages/core/src/router/AbstractRouter.ts:245

Returns the application's absolute base URL, pointing to the public root of the application.

Returns​

string

The application's base URL.

Overrides​

Router.getBaseUrl


getCurrentRouteInfo()​

getCurrentRouteInfo(): object

Defined in: packages/core/src/router/AbstractRouter.ts:273

Returns the information about the currently active route.

Returns​

object

params​

params: RouteParams<{ }>

path​

path: string

route​

route: AbstractRoute<string | RoutePathExpression>

Throws​

Thrown if a route is not define for current path.

Overrides​

Router.getCurrentRouteInfo


getDomain()​

getDomain(): string

Defined in: packages/core/src/router/AbstractRouter.ts:252

Returns the application's domain in the following form ${protocol}//${host}.

Returns​

string

The current application's domain.

Overrides​

Router.getDomain


getHost()​

getHost(): string

Defined in: packages/core/src/router/AbstractRouter.ts:259

Returns application's host (domain and, if necessary, the port number).

Returns​

string

The current application's host.

Overrides​

Router.getHost


getPath()​

getPath(): string

Defined in: packages/core/src/router/AbstractRouter.ts:229

Returns the current path part of the current URL, including the query string (if any).

Returns​

string

The path and query parts of the current URL.

Overrides​

Router.getPath


getProtocol()​

getProtocol(): string

Defined in: packages/core/src/router/AbstractRouter.ts:266

Returns the current protocol used to access the application, terminated by a colon (for example https:).

Returns​

string

The current application protocol used to access the application.

Overrides​

Router.getProtocol


getRouteHandler()​

getRouteHandler(name): AbstractRoute<string | RoutePathExpression> | RouterMiddleware | undefined

Defined in: packages/core/src/router/AbstractRouter.ts:222

Returns specified handler from registered route handlers.

Parameters​

name​

string

The route's unique name.

Returns​

AbstractRoute<string | RoutePathExpression> | RouterMiddleware | undefined

Route with given name or undefined.

Overrides​

Router.getRouteHandler


getRouteHandlers()​

getRouteHandlers(): Map<string, AbstractRoute<string | RoutePathExpression> | RouterMiddleware>

Defined in: packages/core/src/router/AbstractRouter.ts:299

Returns​

Map<string, AbstractRoute<string | RoutePathExpression> | RouterMiddleware>

Inherit Doc​


getRouteHandlersByPath()​

getRouteHandlersByPath(path): object

Defined in: packages/core/src/router/AbstractRouter.ts:635

Returns the route matching the provided URL path part (the path may contain a query) and all middlewares preceding this route definition.

Parameters​

path​

string

The URL path.

Returns​

object

The route matching the path and middlewares preceding it or {} (empty object) if no such route exists.

middlewares​

middlewares: RouterMiddleware[]

route?​

optional route: AbstractRoute<string | RoutePathExpression>


getUrl()​

getUrl(): string

Defined in: packages/core/src/router/AbstractRouter.ts:238

Returns the current absolute URL (including protocol, host, query, etc).

Returns​

string

The current absolute URL.

Overrides​

Router.getUrl


handleError()​

handleError(params, options?, locals?): Promise<void | UnknownParameters>

Defined in: packages/core/src/router/AbstractRouter.ts:422

Handles an internal server error by responding with the appropriate "internal server error" error page.

Parameters​

params​

RouteParams

Parameters extracted from the current URL path and query.

options?​

Partial<RouteOptions>

The options overrides route options defined in the routes.js configuration file.

locals?​

RouteLocals

The locals param is used to pass local data between middlewares.

Returns​

Promise<void | UnknownParameters>

A promise resolved when the error has been handled and the response has been sent to the client, or displayed if used at the client side.

Overrides​

Router.handleError


handleNotFound()​

handleNotFound(params, options?, locals?): Promise<void | UnknownParameters>

Defined in: packages/core/src/router/AbstractRouter.ts:474

Handles a "not found" error by responding with the appropriate "not found" error page.

Parameters​

params​

RouteParams

Parameters extracted from the current URL path and query.

options?​

Partial<RouteOptions>

The options overrides route options defined in the routes.js configuration file.

locals?​

RouteLocals

The locals param is used to pass local data between middlewares.

Returns​

Promise<void | UnknownParameters>

A promise resolved when the error has been handled and the response has been sent to the client, or displayed if used at the client side.

Overrides​

Router.handleNotFound


init()​

init(config): void

Defined in: packages/core/src/router/AbstractRouter.ts:153

Initializes the router with the provided configuration.

Parameters​

config​

Router configuration. The $Protocol field must be the current protocol used to access the application, terminated by a colon (for example https:). The $Root field must specify the URL path pointing to the application's root. The $LanguagePartPath field must be the URL path fragment used as a suffix to the $Root field that specifies the current language. The $Host field must be the application's domain (and the port number if other than the default is used) in the following form: ${protocol}//${host}.

$Host​

string

$LanguagePartPath?​

string

$Protocol?​

string

$Root?​

string

Returns​

void

Overrides​

Router.init


isClientError()​

isClientError(reason): boolean

Defined in: packages/core/src/router/AbstractRouter.ts:527

Tests, if possible, whether the specified error was caused by the client's action (for example wrong URL or request encoding) or by a failure at the server side.

Parameters​

reason​

The encountered error.

Error | Error

Returns​

boolean

true if the error was caused the action of the client.

Overrides​

Router.isClientError


isRedirection()​

isRedirection(reason): boolean

Defined in: packages/core/src/router/AbstractRouter.ts:534

Tests, if possible, whether the specified error lead to redirection.

Parameters​

reason​

The encountered error.

Error | Error

Returns​

boolean

true if the error was caused the action of the redirection.

Overrides​

Router.isRedirection


link(routeName, params): string

Defined in: packages/core/src/router/AbstractRouter.ts:347

Generates an absolute URL (including protocol, domain, etc) for the specified route by substituting the route's parameter placeholders with the provided parameter values.

Parameters​

routeName​

string

The unique name of the route, identifying the route to use.

params​

RouteParams

Parameter values for the route's parameter placeholders. Extraneous parameters will be added as URL query.

Returns​

string

An absolute URL for the specified route and parameters.

Overrides​

Router.link


listen()​

abstract listen(): this

Defined in: packages/core/src/router/AbstractRouter.ts:307

Registers event listeners at the client side window object allowing the router to capture user's history (history pop state - going "back") and page (clicking links) navigation.

The router will start processing the navigation internally, handling the user's navigation to display the page related to the URL resulting from the user's action.

Note that the router will not prevent forms from being submitted to the server.

The effects of this method can be reverted with unlisten. This method has no effect at the server side.

Returns​

this

This router.

Overrides​

Router.listen


redirect()​

redirect(url, options?, action?, locals?): void

Defined in: packages/core/src/router/AbstractRouter.ts:332

Redirects the client to the specified location.

At the server side the method results in responding to the client with a redirect HTTP status code and the Location header.

At the client side the method updates the current URL by manipulating the browser history (if the target URL is at the same domain and protocol as the current one) or performs a hard redirect (if the target URL points to a different protocol or domain).

The method will result in the router handling the new URL and routing the client to the related page if the URL is set at the client side and points to the same domain and protocol.

Parameters​

url​

string

The URL to which the client should be redirected.

options?​

Partial<RouteOptions>

The options overrides route options defined in the routes.js configuration file.

action?​

RouteAction

An action object describing what triggered this routing.

locals?​

RouteLocals

The locals param is used to pass local data between middlewares.

Returns​

void

Overrides​

Router.redirect


remove()​

remove(name): AbstractRouter

Defined in: packages/core/src/router/AbstractRouter.ts:213

Removes the specified route from the router's known routes.

Parameters​

name​

string

The route's unique name, identifying the route to remove.

Returns​

AbstractRouter

This router.

Overrides​

Router.remove


route()​

route(path, options?, action?, locals?): Promise<void | UnknownParameters>

Defined in: packages/core/src/router/AbstractRouter.ts:372

Routes the application to the route matching the providing path, renders the route page and sends the result to the client.

Parameters​

path​

string

The URL path part received from the client, with optional query.

options?​

Partial<RouteOptions>

The options overrides route options defined in the routes.js configuration file.

action?​

RouteAction

An action object describing what triggered this routing.

locals?​

RouteLocals

The locals param is used to pass local data between middlewares.

Returns​

Promise<void | UnknownParameters>

A promise resolved when the error has been handled and the response has been sent to the client, or displayed if used at the client side.

Overrides​

Router.route


unlisten()​

unlisten(): this

Defined in: packages/core/src/router/AbstractRouter.ts:316

Unregisters event listeners at the client side window object allowing the router to capture user's history (history pop state - going "back") and page (clicking links) navigation.

The router will stop processing the navigation internally, handling the user's navigation to display the page related to the URL resulting from the user's action.

Note that the router will not prevent forms from being submitted to the server.

The effects of this method can be reverted with unlisten. This method has no effect at the server side.

Returns​

this

This router.

Overrides​

Router.unlisten


unlistenAll()​

unlistenAll(): AbstractRouter

Defined in: packages/core/src/router/AbstractRouter.ts:325

Handles the cleanup and unregisters all registered router listeners.

Returns​

AbstractRouter

Overrides​

Router.unlistenAll


use()​

use(middleware): AbstractRouter

Defined in: packages/core/src/router/AbstractRouter.ts:201

Adds a new middleware to router.

Parameters​

middleware​

RouterMiddleware

Middleware function accepting routeParams as a first argument, which can be mutated and locals object as second argument. This can be used to pass data between middlewares.

Returns​

AbstractRouter

This router.

Throws​

Thrown if a middleware with the same name already exists.

Overrides​

Router.use