Created
February 22, 2020 12:52
-
-
Save r17x/e6dd69041faad9a754cfec9ba9ee30d1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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