Home

Awesome

trout-demo build status

Trout and code sharing in PureScript

Trout is a type-level routing DSL. Similar to Haskell's Servant library, Trout allows routes to be specified as a data type. For example, a GET /api/tasks route that responds with an Array Task in JSON format can be represented as "api" :/ "tasks" :> Resource (Get (Array Task) JSON). Trout provides various combinators for matching literal URL segments, parsing route parameters, extracting headers, reading the request body, and more.

Multiple options exist for building an HTTP server from a Trout specification. These are helpful because they allow request handlers to focus on domain logic rather than uninteresting protocol-level details. One such option is purescript-hypertrout, originally by Oskar Wickström, who also designed Trout. The library used here, however, is purescript-nodetrout.

Similarly, on the client side, the purescript-trout-client library can generate a convenient interface that hides the low-level details of building an HTTP request, e.g. constructing the URL from various parameters or serializing the request payload.

Using these tools, the server and client layers of this application are constructed from the same API specification. Because the server and client are both written in PureScript, sharing types and even functions is effortless. The Credentials and Task modules, for example, are used both server-side and client-side.

Building and running the app

Using Spago:

spago bundle-app --to static/app.js --main Client && spago run --main Server