← ClaudeAtlas

write-cross-cutting-codelisted

Use when writing a reusable request-pipeline primitive rather than feature logic — a custom decorator (param/metadata/method-wrapper + applyDecorators bundles), a guard (metadata-driven role/scope, or an OR-composition "any of these guards" guard), a pipe (polymorphic/variant DTO validation, or wrapping a built-in pipe to throw your typed error), an interceptor (metadata-driven audit/logging that fires after the response), middleware (correlation id), or a custom validation constraint (sync or async-with-DI via class-validator). NestJS reference, framework-flexible.
kennguyen887/agent-foundation · ★ 1 · Web & Frontend · score 77
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/