Warp is a Bucklescript library enabling http-requests over XHR.
If you find a bug or think some feature is missing, don't hesitate to file an issue or even a pull request.
Every little bit of help is very welcome!
yarn add @space-labs/warp
or
npm install @space-labs/warp --save
Then add @space-labs/warp
as a dependency to bsconfig.json
:
"bs-dependencies": [
+ "@space-labs/warp"
]
Warp.Method.options("https://domain.com/");
Warp.Method.get("https://domain.com/");
Warp.Method.head("https://domain.com/");
Warp.Method.post("https://domain.com/");
Warp.Method.put("https://domain.com/");
Warp.Method.delete("https://domain.com/");
Warp.Method.trace("https://domain.com/");
Warp.Method.connect("https://domain.com/");
let _ = Warp.Method.options "https://domain.com/"
let _ = Warp.Method.get "https://domain.com/"
let _ = Warp.Method.head "https://domain.com/"
let _ = Warp.Method.post "https://domain.com/"
let _ = Warp.Method.put "https://domain.com/"
let _ = Warp.Method.delete "https://domain.com/"
let _ = Warp.Method.trace "https://domain.com/"
let _ = Warp.Method.connect "https://domain.com/"
client->Warp.Settings.async(false);
client->Warp.Settings.timeout(5000);
client->Warp.Settings.withCredentials(true);
client->Warp.Settings.auth("user", "password");
client->Warp.Settings.overrideMimeType("text/plain");
2310: syntax error, consider adding a `;' before
client->Warp.QueryString.set([("key", "value"), ("key2", "value2")]);
client->Warp.QueryString.add("key", "value");
client->Warp.QueryString.remove("key");
2310: syntax error, consider adding a `;' before
client->Warp.FormData.set([("key", "value"), ("key2", "value2")]);
client->Warp.FormData.add("key", "value");
client->Warp.FormData.remove("key");
2310: syntax error, consider adding a `;' before
client->Warp.Header.set([("key", "value"), ("key2", "value2")]);
client->Warp.Header.add("key", "value");
client->Warp.Header.remove("key");
2310: syntax error, consider adding a `;' before
onProgress
client->Warp.Event.onProgress(_event => {
Js.Console.log("We are making progress!")
});
2310: syntax error, consider adding a `;' before
onLoad
The datatype of the response is based on the currently set ResponseType
(option(string)
by default).
client->Warp.Event.onLoad(response => {
switch (response) {
| Belt.Result.Ok(Some(data)) => Js.Console.log(data)
| Belt.Result.Ok(None) => Js.Console.info("Response was empty")
| Belt.Result.Error(message) => Js.Console.error(message)
}
});
2310: syntax error, consider adding a `;' before
onLoadWithStatusCode
Same as onLoad
but gets the requests statusCode as a second argument
client->Warp.Event.onLoadWithStatusCode((response, statusCode) => {
switch (response) {
| Belt.Result.Ok(Some(data)) => Js.Console.log2(data, statusCode)
| Belt.Result.Ok(None) => Js.Console.info2("No Response!", statusCode)
| Belt.Result.Error(message) => Js.Console.error2(message, statusCode)
}
});
2310: syntax error, consider adding a `;' before
onAbort
client->Warp.Event.onAbort(() => {
Js.Console.log("The request got aborted :(")
});
2310: syntax error, consider adding a `;' before
If you want to change the ResponseType
, you have to do it before the onLoad
. If you set it afterwards, your
onLoad
will be removed (because it would assume a wrong type signature).
ResponseType | PayloadType |
---|---|
Text | option(string) |
Json | option(Js.Json.t) |
Document | option(Dom.document) |
ArrayBuffer | option(Js.Typed_array.ArrayBuffer.t) |
client->Warp.ResponseType.setText; // default
client->Warp.ResponseType.setJson;
client->Warp.ResponseType.setDocument;
client->Warp.ResponseType.setArrayBuffer;
2310: syntax error, consider adding a `;' before
client->Warp.send;
2310: syntax error, consider adding a `;' before
Warp.send
returns a function, with which you may cancel the current request. It has a signature of
option(unit => unit)
, so you don't have to do anything when using it inside React.useEffect
.
/* React */
React.useEffect(() => {
Warp.Method.get("https://domain.com/")->Warp.send
});
/* Plain Reason */
let maybeCancel = Warp.Method.get("https://domain.com/")->Warp.send;
switch(maybeCancel) {
| Some(cancel) => cancel();
| None => ();
};
967: syntax error, consider adding a `;' before
Basic
Warp.Method.get("http://localhost:8081/")
->Warp.ResponseType.setJson
->Warp.QueryString.set([
("firstname", "Max"),
("lastname", "Mustermann"),
("username", "max"),
("email", "max@mustermann.de"),
])
->Warp.Header.add("authorization", "Bearer 123")
->Warp.Event.onLoad(response => {
switch (response) {
| Belt.Result.Ok(Some(data)) => Js.Console.log(data)
| Belt.Result.Ok(None) => Js.Console.info("No Response!")
| Belt.Result.Error(message) => Js.Console.error(message)
}
})
->Warp.send;
2310: syntax error, consider adding a `;' before
Sending JSON
let stringifiedJson =
Js.Dict.fromList([
("query", Js.Json.string(operation.text)),
("variables", variables),
])
->Js.Json.object_
->Js.Json.stringify;
Warp.Method.post("http://localhost:8081/")
->Warp.ResponseType.setJson
->Warp.FormData.setJson(stringifiedJson)
->Warp.Header.add("authorization", "Bearer 123")
->Warp.Event.onLoad(response => {
switch (response) {
| Belt.Result.Ok(Some(data)) => Js.Console.log(data)
| Belt.Result.Ok(None) => Js.Console.info("No Response!")
| Belt.Result.Error(message) => Js.Console.error(message)
}
})
->Warp.send;
967: syntax error, consider adding a `;' before
Methods
Headers
Query Strings
Form Data / Body
Response Types
Js.Typed_array.ArrayBuffer.t
Dom.document
Js.Json.t
string
Events
onLoad
onLoadWithStatusCode
onProgress
onAbort
Settings
Request - Simplified HTTP client for inspiration
@hgiraud on Discord had some nice suggestions regarding the API