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

v2.3.0

Published

Auto bootstrap controllers for the koa app.

Downloads

29

Readme

Koa Autoboot

npm version build status coverage

Auto bootstrap routes from a controller folder.

  • Auto build routes from controller files
  • Use the return value as response body
  • Inject ctx, body, query or custom data
  • Build-in validator for body and query base on fastest-validator
  • Log request in and out

Usage

Install

npm install koa-autoboot koa-body

Basic usage

// controllers/IndexController.ts
import Koa from 'koa'
import { Controller, Get, Ctx } from 'koa-autoboot'

@Controller('/')
export default class IndexController {
  @Get()
  @Ctx() // Inject the `ctx` as the argument.
  async greeting(ctx: Koa.Context) {
    ctx.body = 'Hello world'
  }
}
// index.ts
import path from 'path'
import Koa from 'koa'

const app = new Koa()
app.use(
  KoaAutoboot({
    dir: path.join(__dirname, 'controllers'),
  }),
)
app.listen(4000)

curl http://localhost:4000/greeting will get Hello world.

APIs

import KoaAutoboot, { Controller, Post, Route, Middleware, Body } from 'koa-autoboot'

KoaAutoboot(
  // The options
  {
    // string, the folder of controller files
    dir: __dirname,

    // string (optional), the prefix append to all routes path
    prefix: 'api',

    // (string | RegExp)[] | ((s: string) => boolean), which files should be ignore in dir.
    ignore: [],

    // function (optional), parse return value to response body.
    // If return value is `undefined`, parser will not be call.
    // Default parser:
    returnParser: (value: any) => {
      const body = { status: true, message: 'Success', data: null }

      if (value === false || value instanceof Error) {
        body.status = false
        body.message = value.message || 'Fail'
      } else {
        body.data = value
      }

      return body
    },
  },
)

interface GreetingParams {
  name: string
}

/**
 * Mark the class is a router.
 * Pass a router path (optional), the default values is className.toLowerCase().replace('controller', '')
 * For this class, the default router path is `index`
 */
@Controller('/')
// Add koa middlewares to the router.
@Middleware([cors()])
export default class IndexController {
  // Add koa middlewares to a route.
  @Middleware([cors()])

  // Add a `POST` route, pass the path (optional), default is the method name.
  @Post()

  // Add a route, pass the http methods, default is `['GET', 'POST']`.
  @Route(['PUT'])

  // Inject the `ctx.request.body` as the argument, pass the validate schema (optional)
  @Body({ name: 'string' })

  // Inject the `ctx.request.query` as the argument
  @Query()

  // Inject the `ctx` as the argument
  @Ctx()
  async greeting(ctx: Koa.Context, query: any, body: GreetingParams): Promise<string> {
    // Use return value as the response
    return `Hello, ${body.name}`
  }
}

Development

npm install
npm start

Will run the example koa server with nodemon.