andrewMacmurray/elm-concurrent-task/1.1.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.

bytesBody : String -> Bytes -> Body

Put some Bytes in the body of your Request. This allows you to use elm/bytes to have full control over the binary representation of the data you are sending. For example, you could create an archive.zip file and send it along like this:

import Bytes exposing (Bytes)


zipBody : Bytes -> Body
zipBody bytes =
    bytesBody "application/zip" bytes

The first argument is a MIME type of the body. In other scenarios you may want to use MIME types like image/png or image/jpeg instead.

NOTE: Because Bytes can't be sent out of a port (internally ConcurrentTask sends all its arguments out of a port), they are serialised to a base64 encoded String.

Unfortunately some of the performance benefits of Bytes are lost at this point.

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.

expectBytes : Decoder a -> Expect a

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

Discard the response body.

withMetadata : (Metadata -> a -> b) -> Expect a -> Expect b

Include Http metadata in a successful response.

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.