import { Product } from '../models' ; import { Provider , inject , BindingTemplate , BindingScope , BindingFilter , } from '@loopback/context' ; import { extensionFor , extensionFilter } from '@loopback/core' ; /**
 * Interface for recommendation service
 */ export interface RecommenderService { getProductRecommendations ( userId ) : Promise < Product [ ] > ; } export const RECOMMENDER_SERVICE = 'RecommenderService' ; /**
 * A binding template for recommender service extensions
 */ export function recommender ( protocol ) { const asRecommenderService = binding => { extensionFor ( RECOMMENDER_SERVICE ) ( binding ) ; binding . tag ( { protocol } ) . inScope ( BindingScope . SINGLETON ) ; } ; return asRecommenderService ; } const recommenderFilter = binding => { const protocol = process . env . RECOMMENDER_PROTOCOL ?? 'rest' ; return ( extensionFilter ( RECOMMENDER_SERVICE ) ( binding ) && binding . tagMap . protocol === protocol ) ; } ; /**
 * A facade recommender service that selects RESt or gRPC protocol based on
 * the value of `RECOMMENDER_PROTOCOL` environment variable
 */ export class RecommenderServiceProvider implements Provider < RecommenderService > { constructor ( @ inject ( recommenderFilter ) private recommenderServices ) { } value ( ) { return this . recommenderServices [ 0 ] ; } }