RouteProvider


RouteProvider

RouteProvider is an F# Type provider that generates types suitable for routing in a web application.

The RouteProvider library can be installed from NuGet:
PM> Install-Package RouteProvider -Pre

Example

This example initializes RouteProvider with 5 different routes:

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
[<Literal>]
let routes = """
  GET projects/{projectId} as getProject
  PUT projects/{foo:string} as updateProject
  POST projects/{projectName:string} as createProject
  GET projects/{projectId}/comments/{commentId} as getProjectComments
"""

[<Literal>]
let outputPath = __SOURCE_DIRECTORY__ + "\MyRoutes.fs"

type Dummy = IsakSky.RouteProvider<"MyRoutes", // name of generated type
    routes,     // string of routes to base routes on
    false,      // add a generic input type?
    false,      // add a generic output type?
    outputPath>

From this invocation, we'll get a type generated that looks like this

1: 
2: 
3: 
open MyNamespace
open MyNamespace.MyModule // default namespace and module names
type HoverMe = MyRoutes // Hover the type to see the signature

We can then build routes using this type in a strongly typed way:

1: 
2: 
let path1 = MyModule.getProjectComments 123L 4L
let path2 = MyModule.createProject "Voltron"

We can also create a router:

1: 
2: 
3: 
4: 
5: 
6: 
let router : MyRoutes =
  { getProject = fun p -> printfn "Hi project %d" p
    updateProject = fun ps -> printfn "Hi project string %s" ps
    getProjectComments = fun p c -> printfn "Hi project comment %d %d" p c
    createProject = fun p -> printfn "Creating project %d" p
    notFound = None }

We can then dispatch routes to this router like this, and the appropriate handler will be executed:

1: 
2: 
router.DispatchRoute("GET", "projects/4321/comments/1234")
// "You asked for project 4321 and comment 1234"

RouteProvider can also provide generic input arguments to integrate with web libraries. See the Suave example.

Sample projects

Contributing and copyright

The project is hosted on GitHub where you can report issues, fork the project and submit pull requests. If you're adding a new public API, please also consider adding samples that can be turned into a documentation. You might also want to read the library design notes to understand how it works.

The library is available under the MIT license. For more information see the License file in the GitHub repository.

Multiple items
type LiteralAttribute =
  inherit Attribute
  new : unit -> LiteralAttribute

Full name: Microsoft.FSharp.Core.LiteralAttribute

--------------------
new : unit -> LiteralAttribute
val routes : string

Full name: Index.routes
val outputPath : string

Full name: Index.outputPath
Multiple items
type Dummy =
  new : unit -> Dummy

Full name: Index.Dummy

--------------------
Dummy() : Dummy
namespace IsakSky
Multiple items
namespace IsakSky.RouteProvider

--------------------
type RouteProvider =
  new : unit -> RouteProvider

Full name: IsakSky.RouteProvider

--------------------
IsakSky.RouteProvider.RouteProvider() : IsakSky.RouteProvider.RouteProvider
namespace MyNamespace
module MyModule

from MyNamespace
type HoverMe = MyRoutes

Full name: Index.HoverMe
type MyRoutes =
  {getProject: int64 -> unit;
   updateProject: string -> unit;
   createProject: string -> unit;
   getProjectComments: int64 -> int64 -> unit;
   notFound: (string -> string -> unit) option;}
  member DispatchRoute : verb:string * path:string -> unit
  member DispatchRoute : verb:string * uri:Uri -> unit
  member private HandleNotFound : verb:string * path:string -> unit
  static member Router : getProject:(int64 -> unit) * updateProject:(string -> unit) * createProject:(string -> unit) * getProjectComments:(int64 -> int64 -> unit) * ?notFound:(string -> string -> unit) -> MyRoutes

Full name: MyNamespace.MyModule.MyRoutes
val path1 : string

Full name: Index.path1
val getProjectComments : projectId:int64 -> commentId:int64 -> string

Full name: MyNamespace.MyModule.getProjectComments
val path2 : string

Full name: Index.path2
val createProject : projectName:string -> string

Full name: MyNamespace.MyModule.createProject
val router : MyRoutes

Full name: Index.router
val getProject : projectId:int64 -> string

Full name: MyNamespace.MyModule.getProject
val p : int64
val printfn : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
val updateProject : foo:string -> string

Full name: MyNamespace.MyModule.updateProject
val ps : string
val c : int64
val p : string
union case Option.None: Option<'T>
member MyRoutes.DispatchRoute : verb:string * path:string -> unit
member MyRoutes.DispatchRoute : verb:string * uri:System.Uri -> unit
Fork me on GitHub