import Boom from '@hapi/boom' ; import { map } from 'rxjs/operators' ; import { Observable , Subscription } from 'rxjs' ; import { LicensingPluginStart } from '../../../licensing/server' ; import { ILicense } from '../../../licensing/common/types' ; import { checkLicense , GraphLicenseInformation } from '../../common/check_license' ; export class LicenseState { private licenseInformation : GraphLicenseInformation = checkLicense ( undefined ) ; private subscription : Subscription | null = null ; private observable : Observable < GraphLicenseInformation > | null = null ; private _notifyUsage : LicensingPluginStart [ 'featureUsage' ] [ 'notifyUsage' ] | null = null ; private updateInformation ( licenseInformation ) { this . licenseInformation = licenseInformation ; } public start ( license$ ) { this . observable = license$ . pipe ( map ( checkLicense ) ) ; this . subscription = this . observable . subscribe ( this . updateInformation . bind ( this ) ) ; } public setNotifyUsage ( notifyUsage ) { this . _notifyUsage = notifyUsage ; } public notifyUsage ( featureName ) { if ( this . _notifyUsage ) { this . _notifyUsage ( featureName ) ; } } public stop ( ) { if ( this . subscription ) { this . subscription . unsubscribe ( ) ; } } public getLicenseInformation ( ) { return this . licenseInformation ; } public getLicenseInformation$ ( ) { return this . observable ; } } export function verifyApiAccess ( licenseState ) { const licenseCheckResults = licenseState . getLicenseInformation ( ) ; if ( licenseCheckResults . showAppLink && licenseCheckResults . enableAppLink ) { return ; } throw Boom . forbidden ( licenseCheckResults . message ) ; }