Create koa-router controllers using a decorator-based style
KoaEko is koa module allowing you to generate koa-router controllers using decorators
KoaEko was built using multiple packages:
- koa: Website | GitHub | Npm
- koa-router: Website | GitHub | Npm
- sequelize: Website | GitHub | Npm
- sequelize-typescript: Website | GitHub | Npm
- reflect-metadata: Website | GitHub | Npm
You can install KoaEko using npm:
npm i koa-eko
- Classes
- Class decorators
- Method decorators
- EkoRoute(type: RouteType, path: string, name?: string, description?: string)
- EkoGet(path: string, name?: string, description?: string)
- EkoPost(path: string, name?: string, description?: string)
- EkoPut(path: string, name?: string, description?: string)
- EkoDelete(path: string, name?: string, description?: string)
- Enums
KoaEko provides a EkoController you must inherit from if you want KoaEko to work
import { EkoController } from "koa-eko";
class MyController extends EkoController {}
Once your controller inherits from EkoController, it will have access to Routes(): IMiddleware
and AllowedMethods(): IMiddleware
.
You can find more on these functions on koa-router documentation
EkoVersion allows you to set a version for a KoaEko controller
import { EkoController, EkoVersion } from "koa-eko";
@EkoVersion("1.0.0")
class MyController extends EkoController {}
If the tag is missing, the version 1.0.0 will be used by default
EkoRoute declares the attached method as a KoaEko controller route
- type (RouteType): verb of the API route (eg: RouteType.GET)
- path (string): the route relative path (eg: "/")
- name (string) optional: the route name (eg: "Get all users")
- description (string) optional: the route description (eg: "Returns all users with their personal informations")
import { IRouterContext } from "koa-router"
import { EkoController, EkoRoute, RouteType } from "koa-eko"
class UsersController extends EkoController {
public constructor() {
super();
}
@EkoRoute(RouteType.GET, "/" "Get all users", "Returns all users with their personal informations")
public async MyGetRoute(ctx: IRouterContext): Promise<void> {
// Do Stuff
}
}
Alias for EkoRoute(RouteType.GET, path, name, description)
import { IRouterContext } from "koa-router"
import { EkoGet } from "koa-eko"
@EkoGet("/" "Get all users", "Returns all users with their personal informations")
public async MyGetRoute(ctx: IRouterContext): Promise<void> {
// Do Stuff
}
Alias for EkoRoute(RouteType.POST, path, name, description)
import { IRouterContext } from "koa-router"
import { EkoPost } from "koa-eko"
@EkoPost("/" "Add an user", "Add a new user and return it")
public async MyPostRoute(ctx: IRouterContext): Promise<void> {
// Do Stuff
}
Alias for EkoRoute(RouteType.PUT, path, name, description)
import { IRouterContext } from "koa-router"
import { EkoPut } from "koa-eko"
@EkoPut("/{id}" "Patch an user", "Update user's informations")
public async MyPutRoute(ctx: IRouterContext): Promise<void> {
// Do Stuff
}
Alias for EkoRoute(RouteType.DELETE, path, name, description)
import { IRouterContext } from "koa-router"
import { EkoDelete } from "koa-eko"
@EkoDelete("/{id}" "Delete an user", "Delete an existing user")
public async MyDeleteRoute(ctx: IRouterContext): Promise<void> {
// Do Stuff
}
RouteType represents the verbs that KoaEko handles.
export enum RouteType {
GET = "GET",
POST = "POST",
PUT = "PUT",
DELETE = "DELETE"
}
Here is a controller build using KoaEko
import { IRouterContext } from "koa-router";
import { Comment } from "../../POCO/Comment";
import { EkoGet, EkoVersion } from "koa-eko";
@EkoVersion("1.0.0")
export class CommentsController extends EkoController {
public constructor() {
super();
}
@EkoGet("/", "Get all comments", "Get all comments and / or associated users")
public async GetAll(ctx: IRouterContext): Promise<void> {
let comments: Comment[] = await Comment.scope(this.getScope()).all();
ctx.body = comments;
}
}
export default new CommentsController();