2.3.0 • Published 2 years ago

koa-metarouter v2.3.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

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
PostGetPutDeleteDelPatch
LinkUnlinkHeadOptionsAll

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

2.2.9

2 years ago

2.3.0

2 years ago

2.2.8

2 years ago

2.2.5

2 years ago

2.2.4

2 years ago

2.2.7

2 years ago

2.2.6

2 years ago

2.2.1

3 years ago

2.2.3

3 years ago

2.2.2

3 years ago

2.2.0

3 years ago

2.1.2

3 years ago

2.1.4

3 years ago

2.1.3

3 years ago

2.1.1

3 years ago

2.1.0

3 years ago

2.0.1

3 years ago

2.0.0

3 years ago

1.1.0

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago