11import gleam/dynamic . { type Dynamic }
2+ import gleam/fetch/fetch_options . { type FetchOptions }
23import gleam/fetch/form_data . { type FormData }
34import gleam/http/request . { type Request }
45import gleam/http/response . { type Response }
@@ -41,7 +42,27 @@ pub type FetchResponse
4142/// |> fetch.raw_send
4243/// ```
4344@ external ( javascript , "../gleam_fetch_ffi.mjs" , "raw_send" )
44- pub fn raw_send ( a : FetchRequest ) -> Promise ( Result ( FetchResponse , FetchError ) )
45+ pub fn raw_send (
46+ request : FetchRequest ,
47+ ) -> Promise ( Result ( FetchResponse , FetchError ) )
48+
49+ /// Call directly `fetch` with a `Request` and `FetchOptions`,
50+ /// then convert the result back to Gleam.
51+ /// Let you get back a `FetchResponse` instead of the Gleam
52+ /// `gleam/http/response.Response` data.
53+ ///
54+ /// ```gleam
55+ /// request.new()
56+ /// |> request.set_host("example.com")
57+ /// |> request.set_path("/example")
58+ /// |> fetch.to_fetch_request
59+ /// |> fetch.raw_send_with(fetch_options.new())
60+ /// ```
61+ @ external ( javascript , "../gleam_fetch_ffi.mjs" , "raw_send" )
62+ pub fn raw_send_with (
63+ request : FetchRequest ,
64+ options : FetchOptions ,
65+ ) -> Promise ( Result ( FetchResponse , FetchError ) )
4566
4667/// Call `fetch` with a Gleam `Request(String)`, and convert the result back
4768/// to Gleam. Use it to send strings or JSON stringified.
@@ -69,6 +90,34 @@ pub fn send(
6990 } )
7091}
7192
93+ /// Call `fetch` with a Gleam `Request(String)` and `FetchOptions`,
94+ /// then convert the result back to Gleam.
95+ /// Use it to send strings or JSON stringified.
96+ ///
97+ /// If you're looking for something more low-level, take a look at
98+ /// [`raw_send_with`](#raw_send_with).
99+ ///
100+ /// ```gleam
101+ /// let my_data = json.object([#("field", "value")])
102+ /// request.new()
103+ /// |> request.set_host("example.com")
104+ /// |> request.set_path("/example")
105+ /// |> request.set_body(json.to_string(my_data))
106+ /// |> request.set_header("content-type", "application/json")
107+ /// |> fetch.send_with(fetch_options.new())
108+ /// ```
109+ pub fn send_with (
110+ request : Request ( String ) ,
111+ options : FetchOptions ,
112+ ) -> Promise ( Result ( Response ( FetchBody ) , FetchError ) ) {
113+ request
114+ |> to_fetch_request
115+ |> raw_send_with ( options )
116+ |> promise . try_await ( fn ( resp ) {
117+ promise . resolve ( Ok ( from_fetch_response ( resp ) ) )
118+ } )
119+ }
120+
72121/// Call `fetch` with a Gleam `Request(FormData)`, and convert the result back
73122/// to Gleam. Request will be sent as a `multipart/form-data`, and should be
74123/// decoded as-is on servers.
@@ -97,6 +146,36 @@ pub fn send_form_data(
97146 } )
98147}
99148
149+ /// Call `fetch` with a Gleam `Request(FormData)` and `FetchOptions`,
150+ /// then convert the result back to Gleam.
151+ /// Request will be sent as a `multipart/form-data`, and should be
152+ /// decoded as-is on servers.
153+ ///
154+ /// If you're looking for something more low-level, take a look at
155+ /// [`raw_send_with`](#raw_send_with).
156+ ///
157+ /// ```gleam
158+ /// request.new()
159+ /// |> request.set_host("example.com")
160+ /// |> request.set_path("/example")
161+ /// |> request.set_body({
162+ /// form_data.new()
163+ /// |> form_data.append("key", "value")
164+ /// })
165+ /// |> fetch.send_form_data_with(fetch_options.new())
166+ /// ```
167+ pub fn send_form_data_with (
168+ request : Request ( FormData ) ,
169+ options : FetchOptions ,
170+ ) -> Promise ( Result ( Response ( FetchBody ) , FetchError ) ) {
171+ request
172+ |> form_data_to_fetch_request
173+ |> raw_send_with ( options )
174+ |> promise . try_await ( fn ( resp ) {
175+ promise . resolve ( Ok ( from_fetch_response ( resp ) ) )
176+ } )
177+ }
178+
100179/// Call `fetch` with a Gleam `Request(FormData)`, and convert the result back
101180/// to Gleam. Binary will be sent as-is, and you probably want a proper
102181/// content-type added.
@@ -110,7 +189,7 @@ pub fn send_form_data(
110189/// |> request.set_path("/example")
111190/// |> request.set_body(<<"data">>)
112191/// |> request.set_header("content-type", "application/octet-stream")
113- /// |> fetch.send_form_data
192+ /// |> fetch.send_bits
114193/// ```
115194pub fn send_bits (
116195 request : Request ( BitArray ) ,
@@ -123,6 +202,33 @@ pub fn send_bits(
123202 } )
124203}
125204
205+ /// Call `fetch` with a Gleam `Request(FormData)` and `FetchOptions`,
206+ /// then convert the result back to Gleam. Binary will be sent as-is,
207+ /// and you probably want a proper content-type added.
208+ ///
209+ /// If you're looking for something more low-level, take a look at
210+ /// [`raw_send_with`](#raw_send_with).
211+ ///
212+ /// ```gleam
213+ /// request.new()
214+ /// |> request.set_host("example.com")
215+ /// |> request.set_path("/example")
216+ /// |> request.set_body(<<"data">>)
217+ /// |> request.set_header("content-type", "application/octet-stream")
218+ /// |> fetch.send_bits_with(fetch_options.new())
219+ /// ```
220+ pub fn send_bits_with (
221+ request : Request ( BitArray ) ,
222+ options : FetchOptions ,
223+ ) -> Promise ( Result ( Response ( FetchBody ) , FetchError ) ) {
224+ request
225+ |> bitarray_request_to_fetch_request
226+ |> raw_send_with ( options )
227+ |> promise . try_await ( fn ( resp ) {
228+ promise . resolve ( Ok ( from_fetch_response ( resp ) ) )
229+ } )
230+ }
231+
126232/// Convert a Gleam `Request(String)` to a JavaScript
127233/// [`Request`](https://developer.mozilla.org/docs/Web/API/Request), where
128234/// `body` is a string.
0 commit comments