Fastify Snippets for VS Code
[
](https://github.com/sumitYewale/fastify-snippets/blob/HEAD/ https:/marketplace.visualstudio.com/items?itemName=sumit-fastify-snippets.fastify-snippet)
A collection of essential code snippets to supercharge your Fastify development in Visual Studio Code. Write less, code more!
✨ Features
This extension provides a comprehensive set of snippets for common Fastify patterns, allowing you to quickly scaffold:
- Fastify Server Instances: Get a basic server up and running instantly.
- HTTP Routes: Easily create
GET
, POST
, PUT
, DELETE
routes and more.
- Plugins: Boilerplate for creating and registering Fastify plugins.
- Hooks: Quickly add
onRequest
, preHandler
, onSend
and other lifecycle hooks.
- Decorators: Speed up the addition of custom properties/methods to
FastifyInstance
, FastifyRequest
, or FastifyReply
.
- Accessing Decorated Functions: Quickly call custom functions added to the
fastify
instance, request
, or reply
objects.
- Error Handling: Common patterns for custom error handlers.
- Reply Utilities: Snippets for common
reply
methods like send
, status
, redirect
.
- JSON Schemas: Basic structures for defining validation schemas.
🚀 Installation
- Open Visual Studio Code.
- Go to Extensions (Ctrl+Shift+X or Cmd+Shift+X).
- Search for "Fastify Snippets".
- Click "Install".
Alternatively, install directly from the VS Code Marketplace.
💡 Usage
Start typing any of the prefixes below in a JavaScript file (.js
) or a JavaScript-enabled region (e.g., inside <script>
tags in an HTML file). VS Code's IntelliSense will suggest the snippet. Press Tab
to expand it.
Many snippets include tab stops ($1
, $2
, etc.) and placeholder values to guide you. Press Tab
repeatedly to jump between these points and fill in your custom details. Some even offer choices (e.g., get|post|put
) that you can select from.
Snippet Examples
| Prefix | Description | Example Output |
| :-------------------------- | :------------------------------------------------------------------------------ | :--------------------------------------------------------------------------------------------------------- |
| fserver
| Basic Fastify server instance with a root route and logger. | js\nconst fastify = require('fastify')({ logger: true })\n\nfastify.get('/', async (request, reply) => {\n return { hello: 'world' }\n})\n\n// ... server start logic\n
|
| froute
| Generic Fastify route handler (choose method). | js\nfastify.get('/path', async (request, reply) => {\n // Your route logic here\n return { status: 'success' }\n})\n
|
| fpost
| Fastify POST route handler with request body. | js\nfastify.post('/path', async (request, reply) => {\n const body = request.body\n // Handle POST request with body\n return { message: 'Data created', data: body }\n})\n
|
| fplugin
| Boilerplate for a Fastify plugin using fastify-plugin
. | js\nconst fp = require('fastify-plugin')\n\nmodule.exports = fp(async function (fastify, opts) {\n // Your plugin logic here\n}, { name: 'my-fastify-plugin' })\n
|
| fplugindecorate
| Boilerplate for a Fastify plugin that adds a decorator to the Fastify instance. | js\nconst fp = require('fastify-plugin')\n\nasync function myPluginName (fastify, opts) {\n fastify.decorate('myDecoratorName', function (arg) { /* ... */ })\n}\n\nmodule.exports = fp(myPluginName, { name: 'my-plugin-name' })\n
|
| fhookonrequest
| Add an onRequest
hook. | js\nfastify.addHook('onRequest', async (request, reply) => {\n fastify.log.info(`Incoming request for ${request.method} ${request.url}`)\n})\n
|
| fdecorate
| Decorate the Fastify instance with a new property or method. | js\nfastify.decorate('myUtility', function () {\n return 'Decorated value'\n})\n
|
| fastify.myDecoratorName
| Access and call a function decorated onto the Fastify instance. | js\nconst result = fastify.myDecoratorName('value')\n
|
| fcallrequestfn
| Access and call a function decorated onto the Fastify Request object. | js\nconst result = request.myRequestDecoratorName()\n
|
| fcallreplyfn
| Access and call a function decorated onto the Fastify Reply object. | js\nreply.myReplyDecoratorName(data)\n
|
| ferrorhandler
| Custom Fastify error handler (with validation example). | js\nfastify.setErrorHandler(async (error, request, reply) => {\n if (error.validation) { /* ... */ } else { /* ... */ }\n})\n
|
| fjsonschemaobject
| Basic JSON Schema object structure. | json\n{\n type: 'object',\n properties: { propertyName: { type: 'string' } },\n required: ['propertyName']\n}\n
|
(Note: The examples above are simplified for readability. Full snippet output includes proper indentation and all defined tab stops.)
🛠 Contribution
Found a bug or have a suggestion for a new snippet? We welcome contributions!
- Fork the repository.
- Create a new branch (
git checkout -b feature/your-feature
).
- Make your changes.
- Commit your changes (
git commit -m 'feat: Add new snippet for X'
).
- Push to the branch (
git push origin feature/your-feature
).
- Open a Pull Request.
❤️ Support
If you find this extension helpful, please consider leaving a rating and review in the [VS Code Marketplace](https://github.com/sumitYewale/fastify-snippets/blob/HEAD/ https:/marketplace.visualstudio.com/items?itemName=sumit-fastify-snippets.fastify-snippet)!