-
Notifications
You must be signed in to change notification settings - Fork 28
Adds Flagsmith adapter #103
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tiagoapolo
wants to merge
11
commits into
vercel:main
Choose a base branch
from
tiagoapolo:adapter-flagsmith
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+638
−0
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
9adab87
Adds Flagsmith adapter
tiagoapolo f2b0d05
adds tests
tiagoapolo e7293ee
fix README
tiagoapolo 73180a0
fix documentation
tiagoapolo 2789bd2
remove asdf file
tiagoapolo 62c9498
implements getFeature pattern
tiagoapolo 5f64db6
update README
tiagoapolo e9a6a30
Update packages/adapter-flagsmith/README.md
tiagoapolo 2aac8ec
address changes
tiagoapolo 3a4439b
implements provider
tiagoapolo da04118
adds different methods for different types of flags
tiagoapolo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
module.exports = { | ||
root: true, | ||
extends: [require.resolve('@pyra/eslint-config/components')], | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.vercel | ||
.env*.local |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# @flags-sdk/flagsmith | ||
|
||
## 0.1.0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# Flags SDK - Flagsmith Provider | ||
|
||
A provider adapter for the Flags SDK that integrates with [Flagsmith](https://flagsmith.com/), allowing you to use Flagsmith's feature flags and remote configuration in your application. | ||
|
||
## Installation | ||
|
||
```bash | ||
npm install @flags-sdk/flagsmith | ||
``` | ||
|
||
## Usage | ||
|
||
An Environment ID must be provided either using `FLAGSMITH_ENVIRONMENT_ID` environment variable or setting `environmentID` property in the initialization parameters | ||
|
||
```typescript | ||
import { flagsmithAdapter } from '@flags-sdk/flagsmith'; | ||
import { flag } from 'flags'; | ||
|
||
// Lazy Mode | ||
const myFeatureFlag = flag({ | ||
key: 'my-feature', | ||
adapter: flagsmithAdapter.getFeature(), | ||
}); | ||
|
||
// Custom initialization | ||
const myFeatureFlag = flag({ | ||
key: 'my-feature', | ||
adapter: flagsmithAdapter.getFeature({ | ||
key: 'other-feature', | ||
api: 'https://custom-api.com', | ||
environmentID: 'ABC', | ||
}), | ||
}); | ||
``` | ||
|
||
## Configuration | ||
|
||
The adapter accepts all standard Flagsmith configuration options: | ||
|
||
```typescript | ||
interface IInitConfig { | ||
environmentID: string; | ||
api?: string; | ||
cache?: { | ||
enabled?: boolean; | ||
ttl?: number; | ||
}; | ||
enableAnalytics?: boolean; | ||
enableLogs?: boolean; | ||
// ... see Flagsmith documentation for more options | ||
} | ||
``` | ||
|
||
## Features | ||
|
||
- Seamless integration with Flagsmith's feature flag system | ||
- Type-safe flag definitions | ||
- Automatic initialization of the Flagsmith client | ||
- Support for all Flagsmith configuration options | ||
- Proper handling of default values | ||
|
||
## License | ||
|
||
MIT |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
{ | ||
"name": "@flags-sdk/flagsmith", | ||
"version": "0.1.0", | ||
"description": "Flagsmith provider for the Flags SDK", | ||
"keywords": [ | ||
"flags-sdk", | ||
"flagsmith-nodejs", | ||
"vercel", | ||
"feature flags", | ||
"flags" | ||
], | ||
"homepage": "https://flags-sdk.dev", | ||
"bugs": { | ||
"url": "https://github.com/vercel/flags/issues" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/vercel/flags.git" | ||
}, | ||
"license": "MIT", | ||
"author": "", | ||
"sideEffects": false, | ||
"type": "module", | ||
"exports": { | ||
".": { | ||
"import": "./dist/index.js", | ||
"require": "./dist/index.cjs" | ||
} | ||
}, | ||
"main": "./dist/index.js", | ||
"typesVersions": { | ||
"*": { | ||
".": [ | ||
"dist/*.d.ts", | ||
"dist/*.d.cts" | ||
] | ||
} | ||
}, | ||
"files": [ | ||
"dist", | ||
"CHANGELOG.md" | ||
], | ||
"scripts": { | ||
"build": "rimraf dist && tsup", | ||
"dev": "tsup --watch --clean=false", | ||
"eslint": "eslint-runner", | ||
"eslint:fix": "eslint-runner --fix", | ||
"test": "vitest --run", | ||
"test:watch": "vitest", | ||
"type-check": "tsc --noEmit" | ||
}, | ||
"dependencies": { | ||
"flagsmith": "^9.0.5" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "20.11.17", | ||
"eslint-config-custom": "workspace:*", | ||
"flags": "workspace:*", | ||
"msw": "2.6.4", | ||
"rimraf": "6.0.1", | ||
"tsconfig": "workspace:*", | ||
"tsup": "8.0.1", | ||
"typescript": "5.6.3", | ||
"vite": "5.1.1", | ||
"vitest": "1.4.0" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,210 @@ | ||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; | ||
import { flagsmithAdapter } from '.'; | ||
import flagsmith, { IFlagsmithFeature, IState, IIdentity } from 'flagsmith'; | ||
|
||
// Mock the flagsmith module | ||
vi.mock('flagsmith', () => ({ | ||
default: { | ||
init: vi.fn(), | ||
getState: vi.fn(), | ||
identify: vi.fn(), | ||
initialised: false, | ||
}, | ||
})); | ||
|
||
describe('Flagsmith Adapter', () => { | ||
const mockHeaders = {} as any; | ||
const mockCookies = {} as any; | ||
const mockEnvironmentId = 'test-env-id'; | ||
|
||
beforeEach(() => { | ||
vi.resetAllMocks(); | ||
}); | ||
|
||
afterEach(() => { | ||
vi.mocked(flagsmith.init).mockClear(); | ||
vi.clearAllMocks(); | ||
}); | ||
|
||
describe('booleanValue', () => { | ||
it('should initialize the adapter', async () => { | ||
const adapter = flagsmithAdapter.booleanValue(); | ||
expect(adapter).toBeDefined(); | ||
expect(adapter.decide).toBeDefined(); | ||
}); | ||
|
||
it('should return flag enabled state for boolean values', async () => { | ||
const adapter = flagsmithAdapter.booleanValue(); | ||
|
||
vi.mocked(flagsmith.getState).mockReturnValue({ | ||
flags: { | ||
'test-flag': { | ||
enabled: true, | ||
value: 'some-value', | ||
}, | ||
}, | ||
api: 'https://api.flagsmith.com/api/v1/', | ||
} as IState<string>); | ||
|
||
const value = await adapter.decide({ | ||
key: 'test-flag', | ||
defaultValue: false, | ||
entities: undefined, | ||
headers: mockHeaders, | ||
cookies: mockCookies, | ||
}); | ||
|
||
expect(flagsmith.init).toHaveBeenCalledWith({ | ||
fetch: expect.any(Function), | ||
environmentID: mockEnvironmentId, | ||
}); | ||
expect(value).toBe(true); | ||
}); | ||
|
||
it('should return default value when flag is not found', async () => { | ||
const adapter = flagsmithAdapter.booleanValue(); | ||
|
||
vi.mocked(flagsmith.getState).mockReturnValue({ | ||
flags: {}, | ||
api: 'https://api.flagsmith.com/api/v1/', | ||
} as IState<string>); | ||
|
||
const value = await adapter.decide({ | ||
key: 'non-existent-flag', | ||
defaultValue: false, | ||
entities: undefined, | ||
headers: mockHeaders, | ||
cookies: mockCookies, | ||
}); | ||
|
||
expect(value).toBe(false); | ||
}); | ||
}); | ||
|
||
describe('stringValue', () => { | ||
it('should return string value when flag is enabled', async () => { | ||
const adapter = flagsmithAdapter.stringValue(); | ||
|
||
vi.mocked(flagsmith.getState).mockReturnValue({ | ||
flags: { | ||
'test-flag': { | ||
enabled: true, | ||
value: 'test-value', | ||
}, | ||
}, | ||
api: 'https://api.flagsmith.com/api/v1/', | ||
} as IState<string>); | ||
|
||
const value = await adapter.decide({ | ||
key: 'test-flag', | ||
defaultValue: 'default', | ||
entities: undefined, | ||
headers: mockHeaders, | ||
cookies: mockCookies, | ||
}); | ||
|
||
expect(value).toBe('test-value'); | ||
}); | ||
|
||
it('should return default value when flag is disabled', async () => { | ||
const adapter = flagsmithAdapter.stringValue(); | ||
|
||
vi.mocked(flagsmith.getState).mockReturnValue({ | ||
flags: { | ||
'test-flag': { | ||
enabled: false, | ||
value: 'test-value', | ||
}, | ||
}, | ||
api: 'https://api.flagsmith.com/api/v1/', | ||
} as IState<string>); | ||
|
||
const value = await adapter.decide({ | ||
key: 'test-flag', | ||
defaultValue: 'default', | ||
entities: undefined, | ||
headers: mockHeaders, | ||
cookies: mockCookies, | ||
}); | ||
|
||
expect(value).toBe('default'); | ||
}); | ||
}); | ||
|
||
describe('numberValue', () => { | ||
it('should return number value when flag is enabled', async () => { | ||
const adapter = flagsmithAdapter.numberValue(); | ||
|
||
vi.mocked(flagsmith.getState).mockReturnValue({ | ||
flags: { | ||
'test-flag': { | ||
enabled: true, | ||
value: 42, | ||
}, | ||
}, | ||
api: 'https://api.flagsmith.com/api/v1/', | ||
} as IState<string>); | ||
|
||
const value = await adapter.decide({ | ||
key: 'test-flag', | ||
defaultValue: 0, | ||
entities: undefined, | ||
headers: mockHeaders, | ||
cookies: mockCookies, | ||
}); | ||
|
||
expect(value).toBe(42); | ||
}); | ||
|
||
it('should return default value when flag is disabled', async () => { | ||
const adapter = flagsmithAdapter.numberValue(); | ||
|
||
vi.mocked(flagsmith.getState).mockReturnValue({ | ||
flags: { | ||
'test-flag': { | ||
enabled: false, | ||
value: 42, | ||
}, | ||
}, | ||
api: 'https://api.flagsmith.com/api/v1/', | ||
} as IState<string>); | ||
|
||
const value = await adapter.decide({ | ||
key: 'test-flag', | ||
defaultValue: 0, | ||
entities: undefined, | ||
headers: mockHeaders, | ||
cookies: mockCookies, | ||
}); | ||
|
||
expect(value).toBe(0); | ||
}); | ||
}); | ||
|
||
describe('identity handling', () => { | ||
it('should identify user when entities are provided', async () => { | ||
const adapter = flagsmithAdapter.booleanValue(); | ||
const identity: IIdentity = 'test-id'; | ||
|
||
vi.mocked(flagsmith.getState).mockReturnValue({ | ||
flags: { | ||
'test-flag': { | ||
enabled: true, | ||
value: 'test-value', | ||
}, | ||
}, | ||
api: 'https://api.flagsmith.com/api/v1/', | ||
} as IState<string>); | ||
|
||
await adapter.decide({ | ||
key: 'test-flag', | ||
defaultValue: false, | ||
entities: identity, | ||
headers: mockHeaders, | ||
cookies: mockCookies, | ||
}); | ||
|
||
expect(flagsmith.identify).toHaveBeenCalledWith(identity); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you work for Flagsmith? If we merge this PR we'd also put up some actual docs on flags-sdk.dev. Would you be wiling to link to our Flags SDK and those docs from https://docs.flagsmith.com?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @dferber90, just jumping in here since Tiago is on vacation - yes, Tiago works for Flagsmith.
In answer to your question, for sure, we'd be happy to link to your docs from https://docs.flagsmith.com - would you be happy with adding reciprocal backlink(s) too?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great! We'd add you to https://flags-sdk.dev/providers and create a docs page for FlagSmith