@diia-inhouse/features 3.2.193 → 3.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1,3 +1,3 @@
1
- import { FeatureConfig, FeatureContext, FeatureInterface } from "./interfaces/index.js";
1
+ import { FeatureConfig, FeatureContext, FeatureInterface, FeatureVariant, GetLocalizedVariantOptions, GetVariantOptions } from "./interfaces/index.js";
2
2
  import { FeatureService } from "./services/feature.js";
3
- export { FeatureConfig, FeatureContext, FeatureInterface, FeatureService };
3
+ export { FeatureConfig, FeatureContext, FeatureInterface, FeatureService, FeatureVariant, GetLocalizedVariantOptions, GetVariantOptions };
@@ -1,5 +1,6 @@
1
1
  import { Context } from "unleash-client";
2
- import { PlatformType, ProfileFeature, SessionType } from "@diia-inhouse/types";
2
+ import { Language, PlatformType, ProfileFeature, SessionType } from "@diia-inhouse/types";
3
+ import { ValidationRule } from "@diia-inhouse/validators";
3
4
  import { FeatureInterface as FeatureInterface$1 } from "unleash-client/lib/feature.js";
4
5
 
5
6
  //#region src/interfaces/index.d.ts
@@ -23,5 +24,19 @@ interface FeatureContext extends Context {
23
24
  sessionType?: SessionType;
24
25
  profileFeature?: ProfileFeature;
25
26
  }
27
+ interface FeatureVariant<TPayload = unknown> {
28
+ isEnabled: boolean;
29
+ name?: string;
30
+ payload?: TPayload;
31
+ }
32
+ interface GetVariantOptions {
33
+ payloadSchema?: ValidationRule;
34
+ context?: FeatureContext;
35
+ }
36
+ interface GetLocalizedVariantOptions<TField extends string> {
37
+ fields: readonly TField[];
38
+ context?: FeatureContext;
39
+ language?: Language;
40
+ }
26
41
  //#endregion
27
- export { FeatureConfig, FeatureContext, type FeatureInterface$1 as FeatureInterface };
42
+ export { FeatureConfig, FeatureContext, type FeatureInterface$1 as FeatureInterface, FeatureVariant, GetLocalizedVariantOptions, GetVariantOptions };
@@ -1,5 +1,6 @@
1
- import { FeatureConfig, FeatureContext } from "../interfaces/index.js";
1
+ import { FeatureConfig, FeatureContext, FeatureVariant, GetLocalizedVariantOptions, GetVariantOptions } from "../interfaces/index.js";
2
2
  import { AlsData, Logger, OnInit } from "@diia-inhouse/types";
3
+ import { AppValidator } from "@diia-inhouse/validators";
3
4
  import { FeatureInterface } from "unleash-client/lib/feature.js";
4
5
  import { AsyncLocalStorage } from "node:async_hooks";
5
6
  import { EnvService } from "@diia-inhouse/env";
@@ -11,12 +12,21 @@ declare class FeatureService implements OnInit {
11
12
  private readonly logger;
12
13
  private readonly envService;
13
14
  private readonly asyncLocalStorage;
15
+ private readonly validator;
14
16
  private unleash;
15
- constructor(serviceName: string, featureConfig: FeatureConfig, logger: Logger, envService: EnvService, asyncLocalStorage: AsyncLocalStorage<AlsData>);
17
+ constructor(serviceName: string, featureConfig: FeatureConfig, logger: Logger, envService: EnvService, asyncLocalStorage: AsyncLocalStorage<AlsData>, validator: AppValidator);
16
18
  onInit(): Promise<void>;
17
19
  isEnabled(name: string, context?: FeatureContext): boolean;
18
20
  isSomeEnabled(name: string, contexts: FeatureContext[]): boolean;
21
+ getVariant<TPayload = unknown>(name: string, options?: GetVariantOptions): FeatureVariant<TPayload>;
22
+ getLocalizedVariant<TField extends string>(name: string, options: GetLocalizedVariantOptions<TField>): Promise<FeatureVariant<Record<TField, string>>>;
19
23
  getDefinition(name: string): FeatureInterface | undefined;
24
+ private resolveContext;
25
+ private markFlagEnabled;
26
+ private parsePayload;
27
+ private toPayloadValue;
28
+ private resolveLanguage;
29
+ private localizePayload;
20
30
  private parseAppVersion;
21
31
  }
