Trying to figure out providers and decorators in LoopBack 4 has been a bit of a challenge for me.
- What is the primary role of a provider?
- Is it limited to just the sequence or can it be utilized elsewhere?
- Are there specific guidelines that need to be followed?
- What is the recommended approach for using it with a decorator?
The code snippets I have currently implemented are as follows:
export interface MyProviderFn {
(args: any[]): Promise<void>;
}
export class MyActionProvider implements Provider<MyProviderFn> {
public constructor(
@inject(CoreBindings.APPLICATION_INSTANCE)
public app: RestApplication,
@inject.getter(CoreBindings.CONTROLLER_CLASS, { optional: true })
private readonly getController: Getter<Constructor<{}>>,
@inject.getter(CoreBindings.CONTROLLER_METHOD_NAME, { optional: true })
private readonly getMethod: Getter<string>,
) {}
public value(): MyProviderFn {
return args => this.action(args);
}
public async action(args: any[]): Promise<void> {
const controllerClass = await this.getController();
const methodName = await this.getMethod();
if (!controllerClass || !methodName) return;
const metadata = getDecoratorMetadata(controllerClass, methodName);
if (!metadata) {
return;
}
// Provider specific code here
}
}
Is this the correct way to go about it? Is there a cleaner method?