fastify-request-context icon indicating copy to clipboard operation
fastify-request-context copied to clipboard

Allow set the defaultStoreValues by factory

Open gbalcewicz opened this issue 3 years ago • 1 comments

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' } 
  })
});

gbalcewicz avatar Oct 25 '22 09:10 gbalcewicz

would you be open to send a PR for this?

kibertoad avatar Oct 25 '22 09:10 kibertoad

This has been completed, right @kibertoad @mcollina?

voxpelli avatar Jun 10 '23 21:06 voxpelli

@voxpelli yes

kibertoad avatar Jun 10 '23 21:06 kibertoad