Cody2333 / koa-swagger-decorator

using decorator to automatically generate swagger doc for koa-router
348 stars 81 forks source link

Controller resets on each request #160

Open jrnels10 opened 2 years ago

jrnels10 commented 2 years ago

On each request that is made, the controller class is being called new which resets all the properties within it. Using the example within the repo, for each request that is made to the class EggRouter, it will call the constructor. How can I keep the EggRouter from creating a new instance on each request? Thanks!


 import { request, summary, tags, query, body, prefix } from '../../../lib';

const tag = tags(['Egg']);

@prefix('/egg')
export default class EggRouter {
  constructor(){
  console.log('this is being called')
}
  @request('get', '/method1')
  @summary('egg-like controller method1')
  @tag
  @query({
    no: { type: 'string', description: 'nonono' }
  })
  async method1(ctx) {
    const { no } = ctx.validatedQuery;
    ctx.body = this.test;
  }

  @request('post', '/method2')
  @summary('egg-like controller method2')
  @tag
  @body({
    yes: {
      type: 'string',
      require: true,
      description: 'yesyesyes',
      nullable: true
    }
  })
  async method2(ctx) {
    const { yes } = ctx.validatedBody;
    ctx.body = 'updated';
  }
}