@@ -1,4 +1,3 @@
1−{-# LANGUAGE CPP #-}
21{-# LANGUAGE RecordWildCards #-}
32{-# LANGUAGE FlexibleContexts #-}
43{-# LANGUAGE MultiParamTypeClasses #-}
@@ -20,11 +19,8 @@ import Elm.Json (jsonParserForType, jsonSerForType)
2019import qualified Elm.Module as Elm
2120import Elm.TyRep (ETCon(..), EType(..), ETypeDef(..), toElmType)
2221import Elm.TyRender (renderElm)
23−#if MIN_VERSION_elm_bridge(0,6,0)
24−import Elm.Versions (ElmVersion(Elm0p19))
25−#else
2622import Elm.Versions (ElmVersion(Elm0p18))
27−#endif
23+
2824import Servant.Elm.Internal.Foreign (LangElm, getEndpoints)
2925import qualified Servant.Foreign as F
3026import System.Directory (createDirectoryIfMissing)
@@ -53,22 +49,16 @@ data ElmOptions = ElmOptions
5349 argument.
5450 -}
5551 urlPrefix :: UrlPrefix
56− , elmTypeAlterations :: (EType -> EType)
52+ , elmTypeAlterations :: (EType -> EType)
5753 -- ^ Alterations to perform on ETypes before code generation.
5854 , elmAlterations :: (ETypeDef -> ETypeDef)
5955 -- ^ Alterations to perform on ETypeDefs before code generation.
60− , elmToString :: (EType -> Text)
56+ , elmToString :: (EType -> Text)
6157 -- ^ Elm functions creating a string from a given type.
6258 , emptyResponseElmTypes :: [EType]
6359 -- ^ Types that represent an empty Http response.
6460 , stringElmTypes :: [EType]
6561 -- ^ 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.
7262 }
7363
7464
@@ -92,9 +82,6 @@ The default options are:
9282> , stringElmTypes =
9383> [ getType (Proxy :: Proxy String)
9484> , getType (Proxy :: Proxy T.Text) ]
95−> , expectJsonMethod = "Http.expectJson"
96−> , expectStringMethod = "Http.expectString"
97−> , httpErrorType = "Http.Error"
9885> }
9986-}
10087defElmOptions :: ElmOptions
@@ -110,9 +97,6 @@ defElmOptions = ElmOptions
11097 [ toElmType (Proxy :: Proxy String)
11198 , toElmType (Proxy :: Proxy T.Text)
11299 ]
113− , expectJsonMethod = "Http.expectJson"
114− , expectStringMethod = "Http.expectString"
115− , httpErrorType = "Http.Error"
116100 }
117101
118102
@@ -131,7 +115,6 @@ The default required imports are:
131115> import Set
132116> import Http
133117> import String
134−> import Url
135118> import Url.Builder
136119-}
137120defElmImports :: Text
@@ -145,7 +128,6 @@ defElmImports =
145128 , "import Set"
146129 , "import Http"
147130 , "import String"
148− , "import Url"
149131 , "import Url.Builder"
150132 ]
151133
@@ -167,12 +149,7 @@ generateElmModuleWith ::
167149generateElmModuleWith options namespace imports rootDir typeDefs api = do
168150 let out =
169151 T.unlines $
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
152+ [ T.pack $ Elm.moduleHeader Elm0p18 moduleName
176153 , ""
177154 , imports
178155 , T.pack $ Elm.makeModuleContentWithAlterations (elmAlterations options) typeDefs
@@ -259,7 +236,7 @@ generateElmForRequest opts request =
259236 fnName =
260237 request ^. F.reqFuncName . to (replace . F.camelCase) . to stext
261238
262− replace = T.replace "-" "" . T.replace "." "" . T.replace " " ""
239+ replace = T.replace "-" "" . T.replace "." ""
263240
264241 typeSignature =
265242 mkTypeSignature opts request
@@ -321,7 +298,7 @@ mkTypeSignature opts request =
321298 toMsgType :: Maybe Doc
322299 toMsgType = do
323300 result <- fmap elmTypeRef $ request ^. F.reqReturnType
324− Just ("(Result" <+> stext (httpErrorType opts) <+> parens result <+> "-> msg)")
301+ Just ("(Result Http.Error " <+> parens result <+> " -> msg)")
325302
326303 returnType :: Maybe Doc
327304 returnType = do
@@ -339,7 +316,7 @@ elmCaptureArg segment =
339316 "capture_" <>
340317 F.captureArg segment ^. F.argName . to (stext . replace . F.unPathSegment)
341318 where
342− replace = T.replace "-" "_" . T.replace " " "_"
319+ replace = T.replace "-" "_"
343320
344321
345322elmQueryArg :: F.QueryArg EType -> Doc
@@ -443,7 +420,7 @@ mkLetParams opts request =
443420 , "Just"
444421 ]
445422 )
446−
423+
447424 where
448425 elmName = elmQueryArg qarg
449426 name = qarg ^. F.queryArgName . F.argName . to (stext . F.unPathSegment)
@@ -510,14 +487,14 @@ mkRequest opts request =
510487 expect =
511488 case request ^. F.reqReturnType of
512489 Just elmTypeExpr
513− | isEmptyType opts $ (elmTypeAlterations opts) elmTypeExpr
490+ | isEmptyType opts elmTypeExpr
514491 -- let elmConstructor = T.pack (renderElm elmTypeExpr)
515492 ->
516− stext (expectStringMethod opts) <> line <+> indent i "(\\x -> case x of" <> line <+>
493+ "Http.expectString " <> line <+> indent i "(\\x -> case x of" <> line <+>
517494 indent i "Err e -> toMsg (Err e)" <> line <+>
518495 indent i "Ok _ -> toMsg (Ok ()))"
519496 Just elmTypeExpr ->
520− stext (expectJsonMethod opts) <+> "toMsg" <+> renderDecoderName ((elmTypeAlterations opts) elmTypeExpr)
497+ "Http.expectJson toMsg" <+> renderDecoderName elmTypeExpr
521498 Nothing -> error "mkHttpRequest: no reqReturnType?"
522499 -- case request ^. F.reqReturnType of
523500 -- Just elmTypeExpr | isEmptyType opts elmTypeExpr ->
@@ -545,8 +522,6 @@ renderDecoderName elmTypeExpr =
545522 parens ("Json.Decode.list " <> parens (renderDecoderName t))
546523 ETyApp (ETyCon (ETCon "Maybe")) t ->
547524 parens ("Json.Decode.maybe " <> parens (renderDecoderName t))
548− ETyApp x y ->
549− parens (renderDecoderName x <+> renderDecoderName y)
550525 ETyCon (ETCon "Int") -> "Json.Decode.int"
551526 ETyCon (ETCon "String") -> "Json.Decode.string"
552527 _ -> ("jsonDec" <> stext (T.pack (renderElm elmTypeExpr)))
@@ -564,7 +539,7 @@ mkUrl opts segments =
564539 where
565540 urlBuilder :: Doc
566541 urlBuilder = case urlPrefix opts of
567− Dynamic -> "Url.Builder.crossOrigin urlBase" :: Doc
542+ Dynamic -> "Url.Builder.absolute" :: Doc
568543 Static url -> "Url.Builder.crossOrigin" <+> dquotes (stext url)
569544
570545 segmentToDoc :: F.Segment EType -> Doc
@@ -574,10 +549,10 @@ mkUrl opts segments =
574549 dquotes (stext (F.unPathSegment path))
575550 F.Cap arg ->
576551 let
577− toStringSrc =
578− toString opts (elmTypeAlterations opts (maybeOf (arg ^. F.argType)))
552+ toStringSrc =
553+ toString opts (maybeOf (arg ^. F.argType))
579554 in
580− "Url.percentEncode " <> pipeRight [elmCaptureArg s, toStringSrc]
555+ pipeRight [elmCaptureArg s, toStringSrc]
581556
582557
583558mkQueryParams
@@ -595,7 +570,7 @@ response body.
595570-}
596571isEmptyType :: ElmOptions -> EType -> Bool
597572isEmptyType opts elmTypeExpr =
598− (elmTypeAlterations opts elmTypeExpr) `elem` emptyResponseElmTypes opts
573+ elmTypeExpr `elem` emptyResponseElmTypes opts
599574
600575
601576{- | Determines whether we call `toString` on URL captures and query params of
@@ -603,7 +578,7 @@ this type in Elm.
603578-}
604579isElmStringType :: ElmOptions -> EType -> Bool
605580isElmStringType opts elmTypeExpr =
606− (elmTypeAlterations opts elmTypeExpr) `elem` stringElmTypes opts
581+ elmTypeExpr `elem` stringElmTypes opts
607582
608583{- | Determines whether a type is 'Maybe a' where 'a' is something akin to a 'String'.
609584-}
@@ -645,7 +620,7 @@ elmListOfMaybes ds = "List.filterMap identity" <$> indent 4 (elmList ds)
645620defaultElmToString :: EType -> Text
646621defaultElmToString argType =
647622 case argType of
648− ETyCon (ETCon "Bool") -> "(\\value -> if value then \"true\" else \"false\")"
623+ ETyCon (ETCon "Bool") -> "(\\value -> if value then \"1\" else \"0\")"
649624 ETyCon (ETCon "Float") -> "String.fromFloat"
650625 ETyCon (ETCon "Char") -> "String.fromChar"
651626 ETyApp (ETyCon (ETCon "Maybe")) v -> "(Maybe.map " <> defaultElmToString v <> " >> Maybe.withDefault \"\")"