|
| 1 | +type modifier = |
| 2 | + | Uppercase |
| 3 | + | Lowercase |
| 4 | + (** this a custom type which we want to be able to serialize/deserialize |
| 5 | + from/to the URL query *) |
| 6 | + |
| 7 | +let modifier_of_url_query k xs = |
| 8 | + match List.assoc_opt k xs with |
| 9 | + | Some "uppercase" -> Ok Uppercase |
| 10 | + | Some "lowercase" -> Ok Lowercase |
| 11 | + | Some _ -> Error "invalid modifier" |
| 12 | + | None -> Error "missing modifier" |
| 13 | + |
| 14 | +let modifier_to_url_query k = function |
| 15 | + | Uppercase -> [ k, "uppercase" ] |
| 16 | + | Lowercase -> [ k, "lowercase" ] |
| 17 | + |
| 18 | +module Options = struct |
| 19 | + open Ppx_deriving_json_runtime.Primitives |
| 20 | + |
| 21 | + type t = { a : int option } [@@deriving json, url_query_via_json] |
| 22 | +end |
| 23 | + |
| 24 | +module User_id : sig |
| 25 | + type t |
| 26 | + |
| 27 | + val inject : string -> t |
| 28 | + val project : t -> string |
| 29 | +end = struct |
| 30 | + type t = string |
| 31 | + |
| 32 | + let inject x = x |
| 33 | + let project x = x |
| 34 | +end |
| 35 | + |
| 36 | +module Level = struct |
| 37 | + type t = Alert | Warning |
| 38 | + |
| 39 | + let to_int = function Alert -> 2 | Warning -> 1 |
| 40 | + |
| 41 | + let of_int = function |
| 42 | + | 2 -> Alert |
| 43 | + | 1 -> Warning |
| 44 | + | _ -> failwith "invalid level" |
| 45 | +end |
| 46 | + |
| 47 | +module Pages = struct |
| 48 | + open Ppx_deriving_router_runtime.Primitives |
| 49 | + |
| 50 | + type user_id = User_id.t |
| 51 | + [@@deriving url_query_via_iso, url_path_via_iso] |
| 52 | + |
| 53 | + type level = Level.t |
| 54 | + [@@deriving |
| 55 | + url_query_via_iso { t = int; inject = of_int; project = to_int }] |
| 56 | + |
| 57 | + type t = |
| 58 | + | Home [@GET "/"] |
| 59 | + | Hello of { |
| 60 | + name : string; |
| 61 | + modifier : modifier option; |
| 62 | + greeting : string option; |
| 63 | + } [@GET "/hello/:name"] |
| 64 | + | Echo_options of { options : Options.t } |
| 65 | + | List_users of { user_ids : user_id list } |
| 66 | + | User_info of { user_id : user_id } |
| 67 | + | User_info_via_path of { user_id : user_id } [@GET "/user/:user_id"] |
| 68 | + | Signal of { level : level } |
| 69 | + | Route_with_implicit_path of { param : string option } |
| 70 | + | Route_with_implicit_path_post [@POST] |
| 71 | + [@@deriving router] |
| 72 | +end |
| 73 | + |
| 74 | +module Api = struct |
| 75 | + open Ppx_deriving_router_runtime.Primitives |
| 76 | + open Ppx_deriving_json_runtime.Primitives |
| 77 | + |
| 78 | + type user = { id : int } [@@deriving json] |
| 79 | + |
| 80 | + type _ t = |
| 81 | + | List_users : user list t [@GET "/"] |
| 82 | + | Create_user : { id : int [@body] } -> user t [@POST "/"] |
| 83 | + | Get_user : { id : int } -> user t [@GET "/:id"] |
| 84 | + | Raw_response : Ppx_deriving_router_runtime.response t |
| 85 | + [@GET "/raw-response"] |
| 86 | + [@@deriving router] |
| 87 | +end |
| 88 | + |
| 89 | +module All = struct |
| 90 | + type _ t = |
| 91 | + | Pages : Pages.t -> Ppx_deriving_router_runtime.response t |
| 92 | + [@prefix "/"] |
| 93 | + | Api : 'a Api.t -> 'a t [@prefix "/nested/api"] |
| 94 | + | Static : { path : string } -> Ppx_deriving_router_runtime.response t |
| 95 | + [@GET "/static/...path"] |
| 96 | + [@@deriving router] |
| 97 | +end |
0 commit comments