named-servant 0.2.0 → 0.3.0
raw patch · 3 files changed
+63/−2 lines, 3 filesdep ~servant
Dependency ranges changed: servant
Files
- named-servant.cabal +3/−2
- src/Servant/Named.hs +35/−0
- src/Servant/Record.hs +25/−0
named-servant.cabal view
@@ -1,12 +1,13 @@ cabal-version: 1.12 name: named-servant-description: support named parameters in servant using the named package-version: 0.2.0+synopsis: support records and named (from the named package) parameters in servant+version: 0.3.0 maintainer: kristof@resonata.be copyright: Kristof Bastiaensen 2020 license: BSD3 license-file: LICENSE build-type: Simple+description: Having positional parameters in Servant can be error prone, especially if you have lot of them, and they have similar types. This package solves that problem by either using records or named parameters (from the named package) to specify servant parameters. To use with servers, use the /named-servant-server/ package, with clients use /named-servant-client/. source-repository head type: git
src/Servant/Named.hs view
@@ -7,6 +7,32 @@ {-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE UndecidableInstances #-} {-# LANGUAGE EmptyDataDecls #-}++{-|+This module uses the named package to match names with parameters. For example, this api:++@+type API = "users" :> (QueryParam "category" Category :>+ QueryParam' '[Required, Strict] "sort_by" SortBy :>+ QueryFlag "with_schema" :>+ QueryParams "filters" Filter :>+ Get '[JSON] User+@++can be written with named:++@+type API = "users" :> (OptionalQueryParam "category" Category :>+ NamedQueryParam "sort_by" SortBy :>+ NamedQueryFlag "with_schema" :>+ NamedQueryParams "filters" Filter :>+ Get '[JSON] User+@++The servant-named-client and servant-named-server will create+functions that use the `named` package to match the names with the+parameters.+-} module Servant.Named (NamedQueryParam, OptionalQueryParam, NamedQueryParams, NamedQueryFlag, NamedQueryParam') where import Servant.API@@ -24,6 +50,15 @@ unarg :: NamedF f a name -> f a unarg (ArgF a) = a++-- | type family to rewrite a named queryparam to a regular+-- queryparam. Useful to define instances for classes that extract+-- information from the API type., for example servant-foreign, or+-- servant-swagger.+type family UnNameParam x where+ UnNameParam (NamedQueryParams sym a) = QueryParams sym a+ UnNameParam (NamedQueryParam' mods sym a) = QueryParam' mods sym a+ UnNameParam (NamedQueryFlag sym) = QueryFlag sym instance (KnownSymbol sym, ToHttpApiData v, HasLink sub, SBoolI (FoldRequired mods))
src/Servant/Record.hs view
@@ -15,6 +15,31 @@ import GHC.TypeLits import GHC.Generics +-- | RecordParam uses the fields in the record to represent the+-- parameters. The name of the field is used as parameter name, and+-- the type is the return type. For example, this api:+--+-- @+-- type API = "users" :> (QueryParam "category" Category :>+-- QueryParam' '[Required, Strict] "sort_by" SortBy :>+-- QueryFlag "with_schema" :>+-- QueryParams "filters" Filter :>+-- Get '[JSON] User+-- @+-- +-- can be written with records:+--+-- @+-- data UserParams = UserParams+-- { category :: Maybe Category+-- , sort_by :: Sortby+-- , with_schema :: Bool+-- , filters :: [Filter]+-- }+--+-- type API = "users" :> RecordParam UserParams :> Get '[JSON] User+-- @+ data RecordParam (a :: *) type family ServantAppend x y where