Skip to content

Commit 404f77c

Browse files
authored
Fix Servant parameter handling (#875)
* Fix Servant parameter encoding The CPP is necessary, since `toUrlPiece` is incorrect, but we're tied to an old `http-api-data` on GHCJS. See fizruk/http-api-data#120. * Make query params optional when specified as such
1 parent 4fcce33 commit 404f77c

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

src/Miso/Fetch.hs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
{-# LANGUAGE CPP #-}
1212
{-# LANGUAGE RecordWildCards #-}
1313
{-# LANGUAGE TemplateHaskell #-}
14+
{-# LANGUAGE UndecidableInstances #-}
15+
{-# LANGUAGE FlexibleContexts #-}
1416
-----------------------------------------------------------------------------
1517
-- |
1618
-- Module : Miso.Fetch
@@ -64,6 +66,7 @@ import Data.Proxy (Proxy(..))
6466
import GHC.TypeLits
6567
import Language.Javascript.JSaddle (JSM)
6668
import Servant.API
69+
import Servant.API.Modifiers
6770
-----------------------------------------------------------------------------
6871
import Miso.FFI.Internal (fetchJSON)
6972
import Miso.Lens
@@ -130,19 +133,25 @@ instance (Fetch api, KnownSymbol path) => Fetch (path :> api) where
130133
options_ :: FetchOptions
131134
options_ = options & currentPath %~ (<> ms "/" <> path)
132135
-----------------------------------------------------------------------------
133-
instance (Show a, Fetch api, KnownSymbol path) => Fetch (Capture path a :> api) where
136+
instance (ToHttpApiData a, Fetch api, KnownSymbol path) => Fetch (Capture path a :> api) where
134137
type ToFetch (Capture path a :> api) = a -> ToFetch api
135138
fetchWith Proxy options arg = fetchWith (Proxy @api) options_
136139
where
137140
options_ :: FetchOptions
138-
options_ = options & currentPath %~ (<> ms "/" <> ms (show arg))
141+
options_ = options & currentPath %~ (<> ms "/" <> ms (toEncodedUrlPiece arg))
139142
-----------------------------------------------------------------------------
140-
instance (Show a, Fetch api, KnownSymbol name) => Fetch (QueryParam name a :> api) where
141-
type ToFetch (QueryParam name a :> api) = a -> ToFetch api
143+
instance (ToHttpApiData a, Fetch api, SBoolI (FoldRequired mods), KnownSymbol name) => Fetch (QueryParam' mods name a :> api) where
144+
type ToFetch (QueryParam' mods name a :> api) = RequiredArgument mods a -> ToFetch api
142145
fetchWith Proxy options arg = fetchWith (Proxy @api) options_
143146
where
147+
param (x :: a) = [(ms "/", ms (enc x))]
148+
#if MIN_VERSION_http_api_data(0,5,1)
149+
enc = toEncodedQueryParam
150+
#else
151+
enc = toEncodedUrlPiece
152+
#endif
144153
options_ :: FetchOptions
145-
options_ = options & queryParams <>~ [(ms "/", ms (show arg))]
154+
options_ = options & queryParams <>~ foldRequiredArgument (Proxy @mods) param (foldMap param) arg
146155
-----------------------------------------------------------------------------
147156
instance (Fetch api, KnownSymbol name) => Fetch (QueryFlag name :> api) where
148157
type ToFetch (QueryFlag name :> api) = Bool -> ToFetch api

0 commit comments

Comments
 (0)