fastify / fastify-request-context

Request-scoped storage support, based on Asynchronous Local Storage (with fallback to cls-hooked)
MIT License
153 stars 13 forks source link

Enable default context items derived from `request` #156

Closed voxpelli closed 1 year ago

voxpelli commented 1 year ago

Prerequisites

🚀 Feature Proposal

In #136 / #137 defaultStoreValues was allowed to be a factory, but no parameters was actually sent to it.

I would like it if it was sent the request instance as an argument.

Motivation

Sending request as an argument to the defaultStoreValues factory allows one to easily bootstrap the request context with values derived from the request, which is something I have been doing for eg a logging function.

I will do a PR.

Example

fastify.register(fastifyRequestContextPlugin, { 
  hook: 'preValidation',
  defaultStoreValues: () => ({
    log: request.log.child({ foo: 'bar }),
  }),
});