git.iko.soy/haskell-servant/servant-elm
on master help sign in
19 files changed+105−41
since splitsnapshots
unifiedsplit
.github/workflows/build.yaml+41 CHANGELOG.md+5 servant-elm.cabal+1−1 src/Servant/Elm/Internal/Generate.hs+26−12 stack.yaml+5−4 test/Common.hs+7−5 test/CompileSpec.hs+1−1 test/elm-sources/getBooksByIdSource.elm+3−2 test/elm-sources/getBooksByTitleSource.elm+3−3 test/elm-sources/getBooksSource.elm+1−1 test/elm-sources/getNothingSource.elm+2−2 test/elm-sources/getOneSource.elm+1−1 test/elm-sources/getOneWithDynamicUrlSource.elm+1−1 test/elm-sources/getPolymorphicData.elm+1−1 test/elm-sources/getWithaheaderSource.elm+1−1 test/elm-sources/getWitharesponseheaderSource.elm+1−1 test/elm-sources/postBooksSource.elm+2−2 test/elm-sources/postTwoSource.elm+1−1 test/elm-sources/putNothingSource.elm+2−2
.github/workflows/build.yamlnew file+41
@@ -0,0 +1,41 @@
1+name: Haskell CI
2+
3+on:
4+ push:
5+ branches: [ "master" ]
6+ pull_request:
7+ branches: [ "master" ]
8+
9+permissions:
10+ contents: read
11+
12+jobs:
13+ build:
14+
15+ runs-on: ubuntu-latest
16+
17+ steps:
18+ - uses: actions/checkout@v3
19+
20+ - name: Install Nix
21+ uses: cachix/install-nix-action@v20
22+ with:
23+ nix_path: nixpkgs=channel:nixos-unstable
24+
25+ - name: Cache
26+ uses: actions/cache@v3
27+ env:
28+ cache-name: cache-cabal
29+ with:
30+ path: ~/.stack
31+ key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/*.cabal') }}-${{ hashFiles('**/stack.yaml') }}
32+ restore-keys: |
33+ ${{ runner.os }}-build-${{ env.cache-name }}-
34+ ${{ runner.os }}-build-
35+ ${{ runner.os }}-
36+
37+ - name: Build
38+ run: stack build --test --no-run-tests
39+
40+ - name: Run tests
41+ run: stack test
CHANGELOG.md+5
@@ -1,3 +1,8 @@
1+0.7.2
2+-----
3+
4+* Commit a80f8b41d37d11778d2c27e6b38e881a71e85343. Fix for elm-bridge-0.6.0
5+
160.7.1
27-----
38
servant-elm.cabal+1−1
@@ -1,5 +1,5 @@
11name: servant-elm
2−version: 0.7.1
2+version: 0.7.3
33synopsis: Automatically derive Elm functions to query servant webservices.
44description: Please see README.md
55homepage: http://github.com/mattjbray/servant-elm#readme
src/Servant/Elm/Internal/Generate.hs+26−12
@@ -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-}
stack.yaml+5−4
@@ -1,8 +1,9 @@
1−resolver: lts-15.8
1+resolver: lts-20.11
22packages:
33- '.'
4−extra-deps:
5−# - elm-bridge-0.5.2
6−- ../elm-bridge
74flags: {}
85extra-package-dbs: []
6+nix:
7+ enable: true
8+ packages:
9+ - zlib
test/Common.hs+7−5
@@ -5,9 +5,9 @@ module Common where
55
66import Data.Proxy (Proxy (Proxy))
77import Data.Text (Text)
8−import Servant.API ((:<|>), (:>), Capture, Get, GetNoContent, Header,
8+import Servant.API ((:<|>), (:>), Capture, Get, Header,
99 Header', Headers, JSON, Post,
10− PostNoContent, Put, QueryFlag, QueryParam,
10+ Put, QueryFlag, QueryParam,
1111 QueryParam', QueryParams, ReqBody, Required)
1212import Servant.Elm (deriveBoth, defaultOptions)
1313
@@ -27,7 +27,7 @@ type TestApi =
2727 :> Capture "id" Int
2828 :> Get '[JSON] Book
2929 :<|> "books"
30− :> Capture "title" Text
30+ :> Capture "title space" Text
3131 :> Get '[JSON] Book
3232 :<|> "books"
3333 :> QueryFlag "published"
@@ -38,9 +38,11 @@ type TestApi =
3838 :> Get '[JSON] [Book]
3939 :<|> "books"
4040 :> ReqBody '[JSON] Book
41− :> PostNoContent '[JSON] ()
41+ -- :> PostNoContent '[JSON] ()
42+ :> Post '[JSON] ()
4243 :<|> "nothing"
43− :> GetNoContent '[JSON] ()
44+ :> Get '[JSON] ()
45+ -- :> GetNoContent '[JSON] ()
4446 :<|> "nothing"
4547 :> Put '[JSON] () -- old way to specify no content
4648 :<|> "with-a-header"
test/CompileSpec.hs+1−1
@@ -45,7 +45,7 @@ spec = do
4545 api = (Proxy :: Proxy TestApi)
4646 let out =
4747 T.unlines $
48− [ T.pack $ Elm.moduleHeader Elm0p18 moduleName
48+ [ T.pack $ Elm.moduleHeader Elm0p19 moduleName
4949 , ""
5050 , imports
5151 , ""
test/elm-sources/getBooksByIdSource.elm+3−2
@@ -8,7 +8,7 @@ type Book = Book
88jsonDecBook : Json.Decode.Decoder Book
99jsonDecBook = Debug.todo ""
1010
11−getBooksById : Int -> (Result Http.Error (Book) -> msg) -> Cmd msg
11+getBooksById : Int -> (Result Http.Error (Book) -> msg) -> Cmd msg
1212getBooksById capture_id toMsg =
1313 let
1414 params =
@@ -24,7 +24,8 @@ getBooksById capture_id toMsg =
2424 , url =
2525 Url.Builder.crossOrigin ""
2626 [ "books"
27− , (capture_id |> String.fromInt)
27+ , Url.percentEncode (capture_id
28+ |> String.fromInt)
2829 ]
2930 params
3031 , body =
test/elm-sources/getBooksByTitleSource.elm+3−3
@@ -7,8 +7,8 @@ import Json.Decode as J
77type alias Book = {}
88jsonDecBook = J.succeed {}
99
10−getBooksByTitle : String -> (Result Http.Error (Book) -> msg) -> Cmd msg
11−getBooksByTitle capture_title toMsg =
10+getBooksByTitlespace : String -> (Result Http.Error (Book) -> msg) -> Cmd msg
11+getBooksByTitlespace capture_title_space toMsg =
1212 let
1313 params =
1414 List.filterMap identity
@@ -23,7 +23,7 @@ getBooksByTitle capture_title toMsg =
2323 , url =
2424 Url.Builder.crossOrigin ""
2525 [ "books"
26− , (capture_title)
26+ , Url.percentEncode (capture_title_space)
2727 ]
2828 params
2929 , body =
test/elm-sources/getBooksSource.elm+1−1
@@ -9,7 +9,7 @@ type alias Book = {}
99
1010jsonDecBook = J.succeed {}
1111
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
1313getBooks query_published query_sort query_year query_category query_filters toMsg =
1414 let
1515 params =
test/elm-sources/getNothingSource.elm+2−2
@@ -4,7 +4,7 @@ import Http
44import Url.Builder
55
66
7−getNothing : (Result Http.Error (()) -> msg) -> Cmd msg
7+getNothing : (Result Http.Error (()) -> msg) -> Cmd msg
88getNothing toMsg =
99 let
1010 params =
@@ -25,7 +25,7 @@ getNothing toMsg =
2525 , body =
2626 Http.emptyBody
2727 , expect =
28− Http.expectString
28+ Http.expectString
2929 (\x -> case x of
3030 Err e -> toMsg (Err e)
3131 Ok _ -> toMsg (Ok ()))
test/elm-sources/getOneSource.elm+1−1
@@ -5,7 +5,7 @@ import Json.Decode exposing (..)
55import Url.Builder
66
77
8−getOne : (Result Http.Error (Int) -> msg) -> Cmd msg
8+getOne : (Result Http.Error (Int) -> msg) -> Cmd msg
99getOne toMsg =
1010 let
1111 params =
test/elm-sources/getOneWithDynamicUrlSource.elm+1−1
@@ -5,7 +5,7 @@ import Url.Builder
55import Json.Decode exposing (..)
66
77
8−getOne : String -> (Result Http.Error (Int) -> msg) -> Cmd msg
8+getOne : String -> (Result Http.Error (Int) -> msg) -> Cmd msg
99getOne urlBase toMsg =
1010 let
1111 params =
test/elm-sources/getPolymorphicData.elm+1−1
@@ -14,7 +14,7 @@ jsonDecSomeRecord : Json.Decode.Decoder SomeRecord
1414jsonDecSomeRecord = Debug.todo "finish"
1515
1616
17−getPolymorphicData : (Result Http.Error ((PolymorphicData (List String) SomeRecord)) -> msg) -> Cmd msg
17+getPolymorphicData : (Result Http.Error ((PolymorphicData (List String) SomeRecord)) -> msg) -> Cmd msg
1818getPolymorphicData toMsg =
1919 let
2020 params =
test/elm-sources/getWithaheaderSource.elm+1−1
@@ -5,7 +5,7 @@ import Url.Builder
55import Json.Decode exposing (..)
66
77
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
99getWithaheader header_myStringHeader header_MyIntHeader header_MyRequiredStringHeader header_MyRequiredIntHeader toMsg =
1010 let
1111 params =
test/elm-sources/getWitharesponseheaderSource.elm+1−1
@@ -5,7 +5,7 @@ import Url.Builder
55import Json.Decode exposing (..)
66
77
8−getWitharesponseheader : (Result Http.Error (String) -> msg) -> Cmd msg
8+getWitharesponseheader : (Result Http.Error (String) -> msg) -> Cmd msg
99getWitharesponseheader toMsg =
1010 let
1111 params =
test/elm-sources/postBooksSource.elm+2−2
@@ -7,7 +7,7 @@ import Json.Encode as Enc
77type alias Book = {}
88jsonEncBook = \b -> Enc.object []
99
10−postBooks : Book -> (Result Http.Error (()) -> msg) -> Cmd msg
10+postBooks : Book -> (Result Http.Error (()) -> msg) -> Cmd msg
1111postBooks body toMsg =
1212 let
1313 params =
@@ -28,7 +28,7 @@ postBooks body toMsg =
2828 , body =
2929 Http.jsonBody (jsonEncBook body)
3030 , expect =
31− Http.expectString
31+ Http.expectString
3232 (\x -> case x of
3333 Err e -> toMsg (Err e)
3434 Ok _ -> toMsg (Ok ()))
test/elm-sources/postTwoSource.elm+1−1
@@ -6,7 +6,7 @@ import Json.Encode
66import Url.Builder
77
88
9−postTwo : String -> (Result Http.Error ((Maybe Int)) -> msg) -> Cmd msg
9+postTwo : String -> (Result Http.Error ((Maybe Int)) -> msg) -> Cmd msg
1010postTwo body toMsg =
1111 let
1212 params =
test/elm-sources/putNothingSource.elm+2−2
@@ -4,7 +4,7 @@ import Http
44import Url.Builder
55
66
7−putNothing : (Result Http.Error (()) -> msg) -> Cmd msg
7+putNothing : (Result Http.Error (()) -> msg) -> Cmd msg
88putNothing toMsg =
99 let
1010 params =
@@ -25,7 +25,7 @@ putNothing toMsg =
2525 , body =
2626 Http.emptyBody
2727 , expect =
28− Http.expectString
28+ Http.expectString
2929 (\x -> case x of
3030 Err e -> toMsg (Err e)
3131 Ok _ -> toMsg (Ok ()))