Cody2333 / koa-swagger-decorator

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

Add support for header validation #185

Open regis-underdog opened 2 weeks ago

regis-underdog commented 2 weeks ago

Swagger supports header parameters as described here https://swagger.io/docs/specification/describing-parameters/#header-parameters

The example I would like to see is add a @header() parameter like this example for a GitHub webhook:

export default class GitHubWebHookRouter {
  @request('post', '/webhook/github')
  @summary('Receive webhooks from GitHub.')
  @body(WebhookEvent)
  @header({'x-github-delivery': { type: string, required: true, default: '', description: 'GitHub ID' }})
  @header({'x-github-event': { type: string, required: true, default: '', description: 'GitHub Event' }})
  @header({'x-hub-signature-256': { type: string, required: true, default: '', description: 'GitHub 256 Signature' }})
  @header({'x-github-hook-installation-target-id': { type: string, required: true, default: '', description: 'GitHub Target ID' }})
  static async receiveGitHubWebHookController(ctx) {
    const payload = ctx.request.body;
    const id = ctx.request.headers['x-github-delivery'];
    const type = ctx.request.headers['x-github-event'];
    const targetId = parseInt(ctx.request.headers['x-github-hook-installation-target-id']);
    const verified = verify(
      GITHUB_WEBHOOK_SECRET,
      JSON.stringify(payload),
      ctx.request.headers['x-hub-signature-256']
    );
...
  }
}