@@ -53,16 +53,22 @@ data ElmOptions = ElmOptions
5353 argument.
5454 -}
5555 urlPrefix :: UrlPrefix
56− , elmTypeAlterations :: (EType -> EType)
56+ , elmTypeAlterations :: (EType -> EType)
5757 -- ^ Alterations to perform on ETypes before code generation.
5858 , elmAlterations :: (ETypeDef -> ETypeDef)
5959 -- ^ Alterations to perform on ETypeDefs before code generation.
60− , elmToString :: (EType -> Text)
60+ , elmToString :: (EType -> Text)
6161 -- ^ Elm functions creating a string from a given type.
6262 , emptyResponseElmTypes :: [EType]
6363 -- ^ Types that represent an empty Http response.
6464 , stringElmTypes :: [EType]
6565 -- ^ 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.
6672 }
6773
6874
@@ -86,6 +92,9 @@ The default options are:
8692> , stringElmTypes =
8793> [ getType (Proxy :: Proxy String)
8894> , getType (Proxy :: Proxy T.Text) ]
95+> , expectJsonMethod = "Http.expectJson"
96+> , expectStringMethod = "Http.expectString"
97+> , httpErrorType = "Http.Error"
8998> }
9099-}
91100defElmOptions :: ElmOptions
@@ -101,6 +110,9 @@ defElmOptions = ElmOptions
101110 [ toElmType (Proxy :: Proxy String)
102111 , toElmType (Proxy :: Proxy T.Text)
103112 ]
113+ , expectJsonMethod = "Http.expectJson"
114+ , expectStringMethod = "Http.expectString"
115+ , httpErrorType = "Http.Error"
104116 }
105117
106118
@@ -119,6 +131,7 @@ The default required imports are:
119131> import Set
120132> import Http
121133> import String
134+> import Url
122135> import Url.Builder
123136-}
124137defElmImports :: Text
@@ -132,6 +145,7 @@ defElmImports =
132145 , "import Set"
133146 , "import Http"
134147 , "import String"
148+ , "import Url"
135149 , "import Url.Builder"
136150 ]
137151
@@ -245,7 +259,7 @@ generateElmForRequest opts request =
245259 fnName =
246260 request ^. F.reqFuncName . to (replace . F.camelCase) . to stext
247261
248− replace = T.replace "-" "" . T.replace "." ""
262+ replace = T.replace "-" "" . T.replace "." "" . T.replace " " ""
249263
250264 typeSignature =
251265 mkTypeSignature opts request
@@ -307,7 +321,7 @@ mkTypeSignature opts request =
307321 toMsgType :: Maybe Doc
308322 toMsgType = do
309323 result <- fmap elmTypeRef $ request ^. F.reqReturnType
310− Just ("(Result Http.Error " <+> parens result <+> " -> msg)")
324+ Just ("(Result" <+> stext (httpErrorType opts) <+> parens result <+> "-> msg)")
311325
312326 returnType :: Maybe Doc
313327 returnType = do
@@ -325,7 +339,7 @@ elmCaptureArg segment =
325339 "capture_" <>
326340 F.captureArg segment ^. F.argName . to (stext . replace . F.unPathSegment)
327341 where
328− replace = T.replace "-" "_"
342+ replace = T.replace "-" "_" . T.replace " " "_"
329343
330344
331345elmQueryArg :: F.QueryArg EType -> Doc
@@ -496,14 +510,14 @@ mkRequest opts request =
496510 expect =
497511 case request ^. F.reqReturnType of
498512 Just elmTypeExpr
499− | isEmptyType opts elmTypeExpr
513+ | isEmptyType opts $ (elmTypeAlterations opts) elmTypeExpr
500514 -- let elmConstructor = T.pack (renderElm elmTypeExpr)
501515 ->
502− "Http.expectString " <> line <+> indent i "(\\x -> case x of" <> line <+>
516+ stext (expectStringMethod opts) <> line <+> indent i "(\\x -> case x of" <> line <+>
503517 indent i "Err e -> toMsg (Err e)" <> line <+>
504518 indent i "Ok _ -> toMsg (Ok ()))"
505519 Just elmTypeExpr ->
506− "Http.expectJson toMsg" <+> renderDecoderName elmTypeExpr
520+ stext (expectJsonMethod opts) <+> "toMsg" <+> renderDecoderName ((elmTypeAlterations opts) elmTypeExpr)
507521 Nothing -> error "mkHttpRequest: no reqReturnType?"
508522 -- case request ^. F.reqReturnType of
509523 -- Just elmTypeExpr | isEmptyType opts elmTypeExpr ->
@@ -561,9 +575,9 @@ mkUrl opts segments =
561575 F.Cap arg ->
562576 let
563577 toStringSrc =
564− toString opts (maybeOf (arg ^. F.argType))
578+ toString opts (elmTypeAlterations opts (maybeOf (arg ^. F.argType)))
565579 in
566− pipeRight [elmCaptureArg s, toStringSrc]
580+ "Url.percentEncode " <> pipeRight [elmCaptureArg s, toStringSrc]
567581
568582
569583mkQueryParams
@@ -581,7 +595,7 @@ response body.
581595-}
582596isEmptyType :: ElmOptions -> EType -> Bool
583597isEmptyType opts elmTypeExpr =
584− elmTypeExpr `elem` emptyResponseElmTypes opts
598+ (elmTypeAlterations opts elmTypeExpr) `elem` emptyResponseElmTypes opts
585599
586600
587601{- | Determines whether we call `toString` on URL captures and query params of
@@ -589,7 +603,7 @@ this type in Elm.
589603-}
590604isElmStringType :: ElmOptions -> EType -> Bool
591605isElmStringType opts elmTypeExpr =
592− elmTypeExpr `elem` stringElmTypes opts
606+ (elmTypeAlterations opts elmTypeExpr) `elem` stringElmTypes opts
593607
594608{- | Determines whether a type is 'Maybe a' where 'a' is something akin to a 'String'.
595609-}