Skip to content

refactor(lib): organizational changes to the package structure - #312

Closed
nhankyjangchan wants to merge 1 commit into
fastify:mainfrom
nhankyjangchan:refactor/code-cleanup
Closed

refactor(lib): organizational changes to the package structure#312
nhankyjangchan wants to merge 1 commit into
fastify:mainfrom
nhankyjangchan:refactor/code-cleanup

Conversation

@nhankyjangchan

Copy link
Copy Markdown

Work performed

  • Extract Symbol.for('skip-override'), Symbol.for('fastify.display-name') and Symbol.for('plugin-meta') into lib/symbols.js, and re-export them from the entry point as fastifyPlugin.symbols.
  • Move metadata normalization and validation into lib/prepareMetadata.js.
  • Improve readability of lib/getPluginName.js and lib/toCamelCase.js.
  • Add JSDoc annotations to all functions.
  • Add FastifyPluginFunction type alias that covers both async and callback plugin signatures.
  • Update error messages to include the actual type of the offending value (e.g. null, array, string) instead of a generic object message.
  • Update tests to cover the new error messages and the extracted modules.
  • Rename the second argument of plugin(fn, ...) from options to metadata, which better describes what it actually is: plugin metadata (name, fastify version, encapsulate), not generic options.

For what?

  • Expose metadata symbols. kSkipOverride, kDisplayName and kPluginMeta are currently private to index.js, so consumers have to write Symbol.for('plugin-meta') and hope the string is correct — Symbol.for never throws on a typo. Exporting them as fastifyPlugin.symbols turns that string into an API.
  • Separate validation from plugin wiring. Metadata normalization and validation live in index.js next to naming, symbols and camelCase conversion. Extracting them into lib/prepareMetadata.js makes the rules testable in isolation and shrinks plugin() to what it actually does.
  • Make error messages actionable. 'The options object should be an object' does not say what the caller passed. The new message reports the actual type (null, array, string, …), which is what you need when a plugin fails to register.
  • Name the argument for what it holds. The second argument of plugin(fn, ...) is not generic options — it is plugin metadata (name, fastify, encapsulate). metadata matches both the content and the existing plugin-meta symbol.
  • Improve developer experience. JSDoc on all exported functions and a FastifyPluginFunction alias covering async and callback signatures make the public API self-describing.

Verified before pull request

  • npm run lint:fix && npm run lint && npm run test passes
  • All tests pass
  • Code follows the project's formatting rules
  • No breaking API changes; only the invalid-metadata error text changed

- extracting symbols into lib/symbols.js
- refactoring to improve the readability of lib/getPluginName.js
- extracting metadata normalization and validation logic into lib/prepareMetadata.js
- adding JSDoc annotations above package functions
- added FastifyPluginFunction type
- added new section of test to verify correct metadata normalization

Signed-off-by: nhankyjangchan <dev.schuchkin.timur@gmail.com>

@Fdawgs Fdawgs left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whilst i'm sure some of this is reasonable, squashing it all into an AI-generated commit and PR isn't.

@Fdawgs Fdawgs closed this Sep 11, 2026
@nhankyjangchan

Copy link
Copy Markdown
Author

Whilst i'm sure some of this is reasonable, squashing it all into an AI-generated commit and PR isn't.

I only used AI to draft the PR text because I didn't want to embarrass myself with my English skills. I’m typing this message using a translator right now, so I hope it’s reasonably readable.

Does the code I submitted also look like "AI slop"? If you’re willing and able, could you point out my mistakes? I didn't use AI to write the code, but the fact that you thought I did suggests the quality is quite poor. I’d really appreciate any criticism—thank you!

(As I mentioned earlier, this text was written using a translator. I really hope this doesn't lead to any awkward misunderstandings.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants