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-4.0.0 lib/jets/resource/api_gateway/cors.rb
jets-3.2.2 lib/jets/resource/api_gateway/cors.rb
jets-3.2.1 lib/jets/resource/api_gateway/cors.rb
jets.benforeva-3.0.17.pre.mount.pre.fix lib/jets/resource/api_gateway/cors.rb
jets-3.2.0 lib/jets/resource/api_gateway/cors.rb
jets-3.1.5 lib/jets/resource/api_gateway/cors.rb
jets-3.1.4 lib/jets/resource/api_gateway/cors.rb
jets-3.1.3 lib/jets/resource/api_gateway/cors.rb
jets-3.1.2 lib/jets/resource/api_gateway/cors.rb
jets-3.1.1 lib/jets/resource/api_gateway/cors.rb
jets-3.1.0 lib/jets/resource/api_gateway/cors.rb
jets-3.0.23 lib/jets/resource/api_gateway/cors.rb
jets-3.0.22 lib/jets/resource/api_gateway/cors.rb
jets-3.0.21 lib/jets/resource/api_gateway/cors.rb
jets-3.0.20 lib/jets/resource/api_gateway/cors.rb
jets-3.0.19 lib/jets/resource/api_gateway/cors.rb
jets-3.0.18 lib/jets/resource/api_gateway/cors.rb
jets-3.0.17 lib/jets/resource/api_gateway/cors.rb
jets-3.0.16 lib/jets/resource/api_gateway/cors.rb
jets-3.0.15 lib/jets/resource/api_gateway/cors.rb