{"id":41893623,"url":"https://github.com/epeejs/routing-controllers-mark","last_synced_at":"2026-01-25T14:14:52.827Z","repository":{"id":45053324,"uuid":"424139678","full_name":"epeejs/routing-controllers-mark","owner":"epeejs","description":"适用于 routing-controllers 的自定义注解拦截功能","archived":false,"fork":false,"pushed_at":"2022-02-08T07:23:59.000Z","size":178,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-10-08T08:55:09.823Z","etag":null,"topics":["aop","koa","routing-controllers"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/epeejs.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-11-03T08:10:29.000Z","updated_at":"2022-12-05T15:24:06.000Z","dependencies_parsed_at":"2022-08-29T21:51:40.066Z","dependency_job_id":null,"html_url":"https://github.com/epeejs/routing-controllers-mark","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/epeejs/routing-controllers-mark","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/epeejs%2Frouting-controllers-mark","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/epeejs%2Frouting-controllers-mark/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/epeejs%2Frouting-controllers-mark/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/epeejs%2Frouting-controllers-mark/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/epeejs","download_url":"https://codeload.github.com/epeejs/routing-controllers-mark/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/epeejs%2Frouting-controllers-mark/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28754159,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-25T13:59:49.818Z","status":"ssl_error","status_checked_at":"2026-01-25T13:59:33.728Z","response_time":113,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["aop","koa","routing-controllers"],"created_at":"2026-01-25T14:14:52.262Z","updated_at":"2026-01-25T14:14:52.793Z","avatar_url":"https://github.com/epeejs.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# routing-controllers-mark\n\n为 [routing-controllers](https://github.com/typestack/routing-controllers) 提供自定义标记拦截能力，实现更加强大易用的切面编程\n\n\u003e 背景：对于一些需要对部分或全局做接口拦截的需求（如鉴权、验签等），常用做法是使用中间件，通过 UseBefore 或 UseAfter 局部或全局使用，但往往对于需要在中间件内读取路由元数据或部分接口不拦截时则无法直接实现\n\u003e\n\u003e 解决方案: 自定义标记+拦截\n\n## 安装\n\n```sh\nyarn add @epeejs/routing-controllers-mark\n```\n\n## 特性\n\n- 提供按组创建标记能力\n- 支持正选与反选注解\n- 拦截函数支持读取路由元数据能力\n\n## 用法\n\n### step1: 创建标记\n\n```ts\nimport { createMark } from '@epeejs/routing-controllers-mark';\n\n// 日志场景\nconst {\n  Mark: Log,\n  RemoveMark: NoLog,\n  MarkMiddleware: LogMiddleware,\n} = createMark({\n  // 拦截函数，当请求接口被标记时执行\n  action(context, route) {\n    console.log(context, route);\n  },\n});\n\n// 可以创建多组，互不影响\n// 验签场景\nconst {\n  Mark: Sign,\n  RemoveMark: NoSign,\n  MarkMiddleware: SignVerifyMiddleware,\n} = createMark({\n  action(context, route) {\n    // ...sign verify\n    // if (!paas) {\n    //   throw new BadRequestError();\n    // }\n  },\n});\n```\n\n### step2: 使用中间件（routing-controllers \u003e= 0.8 版本时不需要这步）\n\n```ts\nuseKoaServer(app, {\n  middlewares: [LogMiddleware],\n});\n```\n\n### step3: 标记需要拦截的接口\n\n```ts\n@Log()\n@Service()\n@JsonController('/posts')\nexport class PostController {\n  constructor(private postRepository: PostRepository) {}\n\n  // 反选\n  @NoLog\n  @Get('/')\n  all() {\n    return this.postRepository.findAll();\n  }\n\n  @Get('/:id')\n  one(@Param('id') id: number) {\n    return this.postRepository.findOne(id);\n  }\n}\n```\n\n## 原理\n\n在使用注解时，会收集相关元数据，在中间件内映射当前请求到对应路由元数据，实现拦截能力\n\n## 更多用法\n\n```ts\nimport { createMark } from '@epeejs/routing-controllers-mark';\n\n// 基于角色鉴权\nconst {\n  Mark: Role,\n  RemoveMark: RemoveRole,\n  MarkMiddleware: AuthorityMiddleware,\n} = createMark({\n  action(context, route) {\n    // action 上标记的内容\n    const { action: role } = route.markContent;\n    // ...role verify\n    // if (!paas) {\n    //   throw new BadRequestError();\n    // }\n  },\n});\n\n@Service()\n@JsonController('/posts')\nexport class PostController {\n  constructor(private postRepository: PostRepository) {}\n\n  // 仅是 admin 角色允许访问\n  @Role('admin')\n  @Get('/')\n  all() {\n    return this.postRepository.findAll();\n  }\n}\n```\n\n## API\n\n### function createMark(options)\n\n#### options.action\n\n拦截函数，当请求接口被标记时执行\n\n##### 定义\n\n```ts\n(context: Context, route: MarkRoute) =\u003e Promise\u003cany\u003e | void\n```\n\n##### 参数\n\n- context：koa 上下文对象\n- route：当前请求路由元数据\n\n```ts\nexport interface MarkRoute {\n  /** 接口控制器元数据 */\n  controller: ControllerMetadataArgs;\n  /** 接口方法元数据 */\n  action: ActionMetadataArgs;\n  /** 接口参数元数据 */\n  params: ParamMetadataArgs[];\n  /** 接口标记内容 */\n  markContent: MarkContent;\n}\nexport interface MarkContent\u003cC = any, A = any\u003e {\n  /** 标记在 controller 上的内容 */\n  controller?: C;\n  /** 标记在 action 上的内容 */\n  action?: A;\n}\n```\n\n#### 返回值\n\n```ts\nexport type MarkType = {\n  /**\n   * 给 action 或 controller 打上标记，标记 controller 时等于标记所有 action\n   * @param content 标记信息\n   */\n  Mark: (content?: any) =\u003e ClassDecorator \u0026 MethodDecorator;\n  /** 只能应用于 action ，用于部分排除*/\n  RemoveMark: MethodDecorator;\n  /** 拦截该标记的中间件 */\n  MarkMiddleware: ClassType\u003cKoaMiddlewareInterface\u003e;\n};\n```\n\n## 代办事项\n\n- [x] 缓存路由规则计算结果\n- [ ] createMark options 增加 global 属性，支持全局应用拦截，同时支持部分排除\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fepeejs%2Frouting-controllers-mark","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fepeejs%2Frouting-controllers-mark","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fepeejs%2Frouting-controllers-mark/lists"}