@coframe/sdk 1.1.23 → 1.1.25

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.
@@ -48,6 +48,75 @@ async function fetchProjectFromEdge(projectId, currentUrl, signal, userContext,
48
48
  }
49
49
  }
50
50
 
51
+ async function requestFeatureFlags(options) {
52
+ var _a, _b;
53
+ const controller = new AbortController();
54
+ const timeout = setTimeout(() => controller.abort(), (_a = options.requestTimeoutMs) !== null && _a !== void 0 ? _a : 3000);
55
+ try {
56
+ const response = await fetch(`${(_b = options.edgeUrl) !== null && _b !== void 0 ? _b : "https://edge.cofra.me"}/api/v1/flags?v=1`, {
57
+ method: 'POST',
58
+ headers: {
59
+ 'Content-Type': 'application/json',
60
+ Authorization: `Bearer ${options.apiKey}`,
61
+ },
62
+ body: JSON.stringify({
63
+ projectId: options.projectId,
64
+ userToken: options.userToken,
65
+ ...(options.selector ? { selector: options.selector } : {}),
66
+ ...(options.currentUrl ? { currentUrl: options.currentUrl } : {}),
67
+ ...(options.languages ? { languages: options.languages } : {}),
68
+ ...(options.browserPlatform
69
+ ? { browserPlatform: options.browserPlatform }
70
+ : {}),
71
+ ...(options.userVisitCount !== undefined
72
+ ? { userVisitCount: options.userVisitCount }
73
+ : {}),
74
+ ...(options.userContext ? { userContext: options.userContext } : {}),
75
+ ...(options.country ? { country: options.country } : {}),
76
+ ...(options.device ? { device: options.device } : {}),
77
+ }),
78
+ signal: controller.signal,
79
+ });
80
+ if (!response.ok)
81
+ return undefined;
82
+ return (await response.json());
83
+ }
84
+ catch (_c) {
85
+ return undefined;
86
+ }
87
+ finally {
88
+ clearTimeout(timeout);
89
+ }
90
+ }
91
+ function evaluateFlags(options) {
92
+ return requestFeatureFlags(options);
93
+ }
94
+ async function getFeatureFlagResult(key, options) {
95
+ const response = await requestFeatureFlags({
96
+ ...options,
97
+ selector: { names: [key] },
98
+ });
99
+ return response === null || response === void 0 ? void 0 : response.flags[key];
100
+ }
101
+ async function getFeatureFlag(key, options) {
102
+ var _a;
103
+ const result = await getFeatureFlagResult(key, options);
104
+ if (!result)
105
+ return undefined;
106
+ return (_a = result.variant) !== null && _a !== void 0 ? _a : result.enabled;
107
+ }
108
+ async function isFeatureEnabled(key, options) {
109
+ var _a, _b;
110
+ return ((_b = (_a = (await getFeatureFlagResult(key, options))) === null || _a === void 0 ? void 0 : _a.enabled) !== null && _b !== void 0 ? _b : options.defaultValue);
111
+ }
112
+ async function getFeatureFlagPayload(key, options) {
113
+ var _a;
114
+ const result = await getFeatureFlagResult(key, options);
115
+ if (!result)
116
+ return undefined;
117
+ return 'payload' in result ? ((_a = result.payload) !== null && _a !== void 0 ? _a : null) : null;
118
+ }
119
+ const getRemoteConfig = getFeatureFlagPayload;
51
120
  function getRequestReferrer(headers) {
52
121
  const get = headers.get;
53
122
  if (typeof get === 'function') {
@@ -124,4 +193,10 @@ async function getProjectData(options) {
124
193
  return null;
125
194
  }
126
195
 
196
+ exports.evaluateFlags = evaluateFlags;
197
+ exports.getFeatureFlag = getFeatureFlag;
198
+ exports.getFeatureFlagPayload = getFeatureFlagPayload;
199
+ exports.getFeatureFlagResult = getFeatureFlagResult;
127
200
  exports.getProjectData = getProjectData;
201
+ exports.getRemoteConfig = getRemoteConfig;
202
+ exports.isFeatureEnabled = isFeatureEnabled;
@@ -46,6 +46,75 @@ async function fetchProjectFromEdge(projectId, currentUrl, signal, userContext,
46
46
  }
47
47
  }
48
48
 
49
+ async function requestFeatureFlags(options) {
50
+ var _a, _b;
51
+ const controller = new AbortController();
52
+ const timeout = setTimeout(() => controller.abort(), (_a = options.requestTimeoutMs) !== null && _a !== void 0 ? _a : 3000);
53
+ try {
54
+ const response = await fetch(`${(_b = options.edgeUrl) !== null && _b !== void 0 ? _b : "https://edge.cofra.me"}/api/v1/flags?v=1`, {
55
+ method: 'POST',
56
+ headers: {
57
+ 'Content-Type': 'application/json',
58
+ Authorization: `Bearer ${options.apiKey}`,
59
+ },
60
+ body: JSON.stringify({
61
+ projectId: options.projectId,
62
+ userToken: options.userToken,
63
+ ...(options.selector ? { selector: options.selector } : {}),
64
+ ...(options.currentUrl ? { currentUrl: options.currentUrl } : {}),
65
+ ...(options.languages ? { languages: options.languages } : {}),
66
+ ...(options.browserPlatform
67
+ ? { browserPlatform: options.browserPlatform }
68
+ : {}),
69
+ ...(options.userVisitCount !== undefined
70
+ ? { userVisitCount: options.userVisitCount }
71
+ : {}),
72
+ ...(options.userContext ? { userContext: options.userContext } : {}),
73
+ ...(options.country ? { country: options.country } : {}),
74
+ ...(options.device ? { device: options.device } : {}),
75
+ }),
76
+ signal: controller.signal,
77
+ });
78
+ if (!response.ok)
79
+ return undefined;
80
+ return (await response.json());
81
+ }
82
+ catch (_c) {
83
+ return undefined;
84
+ }
85
+ finally {
86
+ clearTimeout(timeout);
87
+ }
88
+ }
89
+ function evaluateFlags(options) {
90
+ return requestFeatureFlags(options);
91
+ }
92
+ async function getFeatureFlagResult(key, options) {
93
+ const response = await requestFeatureFlags({
94
+ ...options,
95
+ selector: { names: [key] },
96
+ });
97
+ return response === null || response === void 0 ? void 0 : response.flags[key];
98
+ }
99
+ async function getFeatureFlag(key, options) {
100
+ var _a;
101
+ const result = await getFeatureFlagResult(key, options);
102
+ if (!result)
103
+ return undefined;
104
+ return (_a = result.variant) !== null && _a !== void 0 ? _a : result.enabled;
105
+ }
106
+ async function isFeatureEnabled(key, options) {
107
+ var _a, _b;
108
+ return ((_b = (_a = (await getFeatureFlagResult(key, options))) === null || _a === void 0 ? void 0 : _a.enabled) !== null && _b !== void 0 ? _b : options.defaultValue);
109
+ }
110
+ async function getFeatureFlagPayload(key, options) {
111
+ var _a;
112
+ const result = await getFeatureFlagResult(key, options);
113
+ if (!result)
114
+ return undefined;
115
+ return 'payload' in result ? ((_a = result.payload) !== null && _a !== void 0 ? _a : null) : null;
116
+ }
117
+ const getRemoteConfig = getFeatureFlagPayload;
49
118
  function getRequestReferrer(headers) {
50
119
  const get = headers.get;
51
120
  if (typeof get === 'function') {
@@ -122,4 +191,4 @@ async function getProjectData(options) {
122
191
  return null;
123
192
  }
124
193
 
125
- export { getProjectData };
194
+ export { evaluateFlags, getFeatureFlag, getFeatureFlagPayload, getFeatureFlagResult, getProjectData, getRemoteConfig, isFeatureEnabled };
@@ -0,0 +1,75 @@
1
+ type JsonValue = string | number | boolean | null | JsonValue[] | {
2
+ [key: string]: JsonValue;
3
+ };
4
+ type FeatureFlagValue = boolean | string;
5
+ type FeatureFlagReason = 'inactive' | 'remote_config' | 'condition_match' | 'no_matching_condition' | 'missing_user_token';
6
+ interface FeatureFlagResult {
7
+ key: string;
8
+ enabled: boolean;
9
+ reason: FeatureFlagReason;
10
+ metadata: {
11
+ version: number;
12
+ };
13
+ variant?: string;
14
+ payload?: JsonValue;
15
+ }
16
+ interface FeatureFlagsResponse {
17
+ flags: Record<string, FeatureFlagResult>;
18
+ errorsWhileComputingFlags: boolean;
19
+ requestId: string;
20
+ snapshotVersion: number;
21
+ }
22
+ interface FeatureFlagSelector {
23
+ names?: string[];
24
+ groups?: string[];
25
+ }
26
+ interface FeatureFlagsConfig {
27
+ selector?: FeatureFlagSelector;
28
+ requestTimeoutMs?: number;
29
+ cacheTtlMs?: number;
30
+ }
31
+
32
+ type FeatureFlagsCallback = (flags: Record<string, FeatureFlagResult>, metadata: {
33
+ errorsWhileComputingFlags: boolean;
34
+ snapshotVersion: number;
35
+ }) => void;
36
+ declare function reloadFeatureFlags(): Promise<void>;
37
+ declare function onFeatureFlags(callback: FeatureFlagsCallback): () => void;
38
+ declare function getFeatureFlagResult(key: string): FeatureFlagResult | undefined;
39
+ declare function getFeatureFlag(key: string): FeatureFlagValue | undefined;
40
+ declare function isFeatureEnabled(key: string, options?: {
41
+ defaultValue?: boolean;
42
+ }): boolean | undefined;
43
+ declare function getFeatureFlagPayload(key: string): JsonValue | null | undefined;
44
+ declare const getRemoteConfig: typeof getFeatureFlagPayload;
45
+
46
+ interface FeatureFlagsInitOptions extends FeatureFlagsConfig {
47
+ projectId: string;
48
+ userToken?: string;
49
+ userContext?: Record<string, unknown>;
50
+ consent?: boolean;
51
+ reloadOnSpaNavigation?: boolean;
52
+ }
53
+ declare function initFeatureFlags(options: FeatureFlagsInitOptions): Promise<void>;
54
+ declare function setUserToken(userToken: string): void;
55
+ declare function reset(): string;
56
+ declare function setConsent(enabled: boolean): void;
57
+ declare function getUserToken(): string | undefined;
58
+
59
+ declare const featureFlags: {
60
+ init: typeof initFeatureFlags;
61
+ setUserToken: typeof setUserToken;
62
+ reset: typeof reset;
63
+ setConsent: typeof setConsent;
64
+ getUserToken: typeof getUserToken;
65
+ getFeatureFlag: typeof getFeatureFlag;
66
+ getFeatureFlagPayload: typeof getFeatureFlagPayload;
67
+ getFeatureFlagResult: typeof getFeatureFlagResult;
68
+ getRemoteConfig: typeof getFeatureFlagPayload;
69
+ isFeatureEnabled: typeof isFeatureEnabled;
70
+ onFeatureFlags: typeof onFeatureFlags;
71
+ reloadFeatureFlags: typeof reloadFeatureFlags;
72
+ };
73
+
74
+ export { featureFlags, getFeatureFlag, getFeatureFlagPayload, getFeatureFlagResult, getRemoteConfig, getUserToken, initFeatureFlags, isFeatureEnabled, onFeatureFlags, reloadFeatureFlags, reset, setConsent, setUserToken };
75
+ export type { FeatureFlagResult, FeatureFlagSelector, FeatureFlagValue, FeatureFlagsConfig, FeatureFlagsInitOptions, FeatureFlagsResponse, JsonValue };
@@ -1,6 +1,22 @@
1
1
  import React, { ReactNode } from 'react';
2
2
  export { init, onLog, onTreatmentApplied, setConsent, setFeatureFlag, setPageHydrated, setUserToken, trackConversion } from '@coframe/sdk';
3
3
 
4
+ type JsonValue = string | number | boolean | null | JsonValue[] | {
5
+ [key: string]: JsonValue;
6
+ };
7
+ type FeatureFlagValue = boolean | string;
8
+ type FeatureFlagReason = 'inactive' | 'remote_config' | 'condition_match' | 'no_matching_condition' | 'missing_user_token';
9
+ interface FeatureFlagResult {
10
+ key: string;
11
+ enabled: boolean;
12
+ reason: FeatureFlagReason;
13
+ metadata: {
14
+ version: number;
15
+ };
16
+ variant?: string;
17
+ payload?: JsonValue;
18
+ }
19
+
4
20
  interface UniversalSelector {
5
21
  t: string;
6
22
  fingerprint?: string;
@@ -312,6 +328,11 @@ declare function CoframeProvider({ projectConfig, projectData, onTreatmentApplie
312
328
  declare function useCoframeTreatments(): TreatmentData[] | null;
313
329
  declare function useCoframeTreatment(experimentId: string): TreatmentData | undefined;
314
330
  declare function useCoframeVariant(experimentId: string): string | undefined;
331
+ declare function useFeatureFlagResult(key: string): FeatureFlagResult | undefined;
332
+ declare function useFeatureFlagVariantKey(key: string): FeatureFlagValue | undefined;
333
+ declare const useFeatureFlag: typeof useFeatureFlagVariantKey;
334
+ declare function useFeatureFlagEnabled(key: string): boolean | undefined;
335
+ declare function useFeatureFlagPayload(key: string): JsonValue | null | undefined;
315
336
 
316
- export { CoframeProvider, useCoframeTreatment, useCoframeTreatments, useCoframeVariant };
337
+ export { CoframeProvider, useCoframeTreatment, useCoframeTreatments, useCoframeVariant, useFeatureFlag, useFeatureFlagEnabled, useFeatureFlagPayload, useFeatureFlagResult, useFeatureFlagVariantKey };
317
338
  export type { CoframeProviderProps };
@@ -217,6 +217,32 @@ type PersonalizationHeaders = {
217
217
  'x-bot-reason': BotReason;
218
218
  };
219
219
 
220
+ type JsonValue = string | number | boolean | null | JsonValue[] | {
221
+ [key: string]: JsonValue;
222
+ };
223
+ type FeatureFlagValue = boolean | string;
224
+ type FeatureFlagReason = 'inactive' | 'remote_config' | 'condition_match' | 'no_matching_condition' | 'missing_user_token';
225
+ interface FeatureFlagResult {
226
+ key: string;
227
+ enabled: boolean;
228
+ reason: FeatureFlagReason;
229
+ metadata: {
230
+ version: number;
231
+ };
232
+ variant?: string;
233
+ payload?: JsonValue;
234
+ }
235
+ interface FeatureFlagsResponse {
236
+ flags: Record<string, FeatureFlagResult>;
237
+ errorsWhileComputingFlags: boolean;
238
+ requestId: string;
239
+ snapshotVersion: number;
240
+ }
241
+ interface FeatureFlagSelector {
242
+ names?: string[];
243
+ groups?: string[];
244
+ }
245
+
220
246
  interface GetProjectDataOptions {
221
247
  projectId: string;
222
248
  currentUrl: string;
@@ -232,7 +258,30 @@ interface CoframeProjectPayload {
232
258
  personalizationHeaders?: PersonalizationHeaders;
233
259
  antiflickerCSS?: string;
234
260
  }
261
+ interface EvaluateFlagsOptions {
262
+ apiKey: string;
263
+ projectId: string;
264
+ userToken?: string;
265
+ selector?: FeatureFlagSelector;
266
+ requestTimeoutMs?: number;
267
+ edgeUrl?: string;
268
+ currentUrl?: string;
269
+ languages?: string[];
270
+ browserPlatform?: string;
271
+ userVisitCount?: number;
272
+ userContext?: Record<string, unknown>;
273
+ country?: string;
274
+ device?: 'desktop' | 'mobile';
275
+ }
276
+ declare function evaluateFlags(options: EvaluateFlagsOptions): Promise<FeatureFlagsResponse | undefined>;
277
+ declare function getFeatureFlagResult(key: string, options: Omit<EvaluateFlagsOptions, 'selector'>): Promise<FeatureFlagResult | undefined>;
278
+ declare function getFeatureFlag(key: string, options: Omit<EvaluateFlagsOptions, 'selector'>): Promise<FeatureFlagValue | undefined>;
279
+ declare function isFeatureEnabled(key: string, options: Omit<EvaluateFlagsOptions, 'selector'> & {
280
+ defaultValue?: boolean;
281
+ }): Promise<boolean | undefined>;
282
+ declare function getFeatureFlagPayload(key: string, options: Omit<EvaluateFlagsOptions, 'selector'>): Promise<JsonValue | null | undefined>;
283
+ declare const getRemoteConfig: typeof getFeatureFlagPayload;
235
284
  declare function getProjectData(options: GetProjectDataOptions): Promise<CoframeProjectPayload | null>;
236
285
 
237
- export { getProjectData };
238
- export type { CoframeProjectPayload, GetProjectDataOptions };
286
+ export { evaluateFlags, getFeatureFlag, getFeatureFlagPayload, getFeatureFlagResult, getProjectData, getRemoteConfig, isFeatureEnabled };
287
+ export type { CoframeProjectPayload, EvaluateFlagsOptions, GetProjectDataOptions };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@coframe/sdk",
3
- "version": "1.1.23",
3
+ "version": "1.1.25",
4
4
  "description": "Coframe optimizes your website using generative AI to deliver maximum impact to your audience and allow your site to improve itself as you sleep. Learn more at coframe.com.",
5
5
  "main": "dist/pkg/cf.cjs.js",
6
6
  "module": "dist/pkg/cf.esm.mjs",
@@ -16,6 +16,11 @@
16
16
  "require": "./dist/pkg/server.cjs.js",
17
17
  "types": "./dist/pkg/types/server.d.ts"
18
18
  },
19
+ "./feature-flags": {
20
+ "import": "./dist/pkg/feature-flags.esm.mjs",
21
+ "require": "./dist/pkg/feature-flags.cjs.js",
22
+ "types": "./dist/pkg/types/feature-flags.d.ts"
23
+ },
19
24
  "./react": {
20
25
  "import": "./dist/pkg/react.esm.mjs",
21
26
  "require": "./dist/pkg/react.cjs.js",
@@ -131,4 +136,4 @@
131
136
  "ua-parser-js": "^2.0.10"
132
137
  },
133
138
  "packageManager": "yarn@1.22.22+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e"
134
- }
139
+ }