Skip to content

Instantly share code, notes, and snippets.

@Jwsonic
Created April 10, 2018 04:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jwsonic/9840aece63be89c0d7c1dc406186b175 to your computer and use it in GitHub Desktop.
Save Jwsonic/9840aece63be89c0d7c1dc406186b175 to your computer and use it in GitHub Desktop.
module type ApiClient = {let getFile: unit => string;};
module FakeApiClient: ApiClient = {
let getFile = () => "cat.jpg";
};
let unwrapUnsafely = data =>
switch (data) {
| Some(v) => v
| None => raise(Invalid_argument("unwrapUnsafely called on None"))
};
module HttpApiClient: ApiClient = {
let getFile = () =>
Js.Promise.(
Fetch.fetch("https://aws.random.cat/meow")
|> then_(Fetch.Response.json)
|> then_(json => Js.Json.decodeObject(json) |> resolve)
|> then_(opt => unwrapUnsafely(opt) |> resolve)
|> then_(~file => file |> resolve)
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment