- 💡 Simplified route definition
- 🔑 Non-invasive
- ⚙️ Multiple router instance
- 🔌 Extensible
- 📦 Extremely light
// tsconfig.json
{
"compilerOptions": {
"experimentalDecorators": true,
"emitDecoratorMetadata": true
}
}
npm i koa-metarouter
// ./router/metaRouter.ts
import Router from "@koa/router"; // or 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");
export default metaRouter.router;
// 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)
// 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;
}
@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
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,
};
https://github.com/TsBoot/koa-metarouter/blob/main/test/TestController.ts