I am trying to convert some JS to Reason, along the way I need to type a JSON response and also check if a key exists in an object.
This is my current code:
let api_key = "";
let api_url = "http://ws.audioscrobbler.com/2.0";
let method = "user.getRecentTracks";
let user = "montogeek";
type trackAttr = {
nowplaying: bool
};
type artistT = {
text: string
}
type trackT = {
attr: trackAttr,
name: string,
artist: artistT
};
type recentTrackT = {
track: array(Js.Dict.t(trackT))
};
type response = {
recenttracks: recentTrackT
};
Js.Promise.(Fetch.fetch(api_url ++ "?method=" ++ method ++ "&" ++ user ++ "=" ++ user ++ "&limit=1&format=json&api_key=" ++ api_key)
|> then_(Fetch.Response.json)
|> then_(json: response => {
let lasttrack = json.recenttracks.track[0];
let online = switch (Js.Dict.get(lasttrack, "attr")) {
| None => false
| Some(track) => track.attr.nowplaying
};
let info = online ? "Enjoying" ++ lasttrack.name ++ "by " ++ lasttrack.artist["#text"] ++ "}" : "";
{ online, info }
}));
Currently I am getting this error:
We've found a bug for you!
/Users/montogeek/Infinite/Lov/online/src/lastfm.re 37:41-49
35 ┆ | Some(track) => track.attr.nowplaying
36 ┆ };
37 ┆ let info = online ? "Enjoying" ++ lasttrack.name ++ "by " ++ lasttrack
.artist["#text"] ++ "}" : "";
38 ┆
39 ┆ { online, info }
This has type:
Js.Dict.t(trackT) (defined as Js.Dict.t(trackT))
But somewhere wanted:
trackT
I can't remove Js.Dict.t
type because Js.Dict.get
won't like it.
How can I type the response so it works?
Thanks!