structure-a-shared-backend-liblisted
Install: claude install-skill kennguyen887/agent-foundation
# Structure a shared backend library
A library of cross-cutting infrastructure (`@org/infra-*`) that every backend service imports, so the
fleet is consistent and DRY. This is the *where & how it's packaged*; the framework primitives inside
it are in `write-cross-cutting-code` and `design-an-error-model`. Frontend equivalent:
`structure-a-shared-ui-lib`.
## When to use
You're starting or reorganizing the shared lib behind a fleet of services, deciding which package a
new primitive goes in, or pulling duplicated infra out of services into one place.
## 1. Split into focused packages by dependency weight
- **Several small packages, not one mega-package** — each owns one concern and pulls only the deps it
needs, so a service that wants typed errors doesn't drag in the whole AWS/cache stack.
- **Order packages from zero-dep core → heavier**, and let the light ones be depended on by the heavy
ones (never the reverse), so there are **no cycles**:
- `infra-exception` / `infra-types` — **zero framework deps**; the error model + shared types. Anything
can import it.
- `infra-auth` — guards/decorators; depends on the error package (peer), nothing heavier.
- `infra-cqrs` — base command/query/event classes; orthogonal, used by event-driven services.
- `infra-common` — the workhorse (pipes, interceptors, middleware, DTOs, base entity, utils, cache,
messaging clients). Highest reuse; may depend on the lighter packages.
```
@org/infra-exception (0 deps) ←─ @org/