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-dec-router

v0.0.4

Published

An ES6 decorator + class based router, support inherit, override, priority, auto load controllers, etc.

Downloads

422

Readme

koa-dec-router

  • An ES6 decorator + class based router, support inherit, override, priority, auto load controllers, etc.
  • Using koa-router under the hood.
  • Examples

Build Status npm npm

Install

npm i koa-dec-router

or

yarn add koa-dec-router

Demo

app.js


import Koa from 'koa'
import DecRouter from 'koa-dec-router'

const decRouter = DecRouter({
  controllersDir: `${__dirname}/controllers`,
  before: null, // global middleware
  after: null, // global middleware
})

const app = new Koa()

// decRouter.router: `koa-router` instance
app.use(decRouter.router.routes())
app.use(decRouter.router.allowedMethods())

controllers/api.js

import { controller, get, post } from 'dec-router'

async function apiHandler(ctx, next) {
  console.log('handle all api and subclass\'s')
  await next()
}

@controller('/api', apiHandler)
export default class Api {
  async getApiCommon(ctx) {
    // ...
    return // some common data
  }
}

controllers/posts.js


import { controller, get, post } from 'dec-router'
import Api from './api'

async function postHandler(ctx, next) {
  console.log('handle post')
  await next()
}

// define multi controller class in one file. You can passing {expose: false} to disable exposing this controller, which can still be inherit.
@controller('/subpost')
export class Subpost {
  @get('s')
  async list(ctx) {
    ctx.body = 'get subpost'
  }

}

@controller('/post')
export default class Post extends Api {

  @get('s') // final path = parent controller path + controller path + method path
  async list(ctx) {
    const commonData = await super.getApiCommon()
    ctx.body = 'get posts'
  }

  @get('/:id', {priority: -1}) // wildcard using low priority, let `special` method handle first
  async get(ctx) {
    ctx.body = 'get' + ctx.params.id
  }

  @get('/special')
  async special(ctx) {
    ctx.body = 'special post'
  }
}

Console output

To output all routes generated by dec-router, you can run your app like

DEBUG=dec-router,your-app:* node ./your-app.js

For windows, using cross-env

cross-env DEBUG=dec-router,your-app:* node ./your-app.js

See more about DEBUG

API Reference

DecRouter(options)

  • options: {object}
    • controllersDir: {string} controllers directory
    • before: {function} optional, first middleware for all controller methods
    • after: {function} optional, last middleware for all controller methods (before controller method)
    • autoLoadControllers: {bool} optional, default is true

@controller(path, opts, ...middlewares)

Controller decorator.

  • path: {string} optional, path of this controller, default is '/' + slug(cls.name), can be inherited
  • opts: {object} optional, options, cannot be inherited
    • ignoreParentPath: {bool} optional, default is false
    • ignoreParentMdws: {bool} optional, default is false
    • expose: {bool} optional, expose as a route default is false
  • middlewares: {Array} optional, koa middlewares, can be inherited

@route(method, path, opts, ...middlewares)

Controller method decorator, default would totally override superclass's method with same path (not same name), including method, opts, middlewares, etc.

  • method: {string}, one of 'get', 'head', 'post', 'put', 'delete', 'patch', 'use';
  • path: {string} optional, path of this method, default is '/' + slug(method.name)
  • opts: {object} optional, options
    • priority: {number} optional, larger before smaller, default is 0
    • ignoreCtrlPath: {bool} optional, ignore all controller path, including superclass's, default is false
  • middlewares: {Array} optional, koa middlewares

@get(path, opts, ..middlewares)

alias of @route('get', path, opts, ...middlewares)

@head(path, opts, ..middlewares)

alias of @route('head', path, opts, ...middlewares)

@post(path, opts, ..middlewares)

alias of @route('post', path, opts, ...middlewares)

@put(path, opts, ..middlewares)

alias of @route('put', path, opts, ...middlewares)

@del(path, opts, ..middlewares)

alias of @route('delete', path, opts, ...middlewares)

@patch(path, opts, ..middlewares)

alias of @route('patch', path, opts, ...middlewares)

@all(path, opts, ..middlewares)

alias of @route('use', path, opts, ...middlewares)