---
name: "fastify-plugin-encapsulation"
description: "Architect modular Fastify applications using plugin encapsulation and dependency decorators."
metadata:
  author: "Fastify"
  version: "1.0.0"
  runtime_compatibility:
    - "codex"
    - "claude-code"
    - "cursor"
    - "windsurf"
triggers:
  - "fastify plugin encapsulation"
  - "use fastify-plugin fp"
  - "decorate fastify instance"
permissions:
  fs_read: true
  fs_write: "workspace"
  shell_exec: false
  net_access: false
---

# Fastify Plugin Encapsulation Architecture (fastify-plugin)

Implements Fastify encapsulation rules: creating encapsulated feature scopes, sharing database connections via fastify-plugin (fp), decorating request and server instances (fastify.decorate), and lifecycle hooks.

## When to Invoke
Invoke this skill whenever you need to:
- fastify plugin encapsulation
- use fastify-plugin fp
- decorate fastify instance

## Execution SOP & Rules
1. Wrap shared utility plugins with fastify-plugin: export default fp(async (fastify, opts) => { ... }).
2. Decorate server instance safely: fastify.decorate('db', dbClient).
3. Register plugins with prefixes: fastify.register(authRoutes, { prefix: '/api/auth' }).
4. Understand that un-wrapped plugins retain encapsulation and do not leak hooks to parent scopes.

## Verification Checklist
- Run local linting, syntax validation, and type-checks before reporting completion.
- Ensure all created or modified files adhere to workspace boundaries and avoid uncommitted secrets.
- Verify tests or build commands exit cleanly with return code 0.
