1.0.15 • Published 6 months ago
@aiqfy/feature-sdk v1.0.15
AIQ Feature SDK
A TypeScript SDK for managing feature flags in your frontend applications.
Installation
npm install @aiq/feature-sdkBasic Setup
import { FeatureSDK } from '@aiq/feature-sdk';
const featureSDK = new FeatureSDK({
apiKey: 'your-api-key', // The SDK key generated from your backend
baseUrl: 'https://your-api-url',
projectId: 'your-project-id', // Required: The project ID this SDK belongs to
userId: 'optional-user-id',
environment: 'optional-environment'
});Getting Your SDK Key
- Create an SDK in your backend:
// Using the Go backend API
response, err := client.CreateSDKAPI(ctx, &pb.CreateSDKRequest{
Name: "My Frontend SDK",
ProjectId: "your-project-id"
})- Use the generated SDK key in your frontend:
const featureSDK = new FeatureSDK({
apiKey: response.sdk.key, // The key will be in format: "aiqfy-xxxxxxxxxxxxxxxx"
baseUrl: 'https://your-api-url',
projectId: response.sdk.projectId
});Checking Feature Flags
// Check if a feature is enabled
const isNewUIEnabled = await featureSDK.isEnabled('new-ui-feature');
// Get a feature variant
const variant = await featureSDK.getVariant('ab-test-feature');
// Get all feature flags for the current project
const allFlags = await featureSDK.getAllFlags();React Example
import React, { useEffect, useState } from 'react';
import { FeatureSDK } from '@aiq/feature-sdk';
const featureSDK = new FeatureSDK({
apiKey: process.env.REACT_APP_FEATURE_API_KEY,
baseUrl: process.env.REACT_APP_FEATURE_API_URL,
projectId: process.env.REACT_APP_FEATURE_PROJECT_ID
});
function MyComponent() {
const [isNewFeatureEnabled, setIsNewFeatureEnabled] = useState(false);
useEffect(() => {
async function checkFeature() {
const enabled = await featureSDK.isEnabled('new-feature');
setIsNewFeatureEnabled(enabled);
}
checkFeature();
}, []);
return (
<div>
{isNewFeatureEnabled ? (
<NewFeatureComponent />
) : (
<OldFeatureComponent />
)}
</div>
);
}Updating User Context
// Update the user ID for feature evaluation
featureSDK.setUserId('new-user-id');
// Update the environment
featureSDK.setEnvironment('production');API Reference
FeatureSDK
Constructor
new FeatureSDK(options: FeatureFlagOptions)Options:
apiKey: Your SDK key for authentication (generated from backend)baseUrl: The base URL of your feature flag serviceprojectId: The project ID this SDK belongs touserId: (Optional) The user ID for feature evaluationenvironment: (Optional) The environment for feature evaluation
Methods
isEnabled(featureId: string): Promise<boolean>- Checks if a feature flag is enabled
getVariant(featureId: string): Promise<string | undefined>- Gets the variant value of a feature flag
getAllFlags(): Promise<FeatureFlag[]>- Gets all feature flags for the current project
getFeatureFlag(featureId: string): Promise<FeatureFlag | undefined>- Gets a specific feature flag
setUserId(userId: string): void- Updates the user ID for feature evaluation
setEnvironment(environment: string): void- Updates the environment for feature evaluation
getProjectId(): string- Gets the current project ID
Features
- Project-scoped feature flags
- TypeScript support
- Automatic caching with configurable timeout
- User and environment context support
- Variant support for A/B testing
- Error handling
- Automatic cache invalidation when context changes