diff --git a/Readme.md b/Readme.md index 25723b2..d9a422f 100644 --- a/Readme.md +++ b/Readme.md @@ -17,6 +17,7 @@ Every little bit of help is very welcome! - [Headers](#headers) - [Events](#events) - [Response Types](#response-types) + - [Request Types](#request-types) - [Sending a Request](#sending-a-request) - [Cancelling Requests](#cancelling-requests) - [Complete Example](#complete-example) @@ -157,6 +158,18 @@ client->Warp.ResponseType.setDocument; client->Warp.ResponseType.setArrayBuffer; ``` +### Request Types + +| RequestType | Header | +| ----------- | ----------------------------------- | +| Form | `application/x-www-form-urlencoded` | +| Json | `application/json` | + +```reason +client->Warp.RequestType.setForm; // default +client->Warp.RequestType.setJson; +``` + ### Sending a Request ```reason @@ -182,7 +195,9 @@ client->Warp.send; }; ``` -### Complete Example +### Examples + +**Basic** ```reason Warp.Method.get("http://localhost:8081/") @@ -204,6 +219,27 @@ Warp.Method.get("http://localhost:8081/") ->Warp.send; ``` +**Sending JSON** + +```reason +Warp.Method.post("http://localhost:8081/") +->Warp.ResponseType.setJson +->Warp.RequestType.setJson +->Warp.FormData.set([ + ("query", ""), + ("variables", Js.Json.stringify("")), +]) +->Warp.Header.add("authorization", "Bearer 123") +->Warp.Event.onLoad(response => { + switch (response) { + | Ok(Some(data)) => Js.Console.log(data) + | Ok(None) => Js.Console.info("No Response!") + | Error(message) => Js.Console.error(message) + } + }) +->Warp.send; +``` + # What state is it in? - [x] Url diff --git a/src/Warp.re b/src/Warp.re index 0da4a3f..ac1e167 100644 --- a/src/Warp.re +++ b/src/Warp.re @@ -4,6 +4,7 @@ module Method = Warp_Method; module QueryString = Warp_QueryString; module FormData = Warp_FormData; module ResponseType = Warp_ResponseType; +module RequestType = Warp_RequestType; module Types = Warp_Types; module Event = Warp_Event; module Settings = Warp_Settings; @@ -21,11 +22,6 @@ let send: ->Belt.List.toArray ->Js.Array.joinWith("&", _); - let formData = - Belt.List.map(client.formData, ((key, value)) => {key ++ "=" ++ value}) - ->Belt.List.toArray - ->Js.Array.joinWith("&", _); - let url = if (Js.List.isEmpty(client.queryString)) { client.url; @@ -201,11 +197,26 @@ let send: | (HEAD, _) | (_, true) => xhr->Warp_XHR.send | (_, false) => - xhr->Warp_XHR.setRequestHeader( - "Content-type", - "application/x-www-form-urlencoded", - ); - xhr->Warp_XHR.sendString(formData); + let data = + switch (client.requestType) { + | "application/json" => + Js.Dict.fromList( + Belt.List.map(client.formData, ((key, value)) => + (key, Js.Json.string(value)) + ), + ) + ->Js.Json.object_ + ->Js.Json.stringify + | "application/x-www-form-urlencoded" => + Belt.List.map(client.formData, ((key, value)) => { + key ++ "=" ++ value + }) + ->Belt.List.toArray + ->Js.Array.joinWith("&", _) + | _ => "" + }; + xhr->Warp_XHR.setRequestHeader("content-type", client.requestType); + xhr->Warp_XHR.sendString(data); }; Some(() => {Warp_XHR.abort(xhr)}); diff --git a/src/Warp.rei b/src/Warp.rei index e02449f..c34e323 100644 --- a/src/Warp.rei +++ b/src/Warp.rei @@ -4,6 +4,7 @@ module Method = Warp_Method; module QueryString = Warp_QueryString; module FormData = Warp_FormData; module ResponseType = Warp_ResponseType; +module RequestType = Warp_RequestType; module Types = Warp_Types; module Event = Warp_Event; module Settings = Warp_Settings; diff --git a/src/Warp/Types/Warp_Types_Client.re b/src/Warp/Types/Warp_Types_Client.re index cdb33ad..6b8d655 100644 --- a/src/Warp/Types/Warp_Types_Client.re +++ b/src/Warp/Types/Warp_Types_Client.re @@ -11,6 +11,7 @@ type t('a) = { formData: list((string, string)), headers: list((string, string)), responseType: 'a, + requestType: string, onLoad: option(Warp_Types_ResponseType.t('a) => unit), onLoadWithStatusCode: option((Warp_Types_ResponseType.t('a), int) => unit), onProgess: option(Dom.progressEvent => unit), diff --git a/src/Warp/Warp_Client.re b/src/Warp/Warp_Client.re index 3bf9c60..1d4e35c 100644 --- a/src/Warp/Warp_Client.re +++ b/src/Warp/Warp_Client.re @@ -12,6 +12,7 @@ let make = (~url, ~method) => { overrideMimeType: None, queryString: [], responseType: Warp_Types_ResponseType.TextResponse(None), + requestType: "application/x-www-form-urlencoded", formData: [], headers: [], onLoad: None, diff --git a/src/Warp/Warp_RequestType.re b/src/Warp/Warp_RequestType.re new file mode 100644 index 0000000..962078b --- /dev/null +++ b/src/Warp/Warp_RequestType.re @@ -0,0 +1,9 @@ +open Warp_Types_Client; + +let setForm = client => { + {...client, requestType: "application/x-www-form-urlencoded"}; +}; + +let setJson = client => { + {...client, requestType: "application/json"}; +}; \ No newline at end of file