package dream-html

  1. Overview
  2. Docs

This PPX provides an extension point to create route paths.

You can add it to your dune file in the usual way: (preprocess (pps dream-html.ppx)).

Then create a path: let%path orders = "/orders".

And use it in a route: Dream_html.get orders (fun req -> ...).

The PPX expands the above path to:

let orders = Dream_html.path "/orders" "/orders"

Ie, it just duplicates the path string to use as two separate format strings with different types for parsing and printing. If you need to actually have a different format string for printing (eg if you need to print the path with query parameters), you can use the underlying Dream_html.path function directly: path "/orders/%s" "/orders/%s?utm_src=%s&utm_campaign=%s".

The PPX also has the benefit that it checks that the path is well-formed at compile time. If you pass in an invalid path you get a compile error:

File "test/route_test.ml", line 1, characters 0-20:
1 | let%path bad = "foo"
    ^^^^^^^^^^^^^^^^^^^^
Error: Invalid path: 'foo'. Paths must start with a '/' character
  • since 3.9.0
OCaml

Innovation. Community. Security.