diff --git a/Readme.md b/Readme.md index 7194fb3..5329b55 100644 --- a/Readme.md +++ b/Readme.md @@ -5,9 +5,11 @@ - [How do I install it?](#how-do-i-install-it) - [API](#api) - [Methods](#methods) + - [Settings](#settings) - [Query Strings](#query-strings) - [Form Data](#form-data) - [Headers](#headers) + - [Events](#events) - [Response Types](#response-types) - [Sending a Request](#sending-a-request) - [Cancelling Requests](#cancelling-requests) @@ -60,11 +62,11 @@ Please do not use it in production yet. - [x] `onAbort` - [x] send - [x] abort -- [ ] async +- [x] async +- [x] timeout +- [x] withCredentials - [ ] overrideMimeType -- [ ] timeout - [ ] auth (username / password) -- [ ] withCredentials - [ ] upload - [ ] statusCode @@ -103,6 +105,14 @@ Warp.Method.trace("https://domain.com/"); Warp.Method.connect("https://domain.com/"); ``` +### Settings + +```reason +client->Warp.Settings.async(false); +client->Warp.Settings.timeout(5000); +client->Warp.Settings.withCredentials(true); +``` + ### Query Strings ```reason diff --git a/src/Warp.re b/src/Warp.re index 2d4388b..9a3555a 100644 --- a/src/Warp.re +++ b/src/Warp.re @@ -6,6 +6,7 @@ module FormData = Warp_FormData; module ResponseType = Warp_ResponseType; module Types = Warp_Types; module Event = Warp_Event; +module Settings = Warp_Settings; let send: type a. @@ -45,78 +46,72 @@ let send: | TRACE => "TRACE" | CONNECT => "CONNECT" }, + ~async=client.async, (), ); + let _ = xhr->Warp_XHR.setWithCredentials(client.withCredentials); + let _ = xhr->Warp_XHR.setTimeout(client.timeout); + Belt.List.forEach(client.headers, ((key, value)) => { xhr->Warp_XHR.setRequestHeader(key, value) }); switch (client.onProgess) { - | Some(onProgress) => - xhr->Warp_XHR.addEventListener(`progress(evt => onProgress(evt))) + | Some(onProgress) => xhr->Warp_XHR.onProgress(evt => onProgress(evt)) | None => () }; switch (client.onAbort) { - | Some(onAbort) => - xhr->Warp_XHR.addEventListener(`abort(_ => onAbort())) + | Some(onAbort) => xhr->Warp_XHR.onAbort(_ => onAbort()) | None => () }; switch (client.onLoad) { | Some(onLoad) => - xhr->Warp_XHR.addEventListener( - `error( - _ => {onLoad(Types.ResponseType.Error(xhr->Warp_XHR.statusText))}, - ), - ); - xhr->Warp_XHR.addEventListener( - `timeout( - _ => {onLoad(Types.ResponseType.Error(xhr->Warp_XHR.statusText))}, - ), - ); + xhr->Warp_XHR.onError(_ => { + onLoad(Types.ResponseType.Error(xhr->Warp_XHR.statusText)) + }); + xhr->Warp_XHR.onTimeout(_ => { + onLoad(Types.ResponseType.Error(xhr->Warp_XHR.statusText)) + }); - xhr->Warp_XHR.addEventListener( - `load( - _ => { - switch (client.responseType) { - | Types.ResponseType.TextResponse(_) => - onLoad( - Types.ResponseType.Ok( - Types.ResponseType.TextResponse( - xhr->Warp_XHR.responseText->Js.Nullable.toOption, - ), - ), - ) - | Types.ResponseType.JSONResponse(_) => - onLoad( - Types.ResponseType.Ok( - Types.ResponseType.JSONResponse( - xhr->Warp_XHR.responseJson->Js.Nullable.toOption, - ), - ), - ) - | Types.ResponseType.DocumentResponse(_) => - onLoad( - Types.ResponseType.Ok( - Types.ResponseType.DocumentResponse( - xhr->Warp_XHR.responseDocument->Js.Nullable.toOption, - ), - ), - ) - | Types.ResponseType.ArrayBufferResponse(_) => - onLoad( - Types.ResponseType.Ok( - Types.ResponseType.ArrayBufferResponse( - xhr->Warp_XHR.responseArrayBuffer->Js.Nullable.toOption, - ), - ), - ) - } - }, - ), - ); + xhr->Warp_XHR.onLoad(_ => { + switch (client.responseType) { + | Types.ResponseType.TextResponse(_) => + onLoad( + Types.ResponseType.Ok( + Types.ResponseType.TextResponse( + xhr->Warp_XHR.responseText->Js.Nullable.toOption, + ), + ), + ) + | Types.ResponseType.JSONResponse(_) => + onLoad( + Types.ResponseType.Ok( + Types.ResponseType.JSONResponse( + xhr->Warp_XHR.responseJson->Js.Nullable.toOption, + ), + ), + ) + | Types.ResponseType.DocumentResponse(_) => + onLoad( + Types.ResponseType.Ok( + Types.ResponseType.DocumentResponse( + xhr->Warp_XHR.responseDocument->Js.Nullable.toOption, + ), + ), + ) + | Types.ResponseType.ArrayBufferResponse(_) => + onLoad( + Types.ResponseType.Ok( + Types.ResponseType.ArrayBufferResponse( + xhr->Warp_XHR.responseArrayBuffer->Js.Nullable.toOption, + ), + ), + ) + } + }); | None => () }; diff --git a/src/Warp.rei b/src/Warp.rei index f5dd286..e02449f 100644 --- a/src/Warp.rei +++ b/src/Warp.rei @@ -6,6 +6,7 @@ module FormData = Warp_FormData; module ResponseType = Warp_ResponseType; module Types = Warp_Types; module Event = Warp_Event; +module Settings = Warp_Settings; let send: Types.Client.t(Types.ResponseType.payload('a)) => option(unit => unit); \ No newline at end of file diff --git a/src/Warp/Types/Warp_Types_Client.re b/src/Warp/Types/Warp_Types_Client.re index 9a2906d..65f79a1 100644 --- a/src/Warp/Types/Warp_Types_Client.re +++ b/src/Warp/Types/Warp_Types_Client.re @@ -1,6 +1,9 @@ type t('a) = { url: string, method: Warp_Types_Method.t, + timeout: int, + async: bool, + withCredentials: bool, queryString: list((string, string)), formData: list((string, string)), headers: list((string, string)), diff --git a/src/Warp/Warp_Client.re b/src/Warp/Warp_Client.re index b4c3823..840ca3f 100644 --- a/src/Warp/Warp_Client.re +++ b/src/Warp/Warp_Client.re @@ -4,6 +4,9 @@ let make = (~url, ~method) => { { url, method, + timeout: 0, + async: true, + withCredentials: false, queryString: [], responseType: Warp_Types_ResponseType.TextResponse(None), formData: [], diff --git a/src/Warp/Warp_Settings.re b/src/Warp/Warp_Settings.re new file mode 100644 index 0000000..208e3c9 --- /dev/null +++ b/src/Warp/Warp_Settings.re @@ -0,0 +1,13 @@ +open Warp_Types_Client; + +let withCredentials = (client, withCredentials) => { + {...client, withCredentials}; +}; + +let async = (client, async) => { + {...client, async}; +}; + +let timeout = (client, timeout) => { + {...client, timeout}; +}; \ No newline at end of file diff --git a/src/Warp/Warp_Settings.rei b/src/Warp/Warp_Settings.rei new file mode 100644 index 0000000..68ef231 --- /dev/null +++ b/src/Warp/Warp_Settings.rei @@ -0,0 +1,6 @@ +let withCredentials: + (Warp_Types_Client.t('a), bool) => Warp_Types_Client.t('a); + +let async: (Warp_Types_Client.t('a), bool) => Warp_Types_Client.t('a); + +let timeout: (Warp_Types_Client.t('a), int) => Warp_Types_Client.t('a); \ No newline at end of file