Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/thespyder/rescript-atdgen-codec-runtime


https://github.com/thespyder/rescript-atdgen-codec-runtime

Last synced: about 1 month ago
JSON representation

Awesome Lists containing this project

README

        

# rescript-atdgen-codec-runtime

`rescript-atdgen-codec-runtime` is a ReScript runtime for
[atdgen](https://github.com/ahrefs/atd), a code-generation tool for JSON handling. It is based on the direct json types of ReScript.

## Installation

```
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`](https://github.com/TheSpyder/rescript-atdgen-generator) to generate the `.ml` and `.mli` files from `.atd` sources.

## A note about ReScript 11
`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](https://github.com/ahrefs/atd/issues/410).

## Usage

Add `rescript-atdgen-generator` to the `bs-dependencies` of `bsconfig.json`.

To write atd type definitions, please have a look at the [atd documentation](https://atd.readthedocs.io/en/latest/).

## Simple example

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)

```reason
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),
);
```

## Full example

The [example](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](https://tech.ahrefs.com/getting-started-with-atdgen-and-bucklescript-1f3a14004081). The article talks about melange a lot, which isn't relevant, but it does still serve as a good introduction to `atd` syntax.

## License and Credits

All code is licensed as MIT. See [LICENSE](LICENSE).

This project was forked from [@ahrefs/bs-atdgen-codec-runtime](https://github.com/ahrefs/bs-atdgen-codec-runtime). That project has now migrated to melange, I made a copy of the commit I forked from [here](https://github.com/thespyder/bs-atdgen-codec-runtime).