1.2.1 • Published 6 months ago

@discordx/koa v1.2.1

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
6 months ago

📖 Introduction

This module is an extension of koa, so the internal behavior (methods, properties, ...) is the same

This library allows you to use TypeScript decorators on koa, it simplifies your code and improves the readability!

💻 Installation

Version 16.6.0 or newer of Node.js is required

npm install koa @koa/router @discordx/koa
yarn add koa @koa/router @discordx/koa

Install required types

npm install --save-dev @types/koa__router
yarn add --dev @types/koa__router

🆕 Features

  • @Router to create a router on class
  • Support multiple server in a single nodejs instance (@Api)
  • Support TSyringe
  • Support ECMAScript

📟 @Get / @Post ...

Decorators for all koa methods, @Get @Post @All @Delete @Head @Link @Unlink @Options

KOA instance available at the end of each handler's arguments

Example

@Router()
class Example {
  @Get("/")
  handle(ctx: RouterContext, next: Next, koa: Koa): Promise<Next> {
    ctx.body = "Hello world!";
    return next();
  }
}

📟 @Middleware

If you add middleware to a class, it will be executed on each route within it.

Example

function Log(ctx: RouterContext, next: Next) {
  console.log(`request: ${ctx.URL}`);
  return next();
}

function Authenticated(ctx: RouterContext, next: Next) {
  ctx.body = "unauthorized required";
  // we are not returning next, to avoid further execution
  return;
}

@Router()
@Middleware(Log) // will execute for all sub routes
class Example {
  @Get("/")
  hello(ctx: RouterContext, next: Next): Promise<Next> {
    ctx.body = "Hello world!";
    return next();
  }

  @Get("/auth")
  @Middleware(Authenticated)
  auth(ctx: RouterContext, next: Next): Promise<Next> {
    ctx.body = "Hello world!";
    return next();
  }
}

📜 Documentation

☎️ Need help?

💖 Thank you

You can support discordx by giving it a GitHub star.

1.2.1

6 months ago

1.2.0

7 months ago

1.1.2

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.0.10

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago