1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-01-11 00:08:30 +01:00

refactor: use requestType instead of isAdmin, optionalIncludes (#4115)

This removes the burden of knowing what optionalIncludes is and also
prevents weird edge cases like isAdmin with optionalIncludes.
This commit is contained in:
Thomas Heartman 2023-07-05 09:32:42 +02:00 committed by GitHub
parent 28cafd6e06
commit 451c67a24b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -16,14 +16,11 @@ import FeatureToggleStore from './feature-toggle-store';
import { Db } from './db'; import { Db } from './db';
import Raw = Knex.Raw; import Raw = Knex.Raw;
type OptionalClientFeatures = Set<'strategy IDs' | 'strategy titles'>;
export interface IGetAllFeatures { export interface IGetAllFeatures {
featureQuery?: IFeatureToggleQuery; featureQuery?: IFeatureToggleQuery;
archived: boolean; archived: boolean;
isAdmin: boolean; requestType: 'client' | 'admin' | 'playground';
userId?: number; userId?: number;
optionalIncludes?: OptionalClientFeatures;
} }
export interface IGetAdminFeatures { export interface IGetAdminFeatures {
@ -54,10 +51,11 @@ export default class FeatureToggleClientStore
private async getAll({ private async getAll({
featureQuery, featureQuery,
archived, archived,
isAdmin, requestType,
userId, userId,
optionalIncludes,
}: IGetAllFeatures): Promise<IFeatureToggleClient[]> { }: IGetAllFeatures): Promise<IFeatureToggleClient[]> {
const isAdmin = requestType === 'admin';
const isPlayground = requestType === 'playground';
const environment = featureQuery?.environment || DEFAULT_ENV; const environment = featureQuery?.environment || DEFAULT_ENV;
const stopTimer = this.timer('getFeatureAdmin'); const stopTimer = this.timer('getFeatureAdmin');
@ -213,15 +211,11 @@ export default class FeatureToggleClientStore
strategies: strategies?.map(({ id, title, ...strategy }) => ({ strategies: strategies?.map(({ id, title, ...strategy }) => ({
...strategy, ...strategy,
...(optionalIncludes?.has('strategy titles') && title ...(isPlayground && title ? { title } : {}),
? { title }
: {}),
// We should not send strategy IDs from the client API, // We should not send strategy IDs from the client API,
// as this breaks old versions of the Go SDK (at least). // as this breaks old versions of the Go SDK (at least).
...(isAdmin || optionalIncludes?.has('strategy IDs') ...(isAdmin || isPlayground ? { id } : {}),
? { id }
: {}),
})), })),
})); }));
@ -309,7 +303,7 @@ export default class FeatureToggleClientStore
return this.getAll({ return this.getAll({
featureQuery, featureQuery,
archived: false, archived: false,
isAdmin: false, requestType: 'client',
}); });
} }
@ -319,8 +313,7 @@ export default class FeatureToggleClientStore
return this.getAll({ return this.getAll({
featureQuery, featureQuery,
archived: false, archived: false,
isAdmin: false, requestType: 'playground',
optionalIncludes: new Set(['strategy titles', 'strategy IDs']),
}); });
} }
@ -332,7 +325,7 @@ export default class FeatureToggleClientStore
return this.getAll({ return this.getAll({
featureQuery, featureQuery,
archived: Boolean(archived), archived: Boolean(archived),
isAdmin: true, requestType: 'admin',
userId, userId,
}); });
} }