{"id":14989296,"url":"https://github.com/harryxiong24/koa-decorator","last_synced_at":"2025-04-12T00:31:59.966Z","repository":{"id":248443289,"uuid":"828439959","full_name":"HarryXiong24/koa-decorator","owner":"HarryXiong24","description":"Koa-decorator is a package that provides a set of decorators to simplify the creation and management of routes in Koa.js applications.","archived":false,"fork":false,"pushed_at":"2024-07-19T03:42:20.000Z","size":1292,"stargazers_count":11,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-25T20:51:12.824Z","etag":null,"topics":["decorator","ioc","koa","nodejs","typescript"],"latest_commit_sha":null,"homepage":"","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/HarryXiong24.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":"2024-07-14T06:42:03.000Z","updated_at":"2025-01-19T17:43:09.000Z","dependencies_parsed_at":"2024-09-24T16:07:30.021Z","dependency_job_id":null,"html_url":"https://github.com/HarryXiong24/koa-decorator","commit_stats":{"total_commits":45,"total_committers":1,"mean_commits":45.0,"dds":0.0,"last_synced_commit":"27ec153547c434edf6676562887c022051b0e942"},"previous_names":["harryxiong24/koa-decorator"],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HarryXiong24%2Fkoa-decorator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HarryXiong24%2Fkoa-decorator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HarryXiong24%2Fkoa-decorator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HarryXiong24%2Fkoa-decorator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/HarryXiong24","download_url":"https://codeload.github.com/HarryXiong24/koa-decorator/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248501407,"owners_count":21114674,"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":["decorator","ioc","koa","nodejs","typescript"],"created_at":"2024-09-24T14:18:05.828Z","updated_at":"2025-04-12T00:31:59.582Z","avatar_url":"https://github.com/HarryXiong24.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# koa-decorator\n\n## Description\n\nKoa-decorator is a package that provides a set of decorators to simplify the creation and management of routes in Koa.js applications.\n\nThese decorators allow you to define routes, middlewares and services directly, making your code cleaner and more maintainable.\n\n## Install\n\nFor npm:\n\n```bash\nnpm install koa-decorator-x\n```\n\nFor pnpm:\n\n```bash\npnpm install koa-decorator-x\n```\n\nFor yarn:\n\n```bash\nyarn add koa-decorator-x\n```\n\n## Usage\n\n### Easy run the server:\n\n```ts\nimport { Application } from 'koa-decorator-x';\n\nconst app = new Application();\n\napp.listen(3000, () =\u003e {\n  console.log('server is listening 3000');\n});\ns;\n```\n\n### Directory conventions\n\nIn the root directory, we have these directories by default:\n\n```bash\n.\n├── controllers\n│   ├── xxxController.ts\n│   ├── xxxController.ts\n├── middlewares\n│   ├── xxx.ts\n│   ├── xxx.ts\n├── services\n│   ├── xxx.ts\n│   ├── xxx.ts\n└── app.ts\n```\n\nAnd our framework will scan the files in these directories automatically. So we have to make sure these directories exists.\n\nIf you want to modify the structure of default directories. Please modify the config synchronously.\n\n```ts\nimport { Application } from 'koa-decorator-x';\n\nconst app = new Application({\n  controllersDir: '/src/controllers',\n  middlewaresDir: '/src/middlewares',\n  servicesDir: '/src/services',\n  middlewares: [],\n});\n```\n\n### Define routers\n\nWe have already pre-installed `@koa/router`.\n\nYou do not need to define routers by using a `router.get('/list', handleList)`.\n\nInstead, you just need to create a controller class in `controllers` directory. And the use `@Controller`, `@GET`, `@POST` etc. The router will be register automatically.\n\n```ts\nimport { Controller, GET, POST } from 'koa-decorator-x';\n\n@Controller('/demo')\nexport default class DemoController {\n  @GET('/list')\n  public async list(ctx: Context): Promise\u003cResult\u003e {\n    return { code: 0, data: data, msg: 'OK' };\n  }\n\n  @POST('/update')\n  public async update(): Promise\u003cResult\u003e {\n    return { code: 0, data: [], msg: 'OK' };\n  }\n}\n```\n\n## Define middlewares\n\nYou just need to create a middleware class in `middlewares` directory. And the use `@Middleware` to define a middleware. The middleware will be register automatically.\n\n```ts\nimport { Inject, Middleware } from 'koa-decorator-x';\nimport { Context, Next } from 'koa';\n\n@Middleware()\nexport class ErrorHandler {\n  async use(ctx: Context, next: Next) {\n    try {\n      console.log('Error Handler Middleware');\n      await next();\n    } catch (err) {\n      console.log(err);\n      ctx.status = 500;\n      ctx.body = { code: 500, message: 'Internal Server Error', data: null };\n    }\n  }\n}\n```\n\nAt the same time, you can also apply third-party middlewares by registering in the `Application`:\n\n```ts\nimport { Application } from 'koa-decorator-x';\n\nconst app = new Application({\n  controllersDir: '/src/controllers',\n  middlewaresDir: '/src/middlewares',\n  servicesDir: '/src/services',\n  middlewares: [],\n});\n```\n\n### Service \u0026 Injectable\n\nThey are the same concept and just use different name. Service is focused on some services you need to provide. Injectable just the class you want to have inject function.\n\nFirst, use `@Service` to register a service class.\n\n```ts\nimport { Service } from 'koa-decorator-x';\n@Service()\nexport default class DemoService {\n  public async index(): Promise\u003c\n    {\n      id: string;\n      name: string;\n      gender: string;\n    }[]\n  \u003e {\n    return [\n      { id: '11111', name: 'harry', gender: 'male' },\n      { id: '22222', name: 'peter', gender: 'female' },\n    ];\n  }\n}\n```\n\nAnd then, you can inject it to any class instances.\n\n```ts\nimport Result from '../models/result';\nimport { Controller, GET, Inject, POST } from 'koa-decorator-x';\nimport DemoService from '../services/demo';\nimport { Context } from 'koa';\n\n@Controller('/demo')\nexport default class DemoController {\n  @Inject()\n  demoService!: DemoService;\n\n  @GET('/list')\n  public async list(ctx: Context): Promise\u003cResult\u003e {\n    const data = await this.demoService.index();\n    return { code: 0, data: data, msg: 'OK' };\n  }\n\n  @POST('/update')\n  public async update(): Promise\u003cResult\u003e {\n    return { code: 0, data: [], msg: 'OK' };\n  }\n}\n```\n\n`Injectable` is the same.\n\n```ts\nimport { Injectable } from 'koa-decorator-x';\n\n@Injectable()\nclass DemoInjectable {\n  public async index(): Promise\u003c\n    {\n      id: string;\n      name: string;\n      gender: string;\n    }[]\n  \u003e {\n    return [\n      { id: '11111', name: 'harry', gender: 'male' },\n      { id: '22222', name: 'peter', gender: 'female' },\n    ];\n  }\n}\n\nexport default DemoInjectable;\n```\n\n```ts\nimport { Inject, Middleware } from 'koa-decorator-x';\nimport { Context, Next } from 'koa';\nimport DemoInjectable from '../utils/demo';\n\n@Middleware()\nexport class ErrorHandler {\n  @Inject()\n  demoInjectable!: DemoInjectable;\n\n  async use(ctx: Context, next: Next) {\n    try {\n      console.log('Error Handler Middleware');\n      console.log('result', await this.demoInjectable.index());\n      await next();\n    } catch (err) {\n      console.log(err);\n      ctx.status = 500;\n      ctx.body = { code: 500, message: 'Internal Server Error', data: null };\n    }\n  }\n}\n```\n\n## Contributing\n\nWe welcome contributions!\n\n## License\n\nThis project is licensed under the MIT License. See the LICENSE file for details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fharryxiong24%2Fkoa-decorator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fharryxiong24%2Fkoa-decorator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fharryxiong24%2Fkoa-decorator/lists"}