Action Result

KambojaJS use Http Response internally to hide complexity on unit testing, instead KambojaJS prefer an action to return ActionResult to make unit testing process require minimum use of mocks and stubs.

ActionResult is a base class with some methods to work with cookie and response type. Further more inherited class then implement detail logic about response send to client code.


class ActionResult{
    type:string
    status:number
    body:any
    header: { [key: string]: string | string[] }
    cookies: Cookie[]
}

type response type, default value text/plain status response status, default value 200 body response body, can be any value convertible to string header response header, a key value pairs of string cookies response cookie, a collection of Cookie object

View Action Result

To return view with specified rendering/template engine, KambojaJS provide ViewActionResult.

Parameters

  • model <any> Data that will be rendered by specified rendering/template engine
  • viewName <string> Name of the view file that will be rendered.

To use ViewActionResult you need to specify the views and view engine configuration on the KambojaApplication like below:

import { KambojaApplication } from "kamboja"
import * as Path from "path"

let app = new KambojaApplication(__dirname)
    .set("views", Path.join(__dirname, "view"))
    .set("view engine", "pug")
    .init()

You can use any view engine supported by Express.

import { Controller, ViewActionResult } from "kamboja"
 
export class ItemsController extends Controller {
    index() {
        return new ViewActionResult({ message:"Hello world!" })
    }
}

Above code will locate for view inside <view folder>/items/index.pug and provide { message:"Hello world!" } model to rendering engine to process.

The view name accessed using relative path with rule:

  1. If not defined will search for <view folder>/<controller>/<action>.pug
  2. If provided action name for example index, will search for index.pug in the same controller
  3. From outside the controller, full view name can be specified using items/index
import { Controller, ViewActionResult } from "kamboja"
 
export class ItemsController extends Controller {
    index() {
        return new ViewActionResult({ message:"Hello world!" })
    }

    secondIndex(){
        return new ViewActionResult({ message:"Hello world!" }, "index")
    }
}

export class OtherItemsController extends Controller {
    index() {
        return new ViewActionResult({ message:"Hello world!" }, "items/index")
    }
}

Above code showing how to access view using relative path, from example above all the 3 actions returned the same view.

Note when returning view from middleware, full view name is required, even the request interceptor used to intercept an action/controller.

JSON Action Result

To return JSON to the http response KambojaJS provide JsonActionResult

Parameters

  • body <any> Json object returned to http response
  • status <number> Response status returned to http response, default 200 if not provided
import { Controller, JsonActionResult } from "kamboja"
 
export class ItemsController extends Controller {
    index() {
        return new JsonActionResult({ message:"Hello world!" })
    }
}

Above code will return { message:"Hello world!" } JSON body and default 200 http status code to the response.

import { Controller, JsonActionResult } from "kamboja"
 
export class ItemsController extends Controller {
    index() {
        return new JsonActionResult({ message:"Invalid data" }, 400)
    }
}

Above code showing example returning json body with 400 (invalid) status code.

Note you can use JsonActionResult to create json service, but for recommended way to create JSON service is using ApiController.

File Action Result

To return file (text file, pdf, images etc) to the http response KambojaJS provide FileActionResult

Parameters

  • path <string> path of the file will be returned.
import { Controller, FileActionResult } from "kamboja"
 
export class ItemsController extends Controller {
    index() {
        return new FileActionResult("readme.txt")
    }
}

Note There is a recommended way to return static file by put them inside the public static folder www, only use FileActionResult when you return a file dynamically.

Redirect Action Result

To redirect to other url KambojaJS provide RedirectActionResult

Parameters

  • path <string> path of the redirection target
import { Controller, RedirectActionResult } from "kamboja"
 
export class ItemsController extends Controller {
    index() {
        return new RedirectActionResult("/dashboard/index")
    }
}

Download Action Result

Return a response file with attachment header. If accessed using browser it will invoke browser download dialog.

Parameters

  • path <string> File path
import { Controller, DownloadActionResult } from "kamboja"
 
export class DownloadController extends Controller {
    book() {
        return new DownloadActionResult("/content/book.pdf")
    }
}

Shorthand

All above action result has appropriate shorthand function for syntax sugar purposes

Action Result Shorthand
ViewActionResult() view()
JsonActionResult() json()
RedirectActionResult() redirect()
FileActionResult() file()
DownloadActionResult() download()

All shorthand function is just create a new instance of appropriate action result.