Skip to content

Commit 26ae618

Browse files
authored
Merge pull request #322 from Flagsmith/chore/add-sentry-init-config
feat: Add sentry config to init
2 parents f57c9ed + ea3b359 commit 26ae618

File tree

5 files changed

+66
-20
lines changed

5 files changed

+66
-20
lines changed

flagsmith-core.ts

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
IFlagsmithResponse,
1010
IFlagsmithTrait,
1111
IInitConfig,
12+
ISentry,
1213
IState,
1314
ITraits,
1415
LoadingState,
@@ -167,7 +168,6 @@ const Flagsmith = class {
167168
console.error(e)
168169
}
169170
}
170-
171171
if (this.dtrum) {
172172
try {
173173
const traits: DynatraceObject = {
@@ -286,36 +286,38 @@ const Flagsmith = class {
286286
ticks: number|null= null
287287
timer: number|null= null
288288
dtrum= null
289+
sentryClient: ISentry | null = null
289290
withTraits?: ITraits|null= null
290291
cacheOptions = {ttl:0, skipAPI: false, loadStale: false, storageKey: undefined as string|undefined}
291292
async init(config: IInitConfig) {
292293
const evaluationContext = toEvaluationContext(config.evaluationContext || this.evaluationContext);
293294
try {
294295
const {
295-
environmentID,
296+
AsyncStorage: _AsyncStorage,
297+
_trigger,
298+
_triggerLoadingState,
299+
angularHttpClient,
296300
api = defaultAPI,
297-
headers,
298-
onChange,
301+
applicationMetadata,
299302
cacheFlags,
303+
cacheOptions,
300304
datadogRum,
301-
onError,
302305
defaultFlags,
303-
fetch: fetchImplementation,
304-
preventFetch,
305-
enableLogs,
306-
enableDynatrace,
307306
enableAnalytics,
308-
realtime,
307+
enableDynatrace,
308+
enableLogs,
309+
environmentID,
309310
eventSourceUrl= "https://realtime.flagsmith.com/",
310-
AsyncStorage: _AsyncStorage,
311+
fetch: fetchImplementation,
312+
headers,
311313
identity,
312-
traits,
314+
onChange,
315+
onError,
316+
preventFetch,
317+
realtime,
318+
sentryClient,
313319
state,
314-
cacheOptions,
315-
angularHttpClient,
316-
_trigger,
317-
_triggerLoadingState,
318-
applicationMetadata,
320+
traits,
319321
} = config;
320322
evaluationContext.environment = environmentID ? {apiKey: environmentID} : evaluationContext.environment;
321323
if (!evaluationContext.environment || !evaluationContext.environment.apiKey) {
@@ -396,6 +398,9 @@ const Flagsmith = class {
396398
}
397399
}
398400

401+
if(sentryClient) {
402+
this.sentryClient = sentryClient
403+
}
399404
if (angularHttpClient) {
400405
// @ts-expect-error
401406
_fetch = angularFetch(angularHttpClient);
@@ -759,6 +764,15 @@ const Flagsmith = class {
759764
if ((usingNewOptions && !options.skipAnalytics) || !options) {
760765
this.evaluateFlag(key, "ENABLED");
761766
}
767+
if(this.sentryClient) {
768+
try {
769+
this.sentryClient.getIntegrationByName(
770+
"FeatureFlags",
771+
)?.addFeatureFlag?.(key, res);
772+
} catch (e) {
773+
console.error(e)
774+
}
775+
}
762776

763777
return res;
764778
};

lib/flagsmith/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "flagsmith",
3-
"version": "9.2.2",
3+
"version": "9.3.0",
44
"description": "Feature flagging to support continuous development",
55
"main": "./index.js",
66
"module": "./index.mjs",

lib/react-native-flagsmith/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-flagsmith",
3-
"version": "9.2.2",
3+
"version": "9.3.0",
44
"description": "Feature flagging to support continuous development",
55
"main": "./index.js",
66
"repository": {

test/sentry.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { waitFor } from '@testing-library/react';
2+
import {defaultState, FLAGSMITH_KEY, getFlagsmith, getStateToCheck, identityState} from './test-constants';
3+
import { promises as fs } from 'fs';
4+
5+
describe('Flagsmith.init', () => {
6+
beforeEach(() => {
7+
// Avoid mocks, but if you need to add them here
8+
});
9+
test('should initialize with expected values', async () => {
10+
const onChange = jest.fn();
11+
const { flagsmith, initConfig, AsyncStorage, mockFetch } = getFlagsmith({ onChange });
12+
const addFeatureFlag = jest.fn();
13+
const integration = { addFeatureFlag };
14+
const client = {
15+
getIntegrationByName: jest.fn().mockReturnValue(integration),
16+
};
17+
await flagsmith.init({...initConfig, sentryClient: client});
18+
flagsmith.hasFeature("zero")
19+
flagsmith.hasFeature("hero")
20+
expect(addFeatureFlag).toHaveBeenCalledWith('zero', false);
21+
expect(addFeatureFlag).toHaveBeenCalledWith('hero', true);
22+
});
23+
});

types.d.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,14 @@ export declare type IDatadogRum = {
7676
}
7777
}
7878

79+
80+
export type ISentry = {
81+
getIntegrationByName(name:"FeatureFlags"): {
82+
addFeatureFlag(flag: string, enabled: boolean): void;
83+
} | undefined;
84+
} | undefined;
85+
86+
7987
export declare enum FlagSource {
8088
"NONE" = "NONE",
8189
"DEFAULT_FLAGS" = "DEFAULT_FLAGS",
@@ -104,6 +112,7 @@ export interface IInitConfig<F extends string | Record<string, any> = string, T
104112
cacheFlags?: boolean;
105113
cacheOptions?: ICacheOptions;
106114
datadogRum?: IDatadogRum;
115+
sentryClient?: ISentryClient;
107116
defaultFlags?: IFlags<FKey<F>>;
108117
fetch?: any;
109118
realtime?: boolean;
@@ -148,7 +157,7 @@ type FKey<F> = F extends string ? F : keyof F;
148157
type FValue<F, K extends FKey<F>> = F extends Record<string, any>
149158
? F[K] | null
150159
: IFlagsmithValue;
151-
160+
152161
/**
153162
* Example usage:
154163
*

0 commit comments

Comments
 (0)