unleash-server
Version:
Unleash is an enterprise ready feature flag service. It provides different strategies for handling feature flags.
102 lines • 3.05 kB
JavaScript
import NotFoundError from '../../lib/error/notfound-error.js';
export default class FakeStrategiesStore {
constructor() {
this.defaultStrategy = {
name: 'default',
description: 'default strategy',
displayName: 'Default',
editable: false,
parameters: [],
deprecated: false,
};
this.strategies = [this.defaultStrategy];
}
count() {
return Promise.resolve(0);
}
async createStrategy(update) {
let params;
if (typeof update.parameters === 'string' ||
typeof update.parameters === 'number') {
if (update.parameters === '') {
params = [];
}
else {
params = JSON.parse(update.parameters);
}
}
else {
params = update.parameters;
}
this.strategies.push({
editable: true,
deprecated: false,
description: '',
displayName: update.name,
...update,
parameters: params,
});
}
async delete(key) {
this.strategies.splice(this.strategies.findIndex((k) => k.name === key), 1);
}
async deleteAll() {
this.strategies = [this.defaultStrategy];
}
async deleteStrategy({ name }) {
return this.delete(name);
}
async deprecateStrategy({ name }) {
const strategy = await this.get(name);
strategy.deprecated = true;
}
destroy() { }
async exists(key) {
return this.strategies.some((s) => s.name === key && !s.deprecated);
}
async get(key) {
const strat = this.strategies.find((s) => s.name === key);
if (strat) {
return strat;
}
throw new NotFoundError(`Could not find strategy with name: ${key}`);
}
async getAll() {
return this.strategies.filter((s) => !s.deprecated);
}
async getEditableStrategies() {
return this.strategies
.filter((s) => s.editable)
.map((s) => {
if (!s.parameters) {
// eslint-disable-next-line no-param-reassign
s.parameters = [];
}
return s;
});
}
async getStrategy(name) {
const strat = this.get(name);
if (strat) {
return strat;
}
throw new NotFoundError(`Could not find strategy with name: ${name}`);
}
async importStrategy(data) {
return this.createStrategy({ editable: true, ...data });
}
async reactivateStrategy({ name }) {
const strategy = await this.get(name);
strategy.deprecated = false;
await this.delete(name);
this.strategies.push(strategy);
}
async updateStrategy(update) {
await this.delete(update.name);
return this.createStrategy(update);
}
async dropCustomStrategies() {
return this.deleteAll();
}
}
//# sourceMappingURL=fake-strategies-store.js.map