Agent Skills › AjayIrkal23/agentic-mercy-10x › fastify-patterns

fastify-patterns

GitHub

Fastify服务端开发规范,涵盖Schema-first路由、Ajv配置、鉴权与租户隔离、统一错误处理及限流。指导开发者构建类型安全、结构清晰且安全的后端服务。

skills/fastify-patterns/SKILL.md AjayIrkal23/agentic-mercy-10x

Trigger Scenarios

询问Fastify最佳实践 需要定义API Schema或路由结构 配置Ajv验证规则 实现鉴权或错误处理中间件

Install

npx skills add AjayIrkal23/agentic-mercy-10x --skill fastify-patterns -g -y
More Options

Use without installing

npx skills use AjayIrkal23/agentic-mercy-10x@fastify-patterns

指定 Agent (Claude Code)

npx skills add AjayIrkal23/agentic-mercy-10x --skill fastify-patterns -a claude-code -g -y

安装 repo 全部 skill

npx skills add AjayIrkal23/agentic-mercy-10x --all -g -y

预览 repo 内 skill

npx skills add AjayIrkal23/agentic-mercy-10x --list

SKILL.md

Frontmatter
{
    "name": "fastify-patterns",
    "metadata": {
        "schema": 1,
        "category": "backend",
        "surfaces": [
            "backend"
        ],
        "triggers": {
            "intents": [],
            "keywords": [
                "fastify",
                "fastify route",
                "fastify plugin",
                "fastify schema",
                "prehandler",
                "onrequest hook",
                "setErrorHandler",
                "json schema",
                "ajv",
                "coercetypes",
                "rate limit",
                "fastify inject",
                "typed routes"
            ]
        },
        "platforms": [
            "linux",
            "darwin",
            "windows"
        ]
    },
    "description": "Fastify server patterns: schema-first routes, Ajv coercion and removeAdditional defaults, preHandler auth and tenant scoping, one central error handler mapping a typed AppError, rate limiting and route tests with inject().",
    "when_to_use": "Use when adding or changing Fastify routes, route schemas, plugins, hooks, preHandlers, the error handler or rate limits."
}

Fastify patterns

Project contract first: the repo's CLAUDE.md, CODEX.md and server_docs/ win over anything here. site-sync-vista specifics: routes register through typedGet/typedPost/... from server/src/utils/typed-routes, layering is *.routes.ts -> *.schema.ts -> *.controller.ts -> service -> model, Ajv runs with coerceTypes: true (set in server/src/app.ts), errors are AppError(message, statusCode, code) from server/src/utils/errors.ts, and the central handler returns the flat { success:false, message, code }.

Routes are schema-first

  • Every route declares schema.params, schema.querystring, schema.body (as applicable) and schema.response for the success codes. Response schemas both document the contract and strip fields you did not list (fast-json-stringify serialises only declared properties), so a missing property in the schema silently disappears from the response.
  • Reuse the repo's route helper (typed routes) instead of raw fastify.get so types flow from the schema to the handler.
  • Keep handlers thin: validate (schema) -> controller -> service -> reply. No DB calls in routes.

Ajv defaults you must know

Fastify's default Ajv options are coerceTypes: 'array', useDefaults: true, removeAdditional: true, allErrors: false:

  • query and path strings are coerced to the declared type ("10" -> 10), so declare numbers and booleans as such instead of parsing by hand;
  • with additionalProperties: false, unknown body fields are removed, not rejected. Set additionalProperties: false deliberately and do not rely on it as a security check;
  • useDefaults fills default: values before the handler runs. Check the repo's ajv.customOptions in the app factory before assuming the defaults.

Auth, tenancy, hooks

  • Authentication runs in an onRequest or preHandler hook (e.g. verifyJWT) attached per route or per encapsulated plugin. A route without the hook is public: say so in the review if that is not intended.
  • Tenant scope comes from the verified token (req.user), never from a query param or body field. Pass it explicitly into services.
  • Decorate the request in a plugin (fastify.decorateRequest) instead of mutating it in handlers; register plugins with fastify-plugin only when the decoration must escape encapsulation.

Errors

  • Throw the repo's typed error (AppError) from services and controllers; never throw a raw Error or reply with ad hoc shapes.
  • One setErrorHandler maps AppError -> its status and envelope, schema validation errors (error.validation) -> 400, and everything else -> 500 with a generic message. Log the full error server-side; never send stack traces or driver messages.
  • reply.code(n).send(...) once per request; in async handlers either return the payload or send it, not both.

Rate limits and payloads

  • Public and auth endpoints get @fastify/rate-limit (globally or per route via config.rateLimit); keys come from the user id when authenticated, IP otherwise.
  • Set bodyLimit for upload-adjacent routes; stream large files instead of buffering.

Tests without a server

Build the app with the repo's factory and call app.inject({ method, url, headers, payload }) in vitest; no listen(), no port. Cover: schema rejection (400), auth missing (401), tenant isolation (another company's id -> 404/403), and the success shape against the response schema. Close the app in afterAll.

Not this skill

Mongo query and index design (mongoose-patterns), envelope design (api-contract-standards), list endpoint semantics (backend-api-standards).

Version History

  • 2315578 Current 2026-10-18 19:21

Same Skill Collection

skills/agentation-react/SKILL.md
skills/baseline-ui/SKILL.md
skills/brand/SKILL.md
skills/context7-mcp/SKILL.md
skills/create-design-md/SKILL.md
skills/debug-broken-ui/SKILL.md
skills/design-system-compliance/SKILL.md
skills/design-taste-frontend/SKILL.md
skills/design/SKILL.md
skills/docker-compose/SKILL.md
skills/ensure-repo-docs/SKILL.md
skills/expo-react-native/SKILL.md
skills/false-green-tests/SKILL.md
skills/fixing-accessibility/SKILL.md
skills/fixing-metadata/SKILL.md
skills/fixing-motion-performance/SKILL.md
skills/grill-with-docs/SKILL.md
skills/higgsfield-brandkit/SKILL.md
skills/higgsfield-video-explainer/SKILL.md
skills/higgsfield-youtube-thumbnail/SKILL.md
skills/idea-refine/SKILL.md
skills/img2threejs/SKILL.md
skills/improve-ui/SKILL.md
skills/invoke-audit/SKILL.md
skills/invoke-clean/SKILL.md
skills/invoke-debug/SKILL.md
skills/invoke-design/SKILL.md
skills/invoke-docs/SKILL.md
skills/invoke-impl/SKILL.md
skills/invoke-plan/SKILL.md
skills/invoke-refactor/SKILL.md
skills/invoke-review/SKILL.md
skills/invoke-security/SKILL.md
skills/invoke-session/SKILL.md
skills/invoke-spec/SKILL.md
skills/invoke-status/SKILL.md
skills/invoke-test/SKILL.md
skills/invoke-update/SKILL.md
skills/invoke-verify/SKILL.md
skills/invoke/SKILL.md
skills/kokonutui/SKILL.md
skills/mmx-cli/h3-video/SKILL.md
skills/modern-web-guidance/SKILL.md
skills/mongoose-patterns/SKILL.md
skills/no-ai-slop/SKILL.md
skills/slides/SKILL.md
skills/test-error-states/SKILL.md
skills/threejs-r3f/SKILL.md
skills/to-issues/SKILL.md

Metadata

Files
0
Version
2315578
Hash
4e97dd7c
Indexed
2026-10-18 19:21

Home - Wiki
Copyright © 2011-2026 iteam. Current version is 2.155.2. UTC+08:00, 2026-10-19 00:44
浙ICP备14020137号-1