import { SPComponentLoader } from "@microsoft/sp-loader" ; import { ServiceScope , ServiceKey , Log } from "@microsoft/sp-core-library" ; import IExtensibilityService from "./IExtensibilityService" ; import { IExtensibilityLibrary } from "@pnp/modern-search-extensibility" ; import { IExtensibilityConfiguration } from "../../models/common/IExtensibilityConfiguration" ; const ExtensibilityService_ServiceKey = "PnPModernSearchExtensibilityService" ; export class ExtensibilityService { private serviceScope : ServiceScope ; public static ServiceKey : ServiceKey < IExtensibilityService > = ServiceKey . create ( ExtensibilityService_ServiceKey , ExtensibilityService ) ; public constructor ( serviceScope ) { this . serviceScope = serviceScope ; } /**
     * Loads the extensibility libraries from the global or site collection app catalog acording to the configuration.
     */ public async loadExtensibilityLibraries ( librairiesConfiguration ) { let extensibilityLibraries = [ ] ; try { const promises = librairiesConfiguration . filter ( configuration => configuration . enabled ) . map ( configuration => { return new Promise < any > ( ( resolve , reject ) => { return SPComponentLoader . loadComponentById ( configuration . id ) . then ( ( extensibilityLibraryComponent ) => { let extensibilityLibrary = undefined ; const libraryMainEntryPoints = Object . keys ( extensibilityLibraryComponent ) . filter ( property => { const extensibilityLibraryPrototype = extensibilityLibraryComponent [ property ] . prototype ; return property . indexOf ( '__' ) === - 1 && ( extensibilityLibraryPrototype . getCustomSuggestionProviders || extensibilityLibraryPrototype . getCustomWebComponents || extensibilityLibraryPrototype . getCustomLayouts || extensibilityLibraryPrototype . registerHandlebarsCustomizations ) ; } ) ; if ( libraryMainEntryPoints . length === 1 ) { extensibilityLibrary = new extensibilityLibraryComponent [ libraryMainEntryPoints [ 0 ] ] ( ) ; } Log . verbose ( ExtensibilityService_ServiceKey , `Extensibility library component with id '${ configuration . id }' and name '${ libraryMainEntryPoints [ 0 ] }' loaded.` , this . serviceScope ) ; resolve ( extensibilityLibrary as IExtensibilityLibrary ) ; } ) . catch ( error => { Log . warn ( ExtensibilityService_ServiceKey , `No extensibility library component found with id '${ configuration . id }'` , this . serviceScope ) ; resolve ( undefined ) ; } ) ; } ) ; } ) ; const responses = await Promise . all ( promises ) ; responses . filter ( response => response ) . forEach ( response => { extensibilityLibraries . push ( response ) ; } ) ; return extensibilityLibraries ; } catch ( error ) { return Promise . resolve ( extensibilityLibraries ) ; } } }