Skip to content

Domain.RequestContext reference

Source: packages/domain/src/RequestContext.ts

A service middleware provides to a request, such as the current actor. It belongs to the request: a command carries it to its aggregate’s decide, and a read carries it to validate. Both may run elsewhere, such as in a Durable Object, so it has a schema. Policies provide it themselves when they send commands.

RequestContext: Declaring a request context

Section titled “RequestContext: Declaring a request context”
import * as Domain from "@alchemy.run/domain";
import * as Schema from "effect/Schema";
export class CurrentActor extends Domain.RequestContext<CurrentActor>()(
"Bank/CurrentActor",
Schema.Struct({ customerId: Schema.String }),
) {}

RequestContext: Providing it from middleware

Section titled “RequestContext: Providing it from middleware”
import * as RpcMiddleware from "effect/rpc/RpcMiddleware";
import * as Schema from "effect/Schema";
import { CurrentActor } from "./CurrentActor.ts";
export class Unauthorized extends Schema.TaggedError<Unauthorized>()("Unauthorized", {}) {}
export class Session extends RpcMiddleware.Service<Session, { provides: CurrentActor }>()(
"Bank/Session",
{ error: Unauthorized },
) {}