Sha256: 8f6a8ddaf75d85f8443b6fce8f86d79a7a2fd1f0125b05fffd3c0c306ef25484

Contents?: true

Size: 1.89 KB

Versions: 129

Compression:

Stored size: 1.89 KB

Contents

module Jets::Resource::ApiGateway
  # Might be weird inheriting from Method because Method has Method#cors also
  # but Cors is essentially a Method class.
  class Cors < Method
    def definition
      {
        cors_logical_id => {
          type: "AWS::ApiGateway::Method",

          properties: {
            resource_id: "!Ref #{resource_id}",
            rest_api_id: "!Ref #{RestApi.logical_id}",
            authorization_type: cors_authorization_type,
            http_method: "OPTIONS",
            method_responses: [{
              status_code: '200',
              response_parameters: response_parameters(true),
              response_models: {},
            }],
            request_parameters: {},
            integration: {
              type: "MOCK",
              request_templates: {
                "application/json": "{statusCode:200}",
              },
              integration_responses: [{
                status_code: '200',
                response_parameters: response_parameters,
                response_templates: {
                  "application/json": '',
                },
              }] # closes integration_responses
            } # closes integration
          } # closes properties
        } # closes logical id
      } # closes definition
    end

    def response_parameters(method_response=false)
      cors_headers.map do |k,v|
        k = "method.response.header.#{k}"
        v = method_response ? true : "'#{v}'" # surround value with single quotes
        [k,v]
      end.to_h
    end

    # Always the pre-flight headers in this case
    def cors_headers
      rack = Jets::Controller::Middleware::Cors.new(Jets.application)
      rack.cors_headers(true)
    end

    def cors_authorization_type
      Jets.config.api.cors_authorization_type || @route.authorization_type || "NONE"
    end

    def cors_logical_id
      "#{resource_logical_id}_cors_api_method"
    end
  end
end

Version data entries

129 entries across 129 versions & 4 rubygems

Version Path
jets-1.9.28 lib/jets/resource/api_gateway/cors.rb
jets-1.9.27 lib/jets/resource/api_gateway/cors.rb
jets-1.9.26 lib/jets/resource/api_gateway/cors.rb
jets-1.9.25 lib/jets/resource/api_gateway/cors.rb
jets-1.9.24 lib/jets/resource/api_gateway/cors.rb
jets-1.9.23 lib/jets/resource/api_gateway/cors.rb
jets-1.9.22 lib/jets/resource/api_gateway/cors.rb
jets-1.9.21 lib/jets/resource/api_gateway/cors.rb
jets-1.9.20 lib/jets/resource/api_gateway/cors.rb
jets-1.9.19 lib/jets/resource/api_gateway/cors.rb
jets-1.9.18 lib/jets/resource/api_gateway/cors.rb
jets-1.9.17 lib/jets/resource/api_gateway/cors.rb
jets-1.9.16 lib/jets/resource/api_gateway/cors.rb
jets-1.9.15 lib/jets/resource/api_gateway/cors.rb
jets-1.9.14 lib/jets/resource/api_gateway/cors.rb
jets-1.9.13 lib/jets/resource/api_gateway/cors.rb
jets-1.9.12 lib/jets/resource/api_gateway/cors.rb
jets-1.9.11 lib/jets/resource/api_gateway/cors.rb
jets-1.9.10 lib/jets/resource/api_gateway/cors.rb
jets-1.9.9 lib/jets/resource/api_gateway/cors.rb