Composable for accessing event scoped data?
Given following server middleware:
and an api handler calling a composable
useSomeCustomComposable()
, is there a way to access the currentUser
-object in that composable without passing it through function arguments?
Above implementation was an experiment. Happy to change that.
Background: That custom composable is used in many routes. For the sake of code dedup, I'd prefer to not extend all calls with something that never changes. I'm propably just blind and it's an easy thing to do, but neither my brain, nor chatgpt were really helpful with that so far.4 Replies
you can use nitro's experimental async context feature to handle this https://nitro.unjs.io/guide/utils#async-context-experimental
Nitro
Server Utils · Nitro
Create web servers with everything you need and deploy them wherever you prefer.
else you could try using async local storage from node https://docs.adonisjs.com/guides/async-local-storage
I haven't seen anyone use it in production yet. I believe it is stable, but I would advise doing some googling to make sure.
amazing!