在Elm(0.18)中,我调用一个http DELETE端点,如果成功响应200并且空体.
在这种情况下(成功)我需要传回一个带有初始id(OnDelete playerId)的消息.但由于身体是空的,我无法从那里解析它.
目前我正在做这样的,但在那里写的更优雅的方式expect的一部分Http.Request:
Http.expectStringResponse (\response -> Ok playerId)
Run Code Online (Sandbox Code Playgroud)
?
这反映了我目前的代码:
deletePlayer : PlayerId -> Cmd Msg
deletePlayer playerId =
deleteRequest playerId
|> Http.send OnDelete
deleteRequest : PlayerId -> Http.Request PlayerId
deleteRequest playerId =
Http.request
{ body = Http.emptyBody
, expect = Http.expectStringResponse (\response -> Ok playerId)
, headers = []
, method = "DELETE"
, timeout = Nothing
, url = "http://someHost/players/" ++ playerId
, withCredentials = False
}
type alias PlayerId =
String
Run Code Online (Sandbox Code Playgroud)
榆树v0.19补充道expectWhatever.它Result在检查错误时表现略有不同,但效果类似.
我expectUnit为"空"200响应创建了一个帮助器.
expectUnit : Expect ()
expectUnit =
Http.expectStringResponse << always <| Ok ()
deleteThing : String -> Request ()
deleteThing path =
Http.request
{ method = "DELETE"
, headers = []
, url = "http://localhost/api"
, body = Http.jsonBody <| Encode.object [ ( "path", Encode.string path ) ]
, expect = expectUnit
, timeout = Nothing
, withCredentials = False
}
Run Code Online (Sandbox Code Playgroud)
但对你来说,你能得到的最好的是.
{ ...
, expect = Http.expectStringResponse << always <| Ok playerId
...
}
Run Code Online (Sandbox Code Playgroud)
或者你可以创建一个帮助(这实际上是为singleton或pure为Expect)
alwaysExpect : a -> Expect a
alwaysExpect =
Http.expectStringResponse << always << Ok
Run Code Online (Sandbox Code Playgroud)
哪个可以用
{ ...
, expect = alwaysExpect playerId
...
}
Run Code Online (Sandbox Code Playgroud)