Skip to content

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
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/adapter-flagsmith/.eslintrc.cjs
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')],
};
2 changes: 2 additions & 0 deletions packages/adapter-flagsmith/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.vercel
.env*.local
3 changes: 3 additions & 0 deletions packages/adapter-flagsmith/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# @flags-sdk/flagsmith

## 0.1.0
64 changes: 64 additions & 0 deletions packages/adapter-flagsmith/README.md
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.
Copy link
Collaborator

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?

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?

Copy link
Collaborator

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


## 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
70 changes: 70 additions & 0 deletions packages/adapter-flagsmith/package.json
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"
}
}
210 changes: 210 additions & 0 deletions packages/adapter-flagsmith/src/index.test.ts
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);
});
});
});
Loading