@ogcio/building-blocks-sdk 0.0.12 → 0.0.13

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ogcio/building-blocks-sdk",
3
- "version": "0.0.12",
3
+ "version": "0.0.13",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "type": "module",
@@ -28,13 +28,6 @@ t.test("FeatureFlags", async (t) => {
28
28
  featureFlags = new FeatureFlags({ baseUrl, getTokenFn });
29
29
  });
30
30
 
31
- t.test(
32
- "should initialize unleash client with correct parameters",
33
- async (t) => {
34
- t.ok(featureFlags.isConnected);
35
- },
36
- );
37
-
38
31
  t.test("should return false if flag is not enabled", async (t) => {
39
32
  isEnabled = false;
40
33
  const result = await featureFlags.isFlagEnabled("test-flag");
@@ -1,45 +1,48 @@
1
1
  import type createClient from "openapi-fetch";
2
- import {
3
- type Context,
4
- InMemStorageProvider,
5
- type Unleash,
6
- initialize,
7
- } from "unleash-client";
8
2
  import type { BaseApiClientParams } from "../../../types/index.js";
9
3
  import { FEATURE_FLAGS } from "../../../types/index.js";
10
4
  import BaseClient from "../../base-client.js";
11
5
  import { DEFAULT_PROJECT_ID } from "./const.js";
12
6
  import type { components, paths } from "./schema.js";
13
- import { waitForConnection } from "./utils.js";
14
7
 
15
8
  class FeatureFlags extends BaseClient<paths> {
16
9
  declare client: ReturnType<typeof createClient<paths>>;
17
10
  protected serviceName = FEATURE_FLAGS;
18
11
 
19
- private unleashClient: Unleash | null = null;
20
- public isConnected = false;
12
+ private unleashConnectionOptions: {
13
+ url: string | undefined;
14
+ token: string;
15
+ };
21
16
 
22
17
  constructor({ baseUrl, getTokenFn }: BaseApiClientParams) {
23
18
  super({ baseUrl, getTokenFn });
24
19
  const token = getTokenFn ? (getTokenFn(FEATURE_FLAGS) as string) : "";
25
- this.unleashClient = initialize({
26
- appName: this.serviceName,
27
- url: `${baseUrl}/api`,
28
- refreshInterval: 1000,
29
- customHeaders: {
30
- Authorization: token,
31
- },
32
- storageProvider: new InMemStorageProvider(),
33
- });
34
- this.unleashClient.on("error", console.error);
35
- this.unleashClient.on("synchronized", () => {
36
- this.isConnected = true;
37
- });
20
+ this.unleashConnectionOptions = { url: baseUrl, token };
38
21
  }
39
22
 
40
- async isFlagEnabled(name: string, context?: Context) {
41
- await this.waitForConnection();
42
- return this.unleashClient?.isEnabled(name, context, () => false) ?? false;
23
+ // biome-ignore lint/suspicious/noExplicitAny: We cannot import the types from the unleash-client package
24
+ async isFlagEnabled(name: string, context?: any) {
25
+ try {
26
+ const { InMemStorageProvider, startUnleash } = await import(
27
+ "unleash-client"
28
+ );
29
+ type Context = import("unleash-client").Context;
30
+
31
+ const client = await startUnleash({
32
+ appName: this.serviceName,
33
+ url: `${this.unleashConnectionOptions.url}/api`,
34
+ refreshInterval: 1000,
35
+ customHeaders: {
36
+ Authorization: this.unleashConnectionOptions.token,
37
+ },
38
+ storageProvider: new InMemStorageProvider(),
39
+ });
40
+ return client.isEnabled(name, context satisfies Context, () => false);
41
+ } catch {
42
+ throw new Error(
43
+ "unleash-client is not installed or not configured correctly",
44
+ );
45
+ }
43
46
  }
44
47
 
45
48
  async getFeatureFlags(projectId = DEFAULT_PROJECT_ID) {
@@ -64,11 +67,6 @@ class FeatureFlags extends BaseClient<paths> {
64
67
  (reason) => this.formatError(reason),
65
68
  );
66
69
  }
67
-
68
- private async waitForConnection(everyMs = 10) {
69
- return waitForConnection(this, everyMs);
70
- }
71
70
  }
72
71
 
73
72
  export default FeatureFlags;
74
- export type { Context as FeatureFlagsEvaluationContext };
@@ -1,51 +0,0 @@
1
- import t from "tap";
2
- import { FEATURE_FLAGS } from "../../../types/index.js";
3
- import type FeatureFlags from "./index.js";
4
- import { waitForConnection } from "./utils.js";
5
-
6
- t.test("waitForConnection", async (t) => {
7
- t.test("should resolve when featureFlags is connected", async (t) => {
8
- const mockFeatureFlags = {
9
- isConnected: false,
10
- } as FeatureFlags;
11
-
12
- // Simulate connection after 50ms
13
- setTimeout(() => {
14
- mockFeatureFlags.isConnected = true;
15
- }, 50);
16
-
17
- const consoles = t.capture(console, "log");
18
-
19
- const startTime = Date.now();
20
- await waitForConnection(mockFeatureFlags, 10);
21
- const elapsedTime = Date.now() - startTime;
22
-
23
- t.ok(elapsedTime >= 50);
24
- t.match(consoles(), [
25
- { args: [`[${FEATURE_FLAGS}] Connecting...`] },
26
- { args: [/Connected in \d+ms/] },
27
- ]);
28
-
29
- t.end();
30
- });
31
-
32
- t.test("should throw an error if connection times out", async (t) => {
33
- const mockFeatureFlags = {
34
- isConnected: false,
35
- } as FeatureFlags;
36
-
37
- const consoleLogSpy = t.capture(console, "log");
38
-
39
- const startTime = Date.now();
40
- await t.rejects(
41
- waitForConnection(mockFeatureFlags, 10, 30),
42
- `[${FEATURE_FLAGS}] Connection timed out after 30ms`,
43
- );
44
- const elapsedTime = Date.now() - startTime;
45
-
46
- t.ok(elapsedTime >= 30);
47
- t.match(consoleLogSpy(), [{ args: [`[${FEATURE_FLAGS}] Connecting...`] }]);
48
-
49
- t.end();
50
- });
51
- });
@@ -1,42 +0,0 @@
1
- import { FEATURE_FLAGS } from "../../../types/index.js";
2
- import type FeatureFlags from "./index.js";
3
-
4
- const waitForConnection = (
5
- featureFlags: FeatureFlags,
6
- everyMs = 10,
7
- timeoutMs = 5000, // Default timeout of 5 seconds
8
- ): Promise<void> => {
9
- const fn = (
10
- interval: NodeJS.Timeout,
11
- timeout: NodeJS.Timeout,
12
- resolve: () => void,
13
- startTime: number,
14
- ) => {
15
- if (featureFlags.isConnected) {
16
- const elapsedTime = Date.now() - startTime;
17
- console.log(`[${FEATURE_FLAGS}] Connected in ${elapsedTime}ms`);
18
- clearInterval(interval);
19
- clearTimeout(timeout);
20
- resolve();
21
- }
22
- };
23
-
24
- return new Promise((resolve, reject) => {
25
- const startTime = Date.now();
26
- console.log(`[${FEATURE_FLAGS}] Connecting...`);
27
- const interval = setInterval(() => {
28
- fn(interval, timeout, resolve, startTime);
29
- }, everyMs);
30
-
31
- const timeout = setTimeout(() => {
32
- clearInterval(interval);
33
- const message = `[${FEATURE_FLAGS}] Connection timed out after ${timeoutMs}ms`;
34
- console.error(message);
35
- reject(new Error(message));
36
- }, timeoutMs);
37
-
38
- fn(interval, timeout, resolve, startTime);
39
- });
40
- };
41
-
42
- export { waitForConnection };