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

v2.3.0

Published

this project is use Typescript ‘reflect-metadata’ defined koa-router

Downloads

29

Readme

koa-metarouter

  • 💡 Simplified route definition
  • 🔑 Non-invasive
  • ⚙️ Multiple router instance
  • 🔌 Extensible
  • 📦 Extremely light

中文文档

// tsconfig.json
{
  "compilerOptions": {
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true
  }
}

Example

Basic example

npm i koa-metarouter

// ./router/metaRouter.ts
import Router from "@koa/router";
import MetaRouterClass from "koa-metarouter";

const router = new Router();
const metaRouter: MetaRouterClass = new MetaRouterClass(router);
export default metaRouter
const { Controller,Get } = metaRouter
export {
    Controller,
    Get,
}

// ./router/index.ts
import metaRouter from "./metaRouter";
import "../controller/DemoController";
// or import("../controller/DemoController");


// DemoController
import { Controller, Get } from "./metaRouter";

@Controller()
export default class DemoController {
  @Get() // url: /Demo/index
  async index (): Promise<any> {}
}

// ./App.ts
import Koa from "koa";
import router from "./router";
const koa = new Koa();
koa.use(router.routes());
koa.listen(3000)

you can use change-case format default part

 // https://www.npmjs.com/package/change-case

 // https://lodash.com/docs/4.17.15#lowerCase // recommend

  npm i lodash
  // or
  npm i change-case
// ✨ this is default, you can cover it 
metaRouter.classNameFormat = (className : string) : string => {
  const reg = /controller$/i;
    className = className.replace(reg, "");
    return className;
};

// ✨ this is default, you can cover it 
metaRouter.functionNameFormat = (functionName : string) : string => {
  return functionName;
};
export default metaRouter;

You can use the following decorators in Controller

| decorators |||||| | :----: | :----: | :----: | :----: | :----: | :----: | | Post | Get | Put | Delete | Del | Patch | | Link | Unlink | Head | Options | All | |

If you want to respond to any methods, you can use All

// DemoController
import { Get, All } from "./metaRouter";
import { Context, Next } from "koa";

// ✨ Controller is necessary
@Controller({path:"/public"}, ...middleware) 
export default class DemoController {
  @Get()
  async test () : Promise<any> {}

  @Get("/stringPath")
  async stringPath () : Promise<any> {}

  // ✨ if you want defined router name
  @All({name :"requestArgument"})
  async requestArgument () : Promise<any> {}

  // ✨ if you want add middleware
  @All(middleware1,middleware2,...)
  async middleware () : Promise<any> {}
  // or
  @All({path:"/middleware"},middleware1,middleware2,...)
  async middleware () : Promise<any> {}

  // static method 
  @All()
  static async staticTest (ctx:Context,next:Next) : Promise<any> {}
}
interface MethodOptions {
  name ?: string,

  path ?: UrlPath | null | undefined
  method ?: string | Array<string>

  sensitive ?: boolean | undefined;
  strict ?: boolean | undefined;
  end ?: boolean | undefined;
  prefix ?: string | undefined;
  ignoreCaptures ?: boolean | undefined;
}

Redirect

@Controller({})
export default class DemoController {
  // ✨ default statusCode is 301
  @Redirect("/url_c")
  async url_a () : Promise<any> {}

  // ✨ if you want use code 302
  @Redirect("/url_b","/url_c",302)
  async url_b () : Promise<any> {}

  @Get()
  async url_c () : Promise<any> {}


}

✨ more example please look test file

custom usage

use MetaRouter him self

if you want realize custom http Methods, you can use like this

// ./router/metaRouter.ts
import Router from "@koa/router";
import MetaRouterClass, { RouterMethodDecorator } from "koa-metarouter";
const router = new Router({
  methods: [
    "HEAD",
    "OPTIONS",
    "GET",
    "PUT",
    "PATCH",
    "POST",
    "DELETE",
    "PURGE", // add method 这里添加自定义的方法
  ],
});

const metaRouter: MetaRouterClass = new MetaRouterClass(router);

const { All, Redirect, Post, Get, Head, Patch, Del, Delete, MetaRouter, Controller, Options, Link, Unlink, Put } = metaRouter;

// custom Method Name
const Purge: RouterMethodDecorator = (optionsOrMiddleware, ..._middleware) => {
  const { options, middleware } = MetaRouterClass.argumentsFormat(optionsOrMiddleware, ..._middleware);
  if (Array.isArray(options.method)) {
    options.method = [ ...options.method, "purge" ];
  } else if (typeof options.method === "string") {
    options.method = [ options.method, "purge" ];
  }
  return metaRouter.MetaRouter(options, ...middleware);
};

export default metaRouter;
export {
  All,
  Redirect,
  Get,
  Post,
  Del,
  Delete,
  Options,
  Link,
  Unlink,
  Put,
  MetaRouter,
  Controller,
  Head,
  Patch,
  Purge,
};

more detial please see

https://github.com/TsBoot/koa-metarouter/blob/main/test/TestController.ts