furl icon indicating copy to clipboard operation
furl copied to clipboard

Redesign the API

Open Drup opened this issue 6 years ago • 2 comments

New WIP API:

type 'a ty
val string : string ty
val int : int ty
val list : 'a ty -> 'a list ty
val option : 'a ty -> 'a option ty
val flag : string -> bool ty
val regex : Re.t -> string ty

type 'a key = ..

val meth : meth key
val hash : 'a ty -> 'a key
val header : string -> 'a ty -> 'a key

type 'a t

val return : 'a -> 'a t
(** Doesn't match anything *)

(** Lookup keys *)

val get : 'a key -> ('a -> 'b t) -> 'b t
val record : 'a key -> 'a t
val check : 'a key -> 'a -> unit t

(** Lookup path *)

val s : string -> unit t
val v : 'a ty -> 'a t
val (/) : 'a t -> 'b t -> ('a * 'b) t

(** Lookup queries *)

val query : string -> 'a ty -> 'a t

Drup avatar Apr 18 '19 14:04 Drup

type 'a key = private ..

Maybe? To restrict introduction of new constructors.

let-def avatar Apr 18 '19 14:04 let-def

@let-def Precisely, no. I want the concrete implementation of the API to be able to introduce new keys.

  • cohttp would introduce meth/hash/headers
  • an SPA framework would only handle cookies/hash
  • a middleware API would probably introduce some special purpose keys to access the state of other middleware (for instance, auth).

You could track it all via typing + polyvar, but I feel a more lightweight approach does not loose so much safety, and is much nicer to use, and we don't really need extensible function, we can just have:

val match : 'a t -> ('a key -> 'a option) -> Url.t -> 'a

Drup avatar Apr 18 '19 14:04 Drup