@flagwire/sdk-js 0.1.0 → 0.1.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.
Files changed (2) hide show
  1. package/README.md +62 -6
  2. package/package.json +11 -1
package/README.md CHANGED
@@ -1,19 +1,75 @@
1
1
  # `@flagwire/sdk-js`
2
2
 
3
- Zero-dependency browser SDK for FlagWire. Flag rules remain at the edge; the browser receives only
4
- evaluated values.
3
+ Small, framework-agnostic browser SDK for remotely evaluated FlagWire feature flags. Flag rules
4
+ stay at the edge; the browser receives evaluated values.
5
+
6
+ ## Install
7
+
8
+ ```sh
9
+ pnpm add @flagwire/sdk-js
10
+ ```
11
+
12
+ ## Usage
5
13
 
6
14
  ```ts
7
15
  import { createClient } from "@flagwire/sdk-js";
8
16
 
9
17
  const flags = createClient({
10
- clientKey: process.env.NEXT_PUBLIC_FLAGWIRE_CLIENT_KEY!,
11
- context: { key: "user-123", attributes: { plan: "pro" } },
18
+ clientKey: import.meta.env.VITE_FLAGWIRE_CLIENT_KEY,
19
+ context: {
20
+ key: "user-123",
21
+ attributes: { region: "eu-west", betaTester: true },
22
+ },
23
+ stream: true,
12
24
  });
13
25
 
14
26
  await flags.ready();
27
+
15
28
  const enabled = flags.get("checkout-redesign", false);
29
+ const unsubscribe = flags.on("update", (changedKeys) => {
30
+ if (changedKeys.includes("checkout-redesign")) render();
31
+ });
32
+
33
+ // At the application lifecycle boundary:
34
+ unsubscribe();
35
+ flags.close();
16
36
  ```
17
37
 
18
- Call `flags.close()` when the client is no longer needed. The SDK polls safely when streaming is
19
- unavailable and validates persisted warm-cache data before use.
38
+ Create the client in browser-only code. For SSR frameworks, create it after hydration or inside a
39
+ client component and keep a single instance for the application lifecycle.
40
+
41
+ ## Options
42
+
43
+ | Option | Required | Default | Description |
44
+ | ---------------- | -------- | --------------------------- | ------------------------------------------------------------------------------- |
45
+ | `clientKey` | yes | — | Public browser key beginning with `pk_live_` |
46
+ | `context` | yes | — | Evaluation key and optional string, number, boolean, or string-array attributes |
47
+ | `baseUrl` | no | `https://edge.flagwire.dev` | FlagWire edge endpoint |
48
+ | `bootstrap` | no | — | Validated snapshot used before the first network refresh |
49
+ | `pollIntervalMs` | no | `60000` | Background refresh interval |
50
+ | `stream` | no | `false` | Refresh promptly after version notifications |
51
+
52
+ ## Client API
53
+
54
+ - `ready()` resolves after the initial snapshot is available, or rejects if the initial request
55
+ fails and no bootstrap or valid warm cache exists.
56
+ - `get(key, defaultValue)` returns the evaluated value or the supplied default.
57
+ - `detail(key)` returns the value, variant, reason, and flag version when available.
58
+ - `setContext(context)` changes the evaluation context and refreshes the snapshot.
59
+ - `on("update", listener)` subscribes to changed flag keys and returns an unsubscribe function.
60
+ - `flush()` sends queued exposure events.
61
+ - `close()` stops polling and streaming, releases listeners, and attempts a final flush.
62
+
63
+ The SDK validates network and persisted snapshots before use. A valid warm cache can avoid a blank
64
+ startup, while stale or malformed data is ignored. Always provide a safe code default.
65
+
66
+ ## Key safety
67
+
68
+ `pk_live_` client keys are designed for browser use and only authorize remote evaluation. Never
69
+ put a `sk_live_` server key in frontend source, public environment variables, HTML, or client
70
+ bundles.
71
+
72
+ ## Type-safe flags
73
+
74
+ Run [`flagwire-typegen`](../../tools/typegen-cli) and import the generated declaration file once in
75
+ your application. `get()` then infers known keys and value types.
package/package.json CHANGED
@@ -1,8 +1,18 @@
1
1
  {
2
2
  "name": "@flagwire/sdk-js",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "Zero-dependency browser SDK for FlagWire feature flags",
5
+ "keywords": [
6
+ "feature-flags",
7
+ "browser",
8
+ "javascript",
9
+ "typescript"
10
+ ],
5
11
  "license": "MIT",
12
+ "homepage": "https://github.com/flagwire/flagwire-js/tree/main/packages/sdk-js#readme",
13
+ "bugs": {
14
+ "url": "https://github.com/flagwire/flagwire-js/issues"
15
+ },
6
16
  "type": "module",
7
17
  "files": [
8
18
  "dist"