Search code examples
swaggerswagger-2.0aws-serverless

how to add bodyType serverless.yml for post request [serverless-auto-swagger]


Hi mates this is regarding the serverless-auto-swagger library, in serverless.ts we can add bodyType but for yml how to define the request body type? I tried adding a schema as well but it seems not getting.

functions.yml

createSurvey:
  handler: src/functions/survey/createSurveyFunction.handler
  events:
    - http:
        path: /survey
        method: post
        cors: true
        authorizer:
          name: authorizer
          arn: ${self:custom.userPoolArn}

Survey Interface

export interface PostISurvey { surveyTitle?: string; surveyDesc?: string; expireDate?: string; questions?: any[]; }

serverless.yml configs

 autoswagger:
    title: "Survey API Documentation"
    apiType: "http"
    generateSwaggerOnDeploy: true
    typefiles: ["./src/types/surevy.d.ts"]
    schemes: ["http"]
    basePath: "/${opt:stage}"
    apiKeyHeaders: ["authorizer"]

Solution

  • You can define the bodyType to the http event (see doc)

    With your exemple it should be:

    function.yml

    createSurvey:
      handler: src/functions/survey/createSurveyFunction.handler
      events:
        - http:
            path: /survey
            method: post
            cors: true
            bodyType: PostISurvey
            authorizer:
              name: authorizer
              arn: ${self:custom.userPoolArn}