I always used bs-json library to convert Json results from Fetch into ReasonML records. Actually, this is good, as I get runtime validations using the same types, as during compilation time. But this requires a lot of code to encode/decode fields.
So, I decided to try [@bs.deriving abstract]: https://bucklescript.github.io/docs/en/object.html#creation and have issues correctly using it in Fetch promises.
Giving such abstract types:
[@bs.deriving abstract]
type tIssue = {
author: string,
authorLink: string,
summary: string,
linkToArticle: string,
};
[@bs.deriving abstract] **/* btw, how can I declare abstract arrays? :-| */**
type arrayOfIssues = {value: array(tIssue)};
I try to use constructors for ‘tIssue’ and ‘arrayOfIssues’ to get my objects. But these constructors require labeled arguments for each of type fields.
How can I send ‘result’ from following code into constructor?:
Js.Promise.(
Fetch.fetch(
"/issues/" ++ string_of_int(issueNumber) ++ “.json”,
)
|> then_(Fetch.Response.json)
|> then_(result => {
let typedResult = tIssue(result) /* this does not work obviously */
self.send(
RenderIssue(arrayOfIssues(~value=[|typedResult|])),
);
Js.Promise.resolve();
})
|> ignore
)
If I try to access result##author…etc like this:
let typedResult = tIssue(~author=result##author…);
Then, the compiler gives me another valid error:
We’ve found a bug for you!
/home/gladimdim/projects/reasonmlonline/src/components/issue.re 35:18-47:1933 ┆ )
34 ┆ |> then_(Fetch.Response.json)
35 ┆ |> then_(result => {
36 ┆ let typedResult =
. ┆ …
46 ┆ Js.Promise.resolve();
47 ┆ })
48 ┆ |> ignore
49 ┆ )This has type:
Js.Promise.t(Js.t(({… author: string, authorLink: string,
linkToArticle: string, summary: string}
as 'a))) =>
Js.Promise.t(unit)
But somewhere wanted:
Js.Promise.t(Js.Json.t) => 'bThe incompatible parts:
Js.Promise.t(Js.t('a)) (defined as Js.Promise.t(Js.t('a)))
vs
Js.Promise.t(Js.Json.t) (defined as Js.Promise.t(Js.Json.t))Further expanded: Js.t('a) vs Js.Json.t (defined as Js.Json.t)ninja: build stopped: subcommand failed.
Finish compiling(exit: 1)
Is this [bs.deriving abstract] indended to only send data to JS world? And it was not designed to get some data from it?

