andre-dietrich / elm-url-extension / UrlExtension

This is a simple wrapper for elm/url that allows to deal with custom protocols, next to http and https. It does not take into account the different standards for file, ftp, etc, it simply allows to define different protocols, while still relying on the elm/url parsing methods.


type Protocol
    = HTTP
    | HTTPS
    | FILE
    | FTP
    | CUSTOM String

To don't interfere with the original Url.Protocol type, this type is defined in uppercase.


type alias Url =
{ protocol : Protocol
, host : String
, port_ : Maybe Basics.Int
, path : String
, query : Maybe String
, fragment : Maybe String 
}

This is actually only an replacement for the Url.Url type, the only difference is the internal usage of a separate Protocol type.

fromString : String -> Maybe Url

This is a shortcut for fromStringWithProtocol, which uses only use the ftp and file protocol. If you want to define your own custom protocols for internal routing use fromStringWithProtocol.

fromString "file:///home/user/example.png"
--> Just { fragment = Nothing, host = "", path = "/home/user/example.png", port_ = Nothing, protocol = FILE, query = Nothing }

fromString "file://C:/home/user/example.png"
--> Just { fragment = Nothing, host = "C:", path = "/home/user/example.png", port_ = Nothing, protocol = FILE, query = Nothing }

fromString "ftp://internet.address.edu/path/file.gz"
--> Just { fragment = Nothing, host = "internet.address.edu", path = "/path/file.gz", port_ = Nothing, protocol = FTP, query = Nothing }

fromStringWithProtocol : List String -> String -> Maybe Url

Use this, if you want to use more protocols, such as ipfs, dweb, or something custom

fromStringWithProtocol
    ["ipfs", "custom"]
    "ipfs://bafybeiemxf5abjwjbikoz4mc3a3dla6ual3jsgpdr4cjr3oz3evfyavhwq/wiki/Vincent_van_Gogh.html"
--> Just { fragment = Nothing, host = "bafybeiemxf5abjwjbikoz4mc3a3dla6ual3jsgpdr4cjr3oz3evfyavhwq", path = "/wiki/Vincent_van_Gogh.html", port_ = Nothing, protocol = CUSTOM "ipfs", query = Nothing }

fromStringWithProtocol
    ["ipfs", "custom"]
    "custom://RouteTo/http://example.com"
--> Just { fragment = Nothing, host = "RouteTo", path = "/http://example.com", port_ = Nothing, protocol = CUSTOM "custom", query = Nothing }

toString : Url -> String

Use this to turn your Url back into a string:

{ fragment = Nothing
, host = "bafybeiemxf5abjwjbikoz4mc3a3dla6ual3jsgpdr4cjr3oz3evfyavhwq"
, path = "/wiki/Vincent_van_Gogh.html"
, port_ = Nothing
, protocol = CUSTOM "ipfs"
, query = Nothing
}
|> toString
--> "ipfs://bafybeiemxf5abjwjbikoz4mc3a3dla6ual3jsgpdr4cjr3oz3evfyavhwq/wiki/Vincent_van_Gogh.html"

parse : Url.Parser.Parser (a -> a) a -> Url -> Maybe a

Use this function to wrap your custom protocol into Url.Http, so that you can run your parsers from the Url.Parser module.