npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

koa-decorative

v1.1.5

Published

Koa Decorator Based Routing

Downloads

7

Readme

Koa Decorative

KOA Decorative is a library built to simplify routing. This library is designed to route traffic through the use of class based decorators instead of a routing configuration file.

Installation

Install Koa Decorative and the underlying router that Koa Decorative works with using npm:

npm install --save koa-decorative koa-tree-router

This package requires that you enable experimental decorators in your tsconfig.json file for your project:

{
  "compilerOptions": {
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true
  }
}

Usage

index.ts

This is what a basic CRUD controller will look like:

import { Context } from 'koa';
import { Controller, Pre, Get, Post, Put, Patch, Delete } from 'koa-decorative';

// A simple request specific middleware handled before the list action
const preListAction = async (ctx: Context, next: () => Promise<any>) => {
  console.log('before the list action');
  await next();
  console.log('after the list action');
}

@Controller('/simple')
class SimpleController {
  constructor(private simpleService) { }

  @Get()
  @Pre(preListAction)
  list(ctx: Context) {
    const result = this.simpleService.list();

    ctx.body = { data: result };
  }

  @Post()
  create(ctx: Context) {
    const result = this.simpleService.create();

    ctx.body = { data: result };
  }

  @Get('/:id')
  detail(ctx: Context) {
    const result = this.simpleService.detail();

    ctx.body = { data: result };
  }

  @Put('/:id')
  @Patch('/:id')
  update() {
    const result = this.simpleService.update();

    ctx.body = { data: result };
  }

  @Delete('/:id')
  delete() {
    const result = this.simpleService.delete();

    ctx.body = { data: result };
  }
}

export default SimpleController;

Note: All class instances are singletons and not request specific. Take care not to store any request specific information in the class itself. However it is a great place to inject depencencies for the controller.

Create an index.ts in the controllers directory and initialize your controllers:

import { SimpleService } from '../services';

import SimpleController from './SimpleController';

const simpleService = new SimpleService();

new SimpleController(simpleService);

Then just build the routes in your apps main index.ts or app.ts file:

import { buildRoutes } from 'koa-decorative';

import './controllers';

const app = new Koa();

app.use(buildRoutes());

app.listen(3000);

The routes defined in the controllers will be automatically registered with the route manager. And you just need to pass the built routes to app.use to automatically build the routing for all of the initialized controllers.