CAITSGet started

Architecture rules

npm run lint:architecture checks these 77 rules, in under a second, and npm run check checks them on every run. A rule that breaks gives the file and the line, the rule, what is wrong, and how to fix it. To remove a rule, change one, or add your own, see Change the rules and the tools.

Contexts and sides

sides-hold-contexts

src/backend and src/frontend hold only contexts. The backend also holds its integration tests.

context-content

A context holds its domain, its infra, its use cases in command/ and query/, its di.ts and its context map. A frontend context adds event/ and presentation/.

context-has-di

Each folder of src/backend and src/frontend is a context, with a di.ts. That's how the kernel finds it.

contexts-isolated

A context never imports another context, except shared. Contexts talk through events on the shared bus.

tests-in-one-context

A test reads only its own context and shared. No test crosses contexts: each one is tested alone.

sides-apart

Frontend and backend never import each other. They talk only through the HTTP API.

contexts-without-routes

A context never imports the routes. The routes use the contexts, not the other way around.

imports-by-shortcut

No ../ in src. A context imports its own files with #context/, and shared with #shared/ or #frontend-shared/. An import doesn't change when its file moves.

Layers

Every import points inward, toward the domain. Each rule forbids one pair of layers.

domain-imports-no-application

The domain never imports a use case, nor an event of the frontend. The use cases depend on the domain.

domain-imports-no-presentation

The domain never imports a controller, a view or the store.

domain-imports-no-infra

The domain never imports an adapter. It declares what it needs as a port, and the infra implements it.

application-imports-no-presentation

A use case never imports a controller, a view or the store. The controller sends the use case its DTO; in the frontend, the store and the views hear the use cases through events.

application-imports-no-infra

A use case never imports an adapter. It depends on the port, and di.ts binds the adapter.

presentation-imports-no-infra

A controller, a view or the store never imports an adapter. It goes through a use case, whose handler uses the port.

infra-imports-no-presentation

An adapter never imports a controller, a view or the store.

infra-imports-no-application

In the backend, an adapter never imports a use case. The infra of the frontend may import its application.

di-imports-no-application

di.ts never imports a use case: the kernel finds the handlers and the DTOs by itself.

di-imports-no-presentation

di.ts never imports a controller, a view or the store.

context-map-read-by-kernel

No layer imports a context map: the kernel reads it.

entity-without-port

An entity never calls a port. It holds the business rules; the handlers call the ports.

The packages of each layer

quality/imports.mjs lists the packages that each layer may import. A layer without a list imports any package. See The packages of each layer.

domain-imports-only-listed-packages

The domain imports only the packages that quality/imports.mjs lists for it. By default, the kernel's Result, Token and DomainEvent: no Node API, no package of npm.

application-imports-only-listed-packages

The application imports only the packages that quality/imports.mjs lists for it. By default, those of the domain, and the kernel's EventPublisher.

presentation-imports-only-listed-packages

When quality/imports.mjs gives the presentation a list, it imports only those packages. By default, it has none.

infra-imports-only-listed-packages

When quality/imports.mjs gives the infra a list, it imports only those packages. By default, it has none.

di-imports-only-listed-packages

When quality/imports.mjs gives di.ts a list, it imports only those packages. By default, it has none.

context-map-imports-only-listed-packages

When quality/imports.mjs gives the context map a list, it imports only those packages. By default, it has none.

zod-in-presentation

In a layer without a list, only the presentation, and the infra of the frontend, import zod. It checks the data that comes in; the other layers get typed values.

marko-in-frontend-presentation

In a layer without a list, only the frontend presentation and the routes import Marko.

The domain

domain-shape

The domain holds entities, value objects, errors, domain events and ports. Each kind has its folder and its file name pattern.

domain-file-exports-one-name

A domain file exports one name. A port file exports its interface and its token, under the same name.

entity-is-class

An entity or a value object is an exported class, which holds the rules of its values.

error-extends-error

A domain error is an exported class named …Error that extends Error.

Ports, adapters, contracts

port-folder-named-after-port

A port's folder is the name of its file in kebab-case: NoteRepository.ts lives in port/note-repository/. Its adapters use the same folder name in infra/.

port-has-contract

Each port has a contract, {Port}.contract.ts: what every adapter must do.

contract-next-to-port

A contract lives next to its port, in domain/port/{port}/.

contract-runs-in-adapter-test

At least one adapter test runs each contract. A contract that no test runs checks nothing.

backend-infra-shape

A backend infra holds only adapters and their tests: infra/{port}/{implementation}-{port}.ts.

infra-folder-implements-port

Each folder of infra/ has its port in the folder of the same name in the domain: infra/{port}/ goes with domain/port/{port}/.

adapter-implements-port

An adapter implements the port of its folder: the interface that it imports from domain/port/{port}/, whatever its name.

adapter-test-runs-contract

An adapter's test runs its port's contract. All the adapters of a port behave the same.

Use cases

backend-use-case-shape

A backend use case is one folder, command/{use-case}/ or query/{use-case}/: its DTOs, its handler, its controllers, and their tests. Each test's name says its layer.

frontend-use-case-shape

A frontend use case is one folder too: its DTOs, its handler and its test. No controller: the store sends its DTOs.

use-cases-isolated

A use case imports nothing from another use case. What two use cases share lives in the domain.

handler-answers-output

A backend handler answers with output DTOs: the return type of its execute names only classes of the DTO file of its use case, one or several, such as Promise<AddMessageOutput>.

controller-extends-controller

A controller is an exported class named …Controller that extends a controller of the kernel: Controller for a request, TimerController for a timer, StreamController for a stream.

controllers-read-the-request-of-caits

A controller reads the Request of CAITS, from @ingenioz-it/caits/messaging/Request, which gives the content of the body at each call. It doesn't name the Request of the web, which is global and reads its body only once, and it doesn't build a request.

The frontend

frontend-event-shape

The events of a frontend context live in event/, one file each.

event-file-exports-its-event

An event file exports one name: its event, a class, or the type that joins the events of the context.

event-type-names-context

An event class has readonly type = "{context}.{event}" as const: its context, then the name of its class without the context, in kebab-case. The store reads it.

events-built-with-new

Events are made with new SomeEvent(…), never as object literals. The class gets the type and the data right.

frontend-presentation-shape

A frontend presentation holds its views, its components, its store, and the type of its frontend. One folder per view, one per component.

dtos-sent-by-store

In the frontend, only the store and the application import input DTOs. A view publishes events; the store turns them into DTOs.

views-take-own-frontend

A view or a component takes only the frontend of its context. To reach the other contexts, it publishes an event of its own.

components-import-no-view

A component never imports a view. Like a view, it may publish events and read the store.

views-import-no-view

A view never imports another view. What views share is a component.

Shared and the context maps

context-map-translates-shared

A context map translates between its context and shared, both ways. It imports only events, input DTOs and domain values.

context-map-holds-lines

A context map exports one list of lines, made with the kernel's when and share.

context-map-line-direction

share turns an event of the context into events of shared. when turns an event of shared into what the context reacts to. A line the other way would never run.

context-maps-without-cycle

In the frontend, the maps make no cycle of events. A cycle would never end, and the page would freeze.

shared-events-in-map

Outside shared, only the context map imports the events of shared. Handlers, views and stores name only their own context's events.

shared-hears-no-context

The shared context has no context map: it hears no one. The others hear it.

shared-components-are-tags

A component of the frontend's shared context is a Marko tag, component/{name}/index.marko, found through src/frontend/marko.json.

shared-components-placed-by-tag

A context places a shared component by its tag, <badge>, never by importing its file.

Dependency injection

container-built-by-kernel

Only the kernel makes a container. A context gives bindings; the kernel builds them.

controllers-built-by-kernel

Only the kernel makes a controller, with its dependencies. The AbortController of the platform, which stops a fetch, is not a controller of the app: any code makes it.

di-holds-bindings

A di.ts exports one list of bindings: export default [bind(Port).to(Adapter), …]. It builds nothing.

di-read-by-kernel

No layer imports a di.ts. Only the kernel reads it.

The routes

routes-folders

src/routes holds only _frontend, the pages, and _backend, the HTTP handlers.

frontend-routes-content

The frontend routes hold pages, layouts, and their components and styles. No logic, no test. A sitemap or a feed gets a route folder named after its file, which serves a view.

backend-routes-content

The backend routes hold only +handler.ts files, which hand the request to a controller.

routes-on-their-side

A frontend route imports only the frontend; a backend route, only the backend.

routes-through-presentation

A route imports only the presentation: views for a page, controllers for a handler.

routes-take-no-frontend

A route never calls frontendOf. It places views; each view takes its own frontend.

Production and tests

exceptions-in-quality

The code turns off no check: no eslint-disable, @ts-ignore, v8 ignore or Stryker disable, and no skipped test. An exception lives in quality/.

production-imports-no-test-environment

Production code never imports a test.di.ts.

production-imports-no-test-support

Production code never imports the kernel's test helpers, @ingenioz-it/caits/testing.

production-imports-only-src

Production code imports nothing outside src. It imports the kernel as a package, never by its path.