Part 4: Expose an Api
Customers will reach the bank from outside. Give every request a signed-in customer, check ownership on every command and read, and expose the bank through an Api.
Know who is asking
Section titled “Know who is asking”import * as Domain from "@alchemy.run/domain";import * as Schema from "effect/Schema";import { CustomerId } from "./Customer.id.ts";
export class CurrentActor extends Domain.RequestContext<CurrentActor>()( "Bank/CurrentActor", Schema.Struct({ customerId: CustomerId }),) {}CurrentActor is the customer behind a request. It has a schema because it travels with each command to the account that decides it.
Authenticate requests
Section titled “Authenticate requests”import * as Effect from "effect/Effect";import * as Headers from "effect/http/Headers";import * as Layer from "effect/Layer";import * as Option from "effect/Option";import * as RpcMiddleware from "effect/rpc/RpcMiddleware";import * as Schema from "effect/Schema";import { CurrentActor } from "./CurrentActor.context.ts";import { CustomerId } from "./Customer.id.ts";
export class Unauthorized extends Schema.TaggedError<Unauthorized>()("Unauthorized", {}) {}
export class Session extends RpcMiddleware.Service<Session, { provides: CurrentActor }>()( "Bank/Session", { error: Unauthorized },) {}
// Demo only: trusts `Authorization: Bearer <customer id>`. Verify a real session in production.export const SessionLive = Layer.succeed(Session, (effect, { headers }) => Option.match(Headers.get(headers, "authorization"), { onNone: () => Effect.fail(new Unauthorized()), onSome: (value) => value.startsWith("Bearer ") ? Effect.provideService(effect, CurrentActor, { customerId: CustomerId.make(value.slice("Bearer ".length)), }) : Effect.fail(new Unauthorized()), }),);
export const sessionClient = (customerId: string) => RpcMiddleware.layerClient(Session, ({ request, next }) => next({ ...request, headers: Headers.set(request.headers, "authorization", `Bearer ${customerId}`) }), );Session is middleware: it runs before a command or read and provides CurrentActor. SessionLive is the server side, and sessionClient attaches the header from a client. Part 6 replaces the header with a browser cookie.
Refuse someone else’s account
Section titled “Refuse someone else’s account”import * as Domain from "@alchemy.run/domain";
export class NotOwner extends Domain.Rejection("NotOwner") {}NotOwner is the answer for someone else’s account. Deposits and withdrawals both return it, so it gets its own file.
Guard deposits and withdrawals
Section titled “Guard deposits and withdrawals”import { Amount } from "./Amount.ts";import { NotOpen } from "./NotOpen.rejection.ts";import { NotOwner } from "./NotOwner.rejection.ts";import { Session } from "./Session.middleware.ts";
export class MoneyDeposited extends Domain.Event("MoneyDeposited", { // ...}) {}
export class DepositMoney extends Domain.Command("DepositMoney", { middleware: [Session], input: { accountId: AccountId, amount: Amount }, events: [MoneyDeposited], rejects: [NotOpen], rejects: [NotOpen, NotOwner], reply: { balance: Schema.Int },}) {}import { Amount } from "./Amount.ts";import { NotOpen } from "./NotOpen.rejection.ts";import { NotOwner } from "./NotOwner.rejection.ts";import { Session } from "./Session.middleware.ts";
export class MoneyWithdrawn extends Domain.Event("MoneyWithdrawn", { // ...}) {}
export class WithdrawMoney extends Domain.Command("WithdrawMoney", { middleware: [Session], input: { accountId: AccountId, amount: Amount }, events: [MoneyWithdrawn], rejects: [NotOpen, InsufficientFunds], rejects: [NotOpen, NotOwner, InsufficientFunds], reply: { balance: Schema.Int },}) {}Each command lists its middleware, here Session, and every way it can fail. Both change the same way, each in its own file.
Check the owner
Section titled “Check the owner”import * as Domain from "@alchemy.run/domain";import * as Effect from "effect/Effect";import * as Schema from "effect/Schema";import { AccountId } from "./Account.id.ts";import { CurrentActor } from "./CurrentActor.context.ts";import { DepositMoney, MoneyDeposited } from "./DepositMoney.command.ts";import { NotOpen } from "./NotOpen.rejection.ts";import { NotOwner } from "./NotOwner.rejection.ts";import { AccountOpened, AlreadyOpen, OpenAccount } from "./OpenAccount.command.ts";import { InsufficientFunds, MoneyWithdrawn, WithdrawMoney } from "./WithdrawMoney.command.ts";// ...]);type State = typeof State.Type;
// The open account, if the current customer owns it.const owned = Effect.fn(function* (account: State) { if (account._tag !== "Open") return yield* new NotOpen(); const actor = yield* CurrentActor; if (actor.customerId !== account.owner) return yield* new NotOwner(); return account;});// ... decide: { OpenAccount: (account, cmd) => account._tag !== "Unopened" ? new AlreadyOpen() : [new AccountOpened({ owner: cmd.owner })], DepositMoney: (account, cmd) => account._tag !== "Open" ? new NotOpen() : [new MoneyDeposited({ amount: cmd.amount, balanceAfter: account.balance + cmd.amount })], WithdrawMoney: (account, cmd) => account._tag !== "Open" ? new NotOpen() : cmd.amount > account.balance ? new InsufficientFunds({ balance: account.balance, requested: cmd.amount }) : [new MoneyWithdrawn({ amount: cmd.amount, balanceAfter: account.balance - cmd.amount })], DepositMoney: Effect.fn(function* (account, cmd) { const open = yield* owned(account); return [new MoneyDeposited({ amount: cmd.amount, balanceAfter: open.balance + cmd.amount })]; }), WithdrawMoney: Effect.fn(function* (account, cmd) { const open = yield* owned(account); if (cmd.amount > open.balance) { return yield* new InsufficientFunds({ balance: open.balance, requested: cmd.amount }); } return [new MoneyWithdrawn({ amount: cmd.amount, balanceAfter: open.balance - cmd.amount })]; }), }, evolve: {A handler can be an Effect.fn that reads what its command’s middleware provides. owned checks the account is open and belongs to the customer asking. OpenAccount has no middleware: only the policy sends it.
Refuse to register someone else
Section titled “Refuse to register someone else”import * as Schema from "effect/Schema";import { CustomerId } from "./Customer.id.ts";import { Session } from "./Session.middleware.ts";
export class CustomerRegistered extends Domain.Event("CustomerRegistered", { // ...}) {}
export class AlreadyRegistered extends Domain.Rejection("AlreadyRegistered") {}
export class NotYou extends Domain.Rejection("NotYou") {}
export class RegisterCustomer extends Domain.Command("RegisterCustomer", { middleware: [Session], input: { customerId: CustomerId, name: Schema.String }, events: [CustomerRegistered], rejects: [AlreadyRegistered], rejects: [AlreadyRegistered, NotYou],}) {}RegisterCustomer now needs a session, and can refuse with NotYou, which only it returns.
Register only yourself
Section titled “Register only yourself”import * as Domain from "@alchemy.run/domain";import * as Effect from "effect/Effect";import * as Schema from "effect/Schema";import { CurrentActor } from "./CurrentActor.context.ts";import { CustomerId } from "./Customer.id.ts";import { AlreadyRegistered, CustomerRegistered, RegisterCustomer } from "./RegisterCustomer.command.ts";import { AlreadyRegistered, CustomerRegistered, NotYou, RegisterCustomer,} from "./RegisterCustomer.command.ts";
export class Customer extends Domain.Aggregate("Customer", { // ... commands: [RegisterCustomer], keyOf: (cmd) => cmd.customerId, decide: { RegisterCustomer: (customer, cmd) => customer.name !== null ? new AlreadyRegistered() : [new CustomerRegistered({ name: cmd.name })], RegisterCustomer: Effect.fn(function* (customer, cmd, { id }) { const actor = yield* CurrentActor; if (actor.customerId !== id) return yield* new NotYou(); if (customer.name !== null) return yield* new AlreadyRegistered(); return [new CustomerRegistered({ name: cmd.name })]; }), }, evolve: {The handler’s third argument holds the id of the customer being registered, so a customer can register only themselves.
Guard reads of the summary
Section titled “Guard reads of the summary”import * as Domain from "@alchemy.run/domain";import * as Schema from "effect/Schema";import { Account } from "./Account.aggregate.ts";import { NotOwner } from "./NotOwner.rejection.ts";import { Session } from "./Session.middleware.ts";
export class AccountSummary extends Domain.View("AccountSummary", { from: [Account], key: Account, middleware: [Session], rejects: [NotOwner], state: Schema.Struct({ owner: Schema.NullOr(Schema.String), balance: Schema.Int }), initial: { owner: null, balance: 0 }, // ...}) {}A view guards its reads the same way a command does: Session says who is asking, and NotOwner is how a read can be refused.
Let only the owner read the summary
Section titled “Let only the owner read the summary”import * as Domain from "@alchemy.run/domain";import * as Effect from "effect/Effect";import * as Schema from "effect/Schema";import { Account } from "./Account.aggregate.ts";import { CurrentActor } from "./CurrentActor.context.ts";import { NotOwner } from "./NotOwner.rejection.ts";import { Session } from "./Session.middleware.ts";
// ...
export const AccountSummaryLive = AccountSummary.toLayer( Effect.succeed( Effect.fn(function* (_request, summary) { const actor = yield* CurrentActor; if (summary?.owner && summary.owner !== actor.customerId) return yield* new NotOwner(); }), ),);The view’s Layer is its validate: one function, like every other Layer’s handler. It runs on every read, and again on every change while a client watches. A summary that has no owner yet reads as empty, so a new customer can watch for their account to appear.
Provide the summary’s Layer
Section titled “Provide the summary’s Layer”import * as Domain from "@alchemy.run/domain";import * as Layer from "effect/Layer";import { Account } from "./Account.aggregate.ts";import { AccountSummary } from "./AccountSummary.view.ts";import { AccountSummary, AccountSummaryLive } from "./AccountSummary.view.ts";// ...
export const BankHandlers = MainAccountProvisioningLive;export const BankHandlers = Layer.merge(MainAccountProvisioningLive, AccountSummaryLive);A view that declares middleware or rejects needs its Layer, so Bank.layer is a type error until BankHandlers includes it. Every story and Worker that hosts the model gets it from there.
Act as a customer in stories
Section titled “Act as a customer in stories”import { Account } from "../src/Account.aggregate.ts";import { Bank, BankHandlers } from "../src/Bank.model.ts";import { CurrentActor } from "../src/CurrentActor.context.ts";import { CustomerId } from "../src/Customer.id.ts";import { DepositMoney, MoneyDeposited } from "../src/DepositMoney.command.ts";import { NotOpen } from "../src/NotOpen.rejection.ts";// ...
const story = Story.make(Bank, { layer: BankHandlers });const asSam = Story.provide(CurrentActor, { customerId: CustomerId.make("sam") });const account = Account.ref("a-1");
it.effect("opens an account", () => story( asSam, Story.when(new OpenAccount({ accountId: account.id, owner: "sam" })), Story.then(account, new AccountOpened({ owner: "sam" })),// ...it.effect("refuses to open an account twice", () => story( asSam, Story.given(account, new AccountOpened({ owner: "sam" })), Story.when(new OpenAccount({ accountId: account.id, owner: "sam" })),// ...it.effect("deposits into an open account and replies with the balance", () => story( asSam, Story.given(account, new AccountOpened({ owner: "sam" })), Story.when(new DepositMoney({ accountId: account.id, amount: 100 })),// ...
it.effect("refuses a deposit into an account that isn't open", () => story(Story.when(new DepositMoney({ accountId: account.id, amount: 100 })), Story.rejected(new NotOpen())), story( asSam, Story.when(new DepositMoney({ accountId: account.id, amount: 100 })), Story.rejected(new NotOpen()), ),);
it.effect("refuses to withdraw more than the balance", () => story( asSam, Story.given( account,import { AccountSummary } from "../src/AccountSummary.view.ts";import { Bank, BankHandlers } from "../src/Bank.model.ts";import { CurrentActor } from "../src/CurrentActor.context.ts";import { CustomerId } from "../src/Customer.id.ts";import { DepositMoney, MoneyDeposited } from "../src/DepositMoney.command.ts";import { AccountOpened } from "../src/OpenAccount.command.ts";// ...
const story = Story.make(Bank, { layer: BankHandlers });const asSam = Story.provide(CurrentActor, { customerId: CustomerId.make("sam") });const account = Account.ref("a-1");
it.effect("the summary follows the balance", () => story( asSam, Story.given(account, new AccountOpened({ owner: "sam" })), Story.when(new DepositMoney({ accountId: account.id, amount: 100 })),// ...it.effect("the statement has one line per movement", () => story( asSam, Story.given(account, new AccountOpened({ owner: "sam" })), Story.when(new DepositMoney({ accountId: account.id, amount: 100 })),import { AccountSummary } from "../src/AccountSummary.view.ts";import { Bank, BankHandlers } from "../src/Bank.model.ts";import { CurrentActor } from "../src/CurrentActor.context.ts";import { Customer } from "../src/Customer.aggregate.ts";import { CustomerId } from "../src/Customer.id.ts";import { AccountOpened } from "../src/OpenAccount.command.ts";import { CustomerRegistered, RegisterCustomer } from "../src/RegisterCustomer.command.ts";import { CustomerRegistered, NotYou, RegisterCustomer } from "../src/RegisterCustomer.command.ts";
const story = Story.make(Bank, { layer: BankHandlers });const sam = Customer.ref("c-1");const samsAccount = Account.ref("c-1-main");const as = (customerId: string) => Story.provide(CurrentActor, { customerId: CustomerId.make(customerId) });
it.effect("registering opens a main account", () => story( as("c-1"), Story.when(new RegisterCustomer({ customerId: sam.id, name: "sam" })), Story.then(sam, new CustomerRegistered({ name: "sam" })), // ... ),);
it.effect("a customer can only register themselves", () => story(as("c-2"), Story.when(new RegisterCustomer({ customerId: sam.id, name: "sam" })), Story.rejected(new NotYou())),);A story has no middleware, so Story.provide stands in for it, giving the following commands their CurrentActor.
Expose an Api
Section titled “Expose an Api”import * as Domain from "@alchemy.run/domain";import { Bank } from "./Bank.model.ts";
export class BankApi extends Domain.Api("BankApi", { model: Bank, commands: ["RegisterCustomer", "DepositMoney", "WithdrawMoney"], views: ["AccountSummary"],}) {}An Api names its model and lists the commands and views a client may reach. Nothing in it needs a handler: the account decides the commands, and the model hosts the view.
Test the Api
Section titled “Test the Api”import { expect, it } from "@effect/vitest";import * as Domain from "@alchemy.run/domain";import * as Effect from "effect/Effect";import * as Layer from "effect/Layer";import type * as Scope from "effect/Scope";import * as Stream from "effect/Stream";import { AccountId } from "../src/Account.id.ts";import { Bank, BankHandlers } from "../src/Bank.model.ts";import { BankApi } from "../src/BankApi.api.ts";import { CustomerId } from "../src/Customer.id.ts";import { SessionLive, sessionClient } from "../src/Session.middleware.ts";
// The bank in memory, with the same middleware a Worker would serve.const World = SessionLive.pipe( Layer.provideMerge(Bank.layer(BankHandlers).pipe(Layer.provide(Domain.InMemory))),);
const signedInAs = (customerId: string) => BankApi.clientInProcess.pipe(Effect.provide(sessionClient(customerId)));
const inBank = <A, E>(effect: Effect.Effect<A, E, Scope.Scope | Layer.Success<typeof World>>) => effect.pipe(Effect.scoped, Effect.provide(World));
it.live("a customer registers, deposits, and reads only their own balance", () => inBank( Effect.gen(function* () { const sam = yield* signedInAs("c-1"); yield* sam.RegisterCustomer({ customerId: CustomerId.make("c-1"), name: "sam" });
// The policy opens the account shortly after; wait for its summary. const accountId = AccountId.make("c-1-main"); yield* sam.AccountSummary.watch(accountId).pipe( Stream.map(({ state }) => state), Stream.filter((summary) => summary?.owner === "c-1"), Stream.runHead, );
const { reply } = yield* sam.DepositMoney({ accountId, amount: 100 }); expect(reply.balance).toBe(100);
const alex = yield* signedInAs("c-2"); const refused = yield* alex.AccountSummary.get(accountId).pipe(Effect.flip); expect(refused._tag).toBe("NotOwner"); }), ),);clientInProcess calls the Api in the same process, through the real middleware. Each client is signed in as one customer, and someone else’s balance comes back as NotOwner.
bun vitest runSay what holds across the bank
Section titled “Say what holds across the bank”export class Bank extends Domain.Model("Bank", { aggregates: [Account, Customer], views: [AccountSummary, Statement], policies: [MainAccountProvisioning], invariants: { EveryCustomerHasAnAccount: (bank) => bank.Customer.every( (customer) => customer.state.name === null || bank.Account.some( (account) => account.state._tag === "Open" && account.state.owner === customer.id, ), ), },}) {}The account’s rules are about one account. A model’s invariant is given every instance of every aggregate, so it can say what the policy promises: a registered customer has an account. It is false for a moment after a registration, until the policy has opened the account, so it is checked whenever nothing is left to react.
Relate an account to its customer
Section titled “Relate an account to its customer” (account) => account.state._tag === "Open" && account.state.owner === customer.id, ), ), }, relations: { AnAccountBelongsToACustomer: Domain.Relation({ over: { account: Account, customer: Customer }, holds: ({ account, customer }) => account.state._tag !== "Open" || account.state.owner !== customer.id || customer.state.name !== null, }), },}) {}A relation is about a fixed few instances, here one account and one customer. It is claimed of every pair, so it starts by saying which pairs it is about: an open account this customer owns.
Verify the bank
Section titled “Verify the bank”import { it } from "@effect/vitest";import { Story } from "@alchemy.run/domain/Story";import * as Verify from "@alchemy.run/domain/Verify";import * as NodeServices from "@effect/platform-node/NodeServices";import * as Effect from "effect/Effect";import { Account } from "../src/Account.aggregate.ts";import { Bank, BankHandlers } from "../src/Bank.model.ts";import { BankApi } from "../src/BankApi.api.ts";
const story = Story.make(Bank, { layer: BankHandlers });
it.effect("the account keeps its rules", () => Verify.aggregate(Account).pipe(Effect.provide(NodeServices.layer)),);
it.effect("the bank keeps its rules", () => Verify.model(Bank, { apis: [BankApi], story }).pipe(Effect.provide(NodeServices.layer)),);Verify.model proves what it can, then runs every sequence of two commands a client can send through the Api, with the policy reacting, and 200 longer ones picked at random. Given the story runner, it also runs each sequence it finds on the real model.
bun vitest runBank: 12 outcomes, 1 policy
Proved: for every state and every input, after any number of commands ✓ Account.NeverNegative ✓ Account.AWithdrawalNeverAddsMoney
Checked: in every sequence of up to 2 commands through BankApi, with each event delivered twice, each reaction also run behind the others that wait8,452 sequences, 1,335 states ✓ Bank.EveryCustomerHasAnAccount ✓ Bank.AnAccountBelongsToACustomer not proved: OpenAccount { accountId: "", owner: "" } breaks it from { account: { id: "", state: { _tag: "Unopened" } }, customer: { id: "", state: { name: null } } }, states the rules allow No sequence that was run reaches those states. If none can, say so in another relation or an invariant, and this one is proved.
Sampled: 200 longer sequences, of up to 12 commands picked at random (seed 1) ✓ none breaks a rule
Run on the real model: 11 sequences the explorer found and 82 walks of commands picked at random, as stories ✓ the model does what the explorer read in eachThe report says how far each rule holds. The account’s rules are proved. The two rules about the whole bank are checked: no sequence that was run breaks them, with the policy’s events delivered twice and out of order.
The relation is checked and not proved, and the report says why. OpenAccount on its own opens an account for any owner, registered or not. Only the policy sends it, after a registration, so no sequence through the Api gets there. A proof is about one command at a time and can’t use that. A relation is proved when one step writes both sides, which is what a decision does.
Every request now has a customer, every command and read checks it, clients reach the bank through one typed Api, and one test verifies the whole bank.