Home Manual Reference Source
import Route from 'koa-smart/src/routes/Route.js'
public class | source

Route

Static Member Summary

Static Public Members
public static

mounts the tagged function as a DELETE route.

public static

mounts the tagged function as a GET route.

public static

mounts the tagged function as a PATCH route.

public static

mounts the tagged function as a POST route.

public static

mounts the tagged function as a PUT route.

public static

used to set some parameters on an entire class.The supported parameters are middlewares, disable, and routeBase.

public static
public static

displayLog: boolean

if true it will log which route are mount and which are not

Constructor Summary

Public Constructor
public

Member Summary

Public Members
public

an array composed of all the availble routes in the application

version 1.0.0
public

app: Koa

the main Koa application

version 1.0.0
public

disable: boolean

whether the route should be disabled.

public

koaRouter: KoaRouter

the underlying koa router for this particular route

public

middlewares: function[]

the route's registered middlewares

public

model: Model | undefined

the route's own model

public

models: Model[]

an array of all the models available in the application

version 1.0.0
public

prefix: string

the route's prefix

version 1.0.0

Method Summary

Public Methods
public

addRateLimit(middlewares: function[], params: {options: {rateLimit: Object, routePath: string, type: string}})

if a decorator has a rateLimit property, it will add the rate limiting mechanism to the route, with a unique ID for each route in order to differentiate the various routes.

version 1.0.0
public

assert(condition: boolean, status: number, error: string | object, translate: boolean):

checks a condition.

public

assertBadRequest(condition: boolean, error: string | object, translate: boolean):

same as assert, but automatically sets the status to 400 BAD REQUEST

version 2.0.0
public

assertForbidden(condition: boolean, error: string | object, translate: boolean):

same as assert, but automatically sets the status to 403 FORBIDDEN

version 2.0.0
public

assertUnauthorized(condition: boolean, error: string | object, translate: boolean):

same as assert, but automatically sets the status to 401 UNAUTHORIZED

version 2.0.0
public

async beforeRoute(ctx: KoaContext, params: BeforeRouteParams, next: function)

a member which can be overriden, which will always be executed before the route is accessed

public

body(ctx: KoaContext, original: boolean): *

retrieves the context's body, if the request has one.

version 1.0.0
public

log(str: *, args: ...*)

logs a message, but only if the route's logs are set to be displayed.

accepts several parameters

public

mount()

Registers the route and makes it callable once the API is launched.

version 1.0.0
public

queryParam(ctx: KoaContext): Object<string, *>

retrieves the query params in a GET request

version 1.0.0
public

send(ctx: KoaContext, status: number, data: *, message: string):

sets the response's body (with a message + data field) and status.

version 1.0.0
public

sendCreated(ctx: KoaContext, data: *, message: string):

same as send, but automatically sets the status to 201 CREATED

version 1.0.0
public

replies with an empty body, yielding 204 NO CONTENT as the status

version 1.0.0
public

sendOk(ctx: KoaContext, data: *, message: string):

same as send, but automatically sets the status to 200 OK

version 1.0.0
public

throw(status: number, error: string | object, translate: boolean):

throws a formated error to be caught.

version 1.0.0
public

throwBadRequest(error: string | object, translate: boolean):

same as throw, but automatically sets the status to 400 BAD REQUEST

version 2.0.0
public

throwForbidden(error: string | object, translate: boolean):

same as throw, but automatically sets the status to 403 FORBIDDEN

version 2.0.0
public

throwNotFound(error: string | object, translate: boolean):

same as throw, but automatically sets the status to 404 NOT FOUND

version 2.0.0
public

throwUnauthorized(error: string | object, translate: boolean):

same as throw, but automatically sets the status to 401 UNAUTHORIZED

version 2.0.0

Static Public Members

public static Delete(params: ParamsMethodDecorator): Decorator: * source

mounts the tagged function as a DELETE route.

Return:

Decorator

public static Get(params: ParamsMethodDecorator): Decorator: * source

mounts the tagged function as a GET route.

