feat: Chainable @hooks().params() decorator with hookMixin support#3650
Closed
marshallswain wants to merge 7 commits intov6from
Closed
feat: Chainable @hooks().params() decorator with hookMixin support#3650marshallswain wants to merge 7 commits intov6from
marshallswain wants to merge 7 commits intov6from
Conversation
Adds support for a cleaner chainable syntax when defining hook params:
@hooks([]).params('id', 'data')
async myMethod(id: string, data: any) {}
Instead of the more verbose:
@hooks(middleware([]).params('id', 'data'))
async myMethod(id: string, data: any) {}
The ChainableHookDecorator interface supports:
- .params(...params) - define context param names
- .props(props) - add static context properties
- .defaults(fn) - add default context values
These can be chained in any order and combination.
When a service method is decorated with @hooks([]).params(...), hookMixin
now respects those custom params instead of overriding them with defaults.
This enables the use of custom method signatures:
class MyService {
@(hooks([]).params('userId', 'message'))
async notify(userId: string, message: string) {}
}
When registered with feathers, the notify method will have context.userId
and context.message available instead of the default context.data/params.
Changes:
- hookMixin checks for existing params from decorated methods
- For decorated methods, Feathers context (app, path, service, method)
is added to the existing Context prototype instead of re-wrapping
- Added 5 tests for hookMixin respecting decorator params
Pending integration into main hooks documentation during v6-documentation rewrite.
- Add clone() method to HookManager for creating modified copies - Refactor createChainableDecorator to use clone() instead of manual property copying - Create createFeathersContextProps() helper to define Feathers context props once - Add propsFromDescriptors() helper to convert PropertyDescriptorMap to HookContextData - Tighten decorator types: use AnyFunction instead of Function, specify decorator context types - Simplify hookMixin by using the shared helper functions
- Extract shared statusCodeDescriptor to avoid duplication - Split into createFeathersProps() for HookContextData (standard path) - Split into createFeathersDescriptors() for PropertyDescriptorMap (decorated path) - Remove propsFromDescriptors() conversion function - no longer needed
Use clone().params/props/defaults() instead of directly accessing internal _params/_props/_defaults properties.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds a chainable
@hooks().params()decorator syntax that allows defining custom parameter names for hook context on service methods. When registered as a Feathers service,hookMixinrespects these custom params instead of overriding them with defaults.Usage
Chainable Methods
.params(...names)- Define context parameter names.props(properties)- Add static properties to context.defaults(initializer)- Provide default values via functionAll can be chained:
Changes
packages/feathers/src/hooks/base.ts- Addedclone()method toHookManagerpackages/feathers/src/hooks/hooks.ts- AddedChainableHookDecoratorinterface andcreateChainableDecorator()packages/feathers/src/hooks.ts- ModifiedhookMixin()to detect and respect custom params from decoratorspackages/feathers/src/hooks/decorator.test.ts- Added 11 new testswebsite/content/api/hooks-chainable-params.md- Documentation (pending integration into docs rewrite)Why
Previously, custom service methods were limited to the default hook context params (
data,params,id). If you had a method likenotify(userId, message), hooks would receivectx.dataandctx.paramsinstead ofctx.userIdandctx.message.This change allows:
Based on this comment.