From b1ea2c3b8817a68fadabe0edc6ba2ae651ec26d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gast=C3=B3n=20Fournier?= Date: Tue, 31 Oct 2023 12:38:21 +0100 Subject: [PATCH] chore: expose instanceId so it can be used from addons (#5231) ## About the changes A bit of boy scouting trying to expose the instanceId --- src/lib/db/setting-store.ts | 11 ++++++----- .../instance-stats/instance-stats-service.ts | 4 ++-- src/lib/server-impl.ts | 5 ++--- src/lib/services/setting-service.ts | 12 ++++++++++-- src/lib/services/version-service.ts | 17 ++++++++++++----- src/lib/types/model.ts | 1 - src/lib/types/stores/settings-store.ts | 1 + 7 files changed, 33 insertions(+), 18 deletions(-) diff --git a/src/lib/db/setting-store.ts b/src/lib/db/setting-store.ts index d6e7fc697d..df5db4b7ba 100644 --- a/src/lib/db/setting-store.ts +++ b/src/lib/db/setting-store.ts @@ -14,7 +14,6 @@ export default class SettingStore implements ISettingStore { this.logger = getLogger('settings-store.ts'); } - // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types async updateRow(name: string, content: any): Promise { return this.db(TABLE) .where('name', name) @@ -23,7 +22,6 @@ export default class SettingStore implements ISettingStore { }); } - // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types async insertNewRow(name: string, content: any) { return this.db(TABLE).insert({ name, content }); } @@ -37,8 +35,12 @@ export default class SettingStore implements ISettingStore { return present; } - async get(name: string): Promise { - const result = await this.db.select().from(TABLE).where('name', name); + async get(name: string): Promise { + const result = await this.db + .select() + .from(TABLE) + .where('name', name) + .limit(1); if (result.length > 0) { return result[0].content; @@ -46,7 +48,6 @@ export default class SettingStore implements ISettingStore { return undefined; } - // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types async insert(name: string, content: any): Promise { const exists = await this.exists(name); if (exists) { diff --git a/src/lib/features/instance-stats/instance-stats-service.ts b/src/lib/features/instance-stats/instance-stats-service.ts index 8b6890c776..1411d3bc35 100644 --- a/src/lib/features/instance-stats/instance-stats-service.ts +++ b/src/lib/features/instance-stats/instance-stats-service.ts @@ -171,7 +171,7 @@ export class InstanceStatsService { } async hasOIDC(): Promise { - const settings = await this.settingStore.get( + const settings = await this.settingStore.get<{ enabled: boolean }>( 'unleash.enterprise.auth.oidc', ); @@ -179,7 +179,7 @@ export class InstanceStatsService { } async hasSAML(): Promise { - const settings = await this.settingStore.get( + const settings = await this.settingStore.get<{ enabled: boolean }>( 'unleash.enterprise.auth.saml', ); diff --git a/src/lib/server-impl.ts b/src/lib/server-impl.ts index ae7d3da7d3..b13c83dc96 100644 --- a/src/lib/server-impl.ts +++ b/src/lib/server-impl.ts @@ -63,9 +63,8 @@ async function createApp( }; if (!config.server.secret) { - const secret = await stores.settingStore.get('unleash.secret'); - // eslint-disable-next-line no-param-reassign - config.server.secret = secret; + const secret = await stores.settingStore.get('unleash.secret'); + config.server.secret = secret!; } const app = await getApp(config, stores, services, unleashSession, db); diff --git a/src/lib/services/setting-service.ts b/src/lib/services/setting-service.ts index ba3814bb95..237badc070 100644 --- a/src/lib/services/setting-service.ts +++ b/src/lib/services/setting-service.ts @@ -29,8 +29,16 @@ export default class SettingService { this.eventService = eventService; } - async get(id: string, defaultValue?: T): Promise { - const value = await this.settingStore.get(id); + /** + * @deprecated use getWithDefault instead + */ + async get(id: string, defaultValue?: T): Promise { + const value = await this.settingStore.get(id); + return value || defaultValue; + } + + async getWithDefault(id: string, defaultValue: T): Promise { + const value = await this.settingStore.get(id); return value || defaultValue; } diff --git a/src/lib/services/version-service.ts b/src/lib/services/version-service.ts index dbb9681a25..b542de2dfd 100644 --- a/src/lib/services/version-service.ts +++ b/src/lib/services/version-service.ts @@ -188,19 +188,25 @@ export default class VersionService { } async setup(): Promise { - await this.setInstanceId(); + await this.readInstanceId(); await this.checkLatestVersion(); } - async setInstanceId(): Promise { + private async readInstanceId(): Promise { try { - const { id } = await this.settingStore.get('instanceInfo'); + const { id } = (await this.settingStore.get<{ id: string }>( + 'instanceInfo', + )) ?? { id: undefined }; this.instanceId = id; } catch (err) { this.logger.warn('Could not find instanceInfo'); } } + getInstanceId() { + return this.instanceId; + } + async checkLatestVersion(): Promise { if (this.enabled) { try { @@ -336,7 +342,7 @@ export default class VersionService { } async hasOIDC(): Promise { - const settings = await this.settingStore.get( + const settings = await this.settingStore.get<{ enabled: boolean }>( 'unleash.enterprise.auth.oidc', ); @@ -344,7 +350,7 @@ export default class VersionService { } async hasSAML(): Promise { - const settings = await this.settingStore.get( + const settings = await this.settingStore.get<{ enabled: boolean }>( 'unleash.enterprise.auth.saml', ); @@ -356,6 +362,7 @@ export default class VersionService { current: this.current, latest: this.latest || {}, isLatest: this.isLatest, + // @ts-ignore instance id can be undefined but not on the version. What should we do is still unclear. instanceId: this.instanceId, }; } diff --git a/src/lib/types/model.ts b/src/lib/types/model.ts index 63f9718bbd..4a3c0b1e9b 100644 --- a/src/lib/types/model.ts +++ b/src/lib/types/model.ts @@ -349,7 +349,6 @@ export enum PermissionType { } export enum RoleName { - // eslint-disable-next-line @typescript-eslint/no-shadow ADMIN = 'Admin', EDITOR = 'Editor', VIEWER = 'Viewer', diff --git a/src/lib/types/stores/settings-store.ts b/src/lib/types/stores/settings-store.ts index edb8476edc..d5332175cc 100644 --- a/src/lib/types/stores/settings-store.ts +++ b/src/lib/types/stores/settings-store.ts @@ -8,4 +8,5 @@ export interface ISettingInsert { export interface ISettingStore extends Store { insert(name: string, content: T): Promise; updateRow(name: string, content: any): Promise; + get(name: string): Promise; }