samedi 16 février 2019

how to access a class decorator's parameters from a method decorator

I would like to use the information passed to class decorator from a method decorator on the same class. Here is what the (dummy) class looks like:

@classDecorator({
  something: 'very interesting'
})
class MyClass{
  @methodDecorator({
    pure: false
  })
  someMethod() {
    ...
  }
}

Now I'm interested in using the parameter given to the classDecorator ({something: very interesting'}) from within the methodDecorator.

I was hoping I could use the Reflect API, not to no avail:

function classDecorator(info) {
  // classDecorator parameter available here as 'info'

  return function(target, propertyKey, descriptor) {
    return descriptor
  }
}

function methodDecorator(config) {
  // propertyDecorator parameter availabe here as 'config'
  return function(target, propertyKey, descriptor) {

    // I thought I could access the class' decorator params with the reflect api
    Reflect.getMetadata('custom:annotation', target)
    Reflect.getMetadata('design:type', target)
    Reflect.getMetadata('design:paramtypes', target)
    // but all of the above are undefined

    return descriptor
  }

}

Is is possible to access the classDecorator's params from within a methodDecorator on the same class?





Aucun commentaire:

Enregistrer un commentaire