Skip to main content

如何编写一个好的插件

¥How to write a good plugin

首先,感谢你决定为 Fastify 编写一个插件。Fastify 是一个最小的框架,插件是它的优势,所以谢谢。

¥First, thank you for deciding to write a plugin for Fastify. Fastify is a minimal framework and plugins are its strength, so thank you.

Fastify 的核心原则是性能、低开销以及为用户提供良好的体验。编写插件时,记住这些原则很重要。因此,在本文档中,我们将分析优质插件的特性。

¥The core principles of Fastify are performance, low overhead, and providing a good experience to our users. When writing a plugin, it is important to keep these principles in mind. Therefore, in this document, we will analyze what characterizes a quality plugin.

需要一些灵感吗?你可以在我们的问题跟踪器中使用标签 "插件建议"

¥Need some inspiration? You can use the label "plugin suggestion" in our issue tracker!

代码

¥Code

Fastify 使用不同的技术来优化其代码,其中许多技术都记录在我们的指南中。我们强烈建议你阅读 插件指南,以了解可用于构建插件的所有 API,并学习如何使用它们。

¥Fastify uses different techniques to optimize its code, many of them are documented in our Guides. We highly recommend you read the hitchhiker's guide to plugins to discover all the APIs you can use to build your plugin and learn how to use them.

你有疑问或需要建议吗?我们非常乐意为你提供帮助!只需在我们的 帮助存储库 中打开一个问题。

¥Do you have a question or need some advice? We are more than happy to help you! Just open an issue in our help repository.

将插件提交给我们的 生态系统列表 后,我们将审查你的代码并在必要时帮助你改进它。

¥Once you submit a plugin to our ecosystem list, we will review your code and help you improve it if necessary.

文档

¥Documentation

文档非常重要。如果你的插件没有详细记录,我们将不会接受它进入生态系统列表。缺乏高质量的文档会让人们更难使用你的插件,并可能导致它被闲置。

¥Documentation is extremely important. If your plugin is not well documented we will not accept it to the ecosystem list. Lack of quality documentation makes it more difficult for people to use your plugin, and will likely result in it going unused.

如果你想查看一些有关如何记录插件的好示例,请查看:

¥If you want to see some good examples of how to document a plugin take a look at:

许可

¥License

你可以根据自己的喜好许可你的插件,我们不强制执行任何类型的许可。

¥You can license your plugin as you prefer, we do not enforce any kind of license.

我们更喜欢 MIT 许可证,因为我们认为它允许更多人自由使用代码。有关替代许可证的列表,请参阅 OSI 列表 或 GitHub 的 choosealicense.com

¥We prefer the MIT license because we think it allows more people to use the code freely. For a list of alternative licenses see the OSI list or GitHub's choosealicense.com.

示例

¥Examples

始终将示例文件放入你的存储库中。示例对用户非常有帮助,并提供了一种非常快速的方法来测试你的插件。你的用户将会感激不尽。

¥Always put an example file in your repository. Examples are very helpful for users and give a very fast way to test your plugin. Your users will be grateful.

测试

¥Test

对插件进行彻底测试以验证其正常工作非常重要。

¥It is extremely important that a plugin is thoroughly tested to verify that is working properly.

未经测试的插件将不会被生态系统列表接受。缺乏测试并不能激发信任,也不能保证代码将在其依赖的不同版本中继续工作。

¥A plugin without tests will not be accepted to the ecosystem list. A lack of tests does not inspire trust nor guarantee that the code will continue to work among different versions of its dependencies.

我们不强制执行任何测试库。我们使用 node:test 是因为它提供了开箱即用的并行测试和代码覆盖率,但你可以自行选择喜欢的库。我们强烈建议你阅读 插件测试,以了解如何测试你的插件。

¥We do not enforce any testing library. We use node:test since it offers out-of-the-box parallel testing and code coverage, but it is up to you to choose your library of preference. We highly recommend you read the Plugin Testing to learn about how to test your plugins.

代码检查器

¥Code Linter

这不是强制性的,但我们强烈建议你在插件中使用代码检查器。它将确保一致的代码风格并帮助你避免许多错误。

¥It is not mandatory, but we highly recommend you use a code linter in your plugin. It will ensure a consistent code style and help you to avoid many errors.

我们使用 standard 是因为它无需配置即可工作,并且非常容易集成到测试套件中。

¥We use standard since it works without the need to configure it and is very easy to integrate into a test suite.

持续集成

¥Continuous Integration

这不是强制性的,但如果你将代码作为开源发布,则有助于使用持续集成来确保贡献不会破坏你的插件并表明插件按预期工作。对于开源项目,CircleCIGitHub Actions 都是免费的,并且易于设置。

¥It is not mandatory, but if you release your code as open source, it helps to use Continuous Integration to ensure contributions do not break your plugin and to show that the plugin works as intended. Both CircleCI and GitHub Actions are free for open source projects and easy to set up.

此外,你可以启用 Dependabot 之类的服务,这将帮助你保持依赖的最新状态,并发现 Fastify 的新版本是否存在插件问题。

¥In addition, you can enable services like Dependabot, which will help you keep your dependencies up to date and discover if a new release of Fastify has some issues with your plugin.

开始吧!

¥Let's start!

太棒了,现在你已经知道了如何为 Fastify 编写一个好的插件所需的一切了!当你构建了一个(或多个!)后请告诉我们!我们将把它添加到我们文档的 ecosystem 部分!

¥Awesome, now you know everything you need to know about how to write a good plugin for Fastify! After you have built one (or more!) let us know! We will add it to the ecosystem section of our documentation!

如果你想查看一些现实世界的示例,请查看:

¥If you want to see some real world examples, check out:

  • @fastify/view 模板渲染(ejs、pug、handlebars、marko)插件支持 Fastify。

    ¥@fastify/view Templates rendering (ejs, pug, handlebars, marko) plugin support for Fastify.

  • @fastify/mongodb Fastify MongoDB 连接插件,使用它你可以在服务器的每个部分共享相同的 MongoDB 连接池。

    ¥@fastify/mongodb Fastify MongoDB connection plugin, with this you can share the same MongoDB connection pool in every part of your server.

  • @fastify/multipart Fastify 的多部分支持。

    ¥@fastify/multipart Multipart support for Fastify.

  • @fastify/helmet Fastify 的重要安全标头。

    ¥@fastify/helmet Important security headers for Fastify.