@maksymshypytsia/flags-sdk 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 (3) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +159 -43
  3. package/package.json +11 -3
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 ShipaM
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 CHANGED
@@ -1,58 +1,174 @@
1
- # @maksymshypytsia/flags-sdk
2
-
3
- Tiny client for the [feature-flags-saas](https://github.com/ShipaM/feature-flags-saas)
4
- service. It makes **one** request, caches the result in memory and then decides
5
- locally and deterministically whether a flag is on for a user.
6
- No polling, no WebSocket.
7
-
8
- ## Install
9
-
10
- ```bash
11
- npm install @maksymshypytsia/flags-sdk
12
- ```
13
-
14
- ## Quick start
15
-
16
- ```ts
17
- import { getFlags } from "@maksymshypytsia/flags-sdk";
18
- const flags = await getFlags({
19
- apiKey: "ffk_prod_...", // dashboard -> Project -> API keys
20
- userKey: "user-42", // any stable id: user id, email, ...
21
- baseUrl: "https://flags.example.com", // where the service runs
22
- });
23
- if (flags.isEnabled("new-checkout")) {
24
- // show the new checkout
25
- }
26
- ```
1
+ # @maksymshypytsia/flags-sdk
27
2
 
28
- ## Development
3
+ [![npm version](https://img.shields.io/npm/v/@maksymshypytsia/flags-sdk.svg)](https://www.npmjs.com/package/@maksymshypytsia/flags-sdk)
4
+ [![license](https://img.shields.io/npm/l/@maksymshypytsia/flags-sdk.svg)](./LICENSE)
5
+ [![node](https://img.shields.io/node/v/@maksymshypytsia/flags-sdk.svg)](https://nodejs.org)
6
+ ![types](https://img.shields.io/npm/types/@maksymshypytsia/flags-sdk.svg)
7
+
8
+ Tiny JavaScript/TypeScript client for [feature-flags-saas](https://github.com/ShipaM/feature-flags-saas).
9
+
10
+ - **One request.** Flags are fetched once and cached in memory.
11
+ - **Local evaluation.** `isEnabled()` is synchronous: no network, no waiting.
12
+ - **Same answer as the server.** Uses the exact same rollout hash, so a user always lands in the same bucket.
13
+ - **Zero runtime dependencies**, ESM, written in TypeScript (types included).
14
+ - No polling, no WebSocket.
15
+
16
+ ## Installation
29
17
 
30
18
  ```bash
31
- npm install # install dev dependencies
32
- npm run typecheck # tsc --noEmit, type errors only
33
- npm test # vitest run
19
+ npm install @maksymshypytsia/flags-sdk
34
20
  ```
35
21
 
36
- ### Build and pack
22
+ <details>
23
+ <summary>pnpm / yarn / bun</summary>
37
24
 
38
25
  ```bash
39
- npm run build # tsc -> dist/ (old dist/ is deleted first)
40
- ls dist # hashRollout.d.ts hashRollout.js index.d.ts index.js
41
- npm pack --dry-run # list what goes into the package, creates nothing
42
- npm pack # create maksymshypytsia-flags-sdk-0.1.0.tgz
26
+ pnpm add @maksymshypytsia/flags-sdk
27
+ yarn add @maksymshypytsia/flags-sdk
28
+ bun add @maksymshypytsia/flags-sdk
29
+ ```
30
+
31
+ </details>
32
+
33
+ **Requirements:** Node.js 18+ (or any modern browser) because the SDK uses the built-in `fetch`.
34
+ The package is **ESM only**: use `import`, not `require`.
35
+
36
+ ## Quick start
37
+
38
+ ```ts
39
+ import { getFlags } from "@maksymshypytsia/flags-sdk";
40
+
41
+ const flags = await getFlags({
42
+ apiKey: process.env.FLAGS_API_KEY!, // dashboard -> Project -> API keys
43
+ userKey: "user-42", // any stable id: user id, email, ...
44
+ baseUrl: "https://flags.example.com", // where the service runs
45
+ });
46
+
47
+ if (flags.isEnabled("new-checkout")) {
48
+ // show the new checkout
49
+ }
50
+ ```
51
+
52
+ ## How to get the values
53
+
54
+ | Option | Where to get it |
55
+ | --------- | -------------------------------------------------------------------- |
56
+ | `apiKey` | Dashboard -> your project -> **API keys**. Looks like `ffk_prod_...` |
57
+ | `userKey` | Your own stable user identifier (id, email). Never random per visit |
58
+ | `baseUrl` | The address of your feature-flags-saas deployment, no `/api` suffix |
59
+
60
+ Each API key belongs to one environment (`dev`, `staging`, `prod`), so use a separate key per environment.
61
+
62
+ ## Usage
63
+
64
+ ### Server (Node.js, Next.js route, Express...)
65
+
66
+ ```ts
67
+ import { getFlags } from "@maksymshypytsia/flags-sdk";
68
+
69
+ export async function handler(userId: string) {
70
+ const flags = await getFlags({
71
+ apiKey: process.env.FLAGS_API_KEY!,
72
+ userKey: userId,
73
+ baseUrl: process.env.FLAGS_URL!,
74
+ });
75
+ return flags.isEnabled("new-checkout") ? "new" : "old";
76
+ }
77
+ ```
78
+
79
+ Calling `getFlags` for many users is cheap: the network request happens only
80
+ once per `baseUrl` + `apiKey`, every other call reuses the cached data
81
+ (parallel calls share the same request).
82
+
83
+ ### Refresh the flags
84
+
85
+ Flags are **not** refreshed automatically. When you want fresh data:
86
+
87
+ ```ts
88
+ import { clearCache, getFlags } from "@maksymshypytsia/flags-sdk";
89
+
90
+ clearCache(); // forget what we have
91
+ const flags = await getFlags({ apiKey, userKey, baseUrl }); // fetches again
43
92
  ```
44
93
 
45
- Only `dist/`, `README.md` and `package.json` end up in the package
46
- (see `"files"` in `package.json`). Test files are excluded from the build
47
- by `tsconfig.build.json`.
94
+ ### Error handling
48
95
 
49
- ### Try the package locally
96
+ ```ts
97
+ try {
98
+ const flags = await getFlags({ apiKey, userKey, baseUrl });
99
+ } catch (error) {
100
+ // wrong key (HTTP 401), server down, unexpected response...
101
+ // a failed request is never cached, the next call tries again
102
+ }
103
+ ```
104
+
105
+ A sensible pattern is to treat "could not load flags" as "all flags off".
106
+
107
+ ## API
108
+
109
+ ### `getFlags(options): Promise<FlagsClient>`
110
+
111
+ | Option | Type | Description |
112
+ | --------- | -------- | ------------------------------------------------------ |
113
+ | `apiKey` | `string` | Project API key. Sent as `Authorization: Bearer <key>` |
114
+ | `userKey` | `string` | Stable id of the end user |
115
+ | `baseUrl` | `string` | Service URL. A trailing `/` is ignored |
116
+
117
+ Throws `TypeError` if an option is empty, and `Error` if the request fails
118
+ or the response has an unexpected shape.
119
+
120
+ ### `client.isEnabled(flagKey): boolean`
121
+
122
+ `true` only if the flag exists, is enabled **and** the user falls inside the
123
+ flag's rollout percentage. Unknown or disabled flags return `false`.
124
+ Synchronous, makes no network requests, always gives the same answer for the same user and flag.
125
+
126
+ ### `clearCache(): void`
127
+
128
+ Forgets cached flags; the next `getFlags()` fetches again.
129
+
130
+ ### `isEnabledForUser(userKey, flagKey, rollout): boolean`
131
+
132
+ The raw rollout function the server uses. Handy for tests or for evaluating on your own server.
133
+
134
+ ## How it works
50
135
 
51
- Install the `.tgz` into any other project to check it before publishing:
136
+ 1. `getFlags` sends one `GET {baseUrl}/api/v1/flags` with your API key.
137
+ 2. The response (`{ flags: { key: { enabled, rollout } } }`) is cached in memory.
138
+ 3. `isEnabled` computes `FNV-1a(userKey + ":" + flagKey) % 100` and compares it with the `rollout` percentage.
139
+ The same input always gives the same bucket, so a user does not flip between old and new.
140
+
141
+ ## FAQ
142
+
143
+ **Can I use it in the browser?**
144
+ Yes, if the service allows your origin (CORS). Keep in mind that an API key in
145
+ public frontend code is visible to everyone, so do not use a key you consider secret.
146
+
147
+ **Why does `require()` fail?**
148
+ The package is ESM only. Use `import`, or `await import("@maksymshypytsia/flags-sdk")`.
149
+
150
+ **Why does a user get a different result than my colleague?**
151
+ Rollout depends on `userKey`. Make sure you pass the same stable value everywhere.
152
+
153
+ ## Development
52
154
 
53
155
  ```bash
54
- cd ../some-app
55
- npm install ../feature-flags-sdk/maksymshypytsia-flags-sdk-0.1.0.tgz
156
+ npm install
157
+ npm run typecheck # tsc --noEmit
158
+ npm test # vitest
159
+ npm run build # tsc -> dist/
160
+ npm pack --dry-run # see what goes into the package
56
161
  ```
57
162
 
58
- `*.tgz` and `dist/` are in `.gitignore`, so they are never committed.
163
+ Try an unpublished build in another project:
164
+
165
+ ```bash
166
+ npm pack
167
+ cd ../some-app && npm install ../feature-flags-sdk/maksymshypytsia-flags-sdk-0.1.0.tgz
168
+ ```
169
+
170
+ Release: `npm version patch && git push --follow-tags && npm publish`.
171
+
172
+ ## License
173
+
174
+ MIT
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@maksymshypytsia/flags-sdk",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "Tiny client SDK for feature-flags-saas: one fetch, local evaluation.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -32,7 +32,10 @@
32
32
  "keywords": [
33
33
  "feature-flags",
34
34
  "feature-toggles",
35
- "sdk"
35
+ "feature-flag",
36
+ "rollout",
37
+ "sdk",
38
+ "typescript"
36
39
  ],
37
40
  "license": "MIT",
38
41
  "repository": {
@@ -43,5 +46,10 @@
43
46
  "@types/node": "^22.20.5",
44
47
  "typescript": "^5",
45
48
  "vitest": "^5.0.5"
46
- }
49
+ },
50
+ "homepage": "https://github.com/ShipaM/feature-flags-sdk#readme",
51
+ "bugs": {
52
+ "url": "https://github.com/ShipaM/feature-flags-sdk/issues"
53
+ },
54
+ "author": "Maksym Shypytsia"
47
55
  }