我试图在榆树中执行GET请求。该函数返回我正在尝试执行的任务。不幸的是,我的参考资料是Elm 0.17,我收集的是Task.perform的签名已经改变。
fetchTasks: MyModel -> String -> Platform.Task Http.Error (Dict String MyTask)
fetchTasks model apiUrl=
{ method = "GET"
, headers = [ Http.header "Content-Type" "application/json"
, Http.header "Authorization" model.token ]
, url = apiUrl
, body = Http.emptyBody
, expect = Http.expectJson (dict taskDecoder)
, timeout = Nothing
, withCredentials = False }
|> Http.request
|> Http.toTask
fetchTaskCmd : MyModel -> String -> Cmd Msg
fetchTaskCmd model apiUrl =
Task.perform AuthError GetTasksSuccess <| fetchTasks model apiUrl
这是我的GET请求函数和执行任务的命令。 AuthError和GetTasksSuccess都是我定义的Messaging。我在Elm Docs中读到的任务执行的新签名是
perform : (a -> msg) -> Task Never a -> Cmd msg
我需要做些什么来实现我的命令?
答案 0 :(得分:2)
更改比您建议的更大,Http库现在主要使用命令,而不是任务。所以现在写它的方法是:
makeRequest model apiUrl=
Http.request
{ method = "GET"
, headers = [ Http.header "Content-Type" "application/json"
, Http.header "Authorization" model.token ]
, url = apiUrl
, body = Http.emptyBody
, expect = Http.expectJson (dict taskDecoder)
, timeout = Nothing
, withCredentials = False }
fetchTaskCmd : (Result Error a -> Msg) -> MyModel -> String -> Cmd Msg
fetchTaskCmd msgConstructor model apiUrl =
Http.send msgConstructor (makeRequest model apiUrl)
如果您想使用令牌,您可能还需要考虑使用我的elm-jwt库来提供帮助。