Return:

Decorator

public static Patch(params: ParamsMethodDecorator): Decorator: * source

mounts the tagged function as a PATCH route.

Return:

Decorator

public static Post(params: ParamsMethodDecorator): Decorator: * source

mounts the tagged function as a POST route.

Return:

Decorator

public static Put(params: ParamsMethodDecorator): Decorator: * source

mounts the tagged function as a PUT route.

Return:

Decorator

public static Route(params: ParamsClassDecorator): Decorator: * source

used to set some parameters on an entire class.The supported parameters are middlewares, disable, and routeBase.

Return:

Decorator

public static StatusCode: StatusCode source

public static displayLog: boolean source

if true it will log which route are mount and which are not

Public Constructors

public constructor(params: RouteParams) source

Params:

NameTypeAttributeDescription
params RouteParams

the route's parameters

Public Members

public allRoutesInstance: Route[] version 1.0.0 source

an array composed of all the availble routes in the application

public app: Koa version 1.0.0 source

the main Koa application

public disable: boolean source

whether the route should be disabled. disabled routes cannot be called.

public koaRouter: KoaRouter source

the underlying koa router for this particular route

public middlewares: function[] source

the route's registered middlewares

public model: Model | undefined source

the route's own model

public models: Model[] version 1.0.0 source

an array of all the models available in the application

public prefix: string version 1.0.0 source

the route's prefix

Public Methods

public addRateLimit(middlewares: function[], params: {options: {rateLimit: Object, routePath: string, type: string}}) version 1.0.0 source

if a decorator has a rateLimit property, it will add the rate limiting mechanism to the route, with a unique ID for each route in order to differentiate the various routes.

You should not need to call this method directly.

Params:

NameTypeAttributeDescription
middlewares function[]

the array of currently registered middlewares for the given route

params {options: {rateLimit: Object, routePath: string, type: string}}

the route's parameters

public assert(condition: boolean, status: number, error: string | object, translate: boolean): source

checks a condition. If it evaluates to false, throws a formated error to be caught.

Params:

NameTypeAttributeDescription
condition boolean

if set to false; assert will fail and throw.

status number

the error's HTTP status StatusCode

error string | object
  • optional

the error(s) to be yielded by the request

translate boolean

indicates whether the message should be translated or not

Return:

Throw:

ErrorApp

thrown error, should the assert fail.

public assertBadRequest(condition: boolean, error: string | object, translate: boolean): version 2.0.0 source

same as assert, but automatically sets the status to 400 BAD REQUEST

Params:

NameTypeAttributeDescription
condition boolean

if set to false; assert will fail and throw.

error string | object
  • optional

the error(s) to be yielded by the request, default to "Bad request"

translate boolean

indicates whether the message should be translated or not

Return:

Throw:

ErrorApp

thrown error, should the assert fail.

public assertForbidden(condition: boolean, error: string | object, translate: boolean): version 2.0.0 source

same as assert, but automatically sets the status to 403 FORBIDDEN

Params:

NameTypeAttributeDescription
condition boolean

if set to false; assert will fail and throw.

error string | object
  • optional

the error(s) to be yielded by the request, default to "Forbidden"

translate boolean

indicates whether the message should be translated or not

Return:

Throw:

ErrorApp

thrown error, should the assert fail.

public assertUnauthorized(condition: boolean, error: string | object, translate: boolean): version 2.0.0 source

same as assert, but automatically sets the status to 401 UNAUTHORIZED

Params:

NameTypeAttributeDescription
condition boolean

if set to false; assert will fail and throw.

error string | object
  • optional

the error(s) to be yielded by the request, default to "Unauthorized"

translate boolean

indicates whether the message should be translated or not

Return:

Throw:

ErrorApp

thrown error, should the assert fail.

public async beforeRoute(ctx: KoaContext, params: BeforeRouteParams, next: function) source

a member which can be overriden, which will always be executed before the route is accessed

Params:

NameTypeAttributeDescription
ctx KoaContext

