2.0.3 • Published 4 years ago

koa-router-route-generator v2.0.3

Weekly downloads
1
License
MIT
Repository
github
Last release
4 years ago

koa-router-route-generator

A tiny utility for defining koa-router routes in a simpler way, while having the ability to combine multiple routes.

Installation

$ npm install koa-router-route-generator

Whats new in v2.0.0?

Added nested route and route naming support. See below for more information.

Usage

app.js

const Koa = require('koa');
const router = require('./router');

const app = new Koa();

app
  .use(router.routes())
  .use(router.allowedMethods());

router.js

const Router = require('@koa/router');
const routeGenerator = require('koa-router-route-generator');


const dogRouter = {
  get: {
    '/dogs/:name': async ctx => {
      ctx.body = `${ctx.params.name} is a good dog.`;
    }
  },
  post: {
    '/dogs': [
      async (ctx, next) => {
        ctx.body = 'Hey';
        await next();
      },
      async ctx => console.log('You!');
    ]
  }
};

const catRouter = {
  get: {
    '/cats': {
      name: 'cats',
      handler: ctx => ctx.body = 'Cats!',
      children: {
        '/are-cute': [
          ctx => ctx.state.cats = 'Cats!',
          ctx => ctx.body = 'Cats are cute!'
        ],
        '/deeper': {
          // The paths that doesn't have 'handler'
          // will not be extracted as seperate routes!
          name: 'deeper',
          children: {
            '/child': ctx => ctx.body = 'Deeper Childs!'
          }
        }
      }
    }
  }
};

const router = new Router();

routeGenerator(router, [
  dogRouter,
  catRouter
]);

module.exports = router;

API

Route Defenition

ParamTypeDescription
methodStringAllowed method (get, post, etc.).
pathStringRoute path (all valid paths in koa-router).
handlerfunction | function[]Route Middleware(s).
nameString?name parameter in koa-router.
childrenRouteRoute object as itself.
{
  method: {
    path: handler,
    // or
    objectPath: {
      name: pathName,
      hander: handler,
      children: children
    }
    // More paths here!
  },
  // More methods here!
}

Binding Routes to koa-router Instance

ParamTypeDescription
routerRouterkoa-router instance.
routesObject[] | ObjectAn array of routes or a single route.
routeGenerator(router, routes)