@@ -53,16 +53,22 @@ data ElmOptions = ElmOptions
55 urlPrefix :: UrlPrefix
55 urlPrefix :: UrlPrefix
56− , elmTypeAlterations :: (EType -> EType)
56+ , elmTypeAlterations :: (EType -> EType)
57 -- ^ Alterations to perform on ETypes before code generation.
57 -- ^ Alterations to perform on ETypes before code generation.
58 , elmAlterations :: (ETypeDef -> ETypeDef)
58 , elmAlterations :: (ETypeDef -> ETypeDef)
59 -- ^ Alterations to perform on ETypeDefs before code generation.
59 -- ^ Alterations to perform on ETypeDefs before code generation.
60− , elmToString :: (EType -> Text)
60+ , elmToString :: (EType -> Text)
61 -- ^ Elm functions creating a string from a given type.
61 -- ^ Elm functions creating a string from a given type.
62 , emptyResponseElmTypes :: [EType]
62 , emptyResponseElmTypes :: [EType]
63 -- ^ Types that represent an empty Http response.
63 -- ^ Types that represent an empty Http response.
64 , stringElmTypes :: [EType]
64 , stringElmTypes :: [EType]
65 -- ^ Types that represent a String.
65 -- ^ 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.
@@ -86,6 +92,9 @@ The default options are:
86> , stringElmTypes =
92> , stringElmTypes =
87> [ getType (Proxy :: Proxy String)
93> [ getType (Proxy :: Proxy String)
88> , getType (Proxy :: Proxy T.Text) ]
94> , getType (Proxy :: Proxy T.Text) ]
95+> , expectJsonMethod = "Http.expectJson"
96+> , expectStringMethod = "Http.expectString"
97+> , httpErrorType = "Http.Error"
91defElmOptions :: ElmOptions
100defElmOptions :: ElmOptions
@@ -101,6 +110,9 @@ defElmOptions = ElmOptions
101 [ toElmType (Proxy :: Proxy String)
110 [ toElmType (Proxy :: Proxy String)
102 , toElmType (Proxy :: Proxy T.Text)
111 , toElmType (Proxy :: Proxy T.Text)
113+ , expectJsonMethod = "Http.expectJson"
114+ , expectStringMethod = "Http.expectString"
115+ , httpErrorType = "Http.Error"
@@ -119,6 +131,7 @@ The default required imports are:
119> import Set
131> import Set
120> import Http
132> import Http
121> import String
133> import String
122> import Url.Builder
135> import Url.Builder
124defElmImports :: Text
137defElmImports :: Text
@@ -132,6 +145,7 @@ defElmImports =
132 , "import Set"
145 , "import Set"
133 , "import Http"
146 , "import Http"
134 , "import String"
147 , "import String"
135 , "import Url.Builder"
149 , "import Url.Builder"
@@ -245,7 +259,7 @@ generateElmForRequest opts request =
246 request ^. F.reqFuncName . to (replace . F.camelCase) . to stext
260 request ^. F.reqFuncName . to (replace . F.camelCase) . to stext
248− replace = T.replace "-" "" . T.replace "." ""
262+ replace = T.replace "-" "" . T.replace "." "" . T.replace " " ""
250 typeSignature =
264 typeSignature =
251 mkTypeSignature opts request
265 mkTypeSignature opts request
@@ -307,7 +321,7 @@ mkTypeSignature opts request =
307 toMsgType :: Maybe Doc
321 toMsgType :: Maybe Doc
308 toMsgType = do
322 toMsgType = do
309 result <- fmap elmTypeRef $ request ^. F.reqReturnType
323 result <- fmap elmTypeRef $ request ^. F.reqReturnType
310− Just ("(Result Http.Error " <+> parens result <+> " -> msg)")
324+ Just ("(Result" <+> stext (httpErrorType opts) <+> parens result <+> "-> msg)")
312 returnType :: Maybe Doc
326 returnType :: Maybe Doc
313 returnType = do
327 returnType = do
@@ -325,7 +339,7 @@ elmCaptureArg segment =
325 "capture_" <>
339 "capture_" <>
326 F.captureArg segment ^. F.argName . to (stext . replace . F.unPathSegment)
340 F.captureArg segment ^. F.argName . to (stext . replace . F.unPathSegment)
328− replace = T.replace "-" "_"
342+ replace = T.replace "-" "_" . T.replace " " "_"
331elmQueryArg :: F.QueryArg EType -> Doc
345elmQueryArg :: F.QueryArg EType -> Doc
@@ -499,11 +513,11 @@ mkRequest opts request =
499 | isEmptyType opts $ (elmTypeAlterations opts) elmTypeExpr
513 | isEmptyType opts $ (elmTypeAlterations opts) elmTypeExpr
500 -- let elmConstructor = T.pack (renderElm elmTypeExpr)
514 -- let elmConstructor = T.pack (renderElm elmTypeExpr)
502− "Http.expectString " <> line <+> indent i "(\\x -> case x of" <> line <+>
516+ stext (expectStringMethod opts) <> line <+> indent i "(\\x -> case x of" <> line <+>
503 indent i "Err e -> toMsg (Err e)" <> line <+>
517 indent i "Err e -> toMsg (Err e)" <> line <+>
504 indent i "Ok _ -> toMsg (Ok ()))"
518 indent i "Ok _ -> toMsg (Ok ()))"
505 Just elmTypeExpr ->
519 Just elmTypeExpr ->
506− "Http.expectJson toMsg" <+> renderDecoderName ((elmTypeAlterations opts) elmTypeExpr)
520+ stext (expectJsonMethod opts) <+> "toMsg" <+> renderDecoderName ((elmTypeAlterations opts) elmTypeExpr)
507 Nothing -> error "mkHttpRequest: no reqReturnType?"
521 Nothing -> error "mkHttpRequest: no reqReturnType?"
508 -- case request ^. F.reqReturnType of
522 -- case request ^. F.reqReturnType of
509 -- Just elmTypeExpr | isEmptyType opts elmTypeExpr ->
523 -- Just elmTypeExpr | isEmptyType opts elmTypeExpr ->
@@ -561,9 +575,9 @@ mkUrl opts segments =
561 F.Cap arg ->
575 F.Cap arg ->
563 toStringSrc =
577 toStringSrc =
564− toString opts (maybeOf (arg ^. F.argType))
578+ toString opts (elmTypeAlterations opts (maybeOf (arg ^. F.argType)))
566− pipeRight [elmCaptureArg s, toStringSrc]
580+ "Url.percentEncode " <> pipeRight [elmCaptureArg s, toStringSrc]
569mkQueryParams
583mkQueryParams
@@ -581,7 +595,7 @@ response body.
582isEmptyType :: ElmOptions -> EType -> Bool
596isEmptyType :: ElmOptions -> EType -> Bool
583isEmptyType opts elmTypeExpr =
597isEmptyType opts elmTypeExpr =
584− elmTypeExpr `elem` emptyResponseElmTypes opts
598+ (elmTypeAlterations opts elmTypeExpr) `elem` emptyResponseElmTypes opts
587{- | Determines whether we call `toString` on URL captures and query params of
601{- | Determines whether we call `toString` on URL captures and query params of
@@ -589,7 +603,7 @@ this type in Elm.
590isElmStringType :: ElmOptions -> EType -> Bool
604isElmStringType :: ElmOptions -> EType -> Bool
591isElmStringType opts elmTypeExpr =
605isElmStringType opts elmTypeExpr =
592− elmTypeExpr `elem` stringElmTypes opts
606+ (elmTypeAlterations opts elmTypeExpr) `elem` stringElmTypes opts
594{- | Determines whether a type is 'Maybe a' where 'a' is something akin to a 'String'.
608{- | Determines whether a type is 'Maybe a' where 'a' is something akin to a 'String'.