kibana/x-pack/plugins/global_search/common/license_checker.ts
Mikhail Shustov 3ffd128e57
Get rid of global types (#81739) (#81884)
* move global typings to packages/kbn-utility-types

* update all imports

* add tests

* mute error

* update docs

* ok

* rename kbn-utility-types/test --> kbn-utility-types/jest
2020-10-28 16:16:27 +03:00

50 lines
1.5 KiB
TypeScript

/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import { Observable, Subscription } from 'rxjs';
import type { PublicMethodsOf } from '@kbn/utility-types';
import { ILicense } from '../../licensing/common/types';
export type LicenseState = { valid: false; message: string } | { valid: true };
export type CheckLicense = (license: ILicense) => LicenseState;
const checkLicense: CheckLicense = (license) => {
const check = license.check('globalSearch', 'basic');
switch (check.state) {
case 'expired':
return { valid: false, message: 'expired' };
case 'invalid':
return { valid: false, message: 'invalid' };
case 'unavailable':
return { valid: false, message: 'unavailable' };
case 'valid':
return { valid: true };
default:
throw new Error(`Invalid license state: ${check.state}`);
}
};
export type ILicenseChecker = PublicMethodsOf<LicenseChecker>;
export class LicenseChecker {
private subscription: Subscription;
private state: LicenseState = { valid: false, message: 'unknown' };
constructor(license$: Observable<ILicense>) {
this.subscription = license$.subscribe((license) => {
this.state = checkLicense(license);
});
}
public getState() {
return this.state;
}
public clean() {
this.subscription.unsubscribe();
}
}