git.iko.soy/haskell-servant/servant-elm
on master help sign in
Update to http 2.0.0, remove bad symbols
commit495ebe2f1057a6c2018e1fcb32b41e73830f22d7 authorKostiantyn Rybnikov <k-bx@k-bx.com> dateTue 28 May 2019, 15:39 (7 years ago) parentee9d036
1 file changed+50−22
src/Servant/Elm/Internal/Generate.hs+50−22
src/Servant/Elm/Internal/Generate.hs+50−22
@@ -238,7 +238,9 @@ generateElmForRequest opts request =
238238 ]
239239
240240 fnName =
241− request ^. F.reqFuncName . to (T.replace "-" "" . F.camelCase) . to stext
241+ request ^. F.reqFuncName . to (replace . F.camelCase) . to stext
242+
243+ replace = T.replace "-" "" . T.replace "." ""
242244
243245 typeSignature =
244246 mkTypeSignature opts request
@@ -260,7 +262,7 @@ mkTypeSignature opts request =
260262 , headerTypes
261263 , urlCaptureTypes
262264 , queryTypes
263− , catMaybes [bodyType, returnType]
265+ , catMaybes [bodyType, toMsgType, returnType]
264266 ]
265267 where
266268 urlPrefixType :: Maybe Doc
@@ -297,10 +299,15 @@ mkTypeSignature opts request =
297299 bodyType =
298300 fmap elmTypeRef $ request ^. F.reqBody
299301
302+ toMsgType :: Maybe Doc
303+ toMsgType = do
304+ result <- fmap elmTypeRef $ request ^. F.reqReturnType
305+ Just ("(Result Http.Error " <+> parens result <+> " -> msg)")
306+
300307 returnType :: Maybe Doc
301308 returnType = do
302309 result <- fmap elmTypeRef $ request ^. F.reqReturnType
303− pure ("Http.Request" <+> result)
310+ pure ("Cmd msg")
304311
305312
306313elmHeaderArg :: F.HeaderArg EType -> Doc
@@ -312,7 +319,9 @@ elmHeaderArg header =
312319elmCaptureArg :: F.Segment EType -> Doc
313320elmCaptureArg segment =
314321 "capture_" <>
315− F.captureArg segment ^. F.argName . to (stext . F.unPathSegment)
322+ F.captureArg segment ^. F.argName . to (stext . replace . F.unPathSegment)
323+ where
324+ replace = T.replace "-" "_"
316325
317326
318327elmQueryArg :: F.QueryArg EType -> Doc
@@ -360,6 +369,7 @@ mkArgs opts request =
360369 ]
361370 , -- Request body
362371 maybe [] (const [elmBodyArg]) (request ^. F.reqBody)
372+ , pure "toMsg"
363373 ]
364374
365375
@@ -441,8 +451,8 @@ mkRequest opts request =
441451 indent i expect
442452 , "timeout =" <$>
443453 indent i "Nothing"
444− , "withCredentials =" <$>
445− indent i "False"
454+ , "tracker =" <$>
455+ indent i "Nothing"
446456 ])
447457 where
448458 method =
@@ -487,23 +497,41 @@ mkRequest opts request =
487497
488498 expect =
489499 case request ^. F.reqReturnType of
490− Just elmTypeExpr | isEmptyType opts elmTypeExpr ->
491− let elmConstructor =
492− toElmTypeRefWith opts elmTypeExpr
493− in
494− "Http.expectStringResponse" <$>
495− indent i (parens (backslash <> " rsp " <+> "->" <$>
496− indent i ("if String.isEmpty rsp.body then" <$>
497− indent i "Ok" <+> stext elmConstructor <$>
498− "else" <$>
499− indent i ("Err" <+> dquotes "Expected the response body to be empty")) <> line))
500−
501−
500+ Just elmTypeExpr
501+ | isEmptyType opts elmTypeExpr
502+ -- let elmConstructor = T.pack (renderElm elmTypeExpr)
503+ ->
504+ "Http.expectString " <> line <+> indent i "(\\x -> case x of" <> line <+>
505+ indent i "Err e -> toMsg (Err e)" <> line <+>
506+ indent i "Ok _ -> toMsg (Ok ()))"
502507 Just elmTypeExpr ->
503− "Http.expectJson <|" <+> stext (toElmDecoderRefWith opts elmTypeExpr)
504−
505− Nothing ->
506− error "mkHttpRequest: no reqReturnType?"
508+ "Http.expectJson toMsg" <+> renderDecoderName elmTypeExpr
509+ Nothing -> error "mkHttpRequest: no reqReturnType?"
510+ -- case request ^. F.reqReturnType of
511+ -- Just elmTypeExpr | isEmptyType opts elmTypeExpr ->
512+ -- let elmConstructor =
513+ -- toElmTypeRefWith opts elmTypeExpr
514+ -- in
515+ -- "Http.expectStringResponse" <$>
516+ -- indent i (parens (backslash <> " rsp " <+> "->" <$>
517+ -- indent i ("if String.isEmpty rsp.body then" <$>
518+ -- indent i "Ok" <+> stext elmConstructor <$>
519+ -- "else" <$>
520+ -- indent i ("Err" <+> dquotes "Expected the response body to be empty")) <> line))
521+
522+
523+ -- Just elmTypeExpr ->
524+ -- "Http.expectJson <|" <+> stext (toElmDecoderRefWith opts elmTypeExpr)
525+
526+ -- Nothing ->
527+ -- error "mkHttpRequest: no reqReturnType?"
528+
529+renderDecoderName :: EType -> Doc
530+renderDecoderName elmTypeExpr =
531+ case elmTypeExpr of
532+ ETyApp (ETyCon (ETCon "List")) t ->
533+ parens ("Json.Decode.list " <> parens (renderDecoderName t))
534+ _ -> ("jsonDec" <> stext (T.pack (renderElm elmTypeExpr)))
507535
508536
509537mkUrl :: ElmOptions -> [F.Segment EType] -> Doc