Skip to content

Instantly share code, notes, and snippets.

@shubhamp-sf
Created September 13, 2024 07:19
Show Gist options
  • Save shubhamp-sf/6928840fb00e9bcb46ad37dedd1cf0c4 to your computer and use it in GitHub Desktop.
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.
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');
}
}
}
@shubhamp-sf
Copy link
Author

It basically finds overlap of the controllers between the app and the component and unbinds them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment