rescript-atdgen-codec-runtime
is a ReScript runtime for
atdgen, a code-generation tool for JSON handling. It is based on the direct json types of ReScript.
yarn add rescript-atdgen-codec-runtime
This package doesn't include running atdgen
to derive code from type definitions. For that, you need rescript-atdgen-generator
to generate the .ml
and .mli
files from .atd
sources.
atd
is an OCaml tool, and generates OCaml files. ReScript still supports compiling OCaml, but the atd
generated code does not support uncurried mode. The ahrefs team is open to a contribution that generates ReScript instead, if someone is up for the task. For more information and to add your voice to the discussion please follow this github issue.
Add rescript-atdgen-generator
to the bs-dependencies
of bsconfig.json
.
To write atd type definitions, please have a look at the atd documentation.
ReasonML code to query and deserialize the response of a REST API using the meetup.atd
file in example/src
. It requires bs-fetch
.
(TODO: rewrite in ReScript with either rescript-webapi or rescript-fetch)
let get = (url, decode) =>
Js.Promise.(
Fetch.fetchWithInit(
url,
Fetch.RequestInit.make(~method_=Get, ()),
)
|> then_(Fetch.Response.json)
|> then_(json => json |> decode |> resolve)
);
let v: Meetup_t.events =
get(
"http://localhost:8000/events",
Atdgen_codec_runtime.Decode.decode(Meetup_bs.read_events),
);
The example directory contains a full example of a simple cli to read and write data in a JSON file. It is written in OCaml, contributions to rewrite it in ReScript (the syntax swap is a one-line shell command, the style needs a bit more work) would be welcome.
For a complete introduction from atdgen installation to json manipulation, please refer to Getting started with atdgen and melange. The article talks about melange a lot, which isn't relevant, but it does still serve as a good introduction to atd
syntax.
All code is licensed as MIT. See LICENSE.
This project was forked from @ahrefs/bs-atdgen-codec-runtime. That project has now migrated to melange, I made a copy of the commit I forked from here.