Route Decorators

KambojaJS route generation system, generate route automatcally with pattern <namespace>/<class>/<action>. Some convention that change the pattern might applied like the ApiController has different generation style.

In many cases those generation might not match what you like, you can use Route Decorator @route in this case to override the generated route.

Override route

Overriding route is quite easy, you define the new route by using @route decorator with specific http method.

import {Controller, route} from "kamboja-express"

export class UserController extends Controller {
    @route.get("/user/dashboard")
    index(){}
}

Above code will override GET /user/index into GET /user/dashboard. All action can be overridden into a new route.

Priority

@route have the most priority on the route generation system. So any method decorated with @route decorator will get the new route.

For example in the ApiController below:

import {ApiController, route} from "kamboja-express"

export class MyApiController extends ApiController {
    @route.delete("/delete/:id")
    get(id:string){}
}

By using above code, instead of having GET /myapi/:id, route generation system will generate DELETE /delete/:id

Override Only The HTTP Method

In some case if you ok with the route but not the HTTP method, you can override the method by providing @route decorator with appropriate Http method but with empty parameter

import {Controller, route} from "kamboja-express"

export class UserController extends Controller {
    @route.delete()
    index(){}
}

By using above code, you will get the default route /user/index but instead of getting GET method you will get DELETE method.

Multiple Overrides

KambojaJS allowed you to decorate an action with multiple decorators, it will resulting route generation system generate multiple route with a single http handler.

import {Controller, route} from "kamboja-express"

export class UserController extends Controller {
    @route.get("/user/dashboard")
    @route.get("/admin/dashboard")
    @route.get("/superadmin/dashboard")
    index(){}
}

Above code will generate 3 routes which all of them will be handled by the index action.

Class Name Override

Route Decorator enable you to only override the name of the class to anything you like like, you can use @route.root() to do that.

import {Controller, route} from "kamboja-express"

@route.root("/customer")
export class UserController extends Controller {
    index(){}
}

Above code will override ony the controller name

GET /customer/index

Notice that the action name and the HTTP Method is remind the same.

Further more you can add parameter to the @route.root() decorator, its mean that all generated routes related to its actions will have appropriate parameter.

import {Controller, route} from "kamboja-express"

@route.root("/customer/:categoryId")
export class UserController extends Controller {
    index(categoryId:number){}

    @route.post()
    save(categoryId:number){}    
}

Above code will generate 2 routes:

GET /customer/:categoryId/index
POST /customer/:categoryId/save

This behavior useful when you working on nested resource in Restful API

Absolute & Relative

Route Decorator differentiate between absolute and relative route provided in parameter. This behavior resulting different kind of route that will be generated. Absolute route is a route that start with / character, relative route doesn’t have / character in its start.

Some rule applied while generating route with absolute/relative parameter

  1. Absolute: if applied to an action, all its parents (namespace/class) name/overrides is ignored. if applied in a class action name/override is used, but all namespace name/overrides is ignored.
  2. Relative: relative work only to change appropriate place its decorate. For example if its decorate an action, only the action will be changed.

examples:

import {Controller, route} from "kamboja-express"

export namespace Staff
    export class UserController extends Controller {
        @route.get("all")
        getAllOfThem(){}
    }
}

Result:

GET /staff/user/all
import {Controller, route} from "kamboja-express"

export namespace Staff
    @route.root("admin")
    export class UserController extends Controller {
        getAllOfThem(){}
    }
}

Result:

GET /staff/admin/getallofthem
import {Controller, route} from "kamboja-express"

export namespace Staff
    @route.root("admin")
    export class UserController extends Controller {
        @route.get("/admin/all")
        getAllOfThem(){}
    }
}

Result:

GET /admin/all

Notice the @route.root() and the namespace is ignored

import {Controller, route} from "kamboja-express"

export namespace Staff
    @route.root("/admin")
    export class UserController extends Controller {
        @route.get("all")
        getAllOfThem(){}
    }
}

Result:

GET /admin/all

Notice above using combination of absolute and relative route. staff namespace is ignored because the @route.root() using absolute parameter. But the getAllOfThem method override is still used.