@anterprize/fturex 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +226 -0
- package/dist/FtureXClient.d.ts +72 -0
- package/dist/FtureXClient.js +427 -0
- package/dist/angular/feature-toggle.directive.d.ts +32 -0
- package/dist/angular/feature-toggle.directive.js +77 -0
- package/dist/angular/feature-toggle.pipe.d.ts +20 -0
- package/dist/angular/feature-toggle.pipe.js +37 -0
- package/dist/angular/fturex.config.d.ts +7 -0
- package/dist/angular/fturex.config.js +2 -0
- package/dist/angular/fturex.module.d.ts +23 -0
- package/dist/angular/fturex.module.js +48 -0
- package/dist/angular/fturex.service.d.ts +31 -0
- package/dist/angular/fturex.service.js +56 -0
- package/dist/angular/index.d.ts +6 -0
- package/dist/angular/index.js +5 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/opentelemetry/FtureXOtelHook.d.ts +58 -0
- package/dist/opentelemetry/FtureXOtelHook.js +86 -0
- package/dist/opentelemetry/index.d.ts +2 -0
- package/dist/opentelemetry/index.js +1 -0
- package/dist/react/FeatureToggle.d.ts +14 -0
- package/dist/react/FeatureToggle.js +16 -0
- package/dist/react/FeatureToggleProvider.d.ts +15 -0
- package/dist/react/FeatureToggleProvider.js +17 -0
- package/dist/react/index.d.ts +3 -0
- package/dist/react/index.js +3 -0
- package/dist/react/useFeatureToggle.d.ts +27 -0
- package/dist/react/useFeatureToggle.js +104 -0
- package/dist/svelte/index.d.ts +2 -0
- package/dist/svelte/index.js +1 -0
- package/dist/svelte/useFeatureToggle.d.ts +54 -0
- package/dist/svelte/useFeatureToggle.js +85 -0
- package/dist/types.d.ts +122 -0
- package/dist/types.js +1 -0
- package/dist/vue/index.d.ts +1 -0
- package/dist/vue/index.js +2 -0
- package/dist/vue/useFeatureToggle.d.ts +32 -0
- package/dist/vue/useFeatureToggle.js +104 -0
- package/package.json +99 -0
- package/src/vue/FeatureToggle.vue +28 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
# @anterprize/fturex
|
|
2
|
+
|
|
3
|
+
Feature toggle client for React, Vue, Angular, and Svelte applications.
|
|
4
|
+
|
|
5
|
+
Fetches your full feature manifest in the background and evaluates flags locally — no per-call network round-trips. Supports context-based targeting (user, role, tenant, etc.), automatic background refresh, localStorage persistence, and usage statistics.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @anterprize/fturex
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Quick start
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
import { FtureXClient } from '@anterprize/fturex';
|
|
17
|
+
|
|
18
|
+
const client = new FtureXClient(
|
|
19
|
+
{ baseUrl: 'https://your-api.com', appKey: 'your-app-key' },
|
|
20
|
+
{ refreshIntervalSeconds: 30 }
|
|
21
|
+
);
|
|
22
|
+
|
|
23
|
+
await client.initialize();
|
|
24
|
+
|
|
25
|
+
if (await client.isEnabled('my-feature')) {
|
|
26
|
+
// feature is on
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// With context
|
|
30
|
+
if (await client.isEnabledWithContext('beta-feature', { role: 'admin', userId: 'user-123' })) {
|
|
31
|
+
// feature is on for this user
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
client.dispose();
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
---
|
|
38
|
+
|
|
39
|
+
## React
|
|
40
|
+
|
|
41
|
+
```tsx
|
|
42
|
+
import { FeatureToggleProvider, useFeatureToggle, FeatureToggle } from '@anterprize/fturex/react';
|
|
43
|
+
|
|
44
|
+
// Wrap your app
|
|
45
|
+
export function App() {
|
|
46
|
+
return (
|
|
47
|
+
<FeatureToggleProvider config={{ baseUrl: '...', appKey: '...' }}>
|
|
48
|
+
<MyApp />
|
|
49
|
+
</FeatureToggleProvider>
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// Hook
|
|
54
|
+
function MyComponent() {
|
|
55
|
+
const { isEnabled, loading } = useFeatureToggle('my-feature');
|
|
56
|
+
if (loading) return <Spinner />;
|
|
57
|
+
return isEnabled ? <NewUI /> : <OldUI />;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// Component
|
|
61
|
+
function MyOtherComponent() {
|
|
62
|
+
return (
|
|
63
|
+
<FeatureToggle featureName="my-feature">
|
|
64
|
+
<NewUI />
|
|
65
|
+
</FeatureToggle>
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
---
|
|
71
|
+
|
|
72
|
+
## Vue
|
|
73
|
+
|
|
74
|
+
```ts
|
|
75
|
+
// main.ts
|
|
76
|
+
import { createApp } from 'vue';
|
|
77
|
+
import { FtureXClient } from '@anterprize/fturex';
|
|
78
|
+
import { FeatureToggleClientKey } from '@anterprize/fturex/vue';
|
|
79
|
+
|
|
80
|
+
const client = new FtureXClient({ baseUrl: '...', appKey: '...' });
|
|
81
|
+
await client.initialize();
|
|
82
|
+
|
|
83
|
+
const app = createApp(App);
|
|
84
|
+
app.provide(FeatureToggleClientKey, client);
|
|
85
|
+
app.mount('#app');
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
```vue
|
|
89
|
+
<!-- MyComponent.vue -->
|
|
90
|
+
<script setup lang="ts">
|
|
91
|
+
import { useFeatureToggle } from '@anterprize/fturex/vue';
|
|
92
|
+
|
|
93
|
+
const { isEnabled, loading } = useFeatureToggle('my-feature');
|
|
94
|
+
</script>
|
|
95
|
+
|
|
96
|
+
<template>
|
|
97
|
+
<NewUI v-if="!loading && isEnabled" />
|
|
98
|
+
<OldUI v-else-if="!loading" />
|
|
99
|
+
</template>
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
SFC component (requires Vue SFC compiler):
|
|
103
|
+
|
|
104
|
+
```ts
|
|
105
|
+
import FeatureToggle from '@anterprize/fturex/vue/FeatureToggle.vue';
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
---
|
|
109
|
+
|
|
110
|
+
## Angular
|
|
111
|
+
|
|
112
|
+
```ts
|
|
113
|
+
// app.module.ts
|
|
114
|
+
import { FtureXModule } from '@anterprize/fturex/angular';
|
|
115
|
+
|
|
116
|
+
@NgModule({
|
|
117
|
+
imports: [
|
|
118
|
+
FtureXModule.forRoot({ baseUrl: '...', appKey: '...' })
|
|
119
|
+
]
|
|
120
|
+
})
|
|
121
|
+
export class AppModule {}
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
```ts
|
|
125
|
+
// my.component.ts
|
|
126
|
+
import { FtureXService } from '@anterprize/fturex/angular';
|
|
127
|
+
|
|
128
|
+
@Component({ ... })
|
|
129
|
+
export class MyComponent {
|
|
130
|
+
isEnabled = false;
|
|
131
|
+
|
|
132
|
+
constructor(private fturex: FtureXService) {}
|
|
133
|
+
|
|
134
|
+
async ngOnInit() {
|
|
135
|
+
this.isEnabled = await this.fturex.isEnabled('my-feature');
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
```html
|
|
141
|
+
<!-- Template directive -->
|
|
142
|
+
<div *featureToggle="'my-feature'">Only shown when feature is enabled</div>
|
|
143
|
+
|
|
144
|
+
<!-- Pipe -->
|
|
145
|
+
<div *ngIf="'my-feature' | featureToggle | async">...</div>
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
---
|
|
149
|
+
|
|
150
|
+
## Svelte
|
|
151
|
+
|
|
152
|
+
```ts
|
|
153
|
+
// fturex.ts
|
|
154
|
+
import { FtureXClient } from '@anterprize/fturex';
|
|
155
|
+
|
|
156
|
+
export const client = new FtureXClient({ baseUrl: '...', appKey: '...' });
|
|
157
|
+
await client.initialize();
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
```svelte
|
|
161
|
+
<!-- MyComponent.svelte -->
|
|
162
|
+
<script>
|
|
163
|
+
import { useFeatureToggle } from '@anterprize/fturex/svelte';
|
|
164
|
+
import { client } from './fturex';
|
|
165
|
+
|
|
166
|
+
const feature = useFeatureToggle(client, 'my-feature');
|
|
167
|
+
</script>
|
|
168
|
+
|
|
169
|
+
{#if $feature.loading}
|
|
170
|
+
<Spinner />
|
|
171
|
+
{:else if $feature.isEnabled}
|
|
172
|
+
<NewUI />
|
|
173
|
+
{:else}
|
|
174
|
+
<OldUI />
|
|
175
|
+
{/if}
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
---
|
|
179
|
+
|
|
180
|
+
## OpenTelemetry
|
|
181
|
+
|
|
182
|
+
```ts
|
|
183
|
+
import { FtureXClient } from '@anterprize/fturex';
|
|
184
|
+
import { FtureXOtelHook } from '@anterprize/fturex/opentelemetry';
|
|
185
|
+
|
|
186
|
+
const client = new FtureXClient({ baseUrl: '...', appKey: '...' });
|
|
187
|
+
client.addHook(new FtureXOtelHook());
|
|
188
|
+
await client.initialize();
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
---
|
|
192
|
+
|
|
193
|
+
## Configuration
|
|
194
|
+
|
|
195
|
+
### `FtureXConfiguration`
|
|
196
|
+
|
|
197
|
+
| Property | Type | Required | Description |
|
|
198
|
+
|---|---|---|---|
|
|
199
|
+
| `baseUrl` | `string` | yes | API base URL |
|
|
200
|
+
| `appKey` | `string` | yes | API authentication key |
|
|
201
|
+
| `sendStatistics` | `boolean` | no | Report usage statistics (default: `true`) |
|
|
202
|
+
|
|
203
|
+
### `FeatureCacheOptions`
|
|
204
|
+
|
|
205
|
+
| Property | Type | Default | Description |
|
|
206
|
+
|---|---|---|---|
|
|
207
|
+
| `refreshIntervalSeconds` | `number` | `30` | How often the manifest is refreshed |
|
|
208
|
+
| `enableLocalStoragePersistence` | `boolean` | `true` | Persist manifest to `localStorage` |
|
|
209
|
+
| `localStorageKey` | `string` | `feature-toggle-cache` | `localStorage` key |
|
|
210
|
+
|
|
211
|
+
---
|
|
212
|
+
|
|
213
|
+
## Events
|
|
214
|
+
|
|
215
|
+
```typescript
|
|
216
|
+
client.on('ready', () => console.log('Manifest loaded'));
|
|
217
|
+
client.on('update', () => console.log('Manifest refreshed'));
|
|
218
|
+
|
|
219
|
+
client.off('update', handler);
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
---
|
|
223
|
+
|
|
224
|
+
## License
|
|
225
|
+
|
|
226
|
+
MIT — [Anterprize](https://github.com/Anterprize)
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { FtureXConfiguration, FeatureCacheOptions, ContextProperties, FeatureStatistics, FtureXHook } from "./types.js";
|
|
2
|
+
/**
|
|
3
|
+
* FtureX Client
|
|
4
|
+
* Fetches the full feature manifest in the background and evaluates
|
|
5
|
+
* conditions locally — no per-call network round-trips.
|
|
6
|
+
*/
|
|
7
|
+
export declare class FtureXClient {
|
|
8
|
+
private config;
|
|
9
|
+
private cacheOptions;
|
|
10
|
+
private hooks;
|
|
11
|
+
private manifest;
|
|
12
|
+
private listeners;
|
|
13
|
+
private featureHitCounts;
|
|
14
|
+
private featureEnabledCounts;
|
|
15
|
+
private featureDisabledCounts;
|
|
16
|
+
private featureLastAccessed;
|
|
17
|
+
private refreshIntervalId?;
|
|
18
|
+
private statisticsIntervalId?;
|
|
19
|
+
constructor(config: FtureXConfiguration, cacheOptions?: FeatureCacheOptions);
|
|
20
|
+
/**
|
|
21
|
+
* Subscribe to an event.
|
|
22
|
+
* - 'ready' — fires once after the first successful manifest fetch
|
|
23
|
+
* - 'update' — fires after every subsequent manifest refresh
|
|
24
|
+
*/
|
|
25
|
+
on(event: "update" | "ready", callback: () => void): void;
|
|
26
|
+
/** Unsubscribe a previously registered callback. */
|
|
27
|
+
off(event: "update" | "ready", callback: () => void): void;
|
|
28
|
+
private emit;
|
|
29
|
+
/**
|
|
30
|
+
* Initialize the client — loads persisted manifest and starts background services
|
|
31
|
+
*/
|
|
32
|
+
initialize(): Promise<void>;
|
|
33
|
+
/**
|
|
34
|
+
* Stop all background services
|
|
35
|
+
*/
|
|
36
|
+
dispose(): void;
|
|
37
|
+
/**
|
|
38
|
+
* Register a hook that will be called for every flag evaluation.
|
|
39
|
+
*/
|
|
40
|
+
addHook(hook: FtureXHook): void;
|
|
41
|
+
/**
|
|
42
|
+
* Check if a feature is enabled (no context)
|
|
43
|
+
*/
|
|
44
|
+
isEnabled(featureName: string): Promise<boolean>;
|
|
45
|
+
/**
|
|
46
|
+
* Check if a feature is enabled with context properties evaluated locally
|
|
47
|
+
*/
|
|
48
|
+
isEnabledWithContext(featureName: string, context: ContextProperties): Promise<boolean>;
|
|
49
|
+
private determineReason;
|
|
50
|
+
private invokeHooksBefore;
|
|
51
|
+
private invokeHooksAfter;
|
|
52
|
+
private invokeHooksError;
|
|
53
|
+
private findEntry;
|
|
54
|
+
private evaluateEntry;
|
|
55
|
+
private evaluateConditions;
|
|
56
|
+
/** Returns true only when ALL rules in the segment match. */
|
|
57
|
+
private evaluateAndSegment;
|
|
58
|
+
private evaluateDirectValueCondition;
|
|
59
|
+
private evaluateValueListCondition;
|
|
60
|
+
private orderCommaSeparated;
|
|
61
|
+
private refreshManifest;
|
|
62
|
+
private fetchManifestFromApi;
|
|
63
|
+
private startBackgroundRefresh;
|
|
64
|
+
private startStatisticsReporting;
|
|
65
|
+
private trackFeatureHit;
|
|
66
|
+
private sendStatistics;
|
|
67
|
+
private resetStatistics;
|
|
68
|
+
getStatistics(): FeatureStatistics[];
|
|
69
|
+
private saveManifestToStorage;
|
|
70
|
+
private loadManifestFromStorage;
|
|
71
|
+
clearCache(): void;
|
|
72
|
+
}
|