fastify-request-context
fastify-request-context copied to clipboard
Allow set the defaultStoreValues by factory
Prerequisites
- [X] I have written a descriptive issue title
- [X] I have searched existing issues to ensure the feature has not already been requested
🚀 Feature Proposal
The defaultStoreValue could accept a factory function.
Motivation
This is a similar approach used in different store libs (ie. vuex, pinia).
Currently when passing default values by the store, when the value is an object, it is shared between requests what could be not what we want. The side effects of this can be hard to find when someone is not aware of this and instead of setting the whole data by key mutates the members of object what user got from context (like in #125).
Example
fastify.register(fastifyRequestContextPlugin, {
hook: 'preValidation',
defaultStoreValues: () => ({
user: { id: 'system' }
})
});
would you be open to send a PR for this?
This has been completed, right @kibertoad @mcollina?
@voxpelli yes