In this guide you build quotes, a Scraper that reads quotes.toscrape.com, a site made for scraping practice. It reads the site's JSON API page by page and emits one item per quote.
You write four small files: the schemas, the Definition, the Implementation and the package's exports.
Create the package
In your Scraper repository, create packages/quotes. Copy tsconfig.json, tsconfig.build.json and vitest.config.ts unchanged from the Scwrap repository's example Scraper, packages/earthquakes, and write this package.json:
{
"name": "@scwrap/quotes",
"version": "0.1.0",
"private": true,
"files": ["dist", "src"],
"type": "module",
"types": "./src/index.ts",
"exports": {
"./schema": {
"types": "./src/definition.ts",
"import": "./dist/definition.js",
"default": "./dist/definition.js"
},
".": {
"types": "./src/index.ts",
"import": "./dist/index.js",
"default": "./dist/index.js"
}
},
"scripts": {
"typecheck": "tsc --noEmit -p tsconfig.json",
"test": "vitest run",
"build": "tsc -p tsconfig.build.json"
},
"dependencies": {
"@scwrap/domain": "0.5.0",
"effect": "4.2.0"
},
"devDependencies": {
"@effect/vitest": "4.2.0",
"@types/node": "24.13.6",
"typescript": "7.0.2",
"vitest": "5.0.1"
}
}Run pnpm install to link it into the workspace and install @scwrap/domain from npm.
The package has two entry points. @scwrap/quotes exports everything. @scwrap/quotes/schema exports the Definition alone, so a client can import the contract without the scraping code.
Describe the data
Start with what goes in and what comes out. Both are Effect schemas.
Example (src/quote.ts)
import { Schema } from "effect";
export const QuotesQuery = Schema.Struct({
maxPages: Schema.Int.check(Schema.isBetween({ maximum: 10, minimum: 1 })),
});
export const Quote = Schema.Struct({
author: Schema.NonEmptyString,
tags: Schema.Array(Schema.NonEmptyString),
text: Schema.NonEmptyString,
});Make invalid input impossible to express. maxPages must be a whole number from 1 to 10, so a caller cannot ask for page zero or for a thousand pages. The same goes for output: a quote without an author is not a Quote.
Write the Definition
The Definition joins the schemas with an ID and the errors the Scraper can fail with.
Example (src/definition.ts)
import {
Blocked,
RateLimited,
SchemaDrift,
Scraper,
UpstreamDown,
} from "@scwrap/domain";
import { Schema } from "effect";
import { Quote, QuotesQuery } from "./quote.js";
export { Quote, QuotesQuery } from "./quote.js";
export const Quotes = Scraper.make("quotes", {
error: Schema.Union([Blocked, RateLimited, SchemaDrift, UpstreamDown]),
input: QuotesQuery,
output: Quote,
});"quotes" is the Scraper's ID everywhere: in the instance catalog, in URLs and in stored results.
The four errors are Shared errors. They tell a caller what to do next:
| Error | Meaning |
|---|---|
Blocked | The site refused the request, for example with HTTP 403. |
RateLimited | The site asked to slow down, for example with HTTP 429. |
UpstreamDown | The site failed or could not be reached. |
SchemaDrift | The site answered, but not in the shape the Scraper expects. |
Write the Implementation
The Implementation fulfils the Definition. It has two parts: a function that reads one page, and a Layer that turns the input into a stream of pages.
Example (src/api.ts)
import { Download, Scraper } from "@scwrap/domain";
import { Effect, Option, Schema, Stream } from "effect";
import { Quotes } from "./definition.js";
export const quotesOrigin = "https://quotes.toscrape.com";
const Page = Schema.Struct({
has_next: Schema.Boolean,
quotes: Schema.Array(
Schema.Struct({
author: Schema.Struct({ name: Schema.String }),
tags: Schema.Array(Schema.String),
text: Schema.String,
})
),
});
const readPage = (page: number) =>
Download.json(`${quotesOrigin}/api/quotes?page=${page}`, Page);
export const QuotesApiLive = Scraper.layer(Quotes, ({ maxPages }) =>
Stream.paginate(1, (page) =>
readPage(page).pipe(
Effect.map(({ has_next: hasNext, quotes }) => [
quotes.map(({ author, tags, text }) => ({
author: author.name,
tags,
text,
})),
hasNext && page < maxPages ? Option.some(page + 1) : Option.none(),
])
)
)
);Here is what each piece does.
Page describes the part of the site's answer the Scraper reads. It is separate from Quote: one is what the site sends, the other is what you promise your callers.
Download.json comes from @scwrap/domain. It sends a GET through the HttpClient, retries transient failures twice, and turns HTTP failures into Shared errors: 401 and 403 become Blocked, 429 becomes RateLimited, anything else becomes UpstreamDown. A challenge page also becomes Blocked. Then it decodes the body with Page. A body that does not match fails with SchemaDrift and a sample of the body, so whoever repairs the Scraper sees what the site sent. See HTTP helpers.
Every download is a span in a trace, named Download.json and carrying the URL.
Scraper.layer builds the Implementation from the Definition and one handler. The handler receives the decoded input and returns a Stream of items. A handler that reads everything in one step can return an Effect of all its items instead.
Stream.paginate starts at page 1. Each step emits one page of quotes and says whether there is a next page. Items reach the caller page by page, not all at the end.
Capabilities come from the type
Download.json needs an HttpClient, so readPage and the handler's Stream need one too. Scraper.layer carries every requirement of the handler into the Layer's type, so the client is a requirement of the Layer:
const QuotesApiLive: Layer.Layer<Rpc.Handler<"quotes">, never, HttpClient.HttpClient>The third type parameter is the list of Capabilities. Nothing else declares what the Scraper needs. A test provides a fake client, a script provides fetch, and a Release provides whatever the instance should use.
Undeclared errors do not compile
Every failure in the handler is one of the four errors the Definition lists. If you remove SchemaDrift from the Definition's error union, Scraper.layer stops compiling, because readPage can still fail with it. The compiler keeps the contract and the code in step.
Export it
Example (src/index.ts)
export { Quotes } from "./definition.js";
export { Quote, QuotesQuery } from "./quote.js";
export { QuotesApiLive, quotesOrigin } from "./api.js";Run it
Scraper.run runs a Scraper in process. It is what an instance and every test use.
Build the package, then save this script as packages/quotes/scripts/run.ts:
Example (Running the Scraper against the live site)
import { Scraper } from "@scwrap/domain";
import { Quotes, QuotesApiLive } from "@scwrap/quotes";
import { Console, Effect, Layer, Stream } from "effect";
import { FetchHttpClient } from "effect/http";
await Effect.runPromise(
Scraper.run(Quotes, { maxPages: 2 }).pipe(
Stream.runForEach((quote) => Console.log(quote)),
Effect.provide(QuotesApiLive.pipe(Layer.provide(FetchHttpClient.layer)))
)
);pnpm --filter @scwrap/quotes build
cd packages/quotes
node scripts/run.ts{
author: 'Albert Einstein',
tags: [ 'change', 'deep-thoughts', 'thinking', 'world' ],
text: '“The world as we have created it is a process of our thinking. It cannot be changed without changing our thinking.”'
}
{
author: 'J.K. Rowling',
tags: [ 'abilities', 'choices' ],
text: '“It is our choices, Harry, that show what we truly are, far more than our abilities.”'
}
...Two pages give twenty quotes.
Two provide calls wire it together. Layer.provide(FetchHttpClient.layer) gives the Implementation its Capability. Effect.provide gives the Run its Implementation.
Scraper.run does more than call your handler:
- It checks the input first.
{ maxPages: 0 }fails withInvalidInputand the site is never called. - It checks every item against
Quote. An item that does not match fails the Run withSchemaDrift. - It passes your declared errors through unchanged.
NOTE
The script is a throwaway. Delete it when you are done.
Next
The Scraper works against the live site. Testing your Scraper proves the same behaviour without the network.