ohanhi/remotedata-http/1.0.1

NOTE: this package is not compatible with Elm 0.19.1

RemoteData.Http

Friendly abstraction over remote API communication in JSON.

Commands

get : String -> (WebData success -> msg) -> Decoder success -> Cmd msg

GET request as a command. Has a no-cache header to ensure data integrity.

type Msg
    = HandleGetCat (WebData Cat)


getCat : Cmd Msg
getCat =
    get "/api/cats/1" HandleGetCat catDecoder
post : String -> (WebData success -> msg) -> Decoder success -> Value -> Cmd msg

POST request as a command.

type Msg
    = HandlePostCat (WebData Cat)


postCat : Cat -> Cmd Msg
postCat cat =
    post "/api/cats/" HandlePostCat catDecoder (encodeCat cat)
put : String -> (WebData success -> msg) -> Decoder success -> Value -> Cmd msg

PUT request as a command.

type Msg
    = HandlePutCat (WebData Cat)


putCat : Cat -> Cmd Msg
putCat cat =
    put "/api/cats/" HandlePutCat catDecoder (encodeCat cat)
patch : String -> (WebData success -> msg) -> Decoder success -> Value -> Cmd msg

PATCH request as a command.

type Msg
    = HandlePatchCat (WebData Cat)


patchCat : Cat -> Cmd Msg
patchCat cat =
    patch "/api/cats/" HandlePatchCat catDecoder (encodeCat cat)
delete : String -> (WebData success -> msg) -> Decoder success -> Value -> Cmd msg

DELETE request as a command.

type Msg
    = HandleDeleteCat (WebData DeleteResponse)


deleteCat : Cat -> Cmd Msg
deleteCat cat =
    delete "/api/cats/" HandleDeleteCat deleteDecoder (encodeCat cat)

Tasks

getTask : String -> Decoder success -> Task Never (WebData success)

GET request as a task. Has a no-cache header to ensure data integrity.

getCatTask : Task Never (WebData Cat)
getCatTask =
    getTask "/api/cats/1" catDecoder
postTask : String -> Decoder success -> Value -> Task Never (WebData success)

POST request as a task.

postCatTask : Cat -> Task Never (WebData Cat)
postCatTask cat =
    postTask "/api/cats/" catDecoder (encodeCat cat)
putTask : String -> Decoder success -> Value -> Task Never (WebData success)

PUT request as a task.

putCatTask : Cat -> Task Never (WebData Cat)
putCatTask cat =
    putTask "/api/cats/" catDecoder (encodeCat cat)
patchTask : String -> Decoder success -> Value -> Task Never (WebData success)

PATCH request as a task.

patchCatTask : Cat -> Task Never (WebData Cat)
patchCatTask cat =
    patchTask "/api/cats/" catDecoder (encodeCat cat)
deleteTask : String -> Decoder success -> Value -> Task Never (WebData success)

DELETE request as a task.

deleteCatTask : Cat -> Task Never (WebData Cat)
deleteCatTask cat =
    deleteTask "/api/cats/" catDecoder (encodeCat cat)

Additional configuration

type alias Config =
{ headers : List Header
, withCredentials : Bool
, timeout : Maybe Time
}

If you need more control over the request, you can use the <verb>WithConfig function with a record like this.

specialConfig : Config
specialConfig =
    { headers = [ specialHeader, anotherHeader ]
    , withCredentials = True
    , timeout = Nothing
    }


postCat : Cat -> Cmd Msg
postCat cat =
    postWithConfig specialConfig "/api/cats/" HandlePostCat catDecoder (encodeCat cat)
defaultConfig : Config

The default configuration for all requests besides GET:

  • empty headers
  • without credentials
  • no timeout
noCacheConfig : Config

The default configuration for GET requests:

  • a no-cache header
  • without credentials
  • no timeout

Commands with configuration

Config
-> String
-> (WebData success -> msg)
-> Decoder success
-> Cmd msg

GET request as a command, with cache. NB. allowing cache in API GET calls can lead to strange conditions.

getWithConfig noCacheConfig "http://example.com/users.json" userDecoder
  == get "http://example.com/users.json" userDecoder

For a request without any headers, you can use:

getWithConfig defaultConfig url decoder
Config
-> String
-> (WebData success -> msg)
-> Decoder success
-> Value
-> Cmd msg

POST request as a command, with additional Config.

postWithConfig defaultConfig == postTask
Config
-> String
-> (WebData success -> msg)
-> Decoder success
-> Value
-> Cmd msg

PUT request as a command, with additional Config.

putWithConfig defaultConfig == putTask
Config
-> String
-> (WebData success -> msg)
-> Decoder success
-> Value
-> Cmd msg

PATCH request as a command, with additional Config.

patchWithConfig defaultConfig == patchTask
Config
-> String
-> (WebData success -> msg)
-> Decoder success
-> Value
-> Cmd msg

DELETE request as a command, with additional Config.

deleteWithConfig defaultConfig == deleteTask

Tasks with configuration

getTaskWithConfig : Config -> String -> Decoder success -> Task Never (WebData success)

GET request as a task, with additional Config. NB. allowing cache in API GET calls can lead to strange conditions.

getTaskWithConfig noCacheConfig "http://example.com/users.json" userDecoder
  == getTask "http://example.com/users.json" userDecoder

For a request without any headers, you can use:

getTaskWithConfig defaultConfig url decoder
Config
-> String
-> Decoder success
-> Value
-> Task Never (WebData success)

POST request as a task, with additional Config.

postTaskWithConfig defaultConfig == postTask
Config
-> String
-> Decoder success
-> Value
-> Task Never (WebData success)

PUT request as a task, with additional Config.

putTaskWithConfig defaultConfig == putTask
Config
-> String
-> Decoder success
-> Value
-> Task Never (WebData success)

PATCH request as a task, with additional Config.

patchTaskWithConfig defaultConfig == patchTask
Config
-> String
-> Decoder success
-> Value
-> Task Never (WebData success)

DELETE request as a task, with additional Config.

deleteTaskWithConfig defaultConfig == deleteTask

Helpers

url : String -> List ( String, String ) -> String

This is the old url function from evancz/elm-http.

Create a properly encoded URL with a query string. The first argument is the portion of the URL before the query string, which is assumed to be properly encoded already. The second argument is a list of all the key/value pairs needed for the query string. Both the keys and values will be appropriately encoded, so they can contain spaces, ampersands, etc. url "http://example.com/users" [ ("name", "john doe"), ("age", "30") ] -- http://example.com/users?name=john+doe&age=30