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,ordersorbilling, with its own words and rules. It never imports another context. - Thin routes.
src/routesmaps 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/, andsharedwith#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.
- 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.
- 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.
- application
Use case
The DTOs, plain classes, carry the data in and out. The handler's
executetakes the input DTO, asks the domain, saves through a port, and returns the output DTO. It goes incommand/when it changes something, inquery/when it only reads.- Files
command/add-message/ add-message.dto.ts command/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.
- 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.ts domain/error/ InvalidMessageError.ts - Test
- Through the use case tests, which run every rule. A rule that no use case reaches can go.
- 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.ts domain/port/ message-repository/ MessageRepository.contract.ts - Test
- The contract is a test, which each adapter runs.
- 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.ts infra/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.
- Context
Bindings
di.tsgives 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 itsexecutetakes. You register nothing.- Files
di.tstest.di.ts- Test
src/Runs the whole backend on the production bindings.backend/ backend.integration.test.ts
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.
- 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.marko presentation/view/ message-board/ presentMessageBoard.ts presentation/view/ message-board/ loadMessageBoard.ts - Test
MessageBoard.test.tsFor each action: the DTO the store sends. For each state of the store: the HTML.
- 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.ts event/MessagesChanged.ts - Test
- Through the tests of the views and of the use cases.
- presentation
Store
reduceturns each event into the new state.sendturns an event into a DTO for a use case, when one is needed: a new message becomes anAddMessageCommand.MessagesFrontend.tsnames what each view gets: the events it may publish, and this store.- Files
presentation/store/ messagesStore.ts presentation/MessagesFrontend.ts - Test
- Through the tests of the views, which run the real store.
- application
Use case
The handler calls the backend through a port, then publishes the answer as an event:
MessagesChanged, orMessagesChangeFailedwith the reason.- Files
command/add-message/ add-message.dto.ts command/add-message/ add-message.handler.ts - Test
add-message.application.test.tsGiven a DTO: the events it publishes.
- Domain and infra
Port and adapters
The port to the backend, with its contract, in
domain/. Its adapters, ininfra/: HTTP in production, a fake in the tests.- Files
domain/port/ messages-gateway/ MessagesGateway.ts infra/messages-gateway/ http-messages-gateway.ts infra/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.tsswaps 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"]);
});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.
- A handler of
orderspublishes an event of its own:OrderAccepted. - The context map of
ordersshares it as an event ofshared:OrderPlaced. - The context map of
billingturnsOrderPlacedinto its own input, aCreateInvoiceCommand, 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 ruleSee it hold.
Create the example app, break a rule, and run npm run check.
npx @ingenioz-it/caits init my-app --example