CAITSGet started

Architecture

Clean Architecture in TypeScript, enforced.

Every CAITS app is made of the same parts. Each part has one job, one place, and one kind of test. Learn them on this page, and you can find your way in any CAITS app.

The shape

Two sides, split into contexts.

One project holds the frontend and the backend. Each side is split into contexts: one folder for each part of your business, with the same shape every time.

  • Two sides. The frontend renders the pages, on the server, then in the browser. The backend answers HTTP requests. They share nothing but the HTTP API.
  • Contexts. A context holds one part of the business, such as messages, orders or billing, with its own words and rules. It never imports another context.
  • Thin routes. src/routes maps each address to a view or a controller. No logic, no test: the end-to-end tests go through them.
  • No ../. A context imports its own files with #context/, and shared with #shared/. Move a file, and its imports still work.

my-app

├── src/                
│   ├── backend/        
│   │   └── messages/   a context: domain, use cases, adapters
│   ├── frontend/       
│   │   └── messages/   a context: views, store, use cases
│   └── routes/         
│       ├── _backend/   the API: each handler calls a controller
│       └── _frontend/  the pages: each one places views
├── test/e2e/           the end-to-end tests
├── quality/            the rules, and the setup of each tool
└── AGENTS.md           what your AI agent reads first

Inside a context

Four layers, one direction.

Every import points inward, toward the domain. Your business rules depend on nothing: they test in milliseconds, and outlive your stack.

The layers of a context: the domain at the center, the application around it, then the presentation and the infra. Every dependency points to the center.Domainthe businessApplicationuse casesPresentationInfra
Domain
The business rules: entities, value objects, errors, and the ports the domain needs.
Application
The use cases: one folder per command or query, with its DTOs and its handler.
Presentation
The way in: the HTTP controllers of the backend; the views and the store of the frontend.
Infra
The way out: the adapters that talk to files, databases and APIs.

The backend

A request, part by part.

A request comes in through a controller, runs one use case, and goes back. Here is each part it meets as the example app adds a message: what it does, its files, and its test.

PRESENTATIONAPPLICATIONDOMAININFRARequestInput DTOOutput DTOResponseClientControllerMessageBusHandlerEntitiesRepositoryStorage
  1. presentation

    Controller

    Reads the request into an input DTO, and sends it on the message bus, which takes it to its handler. Turns the answer into a response: 201 with the message, or 422 with the reason.

    File
    command/add-message/add-message.http.controller.ts
    Test
    add-message.http.presentation.test.tsGiven a request: the DTO it sends. Given an answer: the response.
  2. application

    Use case

    The DTOs, plain classes, carry the data in and out. The handler's execute takes the input DTO, asks the domain, saves through a port, and returns the output DTO. It goes in command/ when it changes something, in query/ when it only reads.

    Files
    command/add-message/add-message.dto.tscommand/add-message/add-message.handler.ts
    Test
    add-message.application.test.tsGiven a DTO: the output or the error, and what was saved. The domain runs for real.
  3. domain

    Entity

    Holds the business rules: a message is never empty, and has at most 280 characters. Breaking a rule throws a domain error.

    Files
    domain/entity/Message.tsdomain/error/InvalidMessageError.ts
    Test
    Through the use case tests, which run every rule. A rule that no use case reaches can go.
  4. domain

    Port

    An interface for what the domain needs from the outside world: here, a place to keep the messages. Its contract lists what every adapter must do.

    Files
    domain/port/message-repository/MessageRepository.tsdomain/port/message-repository/MessageRepository.contract.ts
    Test
    The contract is a test, which each adapter runs.
  5. infra

    Adapters

    Do the work behind the port: a JSON file in production, memory in the tests. Each implements the interface that it imports from the port.

    Files
    infra/message-repository/file-message-repository.tsinfra/message-repository/in-memory-message-repository.ts
    Test
    file-message-repository.test.tsRuns the contract of the port. So does the test of the in-memory adapter.
  6. Context

    Bindings

    di.ts gives each port its adapter for production; test.di.ts, its adapter for the tests. CAITS finds the rest by itself: the handlers and the controllers by their files, the DTO of each handler by the type that its execute takes. You register nothing.

    Files
    di.tstest.di.ts
    Test
    src/backend/backend.integration.test.tsRuns the whole backend on the production bindings.

The frontend

An action, around a loop.

The frontend has the same layers, plus events and a store. A view never calls a use case, and a use case never touches the page: events link them, and the store keeps the state.

