1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-11-01 19:07:38 +01:00
unleash.unleash/src/test/fixtures/fake-feature-toggle-client-store.ts
sighphyre e889d8e29c feat: Implement multi token support for client tokens
This adds support for multi project tokens to be created. Backward compatibility is handled at 3 different layers here:

- The API is made backwards compatible though a permissive data type that accepts either a project?: string or projects?: string[] property, validation is done through JOI here, which ensures that projects and project are not set together. In the case of neither, this defaults to the previous default of ALL_PROJECTS
- The service layer method to handle adding tokens has been made tolerant to either of the above case and has been deprecated, a new method supporting only the new structure of using projects has been added
- Existing compatibility for consumers of Unleash as a library should not be affected either, the ApiUser constructor is now tolerant to the the first input and will internally map to the new cleaned structure
2022-04-06 08:11:41 +02:00

72 lines
2.3 KiB
TypeScript

import {
FeatureToggle,
IFeatureToggleClient,
IFeatureToggleQuery,
} from '../../lib/types/model';
import { IFeatureToggleClientStore } from '../../lib/types/stores/feature-toggle-client-store';
export default class FakeFeatureToggleClientStore
implements IFeatureToggleClientStore
{
featureToggles: FeatureToggle[] = [];
async getFeatures(
featureQuery?: IFeatureToggleQuery,
archived: boolean = false,
): Promise<IFeatureToggleClient[]> {
const rows = this.featureToggles.filter((toggle) => {
if (featureQuery.namePrefix) {
if (featureQuery.project) {
return (
toggle.name.startsWith(featureQuery.namePrefix) &&
featureQuery.project.some((project) =>
project.includes(toggle.project),
)
);
}
return toggle.name.startsWith(featureQuery.namePrefix);
}
if (featureQuery.project) {
return featureQuery.project.some((project) =>
project.includes(toggle.project),
);
}
return toggle.archived === archived;
});
const clientRows: IFeatureToggleClient[] = rows.map((t) => ({
...t,
enabled: true,
strategies: [],
description: t.description || '',
type: t.type || 'Release',
stale: t.stale || false,
variants: [],
}));
return Promise.resolve(clientRows);
}
async getClient(
query?: IFeatureToggleQuery,
): Promise<IFeatureToggleClient[]> {
return this.getFeatures(query);
}
async getAdmin(
query?: IFeatureToggleQuery,
archived: boolean = false,
): Promise<IFeatureToggleClient[]> {
return this.getFeatures(query, archived);
}
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
async createFeature(feature: any): Promise<void> {
this.featureToggles.push({
project: feature.project || 'default',
createdAt: new Date(),
archived: false,
...feature,
});
return Promise.resolve();
}
}