Route
Static Member Summary
Static Public Members | ||
public static |
Delete(params: ParamsMethodDecorator): Decorator: * mounts the tagged function as a DELETE route. |
|
public static |
Get(params: ParamsMethodDecorator): Decorator: * mounts the tagged function as a GET route. |
|
public static |
Patch(params: ParamsMethodDecorator): Decorator: * mounts the tagged function as a PATCH route. |
|
public static |
Post(params: ParamsMethodDecorator): Decorator: * mounts the tagged function as a POST route. |
|
public static |
Put(params: ParamsMethodDecorator): Decorator: * mounts the tagged function as a PUT route. |
|
public static |
Route(params: ParamsClassDecorator): Decorator: * 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 |
constructor(params: RouteParams) |
Member Summary
Public Members | ||
public |
an array composed of all the availble routes in the application |
version 1.0.0 |
public |
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 |
sendNoContent(ctx: KoaContext): 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.
public static Get(params: ParamsMethodDecorator): Decorator: * source
mounts the tagged function as a GET route.
public static Patch(params: ParamsMethodDecorator): Decorator: * source
mounts the tagged function as a PATCH route.
public static Post(params: ParamsMethodDecorator): Decorator: * source
mounts the tagged function as a POST route.
public static Put(params: ParamsMethodDecorator): Decorator: * source
mounts the tagged function as a PUT route.
public static Route(params: ParamsClassDecorator): Decorator: * source
used to set some parameters on an entire class.The supported parameters are middlewares, disable, and routeBase.
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:
Name | Type | Attribute | Description |
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 disable: boolean source
whether the route should be disabled. disabled routes cannot be called.
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:
Name | Type | Attribute | Description |
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:
Name | Type | Attribute | Description |
condition | boolean | if set to false; assert will fail and throw. |
|
status | number | the error's HTTP status StatusCode |
|
error | string | object |
|
the error(s) to be yielded by the request |
translate | boolean | indicates whether the message should be translated or not |
Return:
Throw:
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:
Name | Type | Attribute | Description |
condition | boolean | if set to false; assert will fail and throw. |
|
error | string | object |
|
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:
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:
Name | Type | Attribute | Description |
condition | boolean | if set to false; assert will fail and throw. |
|
error | string | object |
|
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:
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:
Name | Type | Attribute | Description |
condition | boolean | if set to false; assert will fail and throw. |
|
error | string | object |
|
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:
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:
Name | Type | Attribute | Description |
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:
Name | Type | Attribute | Description |
ctx | KoaContext | koa's context object |
|
original | boolean |
|
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:
Name | Type | Attribute | Description |
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:
Name | Type | Attribute | Description |
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:
Name | Type | Attribute | Description |
ctx | KoaContext | koa's context object |
|
status | number |
|
the HTTP status code to end the request with |
data | * |
|
the data to be yielded by the requests |
message | string |
|
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:
Name | Type | Attribute | Description |
ctx | KoaContext | koa's context object |
|
data | * |
|
the data to be yielded by the requests |
message | string |
|
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:
Name | Type | Attribute | Description |
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:
Name | Type | Attribute | Description |
ctx | KoaContext | koa's context object |
|
data | * |
|
the data to be yielded by the requests |
message | string |
|
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:
Name | Type | Attribute | Description |
status | number | the error's HTTP status StatusCode |
|
error | string | object |
|
the error(s) to be yielded by the request |
translate | boolean | indicates whether the message should be translated or not |
Return:
Throw:
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:
Name | Type | Attribute | Description |
error | string | object |
|
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:
Name | Type | Attribute | Description |
error | string | object |
|
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:
Name | Type | Attribute | Description |
error | string | object |
|
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:
Name | Type | Attribute | Description |
error | string | object |
|
the error(s) to be yielded by the request, default to "Unauthorized" |
translate | boolean | indicates whether the message should be translated or not |