write-cross-cutting-codelisted
Install: claude install-skill kennguyen887/agent-foundation
# Write cross-cutting code
The request-pipeline / aspect layer — decorators, guards, pipes, interceptors, middleware, custom
validators — that lives in the shared lib and keeps auth, validation, logging, audit, and context out
of feature handlers. Examples NestJS/TS, neutral `@org` domain. principle → **▸ Example** → **▸ Other
stacks**. For *feature-body* code see `write-service-code`; for the **error model + exception filter**
see `design-an-error-model`; for where these live see `structure-a-shared-backend-lib`.
## Core principle
**A cross-cutting concern (auth, validation, context extraction, audit, tracing) belongs in ONE
reusable primitive, not copy-pasted into every handler.** Build it once as a decorator/guard/pipe/
interceptor/middleware, unit-test it once, and apply it declaratively. The handler stays pure feature
logic.
## 1. Custom decorators — the three shapes (+ bundling)
- **Param decorator** (`createParamDecorator`) — extract typed context from the request, and validate
it there so handlers receive a ready, trusted object:
```ts
export const CurrentCaller = createParamDecorator((_d, ctx: ExecutionContext): Nullable<Caller> => {
const req = ctx.switchToHttp().getRequest();
return req.headers['x-caller'] ? JSON.parse(req.headers['x-caller']) : null; // typed, parsed once
});
// a header-DTO variant validates the headers as a class and throws your BadRequestError on failure
```
- **Metadata decorator** (`SetMetadata`) — annotate a handler/