How can I detect if a method or property name is already been used in the service provider I want to extend?

How’s it going?

I need to extend the Response service provider to add methods to handle building frecuent responses that will have same structure with different values, without replacing any inbuilt property or method. Even though I can check the docs to find out what names are in use, I want to avoid or make easier to figure out about names matchs that could occur in future because of Adonis upgrades, or something that I’m not able to consider at this moment.

How can I detect in execution time if a method or property name is already been used in the service provider I’m going to extend?

1 Like

Hi @JorgeRamirez!

There are multiple ways and they are all JS specific, not that much Adonis specific.

For example you can use in https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/in

Or hasOwnProperty https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty but this one is not checking for inherited, it’s checking for own properties only

For most stuff can also just make an if statement.
This will run when key exists, but is falsy, that’s why in might be more secure

if (!provider.thing) {
  // No thing in provider
}
1 Like