@flaggly/sdk 0.1.0 → 0.2.0

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 +106 -0
  2. package/package.json +3 -2
package/README.md ADDED
@@ -0,0 +1,106 @@
1
+ # @flaggly/sdk
2
+
3
+ Client SDK for [Flaggly](https://flaggly.dev) -- a self-hosted feature flag service on Cloudflare Workers.
4
+
5
+ ## Install
6
+
7
+ ```sh
8
+ pnpm i @flaggly/sdk
9
+ ```
10
+
11
+ ## Setup
12
+
13
+ Define your flag schema and create a client:
14
+
15
+ ```ts
16
+ import { Flaggly } from '@flaggly/sdk';
17
+
18
+ type Flags = {
19
+ 'dark-mode': { type: 'boolean' };
20
+ 'banner': { type: 'payload'; result: { text: string; color: string } };
21
+ 'checkout': { type: 'variant'; result: string };
22
+ };
23
+
24
+ const flaggly = new Flaggly<Flags>({
25
+ url: 'https://flaggly.example.workers.dev',
26
+ apiKey: 'YOUR_USER_JWT',
27
+ });
28
+ ```
29
+
30
+ Flags are evaluated immediately on creation. Pass `lazy: true` to defer until you call `identify()` or `fetchFlags()`.
31
+
32
+ ## Usage
33
+
34
+ ```ts
35
+ // Type-safe getters
36
+ const isDarkMode = flaggly.getBooleanFlag('dark-mode');
37
+ const banner = flaggly.getPayloadFlag('banner');
38
+ const checkout = flaggly.getVariantFlag('checkout');
39
+ ```
40
+
41
+ ### Identifying users
42
+
43
+ Call `identify()` when a user logs in. This re-evaluates all flags with the user context:
44
+
45
+ ```ts
46
+ await flaggly.identify(userId, { email: user.email, tier: user.tier });
47
+ ```
48
+
49
+ ### React
50
+
51
+ ```ts
52
+ import { Flaggly, FlagValue } from '@flaggly/sdk';
53
+ import { useSyncExternalStore } from 'react';
54
+
55
+ const flaggly = new Flaggly<Flags>({
56
+ url: 'https://flaggly.example.workers.dev',
57
+ apiKey: 'YOUR_USER_JWT',
58
+ lazy: true,
59
+ bootstrap: {
60
+ 'dark-mode': false,
61
+ },
62
+ });
63
+
64
+ export const useFlags = () =>
65
+ useSyncExternalStore(flaggly.store.subscribe, flaggly.store.get, flaggly.store.get);
66
+
67
+ export const useFlag = <K extends keyof Flags>(key: K): FlagValue<Flags[K]> => {
68
+ const data = useFlags();
69
+ return data?.[key].result as FlagValue<Flags[K]>;
70
+ };
71
+ ```
72
+
73
+ ### Cloudflare Workers (service binding)
74
+
75
+ ```ts
76
+ const flaggly = new Flaggly<Flags>({
77
+ url: 'https://flaggly.example.workers.dev',
78
+ apiKey: 'YOUR_USER_JWT',
79
+ lazy: true,
80
+ workerFetch: (url, init) => env.FLAGGLY_SERVICE.fetch(url, init),
81
+ });
82
+ ```
83
+
84
+ ## Options
85
+
86
+ | Option | Type | Default | Description |
87
+ |--------|------|---------|-------------|
88
+ | `url` | `string` | -- | Base URL of your Flaggly worker |
89
+ | `apiKey` | `string` | -- | Public user JWT |
90
+ | `app` | `string` | `"default"` | App identifier |
91
+ | `env` | `string` | `"production"` | Environment identifier |
92
+ | `lazy` | `boolean` | `false` | Defer flag evaluation until manual call |
93
+ | `bootstrap` | `Partial<FlagValues>` | -- | Default values before first fetch |
94
+ | `workerFetch` | `typeof fetch` | `fetch` | Custom fetch for service bindings |
95
+ | `getBackupId` | `() => string` | -- | Custom anonymous user ID generator |
96
+ | `customStorage` | `CustomStorage` | `localStorage` | Storage for backup IDs |
97
+ | `getCurrentRoute` | `() => string \| null` | `window.location.href` | Route provider for non-browser envs |
98
+ | `getRandomId` | `() => string` | `crypto.randomUUID` | UUID generator for non-browser envs |
99
+
100
+ ## Docs
101
+
102
+ Full documentation at [flaggly.dev](https://flaggly.dev).
103
+
104
+ ## License
105
+
106
+ MIT
package/package.json CHANGED
@@ -1,12 +1,13 @@
1
1
  {
2
2
  "name": "@flaggly/sdk",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "Client SDK for Flaggly",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
7
7
  "types": "dist/index.d.ts",
8
8
  "files": [
9
- "dist"
9
+ "dist",
10
+ "README.md"
10
11
  ],
11
12
  "keywords": [
12
13
  "feature-flags",