1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-07-26 13:48:33 +02:00

chore: expose instanceId so it can be used from addons (#5231)

## About the changes
A bit of boy scouting trying to expose the instanceId
This commit is contained in:
Gastón Fournier 2023-10-31 12:38:21 +01:00 committed by GitHub
parent 406b2383da
commit b1ea2c3b88
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 33 additions and 18 deletions

View File

@ -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<void> {
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<any> {
const result = await this.db.select().from(TABLE).where('name', name);
async get<T>(name: string): Promise<T | undefined> {
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<void> {
const exists = await this.exists(name);
if (exists) {

View File

@ -171,7 +171,7 @@ export class InstanceStatsService {
}
async hasOIDC(): Promise<boolean> {
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<boolean> {
const settings = await this.settingStore.get(
const settings = await this.settingStore.get<{ enabled: boolean }>(
'unleash.enterprise.auth.saml',
);

View File

@ -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<string>('unleash.secret');
config.server.secret = secret!;
}
const app = await getApp(config, stores, services, unleashSession, db);

View File

@ -29,8 +29,16 @@ export default class SettingService {
this.eventService = eventService;
}
async get<T>(id: string, defaultValue?: T): Promise<T> {
const value = await this.settingStore.get(id);
/**
* @deprecated use getWithDefault instead
*/
async get<T>(id: string, defaultValue?: T): Promise<T | undefined> {
const value = await this.settingStore.get<T>(id);
return value || defaultValue;
}
async getWithDefault<T>(id: string, defaultValue: T): Promise<T> {
const value = await this.settingStore.get<T>(id);
return value || defaultValue;
}

View File

@ -188,19 +188,25 @@ export default class VersionService {
}
async setup(): Promise<void> {
await this.setInstanceId();
await this.readInstanceId();
await this.checkLatestVersion();
}
async setInstanceId(): Promise<void> {
private async readInstanceId(): Promise<void> {
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<void> {
if (this.enabled) {
try {
@ -336,7 +342,7 @@ export default class VersionService {
}
async hasOIDC(): Promise<boolean> {
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<boolean> {
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,
};
}

View File

@ -349,7 +349,6 @@ export enum PermissionType {
}
export enum RoleName {
// eslint-disable-next-line @typescript-eslint/no-shadow
ADMIN = 'Admin',
EDITOR = 'Editor',
VIEWER = 'Viewer',

View File

@ -8,4 +8,5 @@ export interface ISettingInsert {
export interface ISettingStore extends Store<any, string> {
insert<T>(name: string, content: T): Promise<void>;
updateRow(name: string, content: any): Promise<void>;
get<T>(name: string): Promise<T | undefined>;
}