Koa's context object

params BeforeRouteParams

an object containing all route parameters

next function

the next middleware in the chain

public body(ctx: KoaContext, original: boolean): * version 1.0.0 source

retrieves the context's body, if the request has one.

Params:

NameTypeAttributeDescription
ctx KoaContext

koa's context object

original boolean
  • optional
  • default: false

if set to true, the function will return the body before it is filtered by the param decorator. otherwise, it will return the filtered and transformed body.

Return:

*

public log(str: *, args: ...*) source

logs a message, but only if the route's logs are set to be displayed.

accepts several parameters

Params:

NameTypeAttributeDescription
str *
args ...*

public mount() version 1.0.0 source

Registers the route and makes it callable once the API is launched. the route will be called along with the middlewares that were registered in the decorator.

  you will usually not need to call this method yourself.

public queryParam(ctx: KoaContext): Object<string, *> version 1.0.0 source

retrieves the query params in a GET request

Params:

NameTypeAttributeDescription
ctx KoaContext

koa's context object

Return:

Object<string, *>

public send(ctx: KoaContext, status: number, data: *, message: string): version 1.0.0 source

sets the response's body (with a message + data field) and status.

Params:

NameTypeAttributeDescription
ctx KoaContext

koa's context object

status number
  • optional

the HTTP status code to end the request with

data *
  • optional

the data to be yielded by the requests

message string
  • optional

the message to be yielded by the request

Return:

public sendCreated(ctx: KoaContext, data: *, message: string): version 1.0.0 source

same as send, but automatically sets the status to 201 CREATED

Params:

NameTypeAttributeDescription
ctx KoaContext

koa's context object

data *
  • optional

the data to be yielded by the requests

message string
  • optional

the message to be yielded by the request

Return:

public sendNoContent(ctx: KoaContext): version 1.0.0 source

replies with an empty body, yielding 204 NO CONTENT as the status

Params:

NameTypeAttributeDescription
ctx KoaContext

koa's context object

Return:

public sendOk(ctx: KoaContext, data: *, message: string): version 1.0.0 source

same as send, but automatically sets the status to 200 OK

Params:

NameTypeAttributeDescription
ctx KoaContext

koa's context object

data *
  • optional

the data to be yielded by the requests

message string
  • optional

the message to be yielded by the request

Return:

public throw(status: number, error: string | object, translate: boolean): version 1.0.0 source

throws a formated error to be caught.

Params:

NameTypeAttributeDescription
status number

the error's HTTP status StatusCode

error string | object
  • optional

the error(s) to be yielded by the request

translate boolean

indicates whether the message should be translated or not

Return:

Throw:

ErrorApp

thrown error.

public throwBadRequest(error: string | object, translate: boolean): version 2.0.0 source

same as throw, but automatically sets the status to 400 BAD REQUEST

Params:

NameTypeAttributeDescription
error string | object
  • optional

the error(s) to be yielded by the request, default to "Bad request"

translate boolean

indicates whether the message should be translated or not

Return:

public throwForbidden(error: string | object, translate: boolean): version 2.0.0 source

same as throw, but automatically sets the status to 403 FORBIDDEN

Params:

NameTypeAttributeDescription
error string | object
  • optional

the error(s) to be yielded by the request, default to "Forbidden"

translate boolean

indicates whether the message should be translated or not

Return:

public throwNotFound(error: string | object, translate: boolean): version 2.0.0 source

same as throw, but automatically sets the status to 404 NOT FOUND

Params:

NameTypeAttributeDescription
error string | object
  • optional

the error(s) to be yielded by the request, default to "Not found"

translate boolean

indicates whether the message should be translated or not

Return:

public throwUnauthorized(error: string | object, translate: boolean): version 2.0.0 source

same as throw, but automatically sets the status to 401 UNAUTHORIZED

Params:

NameTypeAttributeDescription
error string | object
  • optional

the error(s) to be yielded by the request, default to "Unauthorized"

translate boolean

indicates whether the message should be translated or not

Return: