network-uri-template: TODO

This is a package candidate release! Here you can preview how this package release will appear once published to the main package index (which can be accomplished via the 'maintain' link below). Please note that once a package has been published to the main package index it cannot be undone! Please consult the package uploading documentation for more information.

[maintain] [Publish]

Warnings:

Please see README.md


[Skip to Readme]

Properties

Versions 0.1.0.0, 0.1.0.0, 0.1.1.0
Change log CHANGELOG.md
Dependencies base (>=4.20.2.0 && <5), containers (>=0.7), megaparsec (>=9.7.0), network-uri (>=2.6.4.2), network-uri-template, optparse-applicative (>=0.18.1.0), prettyprinter (>=1.7.1), prettyprinter-ansi-terminal (>=1.1.3), text (>=2.1.3) [details]
License AGPL-3.0-only
Author
Maintainer Pat Brisbin
Uploaded by PatrickBrisbin at 2025-11-07T18:28:31Z

Modules

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees


Readme for network-uri-template-0.1.0.0

[back to package description]

network-uri-template

Library for parsing and expanding URI Templates, as per RFC 6570.

URI Templates

An example from the RFC:

For example, the following URI Template includes a form-style parameter expression, as indicated by the "?" operator appearing before the variable names.

  http://www.example.com/foo{?query,number}

The expansion process for expressions beginning with the question- mark ("?") operator follows the same pattern as form-style interfaces on the World Wide Web:

  http://www.example.com/foo{?query,number}
                            \_____________/
                               |
                               |
          For each defined variable in [ 'query', 'number' ],
          substitute "?" if it is the first substitution or "&"
          thereafter, followed by the variable name, '=', and the
          variable's value.

If the variables have the values

  query  := "mycelium"
  number := 100

then the expansion of the above URI Template is

  http://www.example.com/foo?query=mycelium&number=100

Alternatively, if 'query' is undefined, then the expansion would be

  http://www.example.com/foo?number=100

or if both variables are undefined, then it would be

  http://www.example.com/foo

For a complete description of URI Templates, consult the RFC or see our extracted test cases.

Example

{-# LANGUAGE OverloadedStrings #-}

module Main
  ( main
  ) where

import Prelude

import Data.Text (Text)
import Data.Text.IO qualified as T
import Data.Map.Strict (Map)
import Data.Map.Strict qualified as Map
import Network.URI.Template
import System.Exit (die)

main :: IO ()
main = do
  let
    vars :: Map VarName VarValue
    vars =
      Map.fromList
        [ ("query", "mycelium")
        , ("number", "100")
        ]

    template :: Text
    template = "http://www.example.com/foo{?query,number}"

    handle = either (die . templateErrorPretty) T.putStrLn

  handle $ processTemplate vars template
  -- => http://www.example.com/foo?query=mycelium&number=100

  handle $ processTemplate (Map.delete "query" vars) template
  -- => http://www.example.com/foo?number=100

  handle $ processTemplate (Map.empty) template
  -- => http://www.example.com/foo

CLI

This project includes a small CLI to experiment with template expansion.

network-uri-template \
  --var 'query  := "mycelium"' \
  --var 'number := "100"' \
  --var 'path   := ("foo", "bar")' \
  --var 'keys   := [("sort","asc"), ("page","2")]' \
  'http://www.example.com{/path*}/foo{?query:2,number}{&keys*}'

License

This project is licensed AGPLv3. See COPYING.