reazen / relude-fetch Goto Github PK
View Code? Open in Web Editor NEWRelude interop library for the fetch API
License: MIT License
Relude interop library for the fetch API
License: MIT License
I'm struggling to understand the decoding example in the tests
let decode: Js.Json.t => Belt.Result.t(t, string) =
json => {
make
<$> JD.intFor("userId", json)
<*> JD.intFor("id", json)
<*> JD.stringFor("title", json)
<*> JD.boolFor("completed", json)
|> Relude.Result.fromValidation
|> Relude.Result.mapError(_ => "Decode failed");
};
Would this be equal to the following?
let decode: Js.Json.t => Belt.Result.t(t, string) =
json => {
make
|> map(JD.intFor("userId", json))
|> ap(JD.intFor("id", json))
|> ap(JD.stringFor("title", json))
|> ap(JD.boolFor("completed", json))
|> Relude.Result.fromValidation
|> Relude.Result.mapError(_ => "Decode failed");
};
With some direction, I'd be glad to open a PR adding examples sans infix operators. It would be nice to able to see the two versions side by side for mere mortals like I.
Instead of taking in a string as the url for fetching, should this lib instead take in the url type from https://github.com/reazen/relude-url ?
Currently, we're assuming (really, bs-fetch
is assuming) that the fetch
global exists. This is true for browsers, but for other environments like Node or React Native, this requires a polyfill like isomorphic-fetch
or cross-fetch
. Importing one of those libraries purely for the side-effect of attaching junk to the global object is really gross (and worse, it's easy to forget to do, which could cause things to break at runtime).
I'm definitely open to ideas, but it seems like the safest thing to do would be to expect ReludeFetch to be constructed with a Fetch
object (module?) that provides the actual fetch implementation. In a more perfect world, maybe we'd have a trifunctor IO like ZIO where the Fetch interface could just be part of the environment.
Hi guys, thanks for the awesome libraries!
I tried to do this using ReludeFetch but the code couldn't comple since the output of ReludeFetch.post
is not compatible with the input of ReludeFetch.Response.StatusCode.ensure2xx
.
module IO = Relude.IO;
module Error = ReludeFetch.Error;
module IOE = IO.WithError({
type t = Error.t(Decco.decodeError)
})
[@decco encode]
type request = {
username: string,
password: string,
};
[@decco decode]
type response = {
token: string,
};
let login = (~username, ~password) => {
ReludeFetch.post(
~headers=Headers.acceptJson,
~body=
ReludeFetch.Body.Content.Json(
request_encode({
username,
password,
}),
),
"/some/login",
)
>>= ReludeFetch.Response.StatusCode.ensure2xx;
};
};
When I look at the this it has Relude.IO.t(Fetch.response, Error.t('decodeError))
but it should be Fetch.Response.t
. Am I compose the wrong function here?
I've been trying to use this library, but hit a wall when trying to work with the response. I'm not sure if this is an error on my side (understanding) or issue with the library as I'm getting the following error with the demo example too:
open Relude;
// Defines our API error type
module Error = {
type t = ReludeFetch.Error.t(string);
let show = error => ReludeFetch.Error.show(a => a, error);
module Type = {
type nonrec t = t;
};
};
// Open the IO infix operators
module IOE = IO.WithError(Error.Type);
open IOE.Infix;
let textResponse = ReludeFetch.fetch("some_url") >>= ReludeFetch.Response.text;
Last line errors with:
Error: This expression has type
Fetch.Response.t => Relude.IO.t(string, ReludeFetch_Error.t('a))
but an expression was expected of type
Fetch.response => IOE.Monad.t('b)
Type Fetch.Response.t is not compatible with type Fetch.response
Link to relude
's IO documentation for info on IO
.
Provide an example of using the IO-based fetch - e.g. in a ReasonReact app?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.