@@ -9,6 +9,71 @@ class FileUploadParams < Imagekit::Internal::Type::BaseModel
99 extend Imagekit ::Internal ::Type ::RequestParameters ::Converter
1010 include Imagekit ::Internal ::Type ::RequestParameters
1111
12+ # Serialize upload options to handle proper formatting for ImageKit backend API.
13+ # Special cases handled:
14+ # - tags: converted to comma-separated string
15+ # - responseFields: converted to comma-separated string
16+ # - extensions: JSON stringified
17+ # - customMetadata: JSON stringified
18+ # - transformation: JSON stringified
19+ #
20+ # @api private
21+ #
22+ # @param params [Object]
23+ #
24+ # @return [Array(Object, Hash{Symbol=>Object})]
25+ def self . dump_request ( params )
26+ state = { can_retry : true }
27+ case ( dumped = dump ( params , state : state ) )
28+ in Hash
29+ serialized = serialize_upload_options ( dumped )
30+ options = Imagekit ::Internal ::Util . coerce_hash! ( serialized [ :request_options ] ) . to_h
31+ request_options = state . fetch ( :can_retry ) ? options : { **options , max_retries : 0 }
32+ [ serialized . except ( :request_options ) , request_options ]
33+ else
34+ [ dumped , nil ]
35+ end
36+ end
37+
38+ # @api private
39+ #
40+ # @param upload_options [Hash{Symbol=>Object}]
41+ #
42+ # @return [Hash{Symbol=>Object}]
43+ def self . serialize_upload_options ( upload_options )
44+ serialized = { }
45+
46+ upload_options . each do |key , value |
47+ # Skip nil values
48+ if value . nil?
49+ serialized [ key ] = value
50+ next
51+ end
52+
53+ serialized [ key ] = case key
54+ when :tags
55+ # Tags should be comma-separated string
56+ value . is_a? ( Array ) ? value . join ( ',' ) : value
57+ when :responseFields
58+ # Response fields should be comma-separated string
59+ value . is_a? ( Array ) ? value . join ( ',' ) : value
60+ when :extensions
61+ # Extensions should be JSON stringified
62+ value . is_a? ( Array ) ? JSON . generate ( value ) : value
63+ when :customMetadata
64+ # Custom metadata should be JSON stringified
65+ value . is_a? ( Hash ) ? JSON . generate ( value ) : value
66+ when :transformation
67+ # Transformation should be JSON stringified
68+ ( value . is_a? ( Hash ) || value . respond_to? ( :to_h ) ) ? JSON . generate ( value ) : value
69+ else
70+ value
71+ end
72+ end
73+
74+ serialized
75+ end
76+
1277 # @!attribute file
1378 # The API accepts any of the following:
1479 #
0 commit comments