andrewMacmurray/elm-concurrent-task/1.0.0

NOTE: the latest version is 2.0.0

ConcurrentTask.Http

Make concurrent http requests.

The JavaScript runner has this task builtin by default. Internally It uses the fetch api which is widely supported in the Browser and (as of Node 18) in NodeJS.

If needed you can supply a custom implementation like so:

import * as ConcurrentTask from "@andrewmacmurray/elm-concurrent-task"
import { HttpRequest, HttpResponse } from "@andrewmacmurray/elm-concurrent-task"


ConcurrentTask.register({
  tasks: {},
  ports: app.ports,
  builtins: {
    http: customRequest
  },
});


function customRequest(req: HttpRequest): Promise<HttpResponse> {
  return ...<Your Custom Http Request>
}

See the typescript definitions and the fetch adapter to see how to create your own.

Note:

You're not required to use this module for http requests in ConcurrentTask, it's here for convenience. You could create entirely your own from scratch - maybe you want an http package with request caching or special retry logic built in on the JS side.

Requests

{ url : String
, method : String
, headers : List Header
, body : Body
, expect : Expect a
, timeout : Maybe Int
}
-> ConcurrentTask Error a

Send an Http request - similar to elm/http's Http.Task

get :
{ url : String
, headers : List Header
, expect : Expect a
, timeout : Maybe Int
}
-> ConcurrentTask Error a

Send an Http GET request

{ url : String
, headers : List Header
, body : Body
, expect : Expect a
, timeout : Maybe Int
}
-> ConcurrentTask Error a

Send an Http POST request

Body

type Body

Send data in your http request.

Create an empty body for your request. This is useful for GET requests and POST requests where you are not sending any data.

stringBody : String -> String -> Body

Put a String in the body of your request. Defining jsonBody looks like this:

import Json.Encode as Encode


jsonBody : Encode.Value -> Body
jsonBody value =
    stringBody "application/json" (Encode.encode 0 value)

The first argument is a MIME type of the body.

jsonBody : Value -> Body

Put some JSON value in the body of your request. This will automatically add the Content-Type: application/json header.

Expect

type Expect a

Describe what you expect to be returned in an http response body.

expectJson : Decoder a -> Expect a

Expect the response body to be JSON, decode it using the supplied decoder.

Expect the response body to be a String.

Discard the response body.

Headers

Error

type Error
= BadUrl String
| Timeout
| NetworkError
| BadStatus Metadata Value
| BadBody Metadata Value Error

A Request can fail in a couple ways:

  • BadUrl means you did not provide a valid URL.
  • Timeout means it took too long to get a response.
  • NetworkError means the user turned off their wifi, went in a cave, etc.
  • BadStatus means you got a response back, but the status code indicates failure. Contains:
    • The response Metadata.
    • The raw response body as a Json.Decode.Value.
  • BadBody means you got a response back with a nice status code, but the body of the response was something unexpected. Contains:
    • The response Metadata.
    • The raw response body as a Json.Decode.Value.
    • The Json.Decode.Error that caused the error.
type alias Metadata =
{ url : String
, statusCode : Int
, statusText : String
, headers : Dict String String
}

Extra information about the response:

  • url of the server that actually responded (so you can detect redirects)
  • statusCode like 200 or 404
  • statusText describing what the statusCode means a little
  • headers like Content-Length and Expires

Note:

It is possible for a response to have the same header multiple times. In that case, all the values end up in a single entry in the headers dictionary. The values are separated by commas, following the rules outlined here.