mobx-keystone icon indicating copy to clipboard operation
mobx-keystone copied to clipboard

Custom action metadata via decorators

Open exception-producer opened this issue 7 months ago • 2 comments

Hi! First of all thank you for this amazing library, it really is a joy to work with.

We are currently running into a wall when trying to augment the model actions with additional metadata, for example permissions required to execute it. We initially tried simply attaching the metadata to the descriptor, but since mobx-keystone converts these properties to wrappedAction objects internally, this data is lost. We sadly were not able to work around this when experimenting. Since we also do not have access to the model type when setting the decorator, using an external mapping also failed.

We feel like being able to get additional action metadata during runtime would bring a great benefit to this library. All while keeping the clean structure that mobx-keystone provides by also leveraging decorators:

@modelAction
@requirePermissions(['edit'])
setFoo(bar) {
    ...
}

Is there something we may have missed on how to implement this behavior? Thanks!

exception-producer avatar Apr 11 '25 12:04 exception-producer

As an option - when requirePermissions is called, you can store in global storage it's value with key equal class name + function name.

quolpr avatar Apr 12 '25 11:04 quolpr

Hey @quolpr , thanks for your help! This is definitely a good workaround that wouldn't require any changes to the library. I wasn't able to verify yet, but I feel like there are going to be two caveats:

  • Models of a project cannot have the same class name
  • To work with extended models, figuring out which model an action is from originally would be required

These could easily be solved if we had access to the model id though, since it already puts a "unique" contraint on all models. Do you by any chance know of a way that allows us to get the model id during the initial decorator call?

exception-producer avatar Apr 13 '25 18:04 exception-producer