{"_id":"@bylive/router","_rev":"1-b30cc452c97d2e42a75a5576e8a3f48d","name":"@bylive/router","dist-tags":{"latest":"1.1.0"},"versions":{"1.0.0":{"name":"@bylive/router","version":"1.0.0","keywords":["@bylive/router","koa-router-decorator2","koa-router-decorator","decorator","koa-roter","koa"],"author":"","license":"ISC","_id":"@bylive/router@1.0.0","maintainers":[{"name":"beyonds","email":"1119879311@qq.com"}],"dist":{"shasum":"9778c59203dbda64efc8b87cf645476099e6025f","tarball":"https://registry.npmjs.org/@bylive/router/-/router-1.0.0.tgz","fileCount":26,"integrity":"sha512-mO+O8ngEVqcGvDprKhx3Ffb4+8HuRRBeHtGB7FZ/JgfPt1vSo5m9WMBxENcBgpn3udx4i05Rhz4VmE+TrfkLuw==","signatures":[{"sig":"MEYCIQDemChX2Gyg1zO+VmOYIq46DXGcMyHQu2DinYpdZxsCrgIhAJ3WtihQ/b0yU0VNLiNg2Mq/32CEvsQF/1f7svwQcni9","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":29128},"main":"dist/index.js","_from":"file:bylive-router-1.0.0.tgz","types":"./dist/index.d.ts","scripts":{"test":"echo \"Error: no test specified\" && exit 1","build":"npx tsc","watch":"npx tsc --watch"},"_npmUser":{"name":"beyonds","email":"1119879311@qq.com"},"_resolved":"C:\\Users\\11198\\AppData\\Local\\Temp\\ff7afe9cd6ed6dcdc31106beadea0217\\bylive-router-1.0.0.tgz","_integrity":"sha512-mO+O8ngEVqcGvDprKhx3Ffb4+8HuRRBeHtGB7FZ/JgfPt1vSo5m9WMBxENcBgpn3udx4i05Rhz4VmE+TrfkLuw==","_npmVersion":"8.11.0","description":"一个高效轻量级的基于koa 和koa-router 写的装饰器路由库","directories":{},"_nodeVersion":"16.15.1","publishConfig":{"access":"public","registry":"https://registry.npmjs.org"},"_hasShrinkwrap":false,"_npmOperationalInternal":{"tmp":"tmp/router_1.0.0_1683818763459_0.555751858134057","host":"s3://npm-registry-packages"}},"1.1.0":{"name":"@bylive/router","version":"1.1.0","description":"一个高效轻量级的基于koa 和koa-router 写的装饰器路由库","main":"dist/index.js","scripts":{"test":"echo \"Error: no test specified\" && exit 1","watch":"npx tsc --watch","build":"npx tsc"},"keywords":["@bylive/router","koa-router-decorator2","koa-router-decorator","decorator","koa-roter","koa"],"author":"","license":"ISC","publishConfig":{"access":"public","registry":"https://registry.npmjs.org"},"types":"./dist/index.d.ts","gitHead":"54bd770404cc02a0e696e45319438f1d593afdfc","_id":"@bylive/router@1.1.0","_nodeVersion":"18.3.0","_npmVersion":"8.11.0","dist":{"integrity":"sha512-k4wOwNfEeTF6v0Mh5IYteDUgm/25O3pW1MYRHGNpyn2im/i/3CjDn2xfWVVD34nFg53psuSpNlo/xlZupyQBiw==","shasum":"9b3c5376fccd65acddee447b6eea5bf806a60511","tarball":"https://registry.npmjs.org/@bylive/router/-/router-1.1.0.tgz","fileCount":26,"unpackedSize":32551,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQD4sFD2NRkOujunlLPHfSvBh/M5V8ouphxuu9KueH4RCgIgdHi//wzQ5L/+HBTzMdvpdUFQMFcanMpjlwckZeK2Npg="}]},"_npmUser":{"name":"beyonds","email":"1119879311@qq.com"},"directories":{},"maintainers":[{"name":"beyonds","email":"1119879311@qq.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/router_1.1.0_1721661179333_0.44522006216426147"},"_hasShrinkwrap":false}},"time":{"created":"2023-05-11T15:26:03.401Z","modified":"2024-07-22T15:12:59.727Z","1.0.0":"2023-05-11T15:26:03.667Z","1.1.0":"2024-07-22T15:12:59.547Z"},"license":"ISC","keywords":["@bylive/router","koa-router-decorator2","koa-router-decorator","decorator","koa-roter","koa"],"description":"一个高效轻量级的基于koa 和koa-router 写的装饰器路由库","maintainers":[{"name":"beyonds","email":"1119879311@qq.com"}],"readme":"# koa-router-decorator2\r\n一个高效轻量级的基于koa 和koa-router 写的装饰器路由库\r\n\r\n#### 示例1\r\n> src/index.ts\r\n\r\n```typescript\r\nimport {ResigerRouter} from \"koa-router-decorator2\";\r\nimport koaRouter from \"koa-router\";\r\nimport Koa from \"koa\"\r\nimport { setGolbalGuards, setGolbalMiddleware, UserController, Validation } from \"./UserController\";\r\n\r\nconst app = new Koa()\r\nconst rotuer = new koaRouter({\r\n  prefix: \"/api\",\r\n});\r\n\r\n\r\n/**一个http服务请求的执行顺序\r\n *全局中间件--setGolbalMiddleware\r\n  类中间件--ClassMiddleware\r\n  方法中间件--MethondMiddleware\r\n  全局守卫 useAuthGuards { useConfig: { userId: '1', userName: '123' } } 12\r\n  类守卫 ClassGuards { useConfig: { userId: '1', userName: '123' } }\r\n  方法守卫 MethondGuard { useConfig: { userId: '1', userName: '123' } } 这是元数据\r\n  管道 Validation-apply: [ { value: { id: '12' }, type: [class useDto] } ]\r\n  路由层:getbefore state: { useConfig: { userId: '1', userName: '123' } } query: { id: '12' }\r\n * \r\n */\r\n\r\n\r\n // 注册控制器\r\n  ResigerRouter(rotuer, UserController,{\r\n    midwares:[setGolbalMiddleware], // 中间件\r\n    guards:[setGolbalGuards], // 守卫\r\n    pipes:[ new Validation() ] // 管道: 一般用与请求的参数默认设置，转换，验证\r\n  });\r\n\r\n  // 全局错误捕获处理\r\n  const HttpExceptionFilter =  async (error:Error,ctx:Koa.DefaultContext)=>{\r\n    ctx.body =  {message:error.message?error.message:error,status:false,success:false,path: ctx.url,}\r\n  }\r\n\r\n  //全局响应处理(可以省略)\r\n  const HttpResponseInterceptor = async (ctx:Koa.DefaultContext)=>{\r\n    if (ctx.response.is(\"text/plain\") || ctx.response.is(\"json\")) {\r\n\r\n      ctx.body = await { timestamp: new Date(), code: 200, data: ctx.body, status: true, message: \"success\"};\r\n    }\r\n  }\r\n\r\n  // 设置第一个中间，可以捕获全局响应和错误处理\r\n  const firstMiddlWare = async (ctx:Koa.DefaultContext,next:Function)=>{\r\n    try {\r\n      await next();\r\n      HttpResponseInterceptor(ctx) ;//响应处理\r\n    } catch (error) {\r\n      ctx.app.emit(\"error\", error, ctx); // 错误处理\r\n    }\r\n  }\r\n\r\n  app.use(firstMiddlWare)\r\n  app.use(rotuer.routes()); // 使用路由中间\r\n  app.listen( 8080,()=>{\r\n    console.log(\"server is run in prot 8080\")\r\n  } )\r\n  app.on(\"error\", HttpExceptionFilter)\r\n\r\n\r\n\r\n```\r\n\r\n> src/UserController.ts\r\n\r\n```typescript\r\n// import {\r\n//     Controller,\r\n//     GET,\r\n//     Query,\r\n//     Ctx,\r\n//     Guard,\r\n//     Pipe,\r\n//     IContextOption,\r\n//     IGuard,\r\n//     Use,\r\n//     setMetadata,\r\n//   } from \"../../index\";\r\n  import {\r\n    Controller,\r\n    GET,\r\n    Query,\r\n    Ctx,\r\n    Guard,\r\n    Pipe,\r\n    IContextOption,\r\n    IGuard,\r\n    Use,\r\n    setMetadata,\r\n    Cookie,\r\n  } from \"koa-router-decorator2\";\r\n  import Koa from \"koa\"\r\n   import * as Cookies from 'cookies';\r\n\r\n  // DTO\r\n  class useDto{\r\n    id:string=\"12\"\r\n    name:string=\"name\"\r\n\r\n  }\r\n  // 中间件,设置全局 参数\r\n  // const \r\n  export const setGolbalMiddleware = async(ctx:Koa.DefaultContext,next:Function)=>{\r\n    console.log(\"全局中间件--setGolbalMiddleware\")\r\n    ctx.state.useConfig = {\r\n       userId:\"1\",\r\n       userName:\"123\"\r\n    }\r\n    // throw new Error(\"全局中间件--setGolbalMiddleware 错误\")\r\n    await next()\r\n  }\r\n\r\n  //类中间件--ClassMiddleware\r\n  export const ClassMiddleware = async(ctx:Koa.DefaultContext,next:Function)=>{\r\n    console.log(\"类中间件--ClassMiddleware\")\r\n    // throw new Error(\"类中间件--ClassMiddleware 错误\")\r\n\r\n    await next()\r\n  }\r\n\r\n  //方法中间件--MethondMiddleware\r\n  export const MethondMiddleware = async(ctx:Koa.DefaultContext,next:Function)=>{\r\n    console.log(\"方法中间件--MethondMiddleware\")\r\n    // throw new Error(\"方法中间件--MethondMiddleware 错误\")\r\n    await next()\r\n  }\r\n\r\n  // 全局守卫\r\n  export const setGolbalGuards:IGuard = async (option)=>{\r\n    const {ctx } = option\r\n    console.log(\"全局守卫 useAuthGuards\" ,ctx.state,ctx.query.id)\r\n    // throw new Error(\"全局守卫 useAuthGuards 错误\")\r\n    return true\r\n    // return await ctx.query.id===ctx.state.useConfig.userId\r\n  }\r\n\r\n  // 类的守卫\r\n\r\n  export const ClassGuard:IGuard = async (option)=>{\r\n    const {ctx } = option\r\n    console.log(\"类守卫 ClassGuards\" ,ctx.state)\r\n    // throw new Error(\"类守卫 ClassGuards 错误\")\r\n\r\n    return true\r\n  }\r\n  // 方法的守卫\r\n  export const MethondGuard:IGuard = async (option)=>{\r\n    const {ctx,get } = option\r\n    console.log(\"方法守卫 MethondGuard\" ,ctx.state ,get(\"apiName\"))\r\n    // throw new Error(\"方法守卫 MethondGuard 错误\")\r\n\r\n    return true\r\n  }\r\n\r\n  // 管道:作用参数的转换，设置默认值，校验\r\n  export class  Validation extends Pipe {\r\n    apply(option: IContextOption): void | Promise<void> {\r\n       console.log(\"管道 Validation-apply:\", option.getArgs())\r\n      //  throw new Error(\"管道 Validation-apply: 参数有误\")\r\n    }\r\n\r\n  }\r\n\r\n  @Use(ClassMiddleware)\r\n  @Guard(ClassGuard)\r\n  @Controller(\"/user\")\r\n  export  class UserController  {\r\n\r\n    @setMetadata(\"apiName\",'这是元数据')\r\n    @Use(MethondMiddleware)\r\n    @Guard(MethondGuard)\r\n    @GET(\"list\")\r\n    list(@Query() query: useDto, @Ctx() ctx: any,@Query(\"id\") id: string,) {\r\n\r\n      console.log(\"路由层:getbefore\",\"state:\",ctx.state,\"query:\",query);\r\n      return this.getd()\r\n    }\r\n\r\n    @GET(\"setCookies\")\r\n    setCookies(@Cookie() cookie:Cookies,@Ctx() ctx: Koa.DefaultContext){\r\n      cookie.set(\"userInfo\",JSON.stringify(ctx.state.useConfig ))\r\n      return \"设置cokie\"\r\n    }\r\n    @GET(\"getCookies\")\r\n    getCookies(@Cookie() cookie:Cookies){\r\n       return cookie.get(\"userInfo\") || {}\r\n    }\r\n    getd() {\r\n      return \"lallalla\";\r\n    }\r\n  }\r\n\r\n```\r\n\r\nAPI 参数说明\r\n\r\n","readmeFilename":"README.md"}