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-decorator-x

v1.6.3

Published

Koa-decorator is a package that provides a set of decorators to simplify the creation and management of routes in Koa.js applications.

Downloads

122

Readme

koa-decorator

Description

Koa-decorator is a package that provides a set of decorators to simplify the creation and management of routes in Koa.js applications.

These decorators allow you to define routes, middlewares and services directly, making your code cleaner and more maintainable.

Install

For npm:

npm install koa-decorator-x

For pnpm:

pnpm install koa-decorator-x

For yarn:

yarn add koa-decorator-x

Usage

Easy run the server:

import { Application } from 'koa-decorator-x';

const app = new Application();

app.listen(3000, () => {
  console.log('server is listening 3000');
});
s;

Directory conventions

In the root directory, we have these directories by default:

.
├── controllers
│   ├── xxxController.ts
│   ├── xxxController.ts
├── middlewares
│   ├── xxx.ts
│   ├── xxx.ts
├── services
│   ├── xxx.ts
│   ├── xxx.ts
└── app.ts

And our framework will scan the files in these directories automatically. So we have to make sure these directories exists.

If you want to modify the structure of default directories. Please modify the config synchronously.

import { Application } from 'koa-decorator-x';

const app = new Application({
  controllersDir: '/src/controllers',
  middlewaresDir: '/src/middlewares',
  servicesDir: '/src/services',
  middlewares: [],
});

Define routers

We have already pre-installed @koa/router.

You do not need to define routers by using a router.get('/list', handleList).

Instead, you just need to create a controller class in controllers directory. And the use @Controller, @GET, @POST etc. The router will be register automatically.

import { Controller, GET, POST } from 'koa-decorator-x';

@Controller('/demo')
export default class DemoController {
  @GET('/list')
  public async list(ctx: Context): Promise<Result> {
    return { code: 0, data: data, msg: 'OK' };
  }

  @POST('/update')
  public async update(): Promise<Result> {
    return { code: 0, data: [], msg: 'OK' };
  }
}

Define middlewares

You just need to create a middleware class in middlewares directory. And the use @Middleware to define a middleware. The middleware will be register automatically.

import { Inject, Middleware } from 'koa-decorator-x';
import { Context, Next } from 'koa';

@Middleware()
export class ErrorHandler {
  async use(ctx: Context, next: Next) {
    try {
      console.log('Error Handler Middleware');
      await next();
    } catch (err) {
      console.log(err);
      ctx.status = 500;
      ctx.body = { code: 500, message: 'Internal Server Error', data: null };
    }
  }
}

At the same time, you can also apply third-party middlewares by registering in the Application:

import { Application } from 'koa-decorator-x';

const app = new Application({
  controllersDir: '/src/controllers',
  middlewaresDir: '/src/middlewares',
  servicesDir: '/src/services',
  middlewares: [],
});

Service & Injectable

They are the same concept and just use different name. Service is focused on some services you need to provide. Injectable just the class you want to have inject function.

First, use @Service to register a service class.

import { Service } from 'koa-decorator-x';
@Service()
export default class DemoService {
  public async index(): Promise<
    {
      id: string;
      name: string;
      gender: string;
    }[]
  > {
    return [
      { id: '11111', name: 'harry', gender: 'male' },
      { id: '22222', name: 'peter', gender: 'female' },
    ];
  }
}

And then, you can inject it to any class instances.

import Result from '../models/result';
import { Controller, GET, Inject, POST } from 'koa-decorator-x';
import DemoService from '../services/demo';
import { Context } from 'koa';

@Controller('/demo')
export default class DemoController {
  @Inject()
  demoService!: DemoService;

  @GET('/list')
  public async list(ctx: Context): Promise<Result> {
    const data = await this.demoService.index();
    return { code: 0, data: data, msg: 'OK' };
  }

  @POST('/update')
  public async update(): Promise<Result> {
    return { code: 0, data: [], msg: 'OK' };
  }
}

Injectable is the same.

import { Injectable } from 'koa-decorator-x';

@Injectable()
class DemoInjectable {
  public async index(): Promise<
    {
      id: string;
      name: string;
      gender: string;
    }[]
  > {
    return [
      { id: '11111', name: 'harry', gender: 'male' },
      { id: '22222', name: 'peter', gender: 'female' },
    ];
  }
}

export default DemoInjectable;
import { Inject, Middleware } from 'koa-decorator-x';
import { Context, Next } from 'koa';
import DemoInjectable from '../utils/demo';

@Middleware()
export class ErrorHandler {
  @Inject()
  demoInjectable!: DemoInjectable;

  async use(ctx: Context, next: Next) {
    try {
      console.log('Error Handler Middleware');
      console.log('result', await this.demoInjectable.index());
      await next();
    } catch (err) {
      console.log(err);
      ctx.status = 500;
      ctx.body = { code: 500, message: 'Internal Server Error', data: null };
    }
  }
}

Contributing

We welcome contributions!

License

This project is licensed under the MIT License. See the LICENSE file for details.