@swan-io/shared-business 15.1.2 → 15.2.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@swan-io/shared-business",
3
- "version": "15.1.2",
3
+ "version": "15.2.0",
4
4
  "engines": {
5
5
  "node": ">22.12.0"
6
6
  },
@@ -28,7 +28,7 @@
28
28
  "react": ">=19.0.0",
29
29
  "react-dom": ">=19.0.0",
30
30
  "react-native-web": ">=0.21.2",
31
- "@swan-io/lake": "15.1.2"
31
+ "@swan-io/lake": "15.2.0"
32
32
  },
33
33
  "dependencies": {
34
34
  "@formatjs/intl": "^4.1.11",
@@ -40,6 +40,7 @@
40
40
  "iban": "^0.0.14",
41
41
  "react-atomic-state": "^2.1.0",
42
42
  "react-dropzone": "^15.0.0",
43
+ "prism-react-renderer": "^2.4.1",
43
44
  "rifm": "^0.12.1",
44
45
  "ts-pattern": "^5.9.0",
45
46
  "uuid": "^14.0.0"
@@ -49,10 +50,10 @@
49
50
  "@types/react": "^19.2.14",
50
51
  "@types/react-dom": "^19.2.3",
51
52
  "@types/react-native": "^0.72.8",
52
- "react": "^19.2.6",
53
- "react-dom": "^19.2.6",
53
+ "react": "^19.2.7",
54
+ "react-dom": "^19.2.7",
54
55
  "react-native-web": "^0.21.2",
55
56
  "type-fest": "^5.6.0",
56
- "@swan-io/lake": "15.1.2"
57
+ "@swan-io/lake": "15.2.0"
57
58
  }
58
59
  }
@@ -0,0 +1,19 @@
1
+ import { Future, Result } from "@swan-io/boxed";
2
+ import { ReactNode } from "react";
3
+ export declare class FlagClient<Flags extends Record<string, unknown>, Context extends Record<string, string | number | boolean>> {
4
+ private readonly url;
5
+ private context;
6
+ private listeners;
7
+ constructor(url: string, initialContext?: Partial<Context>);
8
+ addListener(listener: (flags: Flags) => void): void;
9
+ removeListener(listener: (flags: Flags) => void): void;
10
+ setContext(context: Partial<Context>): void;
11
+ fetchFlags(): Future<Result<Flags, unknown>>;
12
+ }
13
+ export declare const createFlagsProvider: <Flags extends Record<string, unknown>, Context extends Record<string, string | number | boolean>>(client: FlagClient<Flags, Context>, flagDefaults: Flags) => {
14
+ FlagsProvider: ({ children, loader }: {
15
+ children: ReactNode;
16
+ loader?: ReactNode;
17
+ }) => string | number | bigint | boolean | Iterable<ReactNode> | Promise<string | number | bigint | boolean | import("react").ReactPortal | import("react").ReactElement<unknown, string | import("react").JSXElementConstructor<any>> | Iterable<ReactNode> | null | undefined> | import("react/jsx-runtime").JSX.Element | null;
18
+ useFlag: <K extends keyof Flags>(flagName: K) => Flags[K];
19
+ };
@@ -0,0 +1,71 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ import { Request } from "@swan-io/request";
3
+ import { createContext, useContext, useEffect, useState } from "react";
4
+ export class FlagClient {
5
+ constructor(url, initialContext = {}) {
6
+ this.listeners = [];
7
+ this.url = url;
8
+ this.context = initialContext;
9
+ }
10
+ addListener(listener) {
11
+ this.listeners.push(listener);
12
+ }
13
+ removeListener(listener) {
14
+ const index = this.listeners.indexOf(listener);
15
+ if (index !== -1) {
16
+ this.listeners.splice(index, 1);
17
+ }
18
+ }
19
+ setContext(context) {
20
+ this.context = {
21
+ ...this.context,
22
+ ...context,
23
+ };
24
+ // reload flags when context changes
25
+ this.fetchFlags();
26
+ }
27
+ fetchFlags() {
28
+ const queryParams = new URLSearchParams(this.context).toString();
29
+ return Request.make({
30
+ url: `${this.url}?${queryParams}`,
31
+ method: "GET",
32
+ type: "json",
33
+ })
34
+ .mapOk(response => response.response.getOr({}))
35
+ .tapOk(flags => {
36
+ this.listeners.forEach(listener => listener(flags));
37
+ });
38
+ }
39
+ }
40
+ export const createFlagsProvider = (client, flagDefaults) => {
41
+ const FlagsContext = createContext(null);
42
+ return {
43
+ FlagsProvider: ({ children, loader = null }) => {
44
+ const [flags, setFlags] = useState(null);
45
+ // biome-ignore lint/correctness/useExhaustiveDependencies: we need to run this effect only once, when the component is mounted
46
+ useEffect(() => {
47
+ const listener = (flags) => {
48
+ setFlags(flags);
49
+ };
50
+ client.addListener(listener);
51
+ const future = client.fetchFlags().tapError(() => {
52
+ // If fetching flags fails, we still want to provide the default flags to avoid breaking the app
53
+ setFlags(flagDefaults);
54
+ });
55
+ return () => {
56
+ client.removeListener(listener);
57
+ future.cancel();
58
+ };
59
+ }, []);
60
+ if (flags == null) {
61
+ return loader;
62
+ }
63
+ return _jsx(FlagsContext.Provider, { value: flags, children: children });
64
+ },
65
+ useFlag: (flagName) => {
66
+ var _a;
67
+ const flags = useContext(FlagsContext);
68
+ return (_a = flags === null || flags === void 0 ? void 0 : flags[flagName]) !== null && _a !== void 0 ? _a : flagDefaults[flagName];
69
+ },
70
+ };
71
+ };