Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/thespyder/rescript-atdgen-codec-runtime
https://github.com/thespyder/rescript-atdgen-codec-runtime
Last synced: about 1 month ago
JSON representation
- Host: GitHub
- URL: https://github.com/thespyder/rescript-atdgen-codec-runtime
- Owner: TheSpyder
- License: other
- Created: 2024-04-12T05:16:57.000Z (7 months ago)
- Default Branch: main
- Last Pushed: 2024-05-04T01:34:04.000Z (7 months ago)
- Last Synced: 2024-05-04T18:49:17.664Z (7 months ago)
- Language: ReScript
- Size: 664 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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).