1.0.2 • Published 6 years ago

koa-eko v1.0.2

Weekly downloads
2
License
ISC
Repository
github
Last release
6 years ago

KoaEko

Create koa-router controllers using a decorator-based style

What is KoaEko ?

KoaEko is koa module allowing you to generate koa-router controllers using decorators

Dependencies

KoaEko was built using multiple packages:

Installation

You can install KoaEko using npm:

npm i koa-eko

Reference

EkoController

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

Decorators

EkoVersion(version: string)

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(type: RouteType, path: string, name?: string, description?: string)

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
    }
}

EkoGet(path: string, name?: string, description?: string)

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
}

EkoPost(path: string, name?: string, description?: string)

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
}

EkoPut(path: string, name?: string, description?: string)

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
}

EkoDelete(path: string, name?: string, description?: string)

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
}

Enums

RouteType

RouteType represents the verbs that KoaEko handles.

export enum RouteType {
    GET = "GET",
    POST = "POST",
    PUT = "PUT",
    DELETE = "DELETE"
}

Exemple

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();