1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-10-18 20:09:08 +02:00
unleash.unleash/src/lib/db/feature-toggle-client-store.ts

247 lines
7.9 KiB
TypeScript
Raw Normal View History

import { Knex } from 'knex';
import metricsHelper from '../util/metrics-helper';
import { DB_TIME } from '../metric-events';
import { Logger, LogProvider } from '../logger';
import {
IFeatureToggleClient,
IFeatureToggleQuery,
IStrategyConfig,
} from '../types/model';
import { IFeatureToggleClientStore } from '../types/stores/feature-toggle-client-store';
import { DEFAULT_ENV } from '../util/constants';
import { PartialDeep } from '../types/partial';
Complete open api schemas for project features controller (#1563) * Completed OpenAPI Schemas for ProjectFeatures Controller Completed OpenAPI Schemas for Feature Controller (tags) * Completed OpenAPI Schemas for ProjectFeatures Controller Completed OpenAPI Schemas for Feature Controller (tags) * bug fix * bug fix * fix merge conflicts, some refactoring * fix merge conflicts, some refactoring * fix merge conflicts, some refactoring * added emptyResponse, patch feature operation schemas and request * added emptyResponse, patch feature operation schemas and request * patch strategy * patch strategy * update strategy * update strategy * fix pr comment * fix pr comments * improvements * added operationId to schema for better generation * fix pr comment * fix pr comment * fix pr comment * improvements to generated and dynamic types * improvements to generated and dynamic types * improvements to generated and dynamic types * Update response types to use inferred types * Update addTag response status to 201 * refactor: move schema ref destructuring into createSchemaObject * made serialize date handle deep objects * made serialize date handle deep objects * add `name` to IFeatureStrategy nad fix tests * fix pr comments * fix pr comments * Add types to IAuthRequest * Sync StrategySchema for FE and BE - into the rabbit hole * Sync model with OAS spec * Completed OpenAPI Schemas for ProjectFeatures Controller Completed OpenAPI Schemas for Feature Controller (tags) * Completed OpenAPI Schemas for ProjectFeatures Controller Completed OpenAPI Schemas for Feature Controller (tags) * bug fix * bug fix * fix merge conflicts, some refactoring * fix merge conflicts, some refactoring * fix merge conflicts, some refactoring * added emptyResponse, patch feature operation schemas and request * added emptyResponse, patch feature operation schemas and request * patch strategy * patch strategy * update strategy * update strategy * fix pr comment * fix pr comments * improvements * added operationId to schema for better generation * fix pr comment * fix pr comment * fix pr comment * improvements to generated and dynamic types * improvements to generated and dynamic types * improvements to generated and dynamic types * Update response types to use inferred types * Update addTag response status to 201 * refactor: move schema ref destructuring into createSchemaObject * made serialize date handle deep objects * made serialize date handle deep objects * add `name` to IFeatureStrategy nad fix tests * fix pr comments * fix pr comments * Add types to IAuthRequest * Sync StrategySchema for FE and BE - into the rabbit hole * Sync model with OAS spec * Completed OpenAPI Schemas for ProjectFeatures Controller Completed OpenAPI Schemas for Feature Controller (tags) * Completed OpenAPI Schemas for ProjectFeatures Controller Completed OpenAPI Schemas for Feature Controller (tags) * bug fix * bug fix * fix merge conflicts, some refactoring * fix merge conflicts, some refactoring * fix merge conflicts, some refactoring * added emptyResponse, patch feature operation schemas and request * added emptyResponse, patch feature operation schemas and request * patch strategy * patch strategy * update strategy * update strategy * fix pr comment * fix pr comments * improvements * added operationId to schema for better generation * fix pr comment * fix pr comment * fix pr comment * improvements to generated and dynamic types * improvements to generated and dynamic types * improvements to generated and dynamic types * Update response types to use inferred types * Update addTag response status to 201 * refactor: move schema ref destructuring into createSchemaObject * made serialize date handle deep objects * made serialize date handle deep objects * add `name` to IFeatureStrategy nad fix tests * fix pr comments * fix pr comments * Add types to IAuthRequest * Sync StrategySchema for FE and BE - into the rabbit hole * Sync model with OAS spec * Completed OpenAPI Schemas for ProjectFeatures Controller Completed OpenAPI Schemas for Feature Controller (tags) * Completed OpenAPI Schemas for ProjectFeatures Controller Completed OpenAPI Schemas for Feature Controller (tags) * bug fix * bug fix * fix merge conflicts, some refactoring * fix merge conflicts, some refactoring * fix merge conflicts, some refactoring * added emptyResponse, patch feature operation schemas and request * added emptyResponse, patch feature operation schemas and request * patch strategy * patch strategy * update strategy * update strategy * fix pr comment * fix pr comments * improvements * added operationId to schema for better generation * fix pr comment * fix pr comment * fix pr comment * improvements to generated and dynamic types * improvements to generated and dynamic types * improvements to generated and dynamic types * Update response types to use inferred types * Update addTag response status to 201 * refactor: move schema ref destructuring into createSchemaObject * made serialize date handle deep objects * made serialize date handle deep objects * add `name` to IFeatureStrategy nad fix tests * fix pr comments * fix pr comments * Add types to IAuthRequest * Sync StrategySchema for FE and BE - into the rabbit hole * Sync model with OAS spec * revert * revert * revert * revert * revert * mapper * revert * revert * revert * remove serialize-dates.ts * remove serialize-dates.ts * remove serialize-dates.ts * remove serialize-dates.ts * remove serialize-dates.ts * revert * revert * add mappers * add mappers * fix pr comments * ignore report.json * ignore report.json * Route permission required Co-authored-by: olav <mail@olav.io>
2022-05-18 15:17:09 +02:00
import EventEmitter from 'events';
import FeatureToggleStore from './feature-toggle-store';
import { ensureStringValue } from '../util/ensureStringValue';
import { mapValues } from '../util/map-values';
export interface FeaturesTable {
name: string;
description: string;
type: string;
stale: boolean;
variants: string;
project: string;
last_seen_at?: Date;
created_at?: Date;
}
export default class FeatureToggleClientStore
implements IFeatureToggleClientStore
{
private db: Knex;
private logger: Logger;
private inlineSegmentConstraints: boolean;
private timer: Function;
constructor(
db: Knex,
eventBus: EventEmitter,
getLogger: LogProvider,
inlineSegmentConstraints: boolean,
) {
this.db = db;
this.logger = getLogger('feature-toggle-client-store.ts');
this.inlineSegmentConstraints = inlineSegmentConstraints;
this.timer = (action) =>
metricsHelper.wrapTimer(eventBus, DB_TIME, {
store: 'feature-toggle',
action,
});
}
private async getAll(
featureQuery?: IFeatureToggleQuery,
archived: boolean = false,
isAdmin: boolean = true,
feat(#1873/playground): Return detailed information on feature toggle evaluation (#1839) * Feat: return reasons why a feature evaluated to true or false Note: this is very rough and just straight ripped from the nodejs client. It will need a lot of work, but is a good place to start * Feat: add suggested shape for new payload * Chore: minor cleanup * Wip: make server compile again * Remove unused schema ref * Export new schemas * Chore: fix some tests to use sub property * Fix: fix some tests * Refactor: rename some variables, uncomment some stuff * Add segments type to bootstrap options * Add segments capability to offline feature evaluator * Fix function calls after turning params into an option abject * Feat: test strategy order, etc * Feat: add test to check that all strats are returned correctly * Feat: allow you to include strategy ids in clients * Wip: hook up segments in the offline client. Note: compared to regular clients, they still fail * Feat: add segments validation * Fix: fix test case invariant. * Chore: revert to returning only `boolean` from strategies. This _should_ make it work with custom strategies too 🤞 * Feat: make more properties of the returned feature required * Wip: add some comments and unfinished tests for edge cases * Feat: add `isEnabledInCurrentEnvironment` prop * Feat: consider more strategy failure cases * Feat: test that isenabledinenvironment matches expectations * Feat: add unknown strategies * Fix: fix property access typo * Feat: add unknown strategy for fallback purposes * Feat: test edge case: all unknown strategies * Feat: add custom strategy to arbitrary * Feat: test that features can be true, even if not enabled in env * Chore: add some comments * Wip: fix sdk tests * Remove comments, improve test logging * Feat: add descriptions and examples to playground feature schema * Switch `examples` for `example` * Update schemas with descriptions and examples * Fix: update snapshot * Fix: openapi example * Fix: merge issues * Fix: fix issue where feature evaluation state was wrong * Chore: update openapi spec * Fix: fix broken offline client tests * Refactor: move schemas into separate files * Refactor: remove "reason" for incomplete evaluation. The only instances where evaluation is incomplete is when we don't know what the strategy is. * Refactor: move unleash node client into test and dev dependencies * Wip: further removal of stuff * Chore: remove a bunch of code that we don't use * Chore: remove comment * Chore: remove unused code * Fix: fix some prettier errors * Type parameters in strategies to avoid `any` * Fix: remove commented out code * Feat: make `id` required on playground strategies * Chore: remove redundant type * Fix: remove redundant if and fix fallback evaluation * Refactor: reduce nesting and remove duplication * Fix: remove unused helper function * Refactor: type `parameters` as `unknown` * Chore: remove redundant comment * Refactor: move constraint code into a separate file * Refactor: rename `unleash` -> `feature-evaluator` * Rename class `Unleash` -> `FeatureEvaluator` * Refactor: remove this.ready and sync logic from feature evaluator * Refactor: remove unused code, rename config type * Refactor: remove event emission from the Unleash client * Remove unlistened-for events in feature evaluator * Refactor: make offline client synchronous; remove code * Fix: update openapi snapshot after adding required strategy ids * Feat: change `strategies` format. This commit changes the format of a playground feature's `strategies` properties from a list of strategies to an object with properties `result` and `data`. It looks a bit like this: ```ts type Strategies = { result: boolean | "unknown", data: Strategy[] } ``` The reason is that this allows us to avoid the breaking change that was previously suggested in the PR: `feature.isEnabled` used to be a straight boolean. Then, when we found out we couldn't necessarily evaluate all strategies (custom strats are hard!) we changed it to `boolean | 'unevaluated'`. However, this is confusing on a few levels as the playground results are no longer the same as the SDK would be, nor are they strictly boolean anymore. This change reverts the `isEnabled` functionality to what it was before (so it's always a mirror of what the SDK would show). The equivalent of `feature.isEnabled === 'unevaluated'` now becomes `feature.isEnabled && strategy.result === 'unknown'`. * Fix: Fold long string descriptions over multiple lines. * Fix: update snapshot after adding line breaks to descriptions
2022-08-04 15:41:52 +02:00
includeStrategyIds?: boolean,
): Promise<IFeatureToggleClient[]> {
const environment = featureQuery?.environment || DEFAULT_ENV;
const stopTimer = this.timer('getFeatureAdmin');
let selectColumns = [
'features.name as name',
'features.description as description',
'features.type as type',
'features.project as project',
'features.stale as stale',
'features.impression_data as impression_data',
'features.variants as variants',
'features.created_at as created_at',
'features.last_seen_at as last_seen_at',
'fe.enabled as enabled',
'fe.environment as environment',
'fs.id as strategy_id',
'fs.strategy_name as strategy_name',
'fs.parameters as parameters',
'fs.constraints as constraints',
'segments.id as segment_id',
'segments.constraints as segment_constraints',
];
let query = this.db('features')
.select(selectColumns)
.modify(FeatureToggleStore.filterByArchived, archived)
2022-08-10 08:56:57 +02:00
.leftJoin(
this.db('feature_strategies')
.select('*')
.where({ environment })
.as('fs'),
'fs.feature_name',
'features.name',
)
2022-08-10 08:56:57 +02:00
.leftJoin(
this.db('feature_environments')
.select('feature_name', 'enabled', 'environment')
.where({ environment })
.as('fe'),
'fe.feature_name',
'features.name',
)
.leftJoin(
'feature_strategy_segment as fss',
`fss.feature_strategy_id`,
`fs.id`,
)
.leftJoin('segments', `segments.id`, `fss.segment_id`);
if (featureQuery) {
if (featureQuery.tag) {
const tagQuery = this.db
.from('feature_tag')
.select('feature_name')
.whereIn(['tag_type', 'tag_value'], featureQuery.tag);
query = query.whereIn('features.name', tagQuery);
}
if (featureQuery.project) {
query = query.whereIn('project', featureQuery.project);
}
if (featureQuery.namePrefix) {
query = query.where(
'features.name',
'like',
`${featureQuery.namePrefix}%`,
);
}
}
const rows = await query;
stopTimer();
const featureToggles = rows.reduce((acc, r) => {
let feature: PartialDeep<IFeatureToggleClient> = acc[r.name] ?? {
strategies: [],
};
if (this.isUnseenStrategyRow(feature, r)) {
Complete open api schemas for project features controller (#1563) * Completed OpenAPI Schemas for ProjectFeatures Controller Completed OpenAPI Schemas for Feature Controller (tags) * Completed OpenAPI Schemas for ProjectFeatures Controller Completed OpenAPI Schemas for Feature Controller (tags) * bug fix * bug fix * fix merge conflicts, some refactoring * fix merge conflicts, some refactoring * fix merge conflicts, some refactoring * added emptyResponse, patch feature operation schemas and request * added emptyResponse, patch feature operation schemas and request * patch strategy * patch strategy * update strategy * update strategy * fix pr comment * fix pr comments * improvements * added operationId to schema for better generation * fix pr comment * fix pr comment * fix pr comment * improvements to generated and dynamic types * improvements to generated and dynamic types * improvements to generated and dynamic types * Update response types to use inferred types * Update addTag response status to 201 * refactor: move schema ref destructuring into createSchemaObject * made serialize date handle deep objects * made serialize date handle deep objects * add `name` to IFeatureStrategy nad fix tests * fix pr comments * fix pr comments * Add types to IAuthRequest * Sync StrategySchema for FE and BE - into the rabbit hole * Sync model with OAS spec * Completed OpenAPI Schemas for ProjectFeatures Controller Completed OpenAPI Schemas for Feature Controller (tags) * Completed OpenAPI Schemas for ProjectFeatures Controller Completed OpenAPI Schemas for Feature Controller (tags) * bug fix * bug fix * fix merge conflicts, some refactoring * fix merge conflicts, some refactoring * fix merge conflicts, some refactoring * added emptyResponse, patch feature operation schemas and request * added emptyResponse, patch feature operation schemas and request * patch strategy * patch strategy * update strategy * update strategy * fix pr comment * fix pr comments * improvements * added operationId to schema for better generation * fix pr comment * fix pr comment * fix pr comment * improvements to generated and dynamic types * improvements to generated and dynamic types * improvements to generated and dynamic types * Update response types to use inferred types * Update addTag response status to 201 * refactor: move schema ref destructuring into createSchemaObject * made serialize date handle deep objects * made serialize date handle deep objects * add `name` to IFeatureStrategy nad fix tests * fix pr comments * fix pr comments * Add types to IAuthRequest * Sync StrategySchema for FE and BE - into the rabbit hole * Sync model with OAS spec * Completed OpenAPI Schemas for ProjectFeatures Controller Completed OpenAPI Schemas for Feature Controller (tags) * Completed OpenAPI Schemas for ProjectFeatures Controller Completed OpenAPI Schemas for Feature Controller (tags) * bug fix * bug fix * fix merge conflicts, some refactoring * fix merge conflicts, some refactoring * fix merge conflicts, some refactoring * added emptyResponse, patch feature operation schemas and request * added emptyResponse, patch feature operation schemas and request * patch strategy * patch strategy * update strategy * update strategy * fix pr comment * fix pr comments * improvements * added operationId to schema for better generation * fix pr comment * fix pr comment * fix pr comment * improvements to generated and dynamic types * improvements to generated and dynamic types * improvements to generated and dynamic types * Update response types to use inferred types * Update addTag response status to 201 * refactor: move schema ref destructuring into createSchemaObject * made serialize date handle deep objects * made serialize date handle deep objects * add `name` to IFeatureStrategy nad fix tests * fix pr comments * fix pr comments * Add types to IAuthRequest * Sync StrategySchema for FE and BE - into the rabbit hole * Sync model with OAS spec * Completed OpenAPI Schemas for ProjectFeatures Controller Completed OpenAPI Schemas for Feature Controller (tags) * Completed OpenAPI Schemas for ProjectFeatures Controller Completed OpenAPI Schemas for Feature Controller (tags) * bug fix * bug fix * fix merge conflicts, some refactoring * fix merge conflicts, some refactoring * fix merge conflicts, some refactoring * added emptyResponse, patch feature operation schemas and request * added emptyResponse, patch feature operation schemas and request * patch strategy * patch strategy * update strategy * update strategy * fix pr comment * fix pr comments * improvements * added operationId to schema for better generation * fix pr comment * fix pr comment * fix pr comment * improvements to generated and dynamic types * improvements to generated and dynamic types * improvements to generated and dynamic types * Update response types to use inferred types * Update addTag response status to 201 * refactor: move schema ref destructuring into createSchemaObject * made serialize date handle deep objects * made serialize date handle deep objects * add `name` to IFeatureStrategy nad fix tests * fix pr comments * fix pr comments * Add types to IAuthRequest * Sync StrategySchema for FE and BE - into the rabbit hole * Sync model with OAS spec * revert * revert * revert * revert * revert * mapper * revert * revert * revert * remove serialize-dates.ts * remove serialize-dates.ts * remove serialize-dates.ts * remove serialize-dates.ts * remove serialize-dates.ts * revert * revert * add mappers * add mappers * fix pr comments * ignore report.json * ignore report.json * Route permission required Co-authored-by: olav <mail@olav.io>
2022-05-18 15:17:09 +02:00
feature.strategies.push(
FeatureToggleClientStore.rowToStrategy(r),
);
}
if (featureQuery?.inlineSegmentConstraints && r.segment_id) {
this.addSegmentToStrategy(feature, r);
} else if (
!featureQuery?.inlineSegmentConstraints &&
r.segment_id
) {
this.addSegmentIdsToStrategy(feature, r);
}
feature.impressionData = r.impression_data;
feature.enabled = !!r.enabled;
feature.name = r.name;
feature.description = r.description;
feature.project = r.project;
feature.stale = r.stale;
feature.type = r.type;
feature.variants = r.variants;
feature.project = r.project;
if (isAdmin) {
feature.lastSeenAt = r.last_seen_at;
feature.createdAt = r.created_at;
}
acc[r.name] = feature;
return acc;
}, {});
const features: IFeatureToggleClient[] = Object.values(featureToggles);
feat(#1873/playground): Return detailed information on feature toggle evaluation (#1839) * Feat: return reasons why a feature evaluated to true or false Note: this is very rough and just straight ripped from the nodejs client. It will need a lot of work, but is a good place to start * Feat: add suggested shape for new payload * Chore: minor cleanup * Wip: make server compile again * Remove unused schema ref * Export new schemas * Chore: fix some tests to use sub property * Fix: fix some tests * Refactor: rename some variables, uncomment some stuff * Add segments type to bootstrap options * Add segments capability to offline feature evaluator * Fix function calls after turning params into an option abject * Feat: test strategy order, etc * Feat: add test to check that all strats are returned correctly * Feat: allow you to include strategy ids in clients * Wip: hook up segments in the offline client. Note: compared to regular clients, they still fail * Feat: add segments validation * Fix: fix test case invariant. * Chore: revert to returning only `boolean` from strategies. This _should_ make it work with custom strategies too 🤞 * Feat: make more properties of the returned feature required * Wip: add some comments and unfinished tests for edge cases * Feat: add `isEnabledInCurrentEnvironment` prop * Feat: consider more strategy failure cases * Feat: test that isenabledinenvironment matches expectations * Feat: add unknown strategies * Fix: fix property access typo * Feat: add unknown strategy for fallback purposes * Feat: test edge case: all unknown strategies * Feat: add custom strategy to arbitrary * Feat: test that features can be true, even if not enabled in env * Chore: add some comments * Wip: fix sdk tests * Remove comments, improve test logging * Feat: add descriptions and examples to playground feature schema * Switch `examples` for `example` * Update schemas with descriptions and examples * Fix: update snapshot * Fix: openapi example * Fix: merge issues * Fix: fix issue where feature evaluation state was wrong * Chore: update openapi spec * Fix: fix broken offline client tests * Refactor: move schemas into separate files * Refactor: remove "reason" for incomplete evaluation. The only instances where evaluation is incomplete is when we don't know what the strategy is. * Refactor: move unleash node client into test and dev dependencies * Wip: further removal of stuff * Chore: remove a bunch of code that we don't use * Chore: remove comment * Chore: remove unused code * Fix: fix some prettier errors * Type parameters in strategies to avoid `any` * Fix: remove commented out code * Feat: make `id` required on playground strategies * Chore: remove redundant type * Fix: remove redundant if and fix fallback evaluation * Refactor: reduce nesting and remove duplication * Fix: remove unused helper function * Refactor: type `parameters` as `unknown` * Chore: remove redundant comment * Refactor: move constraint code into a separate file * Refactor: rename `unleash` -> `feature-evaluator` * Rename class `Unleash` -> `FeatureEvaluator` * Refactor: remove this.ready and sync logic from feature evaluator * Refactor: remove unused code, rename config type * Refactor: remove event emission from the Unleash client * Remove unlistened-for events in feature evaluator * Refactor: make offline client synchronous; remove code * Fix: update openapi snapshot after adding required strategy ids * Feat: change `strategies` format. This commit changes the format of a playground feature's `strategies` properties from a list of strategies to an object with properties `result` and `data`. It looks a bit like this: ```ts type Strategies = { result: boolean | "unknown", data: Strategy[] } ``` The reason is that this allows us to avoid the breaking change that was previously suggested in the PR: `feature.isEnabled` used to be a straight boolean. Then, when we found out we couldn't necessarily evaluate all strategies (custom strats are hard!) we changed it to `boolean | 'unevaluated'`. However, this is confusing on a few levels as the playground results are no longer the same as the SDK would be, nor are they strictly boolean anymore. This change reverts the `isEnabled` functionality to what it was before (so it's always a mirror of what the SDK would show). The equivalent of `feature.isEnabled === 'unevaluated'` now becomes `feature.isEnabled && strategy.result === 'unknown'`. * Fix: Fold long string descriptions over multiple lines. * Fix: update snapshot after adding line breaks to descriptions
2022-08-04 15:41:52 +02:00
if (!isAdmin && !includeStrategyIds) {
// We should not send strategy IDs from the client API,
// as this breaks old versions of the Go SDK (at least).
Complete open api schemas for project features controller (#1563) * Completed OpenAPI Schemas for ProjectFeatures Controller Completed OpenAPI Schemas for Feature Controller (tags) * Completed OpenAPI Schemas for ProjectFeatures Controller Completed OpenAPI Schemas for Feature Controller (tags) * bug fix * bug fix * fix merge conflicts, some refactoring * fix merge conflicts, some refactoring * fix merge conflicts, some refactoring * added emptyResponse, patch feature operation schemas and request * added emptyResponse, patch feature operation schemas and request * patch strategy * patch strategy * update strategy * update strategy * fix pr comment * fix pr comments * improvements * added operationId to schema for better generation * fix pr comment * fix pr comment * fix pr comment * improvements to generated and dynamic types * improvements to generated and dynamic types * improvements to generated and dynamic types * Update response types to use inferred types * Update addTag response status to 201 * refactor: move schema ref destructuring into createSchemaObject * made serialize date handle deep objects * made serialize date handle deep objects * add `name` to IFeatureStrategy nad fix tests * fix pr comments * fix pr comments * Add types to IAuthRequest * Sync StrategySchema for FE and BE - into the rabbit hole * Sync model with OAS spec * Completed OpenAPI Schemas for ProjectFeatures Controller Completed OpenAPI Schemas for Feature Controller (tags) * Completed OpenAPI Schemas for ProjectFeatures Controller Completed OpenAPI Schemas for Feature Controller (tags) * bug fix * bug fix * fix merge conflicts, some refactoring * fix merge conflicts, some refactoring * fix merge conflicts, some refactoring * added emptyResponse, patch feature operation schemas and request * added emptyResponse, patch feature operation schemas and request * patch strategy * patch strategy * update strategy * update strategy * fix pr comment * fix pr comments * improvements * added operationId to schema for better generation * fix pr comment * fix pr comment * fix pr comment * improvements to generated and dynamic types * improvements to generated and dynamic types * improvements to generated and dynamic types * Update response types to use inferred types * Update addTag response status to 201 * refactor: move schema ref destructuring into createSchemaObject * made serialize date handle deep objects * made serialize date handle deep objects * add `name` to IFeatureStrategy nad fix tests * fix pr comments * fix pr comments * Add types to IAuthRequest * Sync StrategySchema for FE and BE - into the rabbit hole * Sync model with OAS spec * Completed OpenAPI Schemas for ProjectFeatures Controller Completed OpenAPI Schemas for Feature Controller (tags) * Completed OpenAPI Schemas for ProjectFeatures Controller Completed OpenAPI Schemas for Feature Controller (tags) * bug fix * bug fix * fix merge conflicts, some refactoring * fix merge conflicts, some refactoring * fix merge conflicts, some refactoring * added emptyResponse, patch feature operation schemas and request * added emptyResponse, patch feature operation schemas and request * patch strategy * patch strategy * update strategy * update strategy * fix pr comment * fix pr comments * improvements * added operationId to schema for better generation * fix pr comment * fix pr comment * fix pr comment * improvements to generated and dynamic types * improvements to generated and dynamic types * improvements to generated and dynamic types * Update response types to use inferred types * Update addTag response status to 201 * refactor: move schema ref destructuring into createSchemaObject * made serialize date handle deep objects * made serialize date handle deep objects * add `name` to IFeatureStrategy nad fix tests * fix pr comments * fix pr comments * Add types to IAuthRequest * Sync StrategySchema for FE and BE - into the rabbit hole * Sync model with OAS spec * Completed OpenAPI Schemas for ProjectFeatures Controller Completed OpenAPI Schemas for Feature Controller (tags) * Completed OpenAPI Schemas for ProjectFeatures Controller Completed OpenAPI Schemas for Feature Controller (tags) * bug fix * bug fix * fix merge conflicts, some refactoring * fix merge conflicts, some refactoring * fix merge conflicts, some refactoring * added emptyResponse, patch feature operation schemas and request * added emptyResponse, patch feature operation schemas and request * patch strategy * patch strategy * update strategy * update strategy * fix pr comment * fix pr comments * improvements * added operationId to schema for better generation * fix pr comment * fix pr comment * fix pr comment * improvements to generated and dynamic types * improvements to generated and dynamic types * improvements to generated and dynamic types * Update response types to use inferred types * Update addTag response status to 201 * refactor: move schema ref destructuring into createSchemaObject * made serialize date handle deep objects * made serialize date handle deep objects * add `name` to IFeatureStrategy nad fix tests * fix pr comments * fix pr comments * Add types to IAuthRequest * Sync StrategySchema for FE and BE - into the rabbit hole * Sync model with OAS spec * revert * revert * revert * revert * revert * mapper * revert * revert * revert * remove serialize-dates.ts * remove serialize-dates.ts * remove serialize-dates.ts * remove serialize-dates.ts * remove serialize-dates.ts * revert * revert * add mappers * add mappers * fix pr comments * ignore report.json * ignore report.json * Route permission required Co-authored-by: olav <mail@olav.io>
2022-05-18 15:17:09 +02:00
FeatureToggleClientStore.removeIdsFromStrategies(features);
}
return features;
}
Complete open api schemas for project features controller (#1563) * Completed OpenAPI Schemas for ProjectFeatures Controller Completed OpenAPI Schemas for Feature Controller (tags) * Completed OpenAPI Schemas for ProjectFeatures Controller Completed OpenAPI Schemas for Feature Controller (tags) * bug fix * bug fix * fix merge conflicts, some refactoring * fix merge conflicts, some refactoring * fix merge conflicts, some refactoring * added emptyResponse, patch feature operation schemas and request * added emptyResponse, patch feature operation schemas and request * patch strategy * patch strategy * update strategy * update strategy * fix pr comment * fix pr comments * improvements * added operationId to schema for better generation * fix pr comment * fix pr comment * fix pr comment * improvements to generated and dynamic types * improvements to generated and dynamic types * improvements to generated and dynamic types * Update response types to use inferred types * Update addTag response status to 201 * refactor: move schema ref destructuring into createSchemaObject * made serialize date handle deep objects * made serialize date handle deep objects * add `name` to IFeatureStrategy nad fix tests * fix pr comments * fix pr comments * Add types to IAuthRequest * Sync StrategySchema for FE and BE - into the rabbit hole * Sync model with OAS spec * Completed OpenAPI Schemas for ProjectFeatures Controller Completed OpenAPI Schemas for Feature Controller (tags) * Completed OpenAPI Schemas for ProjectFeatures Controller Completed OpenAPI Schemas for Feature Controller (tags) * bug fix * bug fix * fix merge conflicts, some refactoring * fix merge conflicts, some refactoring * fix merge conflicts, some refactoring * added emptyResponse, patch feature operation schemas and request * added emptyResponse, patch feature operation schemas and request * patch strategy * patch strategy * update strategy * update strategy * fix pr comment * fix pr comments * improvements * added operationId to schema for better generation * fix pr comment * fix pr comment * fix pr comment * improvements to generated and dynamic types * improvements to generated and dynamic types * improvements to generated and dynamic types * Update response types to use inferred types * Update addTag response status to 201 * refactor: move schema ref destructuring into createSchemaObject * made serialize date handle deep objects * made serialize date handle deep objects * add `name` to IFeatureStrategy nad fix tests * fix pr comments * fix pr comments * Add types to IAuthRequest * Sync StrategySchema for FE and BE - into the rabbit hole * Sync model with OAS spec * Completed OpenAPI Schemas for ProjectFeatures Controller Completed OpenAPI Schemas for Feature Controller (tags) * Completed OpenAPI Schemas for ProjectFeatures Controller Completed OpenAPI Schemas for Feature Controller (tags) * bug fix * bug fix * fix merge conflicts, some refactoring * fix merge conflicts, some refactoring * fix merge conflicts, some refactoring * added emptyResponse, patch feature operation schemas and request * added emptyResponse, patch feature operation schemas and request * patch strategy * patch strategy * update strategy * update strategy * fix pr comment * fix pr comments * improvements * added operationId to schema for better generation * fix pr comment * fix pr comment * fix pr comment * improvements to generated and dynamic types * improvements to generated and dynamic types * improvements to generated and dynamic types * Update response types to use inferred types * Update addTag response status to 201 * refactor: move schema ref destructuring into createSchemaObject * made serialize date handle deep objects * made serialize date handle deep objects * add `name` to IFeatureStrategy nad fix tests * fix pr comments * fix pr comments * Add types to IAuthRequest * Sync StrategySchema for FE and BE - into the rabbit hole * Sync model with OAS spec * Completed OpenAPI Schemas for ProjectFeatures Controller Completed OpenAPI Schemas for Feature Controller (tags) * Completed OpenAPI Schemas for ProjectFeatures Controller Completed OpenAPI Schemas for Feature Controller (tags) * bug fix * bug fix * fix merge conflicts, some refactoring * fix merge conflicts, some refactoring * fix merge conflicts, some refactoring * added emptyResponse, patch feature operation schemas and request * added emptyResponse, patch feature operation schemas and request * patch strategy * patch strategy * update strategy * update strategy * fix pr comment * fix pr comments * improvements * added operationId to schema for better generation * fix pr comment * fix pr comment * fix pr comment * improvements to generated and dynamic types * improvements to generated and dynamic types * improvements to generated and dynamic types * Update response types to use inferred types * Update addTag response status to 201 * refactor: move schema ref destructuring into createSchemaObject * made serialize date handle deep objects * made serialize date handle deep objects * add `name` to IFeatureStrategy nad fix tests * fix pr comments * fix pr comments * Add types to IAuthRequest * Sync StrategySchema for FE and BE - into the rabbit hole * Sync model with OAS spec * revert * revert * revert * revert * revert * mapper * revert * revert * revert * remove serialize-dates.ts * remove serialize-dates.ts * remove serialize-dates.ts * remove serialize-dates.ts * remove serialize-dates.ts * revert * revert * add mappers * add mappers * fix pr comments * ignore report.json * ignore report.json * Route permission required Co-authored-by: olav <mail@olav.io>
2022-05-18 15:17:09 +02:00
private static rowToStrategy(row: Record<string, any>): IStrategyConfig {
return {
id: row.strategy_id,
name: row.strategy_name,
constraints: row.constraints || [],
parameters: mapValues(row.parameters || {}, ensureStringValue),
};
}
Complete open api schemas for project features controller (#1563) * Completed OpenAPI Schemas for ProjectFeatures Controller Completed OpenAPI Schemas for Feature Controller (tags) * Completed OpenAPI Schemas for ProjectFeatures Controller Completed OpenAPI Schemas for Feature Controller (tags) * bug fix * bug fix * fix merge conflicts, some refactoring * fix merge conflicts, some refactoring * fix merge conflicts, some refactoring * added emptyResponse, patch feature operation schemas and request * added emptyResponse, patch feature operation schemas and request * patch strategy * patch strategy * update strategy * update strategy * fix pr comment * fix pr comments * improvements * added operationId to schema for better generation * fix pr comment * fix pr comment * fix pr comment * improvements to generated and dynamic types * improvements to generated and dynamic types * improvements to generated and dynamic types * Update response types to use inferred types * Update addTag response status to 201 * refactor: move schema ref destructuring into createSchemaObject * made serialize date handle deep objects * made serialize date handle deep objects * add `name` to IFeatureStrategy nad fix tests * fix pr comments * fix pr comments * Add types to IAuthRequest * Sync StrategySchema for FE and BE - into the rabbit hole * Sync model with OAS spec * Completed OpenAPI Schemas for ProjectFeatures Controller Completed OpenAPI Schemas for Feature Controller (tags) * Completed OpenAPI Schemas for ProjectFeatures Controller Completed OpenAPI Schemas for Feature Controller (tags) * bug fix * bug fix * fix merge conflicts, some refactoring * fix merge conflicts, some refactoring * fix merge conflicts, some refactoring * added emptyResponse, patch feature operation schemas and request * added emptyResponse, patch feature operation schemas and request * patch strategy * patch strategy * update strategy * update strategy * fix pr comment * fix pr comments * improvements * added operationId to schema for better generation * fix pr comment * fix pr comment * fix pr comment * improvements to generated and dynamic types * improvements to generated and dynamic types * improvements to generated and dynamic types * Update response types to use inferred types * Update addTag response status to 201 * refactor: move schema ref destructuring into createSchemaObject * made serialize date handle deep objects * made serialize date handle deep objects * add `name` to IFeatureStrategy nad fix tests * fix pr comments * fix pr comments * Add types to IAuthRequest * Sync StrategySchema for FE and BE - into the rabbit hole * Sync model with OAS spec * Completed OpenAPI Schemas for ProjectFeatures Controller Completed OpenAPI Schemas for Feature Controller (tags) * Completed OpenAPI Schemas for ProjectFeatures Controller Completed OpenAPI Schemas for Feature Controller (tags) * bug fix * bug fix * fix merge conflicts, some refactoring * fix merge conflicts, some refactoring * fix merge conflicts, some refactoring * added emptyResponse, patch feature operation schemas and request * added emptyResponse, patch feature operation schemas and request * patch strategy * patch strategy * update strategy * update strategy * fix pr comment * fix pr comments * improvements * added operationId to schema for better generation * fix pr comment * fix pr comment * fix pr comment * improvements to generated and dynamic types * improvements to generated and dynamic types * improvements to generated and dynamic types * Update response types to use inferred types * Update addTag response status to 201 * refactor: move schema ref destructuring into createSchemaObject * made serialize date handle deep objects * made serialize date handle deep objects * add `name` to IFeatureStrategy nad fix tests * fix pr comments * fix pr comments * Add types to IAuthRequest * Sync StrategySchema for FE and BE - into the rabbit hole * Sync model with OAS spec * Completed OpenAPI Schemas for ProjectFeatures Controller Completed OpenAPI Schemas for Feature Controller (tags) * Completed OpenAPI Schemas for ProjectFeatures Controller Completed OpenAPI Schemas for Feature Controller (tags) * bug fix * bug fix * fix merge conflicts, some refactoring * fix merge conflicts, some refactoring * fix merge conflicts, some refactoring * added emptyResponse, patch feature operation schemas and request * added emptyResponse, patch feature operation schemas and request * patch strategy * patch strategy * update strategy * update strategy * fix pr comment * fix pr comments * improvements * added operationId to schema for better generation * fix pr comment * fix pr comment * fix pr comment * improvements to generated and dynamic types * improvements to generated and dynamic types * improvements to generated and dynamic types * Update response types to use inferred types * Update addTag response status to 201 * refactor: move schema ref destructuring into createSchemaObject * made serialize date handle deep objects * made serialize date handle deep objects * add `name` to IFeatureStrategy nad fix tests * fix pr comments * fix pr comments * Add types to IAuthRequest * Sync StrategySchema for FE and BE - into the rabbit hole * Sync model with OAS spec * revert * revert * revert * revert * revert * mapper * revert * revert * revert * remove serialize-dates.ts * remove serialize-dates.ts * remove serialize-dates.ts * remove serialize-dates.ts * remove serialize-dates.ts * revert * revert * add mappers * add mappers * fix pr comments * ignore report.json * ignore report.json * Route permission required Co-authored-by: olav <mail@olav.io>
2022-05-18 15:17:09 +02:00
private static removeIdsFromStrategies(features: IFeatureToggleClient[]) {
features.forEach((feature) => {
feature.strategies.forEach((strategy) => {
delete strategy.id;
});
});
}
private isUnseenStrategyRow(
feature: PartialDeep<IFeatureToggleClient>,
row: Record<string, any>,
): boolean {
return (
row.strategy_id &&
!feature.strategies.find((s) => s.id === row.strategy_id)
);
}
private addSegmentToStrategy(
feature: PartialDeep<IFeatureToggleClient>,
row: Record<string, any>,
) {
feature.strategies
.find((s) => s.id === row.strategy_id)
?.constraints.push(...row.segment_constraints);
}
private addSegmentIdsToStrategy(
feature: PartialDeep<IFeatureToggleClient>,
row: Record<string, any>,
) {
const strategy = feature.strategies.find(
(s) => s.id === row.strategy_id,
);
if (!strategy) {
return;
}
if (!strategy.segments) {
strategy.segments = [];
}
strategy.segments.push(row.segment_id);
}
async getClient(
featureQuery?: IFeatureToggleQuery,
feat(#1873/playground): Return detailed information on feature toggle evaluation (#1839) * Feat: return reasons why a feature evaluated to true or false Note: this is very rough and just straight ripped from the nodejs client. It will need a lot of work, but is a good place to start * Feat: add suggested shape for new payload * Chore: minor cleanup * Wip: make server compile again * Remove unused schema ref * Export new schemas * Chore: fix some tests to use sub property * Fix: fix some tests * Refactor: rename some variables, uncomment some stuff * Add segments type to bootstrap options * Add segments capability to offline feature evaluator * Fix function calls after turning params into an option abject * Feat: test strategy order, etc * Feat: add test to check that all strats are returned correctly * Feat: allow you to include strategy ids in clients * Wip: hook up segments in the offline client. Note: compared to regular clients, they still fail * Feat: add segments validation * Fix: fix test case invariant. * Chore: revert to returning only `boolean` from strategies. This _should_ make it work with custom strategies too 🤞 * Feat: make more properties of the returned feature required * Wip: add some comments and unfinished tests for edge cases * Feat: add `isEnabledInCurrentEnvironment` prop * Feat: consider more strategy failure cases * Feat: test that isenabledinenvironment matches expectations * Feat: add unknown strategies * Fix: fix property access typo * Feat: add unknown strategy for fallback purposes * Feat: test edge case: all unknown strategies * Feat: add custom strategy to arbitrary * Feat: test that features can be true, even if not enabled in env * Chore: add some comments * Wip: fix sdk tests * Remove comments, improve test logging * Feat: add descriptions and examples to playground feature schema * Switch `examples` for `example` * Update schemas with descriptions and examples * Fix: update snapshot * Fix: openapi example * Fix: merge issues * Fix: fix issue where feature evaluation state was wrong * Chore: update openapi spec * Fix: fix broken offline client tests * Refactor: move schemas into separate files * Refactor: remove "reason" for incomplete evaluation. The only instances where evaluation is incomplete is when we don't know what the strategy is. * Refactor: move unleash node client into test and dev dependencies * Wip: further removal of stuff * Chore: remove a bunch of code that we don't use * Chore: remove comment * Chore: remove unused code * Fix: fix some prettier errors * Type parameters in strategies to avoid `any` * Fix: remove commented out code * Feat: make `id` required on playground strategies * Chore: remove redundant type * Fix: remove redundant if and fix fallback evaluation * Refactor: reduce nesting and remove duplication * Fix: remove unused helper function * Refactor: type `parameters` as `unknown` * Chore: remove redundant comment * Refactor: move constraint code into a separate file * Refactor: rename `unleash` -> `feature-evaluator` * Rename class `Unleash` -> `FeatureEvaluator` * Refactor: remove this.ready and sync logic from feature evaluator * Refactor: remove unused code, rename config type * Refactor: remove event emission from the Unleash client * Remove unlistened-for events in feature evaluator * Refactor: make offline client synchronous; remove code * Fix: update openapi snapshot after adding required strategy ids * Feat: change `strategies` format. This commit changes the format of a playground feature's `strategies` properties from a list of strategies to an object with properties `result` and `data`. It looks a bit like this: ```ts type Strategies = { result: boolean | "unknown", data: Strategy[] } ``` The reason is that this allows us to avoid the breaking change that was previously suggested in the PR: `feature.isEnabled` used to be a straight boolean. Then, when we found out we couldn't necessarily evaluate all strategies (custom strats are hard!) we changed it to `boolean | 'unevaluated'`. However, this is confusing on a few levels as the playground results are no longer the same as the SDK would be, nor are they strictly boolean anymore. This change reverts the `isEnabled` functionality to what it was before (so it's always a mirror of what the SDK would show). The equivalent of `feature.isEnabled === 'unevaluated'` now becomes `feature.isEnabled && strategy.result === 'unknown'`. * Fix: Fold long string descriptions over multiple lines. * Fix: update snapshot after adding line breaks to descriptions
2022-08-04 15:41:52 +02:00
includeStrategyIds?: boolean,
): Promise<IFeatureToggleClient[]> {
feat(#1873/playground): Return detailed information on feature toggle evaluation (#1839) * Feat: return reasons why a feature evaluated to true or false Note: this is very rough and just straight ripped from the nodejs client. It will need a lot of work, but is a good place to start * Feat: add suggested shape for new payload * Chore: minor cleanup * Wip: make server compile again * Remove unused schema ref * Export new schemas * Chore: fix some tests to use sub property * Fix: fix some tests * Refactor: rename some variables, uncomment some stuff * Add segments type to bootstrap options * Add segments capability to offline feature evaluator * Fix function calls after turning params into an option abject * Feat: test strategy order, etc * Feat: add test to check that all strats are returned correctly * Feat: allow you to include strategy ids in clients * Wip: hook up segments in the offline client. Note: compared to regular clients, they still fail * Feat: add segments validation * Fix: fix test case invariant. * Chore: revert to returning only `boolean` from strategies. This _should_ make it work with custom strategies too 🤞 * Feat: make more properties of the returned feature required * Wip: add some comments and unfinished tests for edge cases * Feat: add `isEnabledInCurrentEnvironment` prop * Feat: consider more strategy failure cases * Feat: test that isenabledinenvironment matches expectations * Feat: add unknown strategies * Fix: fix property access typo * Feat: add unknown strategy for fallback purposes * Feat: test edge case: all unknown strategies * Feat: add custom strategy to arbitrary * Feat: test that features can be true, even if not enabled in env * Chore: add some comments * Wip: fix sdk tests * Remove comments, improve test logging * Feat: add descriptions and examples to playground feature schema * Switch `examples` for `example` * Update schemas with descriptions and examples * Fix: update snapshot * Fix: openapi example * Fix: merge issues * Fix: fix issue where feature evaluation state was wrong * Chore: update openapi spec * Fix: fix broken offline client tests * Refactor: move schemas into separate files * Refactor: remove "reason" for incomplete evaluation. The only instances where evaluation is incomplete is when we don't know what the strategy is. * Refactor: move unleash node client into test and dev dependencies * Wip: further removal of stuff * Chore: remove a bunch of code that we don't use * Chore: remove comment * Chore: remove unused code * Fix: fix some prettier errors * Type parameters in strategies to avoid `any` * Fix: remove commented out code * Feat: make `id` required on playground strategies * Chore: remove redundant type * Fix: remove redundant if and fix fallback evaluation * Refactor: reduce nesting and remove duplication * Fix: remove unused helper function * Refactor: type `parameters` as `unknown` * Chore: remove redundant comment * Refactor: move constraint code into a separate file * Refactor: rename `unleash` -> `feature-evaluator` * Rename class `Unleash` -> `FeatureEvaluator` * Refactor: remove this.ready and sync logic from feature evaluator * Refactor: remove unused code, rename config type * Refactor: remove event emission from the Unleash client * Remove unlistened-for events in feature evaluator * Refactor: make offline client synchronous; remove code * Fix: update openapi snapshot after adding required strategy ids * Feat: change `strategies` format. This commit changes the format of a playground feature's `strategies` properties from a list of strategies to an object with properties `result` and `data`. It looks a bit like this: ```ts type Strategies = { result: boolean | "unknown", data: Strategy[] } ``` The reason is that this allows us to avoid the breaking change that was previously suggested in the PR: `feature.isEnabled` used to be a straight boolean. Then, when we found out we couldn't necessarily evaluate all strategies (custom strats are hard!) we changed it to `boolean | 'unevaluated'`. However, this is confusing on a few levels as the playground results are no longer the same as the SDK would be, nor are they strictly boolean anymore. This change reverts the `isEnabled` functionality to what it was before (so it's always a mirror of what the SDK would show). The equivalent of `feature.isEnabled === 'unevaluated'` now becomes `feature.isEnabled && strategy.result === 'unknown'`. * Fix: Fold long string descriptions over multiple lines. * Fix: update snapshot after adding line breaks to descriptions
2022-08-04 15:41:52 +02:00
return this.getAll(featureQuery, false, false, includeStrategyIds);
}
async getAdmin(
featureQuery?: IFeatureToggleQuery,
archived: boolean = false,
): Promise<IFeatureToggleClient[]> {
return this.getAll(featureQuery, archived, true);
}
}
module.exports = FeatureToggleClientStore;