22
32
  //#endregion
@@ -1,5 +1,6 @@
1
1
  import semver from "semver";
2
- import { startUnleash } from "unleash-client";
2
+ import { PayloadType, startUnleash } from "unleash-client";
3
+ import { Languages } from "@diia-inhouse/types";
3
4
  //#region src/services/feature.ts
4
5
  var FeatureService = class {
5
6
  serviceName;
@@ -7,13 +8,15 @@ var FeatureService = class {
7
8
  logger;
8
9
  envService;
9
10
  asyncLocalStorage;
11
+ validator;
10
12
  unleash = null;
11
- constructor(serviceName, featureConfig, logger, envService, asyncLocalStorage) {
13
+ constructor(serviceName, featureConfig, logger, envService, asyncLocalStorage, validator) {
12
14
  this.serviceName = serviceName;
13
15
  this.featureConfig = featureConfig;
14
16
  this.logger = logger;
15
17
  this.envService = envService;
16
18
  this.asyncLocalStorage = asyncLocalStorage;
19
+ this.validator = validator;
17
20
  }
18
21
  async onInit() {
19
22
  const { isEnabled, url, apiToken } = this.featureConfig;
@@ -45,6 +48,64 @@ var FeatureService = class {
45
48
  return false;
46
49
  }
47
50
  const alsStore = this.asyncLocalStorage.getStore() ?? {};
51
+ const resolvedContext = this.resolveContext(context, alsStore);
52
+ this.logger.debug("Feature flag check", {
53
+ name,
54
+ context: resolvedContext
55
+ });
56
+ const result = this.unleash.isEnabled(name, resolvedContext);
57
+ if (result) this.markFlagEnabled(name, alsStore);
58
+ return result;
59
+ }
60
+ isSomeEnabled(name, contexts) {
61
+ return contexts.some((context) => this.isEnabled(name, context));
62
+ }
63
+ getVariant(name, options = {}) {
64
+ if (!this.unleash) {
65
+ this.logger.warn("Unleash is disabled");
66
+ return { isEnabled: false };
67
+ }
68
+ const alsStore = this.asyncLocalStorage.getStore() ?? {};
69
+ const resolvedContext = this.resolveContext(options.context ?? {}, alsStore);
70
+ const variant = this.unleash.getVariant(name, resolvedContext);
71
+ this.logger.debug("Feature flag variant check", {
72
+ name,
73
+ context: resolvedContext,
74
+ variant: variant.name,
75
+ isFeatureEnabled: variant.feature_enabled
76
+ });
77
+ if (!variant.feature_enabled) return { isEnabled: false };
78
+ this.markFlagEnabled(name, alsStore);
79
+ if (!variant.enabled) return { isEnabled: true };
80
+ const payload = this.parsePayload(name, variant, options.payloadSchema);
81
+ if (payload === void 0) return {
82
+ isEnabled: true,
83
+ name: variant.name
84
+ };
85
+ return {
86
+ isEnabled: true,
87
+ name: variant.name,
88
+ payload
89
+ };
90
+ }
91
+ async getLocalizedVariant(name, options) {
92
+ const { getLocalizedValidationSchema } = await import("@diia-inhouse/i18n");
93
+ const { fields, context, language } = options;
94
+ const { payload, ...variant } = this.getVariant(name, {
95
+ payloadSchema: getLocalizedValidationSchema(fields),
96
+ context
97
+ });
98
+ if (!payload) return variant;
99
+ const alsStore = this.asyncLocalStorage.getStore() ?? {};
100
+ return {
101
+ ...variant,
102
+ payload: this.localizePayload(payload, language ?? this.resolveLanguage(alsStore))
103
+ };
104
+ }
105
+ getDefinition(name) {
106
+ return this.unleash?.getFeatureToggleDefinition(name);
107
+ }
108
+ resolveContext(context, alsStore) {
48
109
  context.userId ??= alsStore.logData?.userIdentifier;
49
110
  context.userIdBase64 ??= context.userId && Buffer.from(context.userId, "hex").toString("base64");
50
111
  context.sessionType ??= alsStore.logData?.sessionType;
@@ -56,24 +117,44 @@ var FeatureService = class {
56
117
  context.mobileUid ??= alsStore.headers?.mobileUid;
57
118
  context.currentTime ??= /* @__PURE__ */ new Date();
58
119
  context.environment ??= this.envService.getEnv();
59
- this.logger.debug("Feature flag check", {
60
- name,
61
- context
62
- });
63
- const result = this.unleash.isEnabled(name, context);
64
- if (result) {
65
- alsStore.logData ??= {};
66
- alsStore.logData.flags ??= [];
67
- if (!alsStore.logData.flags.includes(name)) alsStore.logData.flags.push(name);
68
- this.asyncLocalStorage.enterWith(alsStore);
120
+ return context;
121
+ }
122
+ markFlagEnabled(name, alsStore) {
123
+ alsStore.logData ??= {};
124
+ alsStore.logData.flags ??= [];
125
+ if (!alsStore.logData.flags.includes(name)) alsStore.logData.flags.push(name);
126
+ this.asyncLocalStorage.enterWith(alsStore);
127
+ }
128
+ parsePayload(name, variant, payloadSchema) {
129
+ const { payload: variantPayload } = variant;
130
+ if (!variantPayload || !payloadSchema) return;
131
+ try {
132
+ const payload = this.toPayloadValue(variantPayload);
133
+ this.validator.validate({ payload }, { payload: payloadSchema });
134
+ return payload;
135
+ } catch (err) {
136
+ this.logger.warn("Invalid feature flag variant payload", {
137
+ name,
138
+ variant,
139
+ err
140
+ });
141
+ return;
69
142
  }
70
- return result;
71
143
  }
72
- isSomeEnabled(name, contexts) {
73
- return contexts.some((context) => this.isEnabled(name, context));
144
+ toPayloadValue({ type, value }) {
145
+ switch (type) {
146
+ case PayloadType.JSON: return JSON.parse(value);
147
+ case PayloadType.NUMBER: return Number(value);
148
+ case PayloadType.CSV: return value.split(",");
149
+ default: return value;
150
+ }
74
151
  }
75
- getDefinition(name) {
76
- return this.unleash?.getFeatureToggleDefinition(name);
152
+ resolveLanguage(alsStore) {
153
+ const { acceptLanguage } = alsStore.headers ?? {};
154
+ return Object.values(Languages).find((language) => language === acceptLanguage) ?? Languages.Uk;
155
+ }
156
+ localizePayload(locales, language) {
157
+ return locales[language] ?? locales[Languages.Uk];
77
158
  }
78
159
  parseAppVersion(appVersion) {
79
160
  if (!appVersion) return;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@diia-inhouse/features",
3
- "version": "3.2.193",
3
+ "version": "3.3.0",
4
4
  "description": "Features toggles",
5
5
  "license": "SEE LICENSE IN LICENSE.md",
6
6
  "author": "diia-team",
@@ -39,11 +39,13 @@
39
39
  "@commitlint/cli": "20.4.3",
40
40
  "@diia-inhouse/configs": "7.2.5",
41
41
  "@diia-inhouse/diia-logger": "4.7.49",
42
- "@diia-inhouse/env": "3.3.109",
42
+ "@diia-inhouse/env": "3.3.110",
43
43
  "@diia-inhouse/errors": "2.3.42",
44
+ "@diia-inhouse/i18n": "3.6.0",
44
45
  "@diia-inhouse/oxc-config": "6.1.3",
45
46
  "@diia-inhouse/test": "8.4.8",
46
47
  "@diia-inhouse/types": "14.12.3",
48
+ "@diia-inhouse/validators": "2.2.137",
47
49
  "@types/node": "25.6.2",
48
50
  "@types/semver": "7.7.1",
49
51
  "@vitest/coverage-v8": "4.1.11",
@@ -61,7 +63,14 @@
61
63
  },
62
64
  "peerDependencies": {
63
65
  "@diia-inhouse/env": ">=3.0.0",
64
- "@diia-inhouse/types": ">=13.1.0"
66
+ "@diia-inhouse/i18n": ">=3.6.0",
67
+ "@diia-inhouse/types": ">=13.1.0",
68
+ "@diia-inhouse/validators": ">=2.0.0"
69
+ },
70
+ "peerDependenciesMeta": {
71
+ "@diia-inhouse/i18n": {
72
+ "optional": true
73
+ }
65
74
  },
66
75
  "commitlint": {
67
76
  "extends": "@diia-inhouse/configs/dist/commitlint"