export function addProxyMethodToApiResource()

in source/patterns/@aws-solutions-constructs/core/lib/apigateway-helper.ts [255:335]


export function addProxyMethodToApiResource(params: AddProxyMethodToApiResourceInputParams): api.Method {
  let baseProps: api.AwsIntegrationProps = {
    service: params.service,
    integrationHttpMethod: "POST",
    options: {
      passthroughBehavior: api.PassthroughBehavior.NEVER,
      credentialsRole: params.apiGatewayRole,
      requestParameters: {
        "integration.request.header.Content-Type": params.contentType ? params.contentType : "'application/json'"
      },
      requestTemplates: {
        "application/json": params.requestTemplate
      },
      integrationResponses: [
        {
          statusCode: "200"
        },
        {
          statusCode: "500",
          responseTemplates: {
            "text/html": "Error"
          },
          selectionPattern: "500"
        }
      ]
    }
  };

  let extraProps;

  if (params.action) {
    extraProps = {
      action: params.action
    };
  } else if (params.path) {
    extraProps = {
      path: params.path
    };
  } else {
    throw Error('Either action or path is required');
  }

  // Setup the API Gateway AWS Integration
  baseProps = Object.assign(baseProps, extraProps);
  let apiGatewayIntegration;
  if (params.awsIntegrationProps) {
    const overridenProps = overrideProps(baseProps, params.awsIntegrationProps);
    apiGatewayIntegration = new api.AwsIntegration(overridenProps);
  } else {
    apiGatewayIntegration = new api.AwsIntegration(baseProps);
  }

  const defaultMethodOptions = {
    methodResponses: [
      {
        statusCode: "200",
        responseParameters: {
          "method.response.header.Content-Type": true
        }
      },
      {
        statusCode: "500",
        responseParameters: {
          "method.response.header.Content-Type": true
        },
      }
    ],
    requestValidator: params.requestValidator,
    requestModels: params.requestModel
  };

  let apiMethod;
  // Setup the API Gateway method
  if (params.methodOptions) {
    const overridenProps =  overrideProps(defaultMethodOptions, params.methodOptions);
    apiMethod = params.apiResource.addMethod(params.apiMethod, apiGatewayIntegration, overridenProps);
  } else {
    apiMethod = params.apiResource.addMethod(params.apiMethod, apiGatewayIntegration, defaultMethodOptions);
  }
  return apiMethod;
}