universal-router

  • Version 9.2.1
  • Published
  • 384 kB
  • 1 dependency
  • MIT license

Install

npm i universal-router
yarn add universal-router
pnpm add universal-router

Overview

Isomorphic router for JavaScript web applications

Index

Classes

class UniversalRouter

class UniversalRouter<R = any, C extends RouterContext = RouterContext> {}

    constructor

    constructor(routes: Routes<R, C> | Route<R, C>, options?: RouterOptions<R, C>);

      property baseUrl

      baseUrl: string;

        property options

        options: RouterOptions<R, C>;

          property root

          root: Route<R, C>;

            method resolve

            resolve: (pathnameOrContext: string | ResolveContext) => Promise<RouteResult<R>>;
            • Traverses the list of routes in the order they are defined until it finds the first route that matches provided URL path string and whose action function returns anything other than null or undefined.

            Interfaces

            interface ResolveContext

            interface ResolveContext extends RouterContext {}

              property pathname

              pathname: string;
              • URL which was transmitted to router.resolve().

              interface Route

              interface Route<R = any, C extends RouterContext = RouterContext> {}
              • A Route is a singular route in your application. It contains a path, an action function, and optional children which are an array of Route. C User context that is made union with RouterContext. R Result that every action function resolves to. If the action returns a Promise, R can be the type the Promise resolves to.

              property action

              action?: (context: RouteContext<R, C>, params: RouteParams) => RouteResult<R>;
              • Action method should return anything except null or undefined to be resolved by router otherwise router will throw Page not found error if all matched routes returned nothing.

              property children

              children?: Routes<R, C> | null;
              • An array of Route objects. Nested routes are perfect to be used in middleware routes.

              property match

              match?: MatchFunction<RouteParams>;
              • The route path match function. Used for internal caching.

              property name

              name?: string;
              • A unique string that can be used to generate the route URL.

              property parent

              parent?: Route<R, C> | null;
              • The link to the parent route is automatically populated by the router. Useful for breadcrumbs.

              property path

              path?: Path;
              • A string, array of strings, or a regular expression. Defaults to an empty string.

              interface RouteContext

              interface RouteContext<R = any, C extends RouterContext = RouterContext>
              extends ResolveContext {}

                property baseUrl

                baseUrl: string;
                • Base URL path relative to the path of the current route.

                property next

                next: (resume?: boolean) => Promise<R>;
                • Middleware style function which can continue resolving.

                property params

                params: RouteParams;
                • Matched path params.

                property path

                path: string;
                • Matched path.

                property route

                route: Route<R, C>;
                • Matched route object.

                property router

                router: UniversalRouter<R, C>;
                • Current router instance.

                interface RouteMatch

                interface RouteMatch<R = any, C extends RouterContext = RouterContext> {}

                  property baseUrl

                  baseUrl: string;

                    property params

                    params: RouteParams;

                      property path

                      path: string;

                        property route

                        route: Route<R, C>;

                          interface RouteParams

                          interface RouteParams {}
                          • Params is a key/value object that represents extracted URL parameters.

                          index signature

                          [paramName: string]: string | string[];

                            interface RouterContext

                            interface RouterContext {}
                            • In addition to a URL path string, any arbitrary data can be passed to the router.resolve() method, that becomes available inside action functions.

                            index signature

                            [propName: string]: any;

                              interface RouterOptions

                              interface RouterOptions<R = any, C extends RouterContext = RouterContext>
                              extends ParseOptions,
                              TokensToRegexpOptions,
                              RegexpToFunctionOptions {}

                                property baseUrl

                                baseUrl?: string;

                                  property context

                                  context?: C;

                                    property errorHandler

                                    errorHandler?: ErrorHandler<R>;

                                      property resolveRoute

                                      resolveRoute?: ResolveRoute<R, C>;

                                        Type Aliases

                                        type ErrorHandler

                                        type ErrorHandler<R = any> = (
                                        error: RouteError,
                                        context: ResolveContext
                                        ) => RouteResult<R>;

                                          type ResolveRoute

                                          type ResolveRoute<R = any, C extends RouterContext = RouterContext> = (
                                          context: RouteContext<R, C>,
                                          params: RouteParams
                                          ) => RouteResult<R>;

                                            type RouteError

                                            type RouteError = Error & {
                                            status?: number;
                                            };

                                              type RouteResult

                                              type RouteResult<T> = T | null | undefined | Promise<T | null | undefined>;

                                                type Routes

                                                type Routes<R = any, C extends RouterContext = RouterContext> = Array<Route<R, C>>;
                                                • Routes is an array of type Route. C User context that is made union with RouterContext. R Result that every action function resolves to. If the action returns a Promise, R can be the type the Promise resolves to.

                                                Package Files (1)

                                                Dependencies (1)

                                                Dev Dependencies (0)

                                                No dev dependencies.

                                                Peer Dependencies (0)

                                                No peer dependencies.

                                                Badge

                                                To add a badge like this onejsDocs.io badgeto your package's README, use the codes available below.

                                                You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/universal-router.

                                                • Markdown
                                                  [![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/universal-router)
                                                • HTML
                                                  <a href="https://www.jsdocs.io/package/universal-router"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>