Skip to content

Commit 2744a13

Browse files
author
Andrew Cady
committed
Implement HasForeign instance
1 parent 078a426 commit 2744a13

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

servant-multipart.cabal

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ library
4040
text >=1.2 && <1.3,
4141
transformers >=0.3 && <0.6,
4242
wai >= 3.2 && <3.3,
43-
wai-extra >=3.0 && <3.1
43+
wai-extra >=3.0 && <3.1,
44+
servant-foreign >= 0.11.2
4445
default-language: Haskell2010
4546

4647
executable upload

src/Servant/Multipart.hs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
{-# LANGUAGE MultiParamTypeClasses #-}
1313
{-# LANGUAGE StandaloneDeriving #-}
1414
{-# LANGUAGE UndecidableInstances #-}
15+
{-# LANGUAGE TypeApplications #-}
1516
-- | @multipart/form-data@ support for servant.
1617
--
1718
-- This is mostly useful for adding file upload support to
@@ -35,7 +36,7 @@ module Servant.Multipart
3536
, ToMultipartSample(..)
3637
) where
3738

38-
import Control.Lens ((<>~), (&), view)
39+
import Control.Lens ((<>~), (&), view, (.~))
3940
import Control.Monad.IO.Class
4041
import Control.Monad.Trans.Resource
4142
import Data.Foldable (foldMap)
@@ -49,6 +50,7 @@ import Network.Wai
4950
import Network.Wai.Parse
5051
import Servant
5152
import Servant.Docs
53+
import Servant.Foreign
5254
import Servant.Server.Internal
5355
import System.Directory
5456

@@ -525,3 +527,14 @@ instance (HasDocs api, ToMultipartSample tag a) => HasDocs (MultipartForm tag a
525527
(Proxy :: Proxy a)
526528
]
527529
in docsFor (Proxy :: Proxy api) (endpoint, newAction) opts
530+
531+
instance (HasForeignType lang ftype a, HasForeign lang ftype api)
532+
=> HasForeign lang ftype (MultipartForm t a :> api) where
533+
type Foreign ftype (MultipartForm t a :> api) = Foreign ftype api
534+
535+
foreignFor lang ftype Proxy req =
536+
foreignFor lang ftype (Proxy @api) $
537+
req & reqBody .~ Just t
538+
& reqBodyIsJSON .~ False
539+
where
540+
t = typeFor lang ftype (Proxy @a)

0 commit comments

Comments
 (0)