Created
September 13, 2024 07:19
-
-
Save shubhamp-sf/6928840fb00e9bcb46ad37dedd1cf0c4 to your computer and use it in GitHub Desktop.
Unbind Controllers Inherited from an external loopback 4 component in your lb4 application.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {AuthenticationServiceComponent} from '@sourceloop/authentication-service'; | |
export class Loopback4Application extends BootMixin( | |
ServiceMixin(RepositoryMixin(RestApplication)), | |
) { | |
constructor(options: ApplicationConfig = {}) { | |
super(options); | |
// ... | |
this.component(AuthenticationServiceComponent); | |
this.unbindControllers(AuthenticationServiceComponent); | |
} | |
private unbindControllers(componentClass: Constructor<Component>) { | |
const boundControllers = this.find('controllers.*').map( | |
binding => binding.key, | |
); | |
console.debug('All controllers bound to the app', boundControllers); | |
const componentKey = `components.${componentClass.name}`; | |
const componentInstance = this.getSync<Component>(componentKey); | |
if (componentInstance.controllers) { | |
const componentControllerNames = componentInstance.controllers.map( | |
e => e.name, | |
); | |
console.log( | |
'Controllers names available in the component', | |
componentControllerNames, | |
); | |
boundControllers.forEach(bindingKey => { | |
if (componentControllerNames.includes(bindingKey.split('.')[1])) { | |
this.unbind(bindingKey); | |
console.log(`Unbound ${bindingKey}`); | |
} | |
}); | |
} else { | |
console.log('No controllers found in the component to unbind'); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It basically finds overlap of the controllers between the app and the component and unbinds them.