Skip to content

Commit d480bb9

Browse files
fix: export FlagSource enum as runtime value (#349)
* fix: export FlagSource enum as runtime value Export FlagSource enum from all entry points to make it usable as both a type and runtime value in consumer code. Previously, FlagSource was only exported as a type declaration, which prevented users from comparing against enum values like FlagSource.NONE in their code. Users had to use workarounds with string literals and type assertions. Changes: - Export enum from flagsmith-core.ts - Re-export from types.d.ts referencing the real enum - Add FlagSource export to all entry points: - index.ts (web browser) - isomorphic.ts (SSR/universal) - index.react-native.ts (React Native) - next-middleware.ts (Next.js edge) Users can now use: import { FlagSource } from 'flagsmith'; if (loadingState.source === FlagSource.NONE) { ... } * tests: add test on flagsource export
1 parent 187feda commit d480bb9

File tree

7 files changed

+35
-7
lines changed

7 files changed

+35
-7
lines changed

flagsmith-core.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import { isTraitEvaluationContext, toEvaluationContext, toTraitEvaluationContext
2727
import { ensureTrailingSlash } from './utils/ensureTrailingSlash';
2828
import { SDK_VERSION } from './utils/version';
2929

30-
enum FlagSource {
30+
export enum FlagSource {
3131
"NONE" = "NONE",
3232
"DEFAULT_FLAGS" = "DEFAULT_FLAGS",
3333
"CACHE" = "CACHE",

index.react-native.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ export const createFlagsmithInstance = ()=>{
1414
eventSource: _EventSource
1515
})
1616
}
17+
export { FlagSource } from './flagsmith-core';

index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ export default flagsmith;
1919
export const createFlagsmithInstance = ():IFlagsmith=>{
2020
return core({ AsyncStorage, fetch:_fetch, eventSource:_EventSource})
2121
}
22+
export { FlagSource } from './flagsmith-core';

isomorphic.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,4 @@ export const createFlagsmithInstance = (): IFlagsmith => {
2424
eventSource: typeof window !=='undefined'?eventSource : null
2525
})
2626
}
27+
export { FlagSource } from './flagsmith-core';

next-middleware.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ export default flagsmith;
55
export const createFlagsmithInstance = ():IFlagsmith=>{
66
return core({})
77
}
8+
export { FlagSource } from './flagsmith-core';

test/flagsource-export.test.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { FlagSource } from '../index';
2+
import { FlagSource as FlagSourceIso } from '../isomorphic';
3+
4+
describe('FlagSource Export', () => {
5+
6+
test('should export FlagSource enum with all values', () => {
7+
expect(FlagSource).toBeDefined();
8+
expect(FlagSource.NONE).toBe('NONE');
9+
expect(FlagSource.DEFAULT_FLAGS).toBe('DEFAULT_FLAGS');
10+
expect(FlagSource.CACHE).toBe('CACHE');
11+
expect(FlagSource.SERVER).toBe('SERVER');
12+
});
13+
14+
test('should be usable in runtime value comparisons', () => {
15+
const source = 'NONE' as string;
16+
17+
expect(source === FlagSource.NONE).toBe(true);
18+
expect(source === FlagSource.CACHE).toBe(false);
19+
20+
const mySource: FlagSource = FlagSource.NONE;
21+
expect(mySource).toBe('NONE');
22+
});
23+
24+
test('should export FlagSource from all entry points', () => {
25+
expect(FlagSourceIso).toBeDefined();
26+
expect(FlagSourceIso.NONE).toBe('NONE');
27+
expect(FlagSource.NONE).toBe(FlagSourceIso.NONE);
28+
});
29+
});

types.d.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,7 @@ export type ISentryClient = {
8484
} | undefined;
8585

8686

87-
export declare enum FlagSource {
88-
"NONE" = "NONE",
89-
"DEFAULT_FLAGS" = "DEFAULT_FLAGS",
90-
"CACHE" = "CACHE",
91-
"SERVER" = "SERVER",
92-
}
87+
export { FlagSource } from './flagsmith-core';
9388

9489
export declare type LoadingState = {
9590
error: Error | null, // Current error, resets on next attempt to fetch flags

0 commit comments

Comments
 (0)