Atdgen Workshop Starter Project
Goal
Learn about atdgen
and how it automates the generation of encoders / decoders to make sure different parts of an application stay type safe. To do so, a small server-client app created with Node.js (via bs-express
) and ReasonReact is updated through a series of milestones.
Repo overview
bin
: contains theatdgen
executable, compiled to JavaScriptshared
: contains the shared types between client and serversrc_client
: a ReasonReact app that will be used to visualize the obtained data in the browsersrc_server
: a server that usesbs-express
. It exposes an API endpoint (/refdomains
) that the ReasonReact app insrc_client
will use to fetch data. It includes a filerefdomains.json
with some demo data that represents referring domains.
Getting started
cd atdgen-workshop-starter
# Can use npm too
yarn install
# Runs the bsb watcher to compile the code on change
yarn start
# In a separate terminal, start the API and static server
yarn server
Now, open your browser in http://localhost:8000/
, you should see a table rendered.
Suggested approach
Milestone 1: Use bs-json to encode/decode data
- Start the server by running
yarn server
(ornpm run server
) - Write encoders and decoders for
Refdomain.t
inshared/Refdomain.re
- In
src_client/RefdomainsTable.re
show the data fetched on the UI
Milestone 2: Automate encoders & decoders generation with atdgen
Before starting, read the "2-minutes intro to atdgen" documentation.
- Remove
bs-json
from the package andbsconfig.json
- Add the atdgen runtime as dependency:
yarn add @ahrefs/bs-atdgen-codec-runtime
. This runtime is what will allow later to call functions likeAtdgen_codec_runtime.Decode.decode
from the server and client. - Replace RefDomains.re with a new
refdomains.atd
file, based on the types inRefdomain.re
from milestone 1 (you can see theexamples
folder for a reference). - Use atdgen to generate the encoders and decoders automatically
node bin/atdgen.js -t shared/refdomains.atd
to generate the typesnode bin/atdgen.js -bs shared/refdomains.atd
to generate the functions
- Use the generated
Refdomains_bs
module to replace the calls toRefdomains.decodeMain
andRefdomains.encodeMain
Milestone 3: Keep code in sync through ATD generated types
-
Change
lastVisited
fromstring
todate
. You will have to use the "custom wrappers" feature from atdgen. -
Change
domain_rating
to be a variant, with three values:
Poor
: if domain rating is < 33Average
: if domain rating is > 33 and < 66Good
: if domain rating is > 66
### Stretch goals
- Use atdgen to encode / decode the
stats
field - Wrap the decoding operations on the server with
Belt.Result
types and use atdgen to pass that information to the client - Show colors for each of the three values of the variant
DomainRating