Skip to content

Instantly share code, notes, and snippets.

@AngelMunoz
Created June 30, 2022 19:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AngelMunoz/32a4d06cc4ddcbebaf5e5b577ffb7080 to your computer and use it in GitHub Desktop.
Save AngelMunoz/32a4d06cc4ddcbebaf5e5b577ffb7080 to your computer and use it in GitHub Desktop.
A custom written bindings for https://github.com/krasimir/navigo
module Navigo
open System
open Fable.Core
type DoneFn<'T> = 'T -> unit
type GenerateOptions =
abstract includeRoot: bool
[<StringEnum>]
type ResolveStrategy =
| [<CompiledName "ONE">] One
| [<CompiledName "ALL">] All
type ResolveOptions =
abstract strategy: ResolveStrategy option
abstract hash: bool option
abstract noMatchWarning: bool option
type NavigateOptions =
abstract title: string option
abstract state: obj option
abstract historyAPIMethod: string option
abstract updateBrowserURL: bool option
abstract callHandler: bool option
abstract callHooks: bool option
abstract updateState: bool option
abstract force: bool option
abstract resolveOptions: ResolveOptions option
type RouterOptions =
inherit NavigateOptions
abstract linksSelector: string option
type Route =
abstract name: string
abstract path: string
abstract Handler: 'T -> unit
abstract hooks: RouterHooks
and RouterHooks =
abstract before: ((DoneFn<obj> * Match) -> unit) option
abstract after: (Match -> unit) option
abstract leave: ((DoneFn<obj> * U2<Match, Match array>) -> unit) option
abstract already: (Match -> unit) option
and Match =
abstract url: string
abstract queryString: string
abstract hashString: string
abstract route: Route
abstract data: Option<obj>
abstract ``params``: Option<obj>
[<ImportDefault("navigo")>]
type Navigo(root: string, ?options: obj) =
member _.destroyed: bool = jsNative
member _.current: Match array = jsNative
member _.routes: Route array = jsNative
member _.on<'T>(fn: 'T -> unit, ?hooks: RouterHooks) : Navigo = jsNative
member _.on(fn: Match -> unit, ?hooks: RouterHooks) : Navigo = jsNative
member _.on<'T>(path: string, fn: 'T -> unit, ?hooks: RouterHooks) : Navigo = jsNative
member _.on(path: string, fn: Match -> unit, ?hooks: RouterHooks) : Navigo = jsNative
member _.on(map: obj, ?hooks: RouterHooks) : Navigo = jsNative
member _.off(path: string) : Navigo = jsNative
member _.off<'T>(fn: 'T -> unit) : Navigo = jsNative
member _.navigate(where: string, ?options: NavigateOptions) : unit = jsNative
member _.navigateByName(name: string, ?data: obj, ?options: NavigateOptions) : unit = jsNative
member _.resolve(?path: string, ?resolveOptions: ResolveOptions) : U2<bool, Match> = jsNative
member _.destroy() : unit = jsNative
member _.notFound<'T>(handler: 'T -> unit, ?hooks: RouterHooks) : Navigo = jsNative
member _.notFound(handler: obj -> unit, ?hooks: RouterHooks) : Navigo = jsNative
member _.updatePageLinks() : Navigo = jsNative
member _.link(path: string) : string = jsNative
member _.lastResolved() : (Match array) option = jsNative
member _.generate(name: string, ?data: obj, ?options: GenerateOptions) : string = jsNative
member _.hooks(hooks: RouterHooks) : Navigo = jsNative
member _.getLinkPath(link: obj) : string = jsNative
member _.``match``(path: string) : U2<bool, Match array> = jsNative
member _.matchLocation(path: string, ?currentLocation: string, ?annotatePathWithRoot: bool) : U2<bool, Match> =
jsNative
member _.getCurrentLocation() : Match = jsNative
member _.addBeforeHook(route: U2<string, Route>, hookFunction: Func<_>) : Func<_> = jsNative
member _.addAfterHook(route: U2<string, Route>, hookFunction: Func<_>) : Func<_> = jsNative
member _.addAlreadyHook(route: U2<string, Route>, hookFunction: Func<_>) : Func<_> = jsNative
member _.addLeaveHook(route: U2<string, Route>, hookFunction: Func<_>) : Func<_> = jsNative
member _.getRoute(name: string) : Route option = jsNative
module Router
open System
open Navigo
type Page =
| Root
| Home
| User of string
exception NotFoundExeption of Match
let Router = Navigo("/", {| hash = true |})
let RouterPage =
{ new IObservable<Page> with
override _.Subscribe(cb) =
Router
.on((fun _ -> cb.OnNext Page.Root))
.on("/home", (fun _ -> cb.OnNext Page.Home))
.on(
"/user/:id",
fun (data: {| data: {| id: string option |} option |}) ->
data.data
|> Option.map (fun data -> data.id)
|> Option.flatten
|> Option.map (fun id -> cb.OnNext(Page.User id))
|> Option.defaultWith (fun _ -> cb.OnError(exn "No id found"))
)
.notFound(fun (data: Match) -> cb.OnError(NotFoundExeption data))
.resolve ()
|> ignore
{ new IDisposable with
member _.Dispose() = () } }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment