{"id":20396855,"url":"https://github.com/tsboot/koa-metarouter","last_synced_at":"2026-01-07T08:39:51.870Z","repository":{"id":143799462,"uuid":"414886758","full_name":"TsBoot/koa-metarouter","owner":"TsBoot","description":"this project is use Decorators defined koa-router","archived":false,"fork":false,"pushed_at":"2022-06-07T10:44:28.000Z","size":138,"stargazers_count":2,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-23T09:38:15.339Z","etag":null,"topics":["koa","koa2","router","typescript"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/koa-metarouter","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/TsBoot.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-10-08T07:21:56.000Z","updated_at":"2022-11-02T02:05:05.000Z","dependencies_parsed_at":"2023-05-03T14:32:09.364Z","dependency_job_id":null,"html_url":"https://github.com/TsBoot/koa-metarouter","commit_stats":{"total_commits":36,"total_committers":2,"mean_commits":18.0,"dds":0.02777777777777779,"last_synced_commit":"5f96ccd23830fb2422aa488c50168c480d1a20bf"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TsBoot%2Fkoa-metarouter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TsBoot%2Fkoa-metarouter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TsBoot%2Fkoa-metarouter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TsBoot%2Fkoa-metarouter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/TsBoot","download_url":"https://codeload.github.com/TsBoot/koa-metarouter/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246249218,"owners_count":20747168,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["koa","koa2","router","typescript"],"created_at":"2024-11-15T04:10:02.133Z","updated_at":"2026-01-07T08:39:51.818Z","avatar_url":"https://github.com/TsBoot.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# koa-metarouter\n\n- 💡 Simplified route definition\n- 🔑 Non-invasive\n- ⚙️ Multiple router instance\n- 🔌 Extensible\n- 📦 Extremely light\n\n[中文文档](https://github.com/TsBoot/koa-metarouter/blob/main/README.zh.md)\n\n```json\n// tsconfig.json\n{\n  \"compilerOptions\": {\n    \"experimentalDecorators\": true,\n    \"emitDecoratorMetadata\": true\n  }\n}\n```\n\n# Example\n## Basic example\n\n`npm i koa-metarouter`\n\n```typescript\n// ./router/metaRouter.ts\nimport Router from \"@koa/router\"; // or koa-router\nimport MetaRouterClass from \"koa-metarouter\";\n\nconst router = new Router();\nconst metaRouter: MetaRouterClass = new MetaRouterClass(router);\nexport default metaRouter\nconst { Controller,Get } = metaRouter\nexport {\n    Controller,\n    Get,\n}\n\n// ./router/index.ts\nimport metaRouter from \"./metaRouter\";\nimport \"../controller/DemoController\";\n// or import(\"../controller/DemoController\");\nexport default metaRouter.router;\n\n// DemoController\nimport { Controller, Get } from \"./metaRouter\";\n\n@Controller()\nexport default class DemoController {\n  @Get() // url: /Demo/index\n  async index (): Promise\u003cany\u003e {}\n}\n\n// ./App.ts\nimport Koa from \"koa\";\nimport router from \"./router\";\nconst koa = new Koa();\nkoa.use(router.routes());\nkoa.listen(3000)\n```\n\n### you can use change-case format default part\n```cmd\n // https://www.npmjs.com/package/change-case\n\n // https://lodash.com/docs/4.17.15#lowerCase // recommend\n\n  npm i lodash\n  // or\n  npm i change-case\n```\n\n```typescript\n// ✨ this is default, you can cover it \nmetaRouter.classNameFormat = (className : string) : string =\u003e {\n  const reg = /controller$/i;\n    className = className.replace(reg, \"\");\n    return className;\n};\n\n// ✨ this is default, you can cover it \nmetaRouter.functionNameFormat = (functionName : string) : string =\u003e {\n  return functionName;\n};\nexport default metaRouter;\n\n```\n\n  You can use the following decorators in Controller\n\n\n| decorators ||||||\n|   :----:   |   :----:   |    :----:  |    :----: | :----:  |  :----: |\n|    Post    |     Get    |    Put     |  Delete   |   Del   |  Patch  |\n|    Link    |    Unlink  |    Head    |  Options  |   All   |         |\n\n\n  If you want to respond to any methods, you can use `All`\n\n```typescript\n// DemoController\nimport { Get, All } from \"./metaRouter\";\nimport { Context, Next } from \"koa\";\n\n// ✨ Controller is necessary\n@Controller({path:\"/public\"}, ...middleware) \nexport default class DemoController {\n  @Get()\n  async test () : Promise\u003cany\u003e {}\n\n  @Get(\"/stringPath\")\n  async stringPath () : Promise\u003cany\u003e {}\n\n  // ✨ if you want defined router name\n  @All({name :\"requestArgument\"})\n  async requestArgument () : Promise\u003cany\u003e {}\n\n  // ✨ if you want add middleware\n  @All(middleware1,middleware2,...)\n  async middleware () : Promise\u003cany\u003e {}\n  // or\n  @All({path:\"/middleware\"},middleware1,middleware2,...)\n  async middleware () : Promise\u003cany\u003e {}\n\n  // static method \n  @All()\n  static async staticTest (ctx:Context,next:Next) : Promise\u003cany\u003e {}\n}\n\n```\n\n```typescript\ninterface MethodOptions {\n  name ?: string,\n\n  path ?: UrlPath | null | undefined\n  method ?: string | Array\u003cstring\u003e\n\n  sensitive ?: boolean | undefined;\n  strict ?: boolean | undefined;\n  end ?: boolean | undefined;\n  prefix ?: string | undefined;\n  ignoreCaptures ?: boolean | undefined;\n}\n```\n\n## Redirect\n\n```typescript\n@Controller({})\nexport default class DemoController {\n  // ✨ default statusCode is 301\n  @Redirect(\"/url_c\")\n  async url_a () : Promise\u003cany\u003e {}\n\n  // ✨ if you want use code 302\n  @Redirect(\"/url_b\",\"/url_c\",302)\n  async url_b () : Promise\u003cany\u003e {}\n\n  @Get()\n  async url_c () : Promise\u003cany\u003e {}\n\n\n}\n```\n\u003e   ✨ more example please look test file\n\n## custom usage\n  use MetaRouter him self\n\n  if you want realize custom http Methods, you can use like this\n\n```typescript\n// ./router/metaRouter.ts\nimport Router from \"@koa/router\";\nimport MetaRouterClass, { RouterMethodDecorator } from \"koa-metarouter\";\nconst router = new Router({\n  methods: [\n    \"HEAD\",\n    \"OPTIONS\",\n    \"GET\",\n    \"PUT\",\n    \"PATCH\",\n    \"POST\",\n    \"DELETE\",\n    \"PURGE\", // add method 这里添加自定义的方法\n  ],\n});\n\nconst metaRouter: MetaRouterClass = new MetaRouterClass(router);\n\nconst { All, Redirect, Post, Get, Head, Patch, Del, Delete, MetaRouter, Controller, Options, Link, Unlink, Put } = metaRouter;\n\n// custom Method Name\nconst Purge: RouterMethodDecorator = (optionsOrMiddleware, ..._middleware) =\u003e {\n  const { options, middleware } = MetaRouterClass.argumentsFormat(optionsOrMiddleware, ..._middleware);\n  if (Array.isArray(options.method)) {\n    options.method = [ ...options.method, \"purge\" ];\n  } else if (typeof options.method === \"string\") {\n    options.method = [ options.method, \"purge\" ];\n  }\n  return metaRouter.MetaRouter(options, ...middleware);\n};\n\nexport default metaRouter;\nexport {\n  All,\n  Redirect,\n  Get,\n  Post,\n  Del,\n  Delete,\n  Options,\n  Link,\n  Unlink,\n  Put,\n  MetaRouter,\n  Controller,\n  Head,\n  Patch,\n  Purge,\n};\n\n\n```\n\n# more detial please see\n\n[https://github.com/TsBoot/koa-metarouter/blob/main/__test__/TestController.ts](https://github.com/TsBoot/koa-metarouter/blob/main/__test__/TestController.ts)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftsboot%2Fkoa-metarouter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftsboot%2Fkoa-metarouter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftsboot%2Fkoa-metarouter/lists"}