File: middleware.rb

package info (click to toggle)
ruby-apollo-upload-server 2.1.7-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 160 kB
  • sloc: ruby: 439; makefile: 6; sh: 4
file content (30 lines) | stat: -rw-r--r-- 796 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
require 'apollo_upload_server/graphql_data_builder'

module ApolloUploadServer
  class Middleware
    # Strict mode requires that all mapped files are present in the mapping arrays.
    class_attribute :strict_mode, default: false

    def initialize(app)
      @app = app
    end

    def call(env)
      unless env['CONTENT_TYPE'].to_s.include?('multipart/form-data')
        return @app.call(env)
      end

      request = ActionDispatch::Request.new(env)
      params = request.params

      if params['operations'].present? && params['map'].present?
        result = GraphQLDataBuilder.new(strict_mode: self.class.strict_mode).call(request.params)
        result&.each do |key, value|
          request.update_param(key, value)
        end
      end

      @app.call(env)
    end
  end
end