Transport.http
A transport that POSTs each call to a URL, with optional JSON-RPC batching. It uses the
HttpClient in context, so headers, retries and proxies are set on that client.
import { Effect, Layer } from "effect";import { FetchHttpClient } from "effect/http";import { Transport } from "@ethect/core";import { Eth } from "@ethect/core/rpc";
const Http = Transport.http("http://127.0.0.1:8545").pipe( Layer.provide(FetchHttpClient.layer),);
const block = await Eth.blockNumber().pipe(Effect.provide(Http), Effect.runPromise);FetchHttpClient.layer is the client built on the global fetch, which browsers, Node, Bun,
Deno and workers all have.
Batching
Section titled “Batching”With batch, the calls made together leave as one JSON-RPC batch.
import { Effect, Layer } from "effect";import { FetchHttpClient } from "effect/http";import { Transport } from "@ethect/core";import { Eth } from "@ethect/core/rpc";
const Http = Transport.http("http://127.0.0.1:8545", { batch: { maxSize: 100 } }).pipe( Layer.provide(FetchHttpClient.layer),);
const [chainId, block, gasPrice] = await Effect.all( [Eth.chainId(), Eth.blockNumber(), Eth.gasPrice()], { concurrency: "unbounded" },).pipe(Effect.provide(Http), Effect.runPromise);One HTTP request carries the three calls. See Batching for what counts as “made together”.
A URL with a key in it
Section titled “A URL with a key in it”A provider’s URL often has an API key in it. Read it from the environment as a secret, and give it as it is: the transport never writes it.
import { Config, Effect, Layer } from "effect";import { FetchHttpClient } from "effect/http";import { Transport } from "@ethect/core";
const Http = Layer.unwrap( // a Redacted<string>: printing it gives "<redacted>" Effect.map(Config.Redacted("RPC_URL"), (url) => Transport.http(url)),).pipe(Layer.provide(FetchHttpClient.layer));// Layer<Transport, ConfigError>A failure then names the URL <redacted>. See url for all that changes.
Headers and authentication
Section titled “Headers and authentication”Change the requests on the client, then give that client to the transport.
import { Effect, Layer } from "effect";import { FetchHttpClient, HttpClient, HttpClientRequest } from "effect/http";import { Transport } from "@ethect/core";
const Authenticated = Layer.effect(HttpClient.HttpClient)( Effect.map( HttpClient.HttpClient, HttpClient.mapRequest(HttpClientRequest.bearerToken("my-api-key")), ),).pipe(Layer.provide(FetchHttpClient.layer));
const Http = Transport.http("https://rpc.example.com").pipe( Layer.provide(Authenticated),);Every request of the transport now carries Authorization: Bearer my-api-key.
Retries
Section titled “Retries”HttpClient.retryTransient sends a request again when it fails in a way Effect considers
transient, such as a 429 status.
import { Effect, Layer, Schedule } from "effect";import { FetchHttpClient, HttpClient } from "effect/http";import { Transport } from "@ethect/core";
const Retrying = Layer.effect(HttpClient.HttpClient)( Effect.map( HttpClient.HttpClient, HttpClient.retryTransient({ schedule: Schedule.max([Schedule.exponential("200 millis"), Schedule.recurs(3)]), }), ),).pipe(Layer.provide(FetchHttpClient.layer));
const Http = Transport.http("https://rpc.example.com").pipe(Layer.provide(Retrying));The retry happens below the transport: the call only fails when the schedule ends.
Returns
Section titled “Returns”Layer<Transport, never, HttpClient>
A layer that provides Transport and needs an
HttpClient. It does not provide Subscriptions: HTTP cannot push.
Parameters
Section titled “Parameters”- Type:
Transport.Url | URL
Where the calls are POSTed. A Transport.Url is a string, or a Redacted<string> for a URL
that is a secret.
import { Layer } from "effect";import { FetchHttpClient } from "effect/http";import { Transport } from "@ethect/core";
const Http = Transport.http(new URL("https://rpc.example.com/v1")).pipe( Layer.provide(FetchHttpClient.layer),);A plain string or a URL says the URL is not a secret. A Redacted one is unwrapped only
where the request is made, and nothing of it is written anywhere else.
A string or a URL |
A Redacted<string> |
|
|---|---|---|
the message of a failure |
names the URL | names it <redacted>, or <redacted:label> when it has a label |
the cause of a failure |
the HTTP client’s error, which holds the request | a plain error: the tag of the client’s error, and its message without the URL |
a body the server answered with (reason.body) |
as it was sent | without the URL, where the server repeats it |
| the message of the node’s own error | as it was sent | without the URL, where the node repeats it |
| the HTTP client’s span | recorded, with the URL as an attribute | not recorded |
import { Layer, Redacted } from "effect";import { FetchHttpClient } from "effect/http";import { Transport } from "@ethect/core";
declare const url: string; // from a secret store, never written in the code
const Http = Transport.http(Redacted.make(url, { label: "mainnet" })).pipe( Layer.provide(FetchHttpClient.layer),);// a failure says: The HTTP request to <redacted:mainnet> failedThe URL is looked for as it was given, as a URL parser writes it, and by its parts: the host, the path, the query, and each long piece of the path and of the query, which is where a key is.
- The
dataof a node’s error is passed on as it is. It holds the bytes of a revert, which no text is taken out of. - What you add to the
HttpClientyourself still sees the request. A client that logs its requests logs their URL.
options (optional)
Section titled “options (optional)”- Type:
Transport.HttpOptions
| Field | Type | Default | Description |
|---|---|---|---|
batch |
{ window?, maxSize? } |
no batching | Sends the calls made within window of each other as one JSON-RPC batch. |
batch.window |
a Duration input, such as "10 millis" |
the current tick | How long calls are collected before the batch leaves. |
batch.maxSize |
number |
1000 |
The most calls one batch holds. More calls make more batches. |
import { Layer } from "effect";import { FetchHttpClient } from "effect/http";import { Transport } from "@ethect/core";
const Http = Transport.http("http://127.0.0.1:8545", { batch: { window: "10 millis", maxSize: 50 },}).pipe(Layer.provide(FetchHttpClient.layer));Pass batch: {} for the defaults. Without batch, every call is a request of its own.
Behavior
Section titled “Behavior”- One POST per call, with
Content-Type: application/json. Withbatch, one POST per batch, and the body is a JSON array even when the batch holds one call. - Ids count up from 0 for each transport that is built.
- An error status can still be an answer. When a response has a non-2xx status and its
body is a JSON-RPC response, the body is used: a
429that carries a JSON-RPC error fails withRpcError, not withHttpStatus. - An error that names no request is still the node’s error. A body whose
errorcomes with anullid, or with none, fails the call with thatRpcError. - Batch answers are matched by id, in any order. A call whose answer is missing from the
batch fails alone, with
MalformedResponse, and so does the call named by an entry that is not JSON-RPC. A single error in place of the array fails every call of the batch with that error. - A call that cannot be written as JSON fails alone with
Unserializable. The batch is sent without it. - Batching needs concurrency. Calls share a batch when they start together, as with
Effect.allandconcurrency: "unbounded". Calls run one after the other are one request each. See Batching. - Identical calls are not merged. Two
Eth.blockNumber()in one batch are two entries. - No timeout. Interrupting the call, for example with
Effect.timeout, aborts the HTTP request. - A secret URL is never written. With a
RedactedURL, no failure, log or span of the transport holds it: seeurl.
Errors
Section titled “Errors”Failures of the transport itself are TransportErrors.
Their reason says which:
| Reason | When |
|---|---|
Unreachable |
the HTTP request failed: no connection, a DNS failure, a refused port |
HttpStatus |
a non-2xx status whose body is not JSON-RPC. The reason carries status and body. |
MalformedResponse |
the body is not JSON, not a JSON-RPC response, or empty. The reason carries body. |
Unserializable |
the request cannot be written as JSON. Nothing was sent. |
A JSON-RPC error in the answer is an RpcError, as on every
transport.