Skip to content

Instantly share code, notes, and snippets.

@r17x
Created February 22, 2020 12:52
Show Gist options
  • Save r17x/e6dd69041faad9a754cfec9ba9ee30d1 to your computer and use it in GitHub Desktop.
Save r17x/e6dd69041faad9a754cfec9ba9ee30d1 to your computer and use it in GitHub Desktop.
/* https://api.github.com/users/ri7nz */
open Relude.Globals;
// Define Error, that consume in IO (sideEffect)
module Error = {
type t = ReludeFetch.Error.t(string);
let show = error => ReludeFetch.Error.show(a => a, error);
module Type = {
type nonrec t = t;
}
};
module User = {
[@decco.decode] // yarn add decco (ppx|syntax extension)
type t = {
login: string,
id: int,
name: string,
company: string
};
let decode = Js.Json.t => Belt.Result.t(t, string) =
json => json->t_encode |> Relude.Result.mapError(_ => "Decode Failed");
}
let baseUrl = "https://api.github.com";
let getUser = (username) : IO.t(User.t, Error.t) =>
{j|$baseUrl/users/$username|j}
-> ReludeFetch.fetch
>>= User.decode -> ReludeFetch.Response.Json.decode;
type state = {
user: AsyncResult.t(User.t, Error.t),
}
let initialState = { user: AsyncResult.init };
type action =
| FetchUser(string)
| FetchUserSuccess(User.t)
| FetchUserError(Error.t);
let reducer = (state, action): ReludeReact.Reducer.update(action, state) =>
switch(action){
| Fetch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment