@ogcio/building-blocks-sdk 0.0.10 → 0.0.11

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.
@@ -0,0 +1,51 @@
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
+ });
@@ -0,0 +1,42 @@
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 };
@@ -80,6 +80,14 @@
80
80
  "profile:entitlement:*"
81
81
  ],
82
82
  "updateDefinitions": true
83
+ },
84
+ {
85
+ "name": "featureFlags",
86
+ "openApiDefinitionUrl": "https://app.unleash-hosted.com/demo/docs/openapi.json",
87
+ "openApiDefinitionFormat": "json",
88
+ "citizenPermissions": [],
89
+ "publicServantPermissions": [],
90
+ "updateDefinitions": true
83
91
  }
84
92
  ]
85
93
  }
package/src/index.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { Analytics } from "@ogcio/analytics-sdk";
2
+ import FeatureFlags from "./client/clients/featureFlags/index.js";
2
3
 
3
4
  import Messaging from "./client/clients/messaging/index.js";
4
5
  import Payments from "./client/clients/payments/index.js";
@@ -43,6 +44,10 @@ const getBuildingBlockSDK = (
43
44
  ...services.upload,
44
45
  getTokenFn,
45
46
  }),
47
+ featureFlags: new FeatureFlags({
48
+ ...services.featureFlags,
49
+ getTokenFn,
50
+ }),
46
51
  };
47
52
  };
48
53
 
@@ -1,4 +1,5 @@
1
1
  import type { Analytics } from "@ogcio/analytics-sdk";
2
+ import type FeatureFlags from "../client/clients/featureFlags/index.js";
2
3
  import type Messaging from "../client/clients/messaging/index.js";
3
4
  import type Payments from "../client/clients/payments/index.js";
4
5
  import type Profile from "../client/clients/profile/index.js";
@@ -11,6 +12,7 @@ export const PAYMENTS = "payments" as const;
11
12
  export const PROFILE = "profile" as const;
12
13
  export const SCHEDULER = "scheduler" as const;
13
14
  export const UPLOAD = "upload" as const;
15
+ export const FEATURE_FLAGS = "featureFlags" as const;
14
16
 
15
17
  export type SERVICE_NAME =
16
18
  | typeof ANALYTICS
@@ -18,7 +20,8 @@ export type SERVICE_NAME =
18
20
  | typeof PAYMENTS
19
21
  | typeof PROFILE
20
22
  | typeof SCHEDULER
21
- | typeof UPLOAD;
23
+ | typeof UPLOAD
24
+ | typeof FEATURE_FLAGS;
22
25
 
23
26
  export type TokenFunction = (
24
27
  serviceName: SERVICE_NAME,
@@ -50,6 +53,7 @@ type ServiceClients = {
50
53
  profile: typeof Profile;
51
54
  scheduler: typeof Scheduler;
52
55
  upload: typeof Upload;
56
+ featureFlags: typeof FeatureFlags;
53
57
  };
54
58
 
55
59
  export type BuildingBlocksSDK = {
package/tap.yml ADDED
@@ -0,0 +1,14 @@
1
+ color: true
2
+ coverage-report:
3
+ - text
4
+ exclude:
5
+ - "**/@(fixture*(s)|dist)/**"
6
+ include:
7
+ - "**/@(test?(s)|__test?(s)__)/**/*.test.@(js|cjs|mjs|tap|cts|jsx|mts|ts|tsx)"
8
+ - "**/*.@(test?(s)|spec).@(js|cjs|mjs|tap|cts|jsx|mts|ts|tsx)"
9
+ - "**/test?(s).@(js|cjs|mjs|tap|cts|jsx|mts|ts|tsx)"
10
+ jobs: 1
11
+ reporter: base
12
+ snapshot-clean-cwd: true
13
+ timeout: 30
14
+ allow-incomplete-coverage: true
package/tsconfig.json CHANGED
@@ -1,5 +1,6 @@
1
1
  {
2
2
  "compilerOptions": {
3
+ "types": ["node", "vitest/globals"],
3
4
  "target": "ESNext",
4
5
  "strict": true,
5
6
  "esModuleInterop": true,
@@ -0,0 +1,9 @@
1
+ import { defineConfig } from "vitest/config";
2
+
3
+ export default defineConfig({
4
+ plugins: [],
5
+ test: {
6
+ include: ["**/*.test.ts"],
7
+ globals: true,
8
+ },
9
+ });