flagmint-js-sdk 1.2.23 โ 1.2.25
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 +25 -17
- package/README.md +77 -13
- package/dist/flagmint.cjs.js +8 -8
- package/dist/flagmint.es.js +1454 -1442
- package/dist/flagmint.umd.js +8 -8
- package/dist/sdk/core/client.d.ts +18 -2
- package/dist/sdk/core/helpers/logger.d.ts +4 -0
- package/dist/sdk/core/transports/WebsocketTransport.d.ts +1 -0
- package/package.json +2 -2
package/LICENSE
CHANGED
|
@@ -1,21 +1,29 @@
|
|
|
1
|
-
|
|
1
|
+
BSD 3-Clause License
|
|
2
2
|
|
|
3
|
-
Copyright (c)
|
|
3
|
+
Copyright (c) 2024, Flagmint
|
|
4
|
+
All rights reserved.
|
|
4
5
|
|
|
5
|
-
|
|
6
|
-
|
|
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:
|
|
6
|
+
Redistribution and use in source and binary forms, with or without
|
|
7
|
+
modification, are permitted provided that the following conditions are met:
|
|
11
8
|
|
|
12
|
-
|
|
13
|
-
|
|
9
|
+
1. Redistributions of source code must retain the above copyright notice, this
|
|
10
|
+
list of conditions and the following disclaimer.
|
|
14
11
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
12
|
+
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
13
|
+
this list of conditions and the following disclaimer in the documentation
|
|
14
|
+
and/or other materials provided with the distribution.
|
|
15
|
+
|
|
16
|
+
3. Neither the name of the copyright holder nor the names of its
|
|
17
|
+
contributors may be used to endorse or promote products derived from
|
|
18
|
+
this software without specific prior written permission.
|
|
19
|
+
|
|
20
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
21
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
22
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
23
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
24
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
25
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
26
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
27
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
28
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
29
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
package/README.md
CHANGED
|
@@ -1,9 +1,25 @@
|
|
|
1
1
|
# Flagmint JavaScript SDK
|
|
2
2
|
|
|
3
|
-
**Version:
|
|
3
|
+
**Version: v1.2.25**
|
|
4
4
|
|
|
5
5
|
This SDK provides a javascript framework-agnostic client for evaluating feature flags, with pluggable caching (sync or async) and transport strategies (WebSocket or long-polling). It's designed for both browser and server-side Node.js environments.
|
|
6
6
|
|
|
7
|
+
## ๐ Release Notes
|
|
8
|
+
|
|
9
|
+
### v1.2.25
|
|
10
|
+
- ๐ **Controllable debug logging** โ SDK logs are now silent by default; opt in with `logger.setup({ debugLog: true })`. In production, only connection/disconnection messages are ever emitted regardless of the setting.
|
|
11
|
+
- ๐ **Configurable endpoints** โ `restEndpoint` and `wsEndpoint` can now be passed directly in `FlagClientOptions`, overriding the environment-derived defaults. Useful for proxies, staging overrides, or self-hosted deployments.
|
|
12
|
+
- ๐ก **`subscribe()` delivers current flags immediately** โ the callback is now called synchronously with the current flag state at subscription time, so you never miss the initial value.
|
|
13
|
+
- ๐งน **`destroy()` clears all subscribers** โ calling `destroy()` now also clears the subscriber set, preventing stale callbacks from holding references after teardown.
|
|
14
|
+
- ๐ก๏ธ **WebSocket auth vs rate-limit error discrimination** โ close code `1008`/`4001` now inspects the close reason to distinguish `ERR_AUTH` (invalid API key) from `ERR_RATE_LIMITED` errors. Rate-limit errors include a `resetTime` field parsed from the close reason.
|
|
15
|
+
- ๐ **`initialFlagsReject` on WebSocket transport** โ a post-`onopen` close with a terminal code now correctly rejects the `waitForInitialFlags()` promise (and therefore `init()`), so the error surfaces reliably through `client.ready()` even when the connection opened before the server sent the close frame.
|
|
16
|
+
- ๐ **`onConnectionStateChanged()` exposed** โ consumers can now register a callback to observe WebSocket connection state transitions (`connecting`, `connected`, `disconnected`, `reconnecting`, `failed`).
|
|
17
|
+
|
|
18
|
+
### v1.2.24
|
|
19
|
+
- โจ Add `env` parameter to SDK configuration for explicit environment specification
|
|
20
|
+
- Allows users to pass environment as a variable when NODE_ENV or NEXT_PUBLIC_NODE_ENV cannot be reliably detected
|
|
21
|
+
- Improved environment resolution with fallback to auto-detection
|
|
22
|
+
|
|
7
23
|
## โจ Key Features
|
|
8
24
|
|
|
9
25
|
- ๐ฏ **Framework-Agnostic**: Works with React, Vue, vanilla JS, Node.js, and more
|
|
@@ -57,14 +73,38 @@ await client.updateContext({
|
|
|
57
73
|
| `apiKey` | `string` | **Required** | Your environment API key. |
|
|
58
74
|
| `context` | `Record<string, any>` | `{}` | Initial evaluation context (e.g. user attributes). |
|
|
59
75
|
| `enableOfflineCache` | `boolean` | `true` | Enable caching of flags locally. |
|
|
60
|
-
| `persistContext` | `boolean` | `false` | Persist evaluation context across sessions. |
|
|
76
|
+
| `persistContext` | `boolean` | `false` | Persist evaluation context across sessions. |
|
|
61
77
|
| `cacheAdapter` | `CacheAdapter<C>` | Sync `cacheHelper` | Custom cache implementation (see examples). |
|
|
62
78
|
| `transportMode` | `'auto' \| 'websocket' \| 'long-polling'` | `'auto'` | How to fetch flags: prefer WebSocket, or use long-polling transport. |
|
|
79
|
+
| `restEndpoint` | `string` | env-derived | Override the REST endpoint (useful for proxies or self-hosted setups).|
|
|
80
|
+
| `wsEndpoint` | `string` | env-derived | Override the WebSocket endpoint. |
|
|
63
81
|
| `onError` | `(error: Error) => void` | โ | Callback for transport or initialization errors. Called when operating in degraded mode with cached flags. |
|
|
64
82
|
| `previewMode` | `boolean` | `false` | Evaluate using `rawFlags` only, bypassing remote fetch. |
|
|
65
83
|
| `rawFlags` | `Record<string, FlagValue>` | โ | Local-only flag definitions used when `previewMode: true`. |
|
|
66
84
|
| `deferInitialization` | `boolean` | `false` | If `true`, client initialization is deferred until you call `ready()`. |
|
|
67
85
|
|
|
86
|
+
## ๐ชต Debug Logging
|
|
87
|
+
|
|
88
|
+
By default the SDK is **silent** โ no console output in any environment. To enable verbose logging during development, call `logger.setup()` before creating your client:
|
|
89
|
+
|
|
90
|
+
```ts
|
|
91
|
+
import { logger } from 'flagmint-js-sdk';
|
|
92
|
+
|
|
93
|
+
logger.setup({ debugLog: true });
|
|
94
|
+
|
|
95
|
+
const client = new FlagClient({ apiKey: '...' });
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
In **production** (`NODE_ENV=production`), only connection and disconnection messages are emitted regardless of the `debugLog` setting โ all other internal logs are suppressed.
|
|
99
|
+
|
|
100
|
+
```ts
|
|
101
|
+
// These messages always appear in production:
|
|
102
|
+
// "[WebSocketTransport] Connected"
|
|
103
|
+
// "[WebSocketTransport] Connection closed: 1000"
|
|
104
|
+
|
|
105
|
+
// All other internal logs require debugLog: true in non-production environments
|
|
106
|
+
```
|
|
107
|
+
|
|
68
108
|
## ๐ง Cache Adapters
|
|
69
109
|
|
|
70
110
|
### Sync (Browser / In-Memory)
|
|
@@ -111,6 +151,32 @@ const client = new FlagClient({
|
|
|
111
151
|
});
|
|
112
152
|
```
|
|
113
153
|
|
|
154
|
+
### Custom Endpoints
|
|
155
|
+
|
|
156
|
+
Override the default environment-derived endpoints โ useful for proxies, staging overrides, or self-hosted deployments:
|
|
157
|
+
|
|
158
|
+
```ts
|
|
159
|
+
const client = new FlagClient({
|
|
160
|
+
apiKey: '...',
|
|
161
|
+
restEndpoint: 'https://my-proxy.example.com/evaluator/evaluate',
|
|
162
|
+
wsEndpoint: 'wss://my-proxy.example.com/ws/sdk',
|
|
163
|
+
});
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
### WebSocket Connection State
|
|
167
|
+
|
|
168
|
+
Monitor connection state transitions by accessing the underlying WebSocket transport directly:
|
|
169
|
+
|
|
170
|
+
```ts
|
|
171
|
+
import { WebSocketTransport } from 'flagmint-js-sdk';
|
|
172
|
+
|
|
173
|
+
const ws = new WebSocketTransport(wsUrl, apiKey);
|
|
174
|
+
ws.onConnectionStateChanged((state) => {
|
|
175
|
+
// state: 'connecting' | 'connected' | 'disconnected' | 'reconnecting' | 'failed'
|
|
176
|
+
console.log('WS state:', state);
|
|
177
|
+
});
|
|
178
|
+
```
|
|
179
|
+
|
|
114
180
|
### Transport Performance
|
|
115
181
|
|
|
116
182
|
| Mode | Latency | Bandwidth | Best For |
|
|
@@ -335,7 +401,7 @@ The SDK gracefully handles connection failures using cached flags as a fallback:
|
|
|
335
401
|
|
|
336
402
|
| Scenario | `ready()` Promise | Behavior |
|
|
337
403
|
|----------|-------------------|----------|
|
|
338
|
-
| โ
Cached flags available | Resolves | Operates in degraded mode with cached flags |
|
|
404
|
+
| โ
Cached flags available | Resolves | Operates in degraded mode with cached flags; `onError` is called |
|
|
339
405
|
| โ No cached flags | Rejects | Initialization fails |
|
|
340
406
|
| โ
Connected successfully | Resolves | Normal operation with live updates |
|
|
341
407
|
|
|
@@ -428,17 +494,17 @@ const client = new FlagClient({
|
|
|
428
494
|
- `getFlag<K>(key: K, fallback?: T): T` - Get flag value with optional fallback
|
|
429
495
|
- `getFlags(): FeatureFlags<T>` - Get all flags as an object
|
|
430
496
|
- `updateContext(context: Record<string, any>): Promise<void>` - Update evaluation context (async)
|
|
431
|
-
- `subscribe(callback: (flags) => void): () => void` - Subscribe to flag updates, returns unsubscribe function
|
|
432
|
-
- `destroy(): void` - Close transport connection and
|
|
497
|
+
- `subscribe(callback: (flags) => void): () => void` - Subscribe to flag updates; fires immediately with current flags, returns unsubscribe function
|
|
498
|
+
- `destroy(): void` - Close transport connection, clear all subscribers, and release resources
|
|
433
499
|
|
|
434
500
|
### Subscribing to Flag Updates
|
|
435
501
|
|
|
436
|
-
Instead of event listeners, use the `subscribe()` method:
|
|
502
|
+
Instead of event listeners, use the `subscribe()` method. The callback is called **immediately** with the current flag state at subscription time, then again on every subsequent update:
|
|
437
503
|
|
|
438
504
|
```ts
|
|
439
505
|
const unsubscribe = client.subscribe((flags) => {
|
|
440
506
|
console.log('Flags updated:', flags);
|
|
441
|
-
//
|
|
507
|
+
// Also fires immediately with current flags โ no need to call getFlags() separately
|
|
442
508
|
});
|
|
443
509
|
|
|
444
510
|
// Later, to unsubscribe:
|
|
@@ -502,11 +568,9 @@ sdk/
|
|
|
502
568
|
|
|
503
569
|
**Important**: When `enableOfflineCache: true` (default):
|
|
504
570
|
- Cached flags are loaded even if the server is unreachable
|
|
505
|
-
- `ready()`
|
|
506
|
-
-
|
|
507
|
-
-
|
|
508
|
-
|
|
509
|
-
See the "Error Handling with Cache" section above for examples.
|
|
571
|
+
- `ready()` **resolves** (does not throw) if cached flags are available โ the `onError` callback is invoked instead to signal degraded mode
|
|
572
|
+
- `ready()` only rejects if **no cached flags exist** and the server is unreachable
|
|
573
|
+
- You can safely call `getFlag()` after `ready()` resolves, regardless of whether the connection succeeded
|
|
510
574
|
|
|
511
575
|
### Performance issues
|
|
512
576
|
|
|
@@ -534,6 +598,6 @@ See [GitHub Releases](https://github.com/flagmint/js-sdk/releases) for the compl
|
|
|
534
598
|
|
|
535
599
|
---
|
|
536
600
|
|
|
537
|
-
**License**:
|
|
601
|
+
**License**: BSD 3-Clause License ยฉ Flagmint Team
|
|
538
602
|
|
|
539
603
|
**Maintained with โค๏ธ by the Flagmint Team**
|