Most Scrapers start with an HTTP request. The domain SDK has a few helpers so that every Scraper retries, times out and maps failures the same way.
They all work on Effect's HttpClient, the Capability from effect/http. None of them chooses a client: the Release does.
| Helper | Use it to |
|---|---|
Download.text | Fetch a body as text. |
Download.json | Fetch a body and decode it with a schema. |
Download.bytes | Fetch a body as bytes, such as an image. |
retryHttpClient | Add the same timeout and retry policy to a client you drive yourself. |
httpFailure | Map an HTTP client error to a Shared error. |
httpStatusFailure | Map a status code to a Shared error. |
isTransientHttpFailure | Decide whether a failure is worth retrying. |
decodeScrapedRecord | Check scraped data against a schema, failing with SchemaDrift. |
cleanText, clockTime, numericDate, localTimeToUtc | Read whitespace, times and dates out of scraped text. |
Download
Download.text(target, { encoding?, statuses? })
Download.json(target, schema, { statuses? })
Download.bytes(target, { statuses? })target is a URL, which is fetched with GET, or an HttpClientRequest for anything else: a POST, a body, headers. All three need an HttpClient and share one policy.
Example (Reading one page of the quotes API, from Your first Scraper)
const readPage = (page: number) =>
Download.json(`${quotesOrigin}/api/quotes?page=${page}`, Page);What every download does for you:
| Situation | Result |
|---|---|
| HTTP 401 or 403 | Blocked |
| HTTP 429 | RateLimited |
| Any other non-2xx status, a timeout, a transport error | UpstreamDown |
| An empty body | Blocked |
A Cloudflare challenge page, even with HTTP 200 (text and json) | Blocked |
- Timeouts. The response must arrive within 30 seconds, and so must its body.
- Retries. A GET or HEAD is retried twice when the failure is transient, with exponential backoff starting at 250 milliseconds. Any other method is sent once, because repeating a POST can repeat its effect on the site.
- Spans. Each call opens a
Download.text,Download.jsonorDownload.bytesspan with themethodandurl.
Status overrides
statuses maps single status codes to other errors. Each override receives the failure's reason, and its error joins the errors the download can fail with. An override can return a Shared error or one of the Scraper's own.
Example (A source that answers 402 when its quota is spent)
const feed = yield* Download.json(url, FeatureCollection, {
statuses: { 402: (reason) => new QuotaExhausted({ reason }) },
});Download.text
Returns the body as a string. It is decoded with the character set from the content-type header. When the header names none, the encoding option is used, then UTF-8. Pass it for sources that serve a legacy encoding without declaring it:
const body = yield* Download.text(url, { encoding: "windows-1252" });A character set the runtime cannot decode fails with SchemaDrift.
Download.json
Reads the body as text, parses it as JSON and decodes it with schema. Pass the schema of the JSON value itself, not a Schema.fromJsonString. A body that is not JSON, or that the schema rejects, fails with SchemaDrift: the issue as reason and the first 500 characters of the body as sample. Fields the schema does not name are ignored.
Example (A POST with a form body)
const search = (term: string) =>
Download.json(
HttpClientRequest.post("https://source.example/search").pipe(
HttpClientRequest.bodyUrlParams({ q: term })
),
Results
);Download.bytes
Returns the body as a Uint8Array, for images and other binary data. It does not look for a challenge page.
retryHttpClient
retryHttpClient(client: HttpClient.HttpClient)Wraps a client with the policy Download uses for a GET:
- A non-2xx status is an error.
- Each response must arrive within 30 seconds.
- Transient failures are retried twice, with exponential backoff starting at 250 milliseconds.
Use it when you hand a client to something else that sends the requests, such as an HttpApiClient. It retries every method, and the code that reads the response maps its failures with httpFailure.
httpFailure and httpStatusFailure
httpFailure(error, overrides?)
httpStatusFailure(status: number, reason: string, overrides?)httpFailure maps what an HttpClient fails with, an HttpClientError or a timeout, to a Shared error. httpStatusFailure does the same from a bare status code, for sources where you read the status yourself, such as a browser navigation.
| Input | Result |
|---|---|
| Status 401 or 403 | Blocked |
| Status 429 | RateLimited |
| Any other status | UpstreamDown |
| A timeout or a transport error | UpstreamDown |
overrides maps single status codes to other errors, as statuses does for a download. Download maps failures with these two functions, so a browser navigation and an HTTP request fail the same way.
isTransientHttpFailure
isTransientHttpFailure(error): booleanReturns true for a timeout, a transport error, HTTP 408 and any 5xx status. Everything else, including 403 and 429, is not transient: retrying immediately would only repeat the refusal.
retryHttpClient and Download use it as their retry condition. Use it for your own retries around HTTP calls.
decodeScrapedRecord
decodeScrapedRecord(schema)(read)Takes a schema and returns a function that wraps an Effect. The Effect's result is checked against the schema, unknown fields included. A mismatch fails with SchemaDrift, with the issue as reason and the first 500 characters of the data as sample.
Example (Checking what a browser page returned)
const Rendered = Schema.Struct({
hasNext: Schema.Boolean,
quotes: Schema.Array(Quote),
});
const rendered = yield* page
.evaluate(() => ({
hasNext: document.querySelector(".pager .next a") !== null,
quotes: [],
}))
.pipe(decodeScrapedRecord(Rendered));Use it at every point where data enters your code from outside your types: a parsed HTML table, a page.evaluate result, a mapped API record. It catches drift where it happens, with a sample, instead of later as a vague output mismatch.
Text and date helpers
Scraped pages spell dates and times in many ways. These four helpers read the common forms and return null when a value is not there.
| Helper | Returns |
|---|---|
cleanText(value) | The text with every run of whitespace collapsed to one space and trimmed. undefined becomes "". |
clockTime(value) | The first HH:MM time in the text, accepting :, . or h as separator, zero-padded, or null. |
numericDate(value) | The first day-month-year date in the text, such as 8.8.2026 or 08/08/26, as YYYY-MM-DD, or null. |
localTimeToUtc(date, time, timeZone) | The UTC ISO instant of a local YYYY-MM-DD date and HH:MM time in an IANA time zone, or null when either is null. |
They return strings, not schemas: each Scraper still declares its own output schema.
Sessions and cookies
The helpers return and accept plain HttpClient values, so Effect's own combinators apply. A source that needs a session wraps the client before using it:
const client = (yield* HttpClient.HttpClient).pipe(
HttpClient.withCookiesRef(cookies)
);Keep cookie jars and tokens inside one Run. A Worker isolate serves many requests, and module-level state would leak between them. Lint bans module-level let for that reason.
Source
- download.ts
- download.test.ts: the policy, per method, status and body
- http-failure.ts
- decode-scraped-record.ts
- usgs-feed.ts:
Download.jsonwith a status override anddecodeScrapedRecord