1.5 Routing

Router and Logger built-in services are injectable in any module (as long as they are provided in root module) so we can inject Router into module.

Routes can be static and dynamic

Example of static route:

this.router.addRules([
 {
   methods: [Methods.GET],
   route: "home/id",
   url: "/<id:(\\d+)>/<name:(\\w+)>"
 }
]);

Route definition pattern:

Route: “<module>/<controller>/<action>” 
Route: “<controller>/<action>” which points to root module.

Route url pattern is far more complex since javascript regex don't support regex capture pattern we had to implement our own pattern rewrite in order to support it:

"/home<any>controller/<name:\\w+>/how<many:\\d+>complex-this<can:(\\w+)>/<be:([A-Za-z0-9]+)>"

will match url:

"/home-anymatch-controller/myname/how0000complex-thisisnow/01b2xa3xid"

variables accessable via @Param

@Param("name") // => myname
@Param("many") // => 0000
@Param("can") // => isnow
@Param("be") // => 01b2xa3xid

Each url pattern is wrapped in exact match pattern ^yourpattern$.

"<be:([A-Za-z0-9]+)>" => "^([A-Za-z0-9]+)$"

On route home/id (above in addRules) we can see that we declared two parameters id and name we can access them in controller action via @Param(“id”) or @Param(“name”) or via injected @Inject(Request) reflection via request.getParam() method.

On RouteRule level you have access to any your module service which means you can inject data store connection , any service you need in order to fetch your route and resolve it as you want.

Dynamic route must implement Route interface which have parseRequest and createUrl methods.

All you have to do to enable dynamic router is to assign it to router at module level via:

this.router.addRule(DynamicRouteRule);

And router will be able to register your dynamic route to system.

Example of dynamic routing:

import {Route, Injectable, IResolvedRoute, Inject, Methods, Headers} from "typeix";
import {InMemoryCache} from "./in-memory-cache";
/**
 * Dynamic route rule
 * @constructor
 * @function
 * @name DynamicRouteRule
 *
 * @description
 * Here we can define dynamic route rule which has to implement Route
 */
@Injectable()
export class DynamicRouteRule implements Route {

    @Inject(InMemoryCache)
    cache: InMemoryCache;

    /**
     * Dynamic parse request example
     * @param pathName
     * @param method
     * @param headers
     * @returns {Promise<{method: Methods, params: {}, route: string}>}
     */
    parseRequest(
        pathName: string,
        method: string,
        headers: Headers
    ): Promise<IResolvedRoute|boolean> {
        return Promise.resolve(
            {
                method: Methods.GET,
                params: {
                    pathName,
                    method,
                    headers
                },
                route: "core/not_found"
            }
        );
    }
    /**
     * Create url pattern
     * @param routeName
     * @param params
     * @returns {undefined}
     */
    createUrl(
        routeName: string,
        params: Object):
    Promise<string|boolean> {
        return null;
    }

}

results matching ""

    No results matching ""