@@ -1,3 +1,4 @@
1+{-# LANGUAGE CPP #-}
12{-# LANGUAGE RecordWildCards #-}
23{-# LANGUAGE FlexibleContexts #-}
34{-# LANGUAGE MultiParamTypeClasses #-}
@@ -19,8 +20,11 @@ import Elm.Json (jsonParserForType, jsonSerForType)
1920import qualified Elm.Module as Elm
2021import Elm.TyRep (ETCon(..), EType(..), ETypeDef(..), toElmType)
2122import Elm.TyRender (renderElm)
23+#if MIN_VERSION_elm_bridge(0,6,0)
24+import Elm.Versions (ElmVersion(Elm0p19))
25+#else
2226import Elm.Versions (ElmVersion(Elm0p18))
23−
27+#endif
2428import Servant.Elm.Internal.Foreign (LangElm, getEndpoints)
2529import qualified Servant.Foreign as F
2630import System.Directory (createDirectoryIfMissing)
@@ -49,16 +53,22 @@ data ElmOptions = ElmOptions
4953 argument.
5054 -}
5155 urlPrefix :: UrlPrefix
52− , elmTypeAlterations :: (EType -> EType)
56+ , elmTypeAlterations :: (EType -> EType)
5357 -- ^ Alterations to perform on ETypes before code generation.
5458 , elmAlterations :: (ETypeDef -> ETypeDef)
5559 -- ^ Alterations to perform on ETypeDefs before code generation.
56− , elmToString :: (EType -> Text)
60+ , elmToString :: (EType -> Text)
5761 -- ^ Elm functions creating a string from a given type.
5862 , emptyResponseElmTypes :: [EType]
5963 -- ^ Types that represent an empty Http response.
6064 , stringElmTypes :: [EType]
6165 -- ^ Types that represent a String.
66+ , expectJsonMethod :: Text
67+ -- ^ The function to use for JSON responses.
68+ , expectStringMethod :: Text
69+ -- ^ The function to use for string responses.
70+ , httpErrorType :: Text
71+ -- ^ The type to use for Http errors.
6272 }
6373
6474
@@ -82,6 +92,9 @@ The default options are:
8292> , stringElmTypes =
8393> [ getType (Proxy :: Proxy String)
8494> , getType (Proxy :: Proxy T.Text) ]
95+> , expectJsonMethod = "Http.expectJson"
96+> , expectStringMethod = "Http.expectString"
97+> , httpErrorType = "Http.Error"
8598> }
8699-}
87100defElmOptions :: ElmOptions
@@ -97,6 +110,9 @@ defElmOptions = ElmOptions
97110 [ toElmType (Proxy :: Proxy String)
98111 , toElmType (Proxy :: Proxy T.Text)
99112 ]
113+ , expectJsonMethod = "Http.expectJson"
114+ , expectStringMethod = "Http.expectString"
115+ , httpErrorType = "Http.Error"
100116 }
101117
102118
@@ -115,6 +131,7 @@ The default required imports are:
115131> import Set
116132> import Http
117133> import String
134+> import Url
118135> import Url.Builder
119136-}
120137defElmImports :: Text
@@ -128,6 +145,7 @@ defElmImports =
128145 , "import Set"
129146 , "import Http"
130147 , "import String"
148+ , "import Url"
131149 , "import Url.Builder"
132150 ]
133151
@@ -149,7 +167,12 @@ generateElmModuleWith ::
149167generateElmModuleWith options namespace imports rootDir typeDefs api = do
150168 let out =
151169 T.unlines $
152− [ T.pack $ Elm.moduleHeader Elm0p18 moduleName
170+ [
171+#if MIN_VERSION_elm_bridge(0,6,0)
172+ T.pack $ Elm.moduleHeader Elm0p19 moduleName
173+#else
174+ T.pack $ Elm.moduleHeader Elm0p18 moduleName
175+#endif
153176 , ""
154177 , imports
155178 , T.pack $ Elm.makeModuleContentWithAlterations (elmAlterations options) typeDefs
@@ -236,7 +259,7 @@ generateElmForRequest opts request =
236259 fnName =
237260 request ^. F.reqFuncName . to (replace . F.camelCase) . to stext
238261
239− replace = T.replace "-" "" . T.replace "." ""
262+ replace = T.replace "-" "" . T.replace "." "" . T.replace " " ""
240263
241264 typeSignature =
242265 mkTypeSignature opts request
@@ -298,7 +321,7 @@ mkTypeSignature opts request =
298321 toMsgType :: Maybe Doc
299322 toMsgType = do
300323 result <- fmap elmTypeRef $ request ^. F.reqReturnType
301− Just ("(Result Http.Error " <+> parens result <+> " -> msg)")
324+ Just ("(Result" <+> stext (httpErrorType opts) <+> parens result <+> "-> msg)")
302325
303326 returnType :: Maybe Doc
304327 returnType = do
@@ -316,7 +339,7 @@ elmCaptureArg segment =
316339 "capture_" <>
317340 F.captureArg segment ^. F.argName . to (stext . replace . F.unPathSegment)
318341 where
319− replace = T.replace "-" "_"
342+ replace = T.replace "-" "_" . T.replace " " "_"
320343
321344
322345elmQueryArg :: F.QueryArg EType -> Doc
@@ -420,7 +443,7 @@ mkLetParams opts request =
420443 , "Just"
421444 ]
422445 )
423−
446+
424447 where
425448 elmName = elmQueryArg qarg
426449 name = qarg ^. F.queryArgName . F.argName . to (stext . F.unPathSegment)
@@ -487,14 +510,14 @@ mkRequest opts request =
487510 expect =
488511 case request ^. F.reqReturnType of
489512 Just elmTypeExpr
490− | isEmptyType opts elmTypeExpr
513+ | isEmptyType opts $ (elmTypeAlterations opts) elmTypeExpr
491514 -- let elmConstructor = T.pack (renderElm elmTypeExpr)
492515 ->
493− "Http.expectString " <> line <+> indent i "(\\x -> case x of" <> line <+>
516+ stext (expectStringMethod opts) <> line <+> indent i "(\\x -> case x of" <> line <+>
494517 indent i "Err e -> toMsg (Err e)" <> line <+>
495518 indent i "Ok _ -> toMsg (Ok ()))"
496519 Just elmTypeExpr ->
497− "Http.expectJson toMsg" <+> renderDecoderName elmTypeExpr
520+ stext (expectJsonMethod opts) <+> "toMsg" <+> renderDecoderName ((elmTypeAlterations opts) elmTypeExpr)
498521 Nothing -> error "mkHttpRequest: no reqReturnType?"
499522 -- case request ^. F.reqReturnType of
500523 -- Just elmTypeExpr | isEmptyType opts elmTypeExpr ->
@@ -522,6 +545,8 @@ renderDecoderName elmTypeExpr =
522545 parens ("Json.Decode.list " <> parens (renderDecoderName t))
523546 ETyApp (ETyCon (ETCon "Maybe")) t ->
524547 parens ("Json.Decode.maybe " <> parens (renderDecoderName t))
548+ ETyApp x y ->
549+ parens (renderDecoderName x <+> renderDecoderName y)
525550 ETyCon (ETCon "Int") -> "Json.Decode.int"
526551 ETyCon (ETCon "String") -> "Json.Decode.string"
527552 _ -> ("jsonDec" <> stext (T.pack (renderElm elmTypeExpr)))
@@ -549,10 +574,10 @@ mkUrl opts segments =
549574 dquotes (stext (F.unPathSegment path))
550575 F.Cap arg ->
551576 let
552− toStringSrc =
553− toString opts (maybeOf (arg ^. F.argType))
577+ toStringSrc =
578+ toString opts (elmTypeAlterations opts (maybeOf (arg ^. F.argType)))
554579 in
555− pipeRight [elmCaptureArg s, toStringSrc]
580+ "Url.percentEncode " <> pipeRight [elmCaptureArg s, toStringSrc]
556581
557582
558583mkQueryParams
@@ -570,7 +595,7 @@ response body.
570595-}
571596isEmptyType :: ElmOptions -> EType -> Bool
572597isEmptyType opts elmTypeExpr =
573− elmTypeExpr `elem` emptyResponseElmTypes opts
598+ (elmTypeAlterations opts elmTypeExpr) `elem` emptyResponseElmTypes opts
574599
575600
576601{- | Determines whether we call `toString` on URL captures and query params of
@@ -578,7 +603,7 @@ this type in Elm.
578603-}
579604isElmStringType :: ElmOptions -> EType -> Bool
580605isElmStringType opts elmTypeExpr =
581− elmTypeExpr `elem` stringElmTypes opts
606+ (elmTypeAlterations opts elmTypeExpr) `elem` stringElmTypes opts
582607
583608{- | Determines whether a type is 'Maybe a' where 'a' is something akin to a 'String'.
584609-}