Elm 0.18 Http 请求

Elm 0.18 Http requests

我正在尝试在 elm 中执行 GET 请求。函数 returns 我正在尝试执行的任务。不幸的是,我的参考 material 是 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 都是我定义的消息。我在 Elm 文档中读到的任务执行的新签名是

perform : (a -> msg) -> Task Never a -> Cmd msg

我必须执行哪些操作才能使我的命令正常工作?

变化比您建议的要大,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 库来提供帮助。