git.iko.soy/haskell-servant/servant-elm
on master help sign in
src/Servant/Elm/Internal/Generate.hs+20−8
since splitsnapshots
unifiedsplit
@@ -53,16 +53,22 @@ data ElmOptions = ElmOptions
53 argument.
53 argument.
54 -}
54 -}
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.
66 }
72 }
67
73
68
74
@@ -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"
89> }
98> }
90-}
99-}
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)
103 ]
112 ]
113+ , expectJsonMethod = "Http.expectJson"
114+ , expectStringMethod = "Http.expectString"
115+ , httpErrorType = "Http.Error"
104 }
116 }
105
117
106
118
@@ -247,7 +259,7 @@ generateElmForRequest opts request =
247 fnName =
259 fnName =
248 request ^. F.reqFuncName . to (replace . F.camelCase) . to stext
260 request ^. F.reqFuncName . to (replace . F.camelCase) . to stext
249
261
250− replace = T.replace "-" "" . T.replace "." ""
262+ replace = T.replace "-" "" . T.replace "." "" . T.replace " " ""
251
263
252 typeSignature =
264 typeSignature =
253 mkTypeSignature opts request
265 mkTypeSignature opts request
@@ -309,7 +321,7 @@ mkTypeSignature opts request =
309 toMsgType :: Maybe Doc
321 toMsgType :: Maybe Doc
310 toMsgType = do
322 toMsgType = do
311 result <- fmap elmTypeRef $ request ^. F.reqReturnType
323 result <- fmap elmTypeRef $ request ^. F.reqReturnType
312− Just ("(Result Http.Error " <+> parens result <+> " -> msg)")
324+ Just ("(Result" <+> stext (httpErrorType opts) <+> parens result <+> "-> msg)")
313
325
314 returnType :: Maybe Doc
326 returnType :: Maybe Doc
315 returnType = do
327 returnType = do
@@ -327,7 +339,7 @@ elmCaptureArg segment =
327 "capture_" <>
339 "capture_" <>
328 F.captureArg segment ^. F.argName . to (stext . replace . F.unPathSegment)
340 F.captureArg segment ^. F.argName . to (stext . replace . F.unPathSegment)
329 where
341 where
330− replace = T.replace "-" "_"
342+ replace = T.replace "-" "_" . T.replace " " "_"
331
343
332
344
333elmQueryArg :: F.QueryArg EType -> Doc
345elmQueryArg :: F.QueryArg EType -> Doc
@@ -501,11 +513,11 @@ mkRequest opts request =
501 | isEmptyType opts $ (elmTypeAlterations opts) elmTypeExpr
513 | isEmptyType opts $ (elmTypeAlterations opts) elmTypeExpr
502 -- let elmConstructor = T.pack (renderElm elmTypeExpr)
514 -- let elmConstructor = T.pack (renderElm elmTypeExpr)
503 ->
515 ->
504− "Http.expectString " <> line <+> indent i "(\\x -> case x of" <> line <+>
516+ stext (expectStringMethod opts) <> line <+> indent i "(\\x -> case x of" <> line <+>
505 indent i "Err e -> toMsg (Err e)" <> line <+>
517 indent i "Err e -> toMsg (Err e)" <> line <+>
506 indent i "Ok _ -> toMsg (Ok ()))"
518 indent i "Ok _ -> toMsg (Ok ()))"
507 Just elmTypeExpr ->
519 Just elmTypeExpr ->
508− "Http.expectJson toMsg" <+> renderDecoderName ((elmTypeAlterations opts) elmTypeExpr)
520+ stext (expectJsonMethod opts) <+> "toMsg" <+> renderDecoderName ((elmTypeAlterations opts) elmTypeExpr)
509 Nothing -> error "mkHttpRequest: no reqReturnType?"
521 Nothing -> error "mkHttpRequest: no reqReturnType?"
510 -- case request ^. F.reqReturnType of
522 -- case request ^. F.reqReturnType of
511 -- Just elmTypeExpr | isEmptyType opts elmTypeExpr ->
523 -- Just elmTypeExpr | isEmptyType opts elmTypeExpr ->
@@ -563,7 +575,7 @@ mkUrl opts segments =
563 F.Cap arg ->
575 F.Cap arg ->
564 let
576 let
565 toStringSrc =
577 toStringSrc =
566− toString opts (maybeOf (arg ^. F.argType))
578+ toString opts (elmTypeAlterations opts (maybeOf (arg ^. F.argType)))
567 in
579 in
568 "Url.percentEncode " <> pipeRight [elmCaptureArg s, toStringSrc]
580 "Url.percentEncode " <> pipeRight [elmCaptureArg s, toStringSrc]
569
581
test/Common.hs+1−1
@@ -27,7 +27,7 @@ type TestApi =
27 :> Capture "id" Int
27 :> Capture "id" Int
28 :> Get '[JSON] Book
28 :> Get '[JSON] Book
29 :<|> "books"
29 :<|> "books"
30− :> Capture "title" Text
30+ :> Capture "title space" Text
31 :> Get '[JSON] Book
31 :> Get '[JSON] Book
32 :<|> "books"
32 :<|> "books"
33 :> QueryFlag "published"
33 :> QueryFlag "published"
test/elm-sources/getBooksByIdSource.elm+1−1
@@ -8,7 +8,7 @@ type Book = Book
8jsonDecBook : Json.Decode.Decoder Book
8jsonDecBook : Json.Decode.Decoder Book
9jsonDecBook = Debug.todo ""
9jsonDecBook = Debug.todo ""
10
10
11−getBooksById : Int -> (Result Http.Error (Book) -> msg) -> Cmd msg
11+getBooksById : Int -> (Result Http.Error (Book) -> msg) -> Cmd msg
12getBooksById capture_id toMsg =
12getBooksById capture_id toMsg =
13 let
13 let
14 params =
14 params =
test/elm-sources/getBooksByTitleSource.elm+3−3
@@ -7,8 +7,8 @@ import Json.Decode as J
7type alias Book = {}
7type alias Book = {}
8jsonDecBook = J.succeed {}
8jsonDecBook = J.succeed {}
9
9
10−getBooksByTitle : String -> (Result Http.Error (Book) -> msg) -> Cmd msg
10+getBooksByTitlespace : String -> (Result Http.Error (Book) -> msg) -> Cmd msg
11−getBooksByTitle capture_title toMsg =
11+getBooksByTitlespace capture_title_space toMsg =
12 let
12 let
13 params =
13 params =
14 List.filterMap identity
14 List.filterMap identity
@@ -23,7 +23,7 @@ getBooksByTitle capture_title toMsg =
23 , url =
23 , url =
24 Url.Builder.crossOrigin ""
24 Url.Builder.crossOrigin ""
25 [ "books"
25 [ "books"
26− , Url.percentEncode (capture_title)
26+ , Url.percentEncode (capture_title_space)
27 ]
27 ]
28 params
28 params
29 , body =
29 , body =
test/elm-sources/getBooksSource.elm+1−1
@@ -9,7 +9,7 @@ type alias Book = {}
9
9
10jsonDecBook = J.succeed {}
10jsonDecBook = J.succeed {}
11
11
12−getBooks : Bool -> (Maybe String) -> (Maybe Int) -> String -> (List (Maybe Bool)) -> (Result Http.Error ((List Book)) -> msg) -> Cmd msg
12+getBooks : Bool -> (Maybe String) -> (Maybe Int) -> String -> (List (Maybe Bool)) -> (Result Http.Error ((List Book)) -> msg) -> Cmd msg
13getBooks query_published query_sort query_year query_category query_filters toMsg =
13getBooks query_published query_sort query_year query_category query_filters toMsg =
14 let
14 let
15 params =
15 params =
test/elm-sources/getNothingSource.elm+2−2
@@ -4,7 +4,7 @@ import Http
4import Url.Builder
4import Url.Builder
5
5
6
6
7−getNothing : (Result Http.Error (()) -> msg) -> Cmd msg
7+getNothing : (Result Http.Error (()) -> msg) -> Cmd msg
8getNothing toMsg =
8getNothing toMsg =
9 let
9 let
10 params =
10 params =
@@ -25,7 +25,7 @@ getNothing toMsg =
25 , body =
25 , body =
26 Http.emptyBody
26 Http.emptyBody
27 , expect =
27 , expect =
28− Http.expectString
28+ Http.expectString
29 (\x -> case x of
29 (\x -> case x of
30 Err e -> toMsg (Err e)
30 Err e -> toMsg (Err e)
31 Ok _ -> toMsg (Ok ()))
31 Ok _ -> toMsg (Ok ()))
test/elm-sources/getOneSource.elm+1−1
@@ -5,7 +5,7 @@ import Json.Decode exposing (..)
5import Url.Builder
5import Url.Builder
6
6
7
7
8−getOne : (Result Http.Error (Int) -> msg) -> Cmd msg
8+getOne : (Result Http.Error (Int) -> msg) -> Cmd msg
9getOne toMsg =
9getOne toMsg =
10 let
10 let
11 params =
11 params =
test/elm-sources/getOneWithDynamicUrlSource.elm+1−1
@@ -5,7 +5,7 @@ import Url.Builder
5import Json.Decode exposing (..)
5import Json.Decode exposing (..)
6
6
7
7
8−getOne : String -> (Result Http.Error (Int) -> msg) -> Cmd msg
8+getOne : String -> (Result Http.Error (Int) -> msg) -> Cmd msg
9getOne urlBase toMsg =
9getOne urlBase toMsg =
10 let
10 let
11 params =
11 params =
test/elm-sources/getPolymorphicData.elm+1−1
@@ -14,7 +14,7 @@ jsonDecSomeRecord : Json.Decode.Decoder SomeRecord
14jsonDecSomeRecord = Debug.todo "finish"
14jsonDecSomeRecord = Debug.todo "finish"
15
15
16
16
17−getPolymorphicData : (Result Http.Error ((PolymorphicData (List String) SomeRecord)) -> msg) -> Cmd msg
17+getPolymorphicData : (Result Http.Error ((PolymorphicData (List String) SomeRecord)) -> msg) -> Cmd msg
18getPolymorphicData toMsg =
18getPolymorphicData toMsg =
19 let
19 let
20 params =
20 params =
test/elm-sources/getWithaheaderSource.elm+1−1
@@ -5,7 +5,7 @@ import Url.Builder
5import Json.Decode exposing (..)
5import Json.Decode exposing (..)
6
6
7
7
8−getWithaheader : (Maybe String) -> (Maybe Int) -> String -> Int -> (Result Http.Error (String) -> msg) -> Cmd msg
8+getWithaheader : (Maybe String) -> (Maybe Int) -> String -> Int -> (Result Http.Error (String) -> msg) -> Cmd msg
9getWithaheader header_myStringHeader header_MyIntHeader header_MyRequiredStringHeader header_MyRequiredIntHeader toMsg =
9getWithaheader header_myStringHeader header_MyIntHeader header_MyRequiredStringHeader header_MyRequiredIntHeader toMsg =
10 let
10 let
11 params =
11 params =
test/elm-sources/getWitharesponseheaderSource.elm+1−1
@@ -5,7 +5,7 @@ import Url.Builder
5import Json.Decode exposing (..)
5import Json.Decode exposing (..)
6
6
7
7
8−getWitharesponseheader : (Result Http.Error (String) -> msg) -> Cmd msg
8+getWitharesponseheader : (Result Http.Error (String) -> msg) -> Cmd msg
9getWitharesponseheader toMsg =
9getWitharesponseheader toMsg =
10 let
10 let
11 params =
11 params =
test/elm-sources/postBooksSource.elm+2−2
@@ -7,7 +7,7 @@ import Json.Encode as Enc
7type alias Book = {}
7type alias Book = {}
8jsonEncBook = \b -> Enc.object []
8jsonEncBook = \b -> Enc.object []
9
9
10−postBooks : Book -> (Result Http.Error (()) -> msg) -> Cmd msg
10+postBooks : Book -> (Result Http.Error (()) -> msg) -> Cmd msg
11postBooks body toMsg =
11postBooks body toMsg =
12 let
12 let
13 params =
13 params =
@@ -28,7 +28,7 @@ postBooks body toMsg =
28 , body =
28 , body =
29 Http.jsonBody (jsonEncBook body)
29 Http.jsonBody (jsonEncBook body)
30 , expect =
30 , expect =
31− Http.expectString
31+ Http.expectString
32 (\x -> case x of
32 (\x -> case x of
33 Err e -> toMsg (Err e)
33 Err e -> toMsg (Err e)
34 Ok _ -> toMsg (Ok ()))
34 Ok _ -> toMsg (Ok ()))
test/elm-sources/postTwoSource.elm+1−1
@@ -6,7 +6,7 @@ import Json.Encode
6import Url.Builder
6import Url.Builder
7
7
8
8
9−postTwo : String -> (Result Http.Error ((Maybe Int)) -> msg) -> Cmd msg
9+postTwo : String -> (Result Http.Error ((Maybe Int)) -> msg) -> Cmd msg
10postTwo body toMsg =
10postTwo body toMsg =
11 let
11 let
12 params =
12 params =
test/elm-sources/putNothingSource.elm+2−2
@@ -4,7 +4,7 @@ import Http
4import Url.Builder
4import Url.Builder
5
5
6
6
7−putNothing : (Result Http.Error (()) -> msg) -> Cmd msg
7+putNothing : (Result Http.Error (()) -> msg) -> Cmd msg
8putNothing toMsg =
8putNothing toMsg =
9 let
9 let
10 params =
10 params =
@@ -25,7 +25,7 @@ putNothing toMsg =
25 , body =
25 , body =
26 Http.emptyBody
26 Http.emptyBody
27 , expect =
27 , expect =
28− Http.expectString
28+ Http.expectString
29 (\x -> case x of
29 (\x -> case x of
30 Err e -> toMsg (Err e)
30 Err e -> toMsg (Err e)
31 Ok _ -> toMsg (Ok ()))
31 Ok _ -> toMsg (Ok ()))