PRESENTATIONAPPLICATIONDOMAININFRAEventDTOHTTPEventstateUserViewEventBusStoreHandlerMessageBusEntitiesGatewayServer API
  1. presentation

    View

    A Marko template: it shows the state of the store, and publishes an event when the user acts. Its presenter shapes the state for the template. Its loader fetches the data of the first HTML, on the server.

    Files
    presentation/view/message-board/MessageBoard.markopresentation/view/message-board/presentMessageBoard.tspresentation/view/message-board/loadMessageBoard.ts
    Test
    MessageBoard.test.tsFor each action: the DTO the store sends. For each state of the store: the HTML.
  2. application

    Events

    Say what happened: the user asked for something, or a handler answered. Views and handlers never call each other: they publish events, and the store hears them all.

    Files
    event/MessageAdditionRequested.tsevent/MessagesChanged.ts
    Test
    Through the tests of the views and of the use cases.
  3. presentation

    Store

    reduce turns each event into the new state. send turns an event into a DTO for a use case, when one is needed: a new message becomes an AddMessageCommand. MessagesFrontend.ts names what each view gets: the events it may publish, and this store.

    Files
    presentation/store/messagesStore.tspresentation/MessagesFrontend.ts
    Test
    Through the tests of the views, which run the real store.
  4. application

    Use case

    The handler calls the backend through a port, then publishes the answer as an event: MessagesChanged, or MessagesChangeFailed with the reason.

    Files
    command/add-message/add-message.dto.tscommand/add-message/add-message.handler.ts
    Test
    add-message.application.test.tsGiven a DTO: the events it publishes.
  5. Domain and infra

    Port and adapters

    The port to the backend, with its contract, in domain/. Its adapters, in infra/: HTTP in production, a fake in the tests.

    Files
    domain/port/messages-gateway/MessagesGateway.tsinfra/messages-gateway/http-messages-gateway.tsinfra/messages-gateway/fake-messages-gateway.ts
    Test
    http-messages-gateway.test.tsRuns the contract of the port. So does the test of the fake.

The tests

Tests that run the real code.

Each test goes in where the app goes in (a DTO, a request, a click) and checks what comes out. Only the outside world is replaced.

  • Sociable. A use case test runs the real handler and the real domain. No mock of your own code: you can refactor, and the tests still pass.
  • Honest fakes. test.di.ts swaps the outside world for in-memory adapters. They pass the same contract as the real ones, so a green test tells the truth.
  • One context at a time. A test reads only its own context and shared. A change in one context never breaks the tests of another.
  • Then the whole app. The integration tests run the backend on the production bindings. The end-to-end tests run the pages in a real browser, on the production build.

command/add-message/add-message.application.test.ts

test("keeps the text without its spaces", async ({ app }) => {
  await applicationScenario(app)
    .givenTheDto(new AddMessageCommand("  Hello  "))
    .expectTheOutputToBe(new AddMessageOutput("message-1", "Hello"));

  const kept = await app.get(MessageRepository).list();
  expect(kept.map(message => message.text)).toEqual(["Hello"]);
});
And every test must kill its mutants

Between contexts

Contexts talk through events.

A context never imports another one, and never names it. It tells the others through the context shared, which every context of its side can read.

  1. A handler of orders publishes an event of its own: OrderAccepted.
  2. The context map of orders shares it as an event of shared: OrderPlaced.
  3. The context map of billing turns OrderPlaced into its own input, a CreateInvoiceCommand, which its handler runs.

Each context map has its test, context-map.test.ts: what each line gives, for the event it hears. The example app has one context, so it has no context map yet.

src/backend/orders/context-map.ts

export default [
  share(OrderAccepted, event => [
    new OrderPlaced(event.orderId, event.total)
  ])
];

src/backend/billing/context-map.ts

export default [
  when(OrderPlaced, event => [
    new CreateInvoiceCommand(event.orderId, event.total)
  ])
];

The rules

The 77 rules.

npm run check holds every part above to 77 rules, in under a second. When one breaks, it names the file, the line, and the fix.

  • Contexts and sides 8
  • Layers 12
  • The packages of each layer 8
  • The domain 4
  • Ports, adapters, contracts 8
  • Use cases 6
  • The frontend 9
  • Shared and the context maps 8
  • Dependency injection 4
  • The routes 6
  • Production and tests 4

The rules are code, in a file of your project: remove one, change one, or add your own.

Read every rule

See it hold.

Create the example app, break a rule, and run npm run check.

npx @ingenioz-it/caits init my-app --example