@faststats/flags-sdk 0.0.1 → 0.12.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.
- package/README.md +76 -3
- package/package.json +39 -39
package/README.md
CHANGED
|
@@ -1,4 +1,77 @@
|
|
|
1
|
-
|
|
1
|
+
## FastStats Feature Flags for the Flags SDK
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
> [!IMPORTANT]
|
|
4
|
+
> This repository is in early development and not intended for production use.
|
|
5
|
+
|
|
6
|
+
An adapter that lets the [Flags SDK](https://flags-sdk.dev) resolve flags from
|
|
7
|
+
FastStats. Flags are evaluated on the server by the FastStats flags service, and
|
|
8
|
+
every evaluation shows up in the project's flag analytics.
|
|
9
|
+
|
|
10
|
+
```sh
|
|
11
|
+
npm install flags @faststats/flags-sdk
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
Set `FASTSTATS_FLAGS_TOKEN` to the project token (or an API key scoped to the
|
|
15
|
+
project with the `featureFlagEvaluation:evaluate` permission), then declare flags:
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
import { type FastStatsEntities, fastStatsAdapter } from "@faststats/flags-sdk";
|
|
19
|
+
import { dedupe, flag } from "flags/next";
|
|
20
|
+
|
|
21
|
+
const identify = dedupe(
|
|
22
|
+
async ({ cookies }): Promise<FastStatsEntities> => ({
|
|
23
|
+
identifier: cookies.get("visitor")?.value,
|
|
24
|
+
userId: cookies.get("user")?.value,
|
|
25
|
+
attributes: { plan: "pro" },
|
|
26
|
+
}),
|
|
27
|
+
);
|
|
28
|
+
|
|
29
|
+
export const newCheckout = flag<boolean, FastStatsEntities>({
|
|
30
|
+
key: "new-checkout",
|
|
31
|
+
adapter: fastStatsAdapter,
|
|
32
|
+
identify,
|
|
33
|
+
defaultValue: false,
|
|
34
|
+
});
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
`identify` must return an `identifier` (a stable device or install ID) or a
|
|
38
|
+
`userId` (your ID for a signed-in user). Rollouts bucket by `userId` when it is
|
|
39
|
+
set, so they follow a user across devices.
|
|
40
|
+
|
|
41
|
+
The visitor's `User-Agent` is forwarded so rules can match on `browser`, `os` and
|
|
42
|
+
`device`, and `country` is filled from `x-vercel-ip-country` or `cf-ipcountry`
|
|
43
|
+
unless `attributes` already sets it.
|
|
44
|
+
|
|
45
|
+
A flag falls back to its `defaultValue` when it does not exist, is archived, or
|
|
46
|
+
the service cannot be reached within `timeoutMs`.
|
|
47
|
+
|
|
48
|
+
### Evaluating several flags
|
|
49
|
+
|
|
50
|
+
`evaluate()` resolves flags that share an adapter and `identify` in one request:
|
|
51
|
+
|
|
52
|
+
```ts
|
|
53
|
+
import { evaluate } from "flags/next";
|
|
54
|
+
|
|
55
|
+
const [checkout, limit] = await evaluate([newCheckout, maxUploads]);
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
### Configuring the adapter
|
|
59
|
+
|
|
60
|
+
```ts
|
|
61
|
+
import { createFastStatsAdapter } from "@faststats/flags-sdk";
|
|
62
|
+
|
|
63
|
+
export const fastStatsAdapter = createFastStatsAdapter({
|
|
64
|
+
token: process.env.MY_TOKEN,
|
|
65
|
+
endpoint: "https://flags.example.com",
|
|
66
|
+
timeoutMs: 1000,
|
|
67
|
+
origin: (key) => `https://faststats.dev/project/my-project/feature-flags?flag=${key}`,
|
|
68
|
+
});
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
`token` and `endpoint` fall back to the env when left out. They are read on the
|
|
72
|
+
first evaluation, so a build does not need them.
|
|
73
|
+
|
|
74
|
+
| Variable | Default for |
|
|
75
|
+
| -------------------------- | ------------------------------------------------------ |
|
|
76
|
+
| `FASTSTATS_FLAGS_TOKEN` | `token`. Project token or API key. Required if unset. |
|
|
77
|
+
| `FASTSTATS_FLAGS_ENDPOINT` | `endpoint`. Base URL of the flags service, when self-run. |
|
package/package.json
CHANGED
|
@@ -1,41 +1,41 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
2
|
+
"name": "@faststats/flags-sdk",
|
|
3
|
+
"repository": {
|
|
4
|
+
"type": "git",
|
|
5
|
+
"url": "https://github.com/faststats-dev/faststats-javascript.git"
|
|
6
|
+
},
|
|
7
|
+
"version": "0.12.0",
|
|
8
|
+
"type": "module",
|
|
9
|
+
"main": "./dist/index.js",
|
|
10
|
+
"module": "./dist/index.js",
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"sideEffects": false,
|
|
13
|
+
"publishConfig": {
|
|
14
|
+
"access": "public"
|
|
15
|
+
},
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"types": "./dist/index.d.ts",
|
|
19
|
+
"import": "./dist/index.js",
|
|
20
|
+
"default": "./dist/index.js"
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
"files": [
|
|
24
|
+
"dist"
|
|
25
|
+
],
|
|
26
|
+
"scripts": {
|
|
27
|
+
"build": "tsdown",
|
|
28
|
+
"typecheck": "tsc --noEmit -p tsconfig.json",
|
|
29
|
+
"test": "bun test"
|
|
30
|
+
},
|
|
31
|
+
"peerDependencies": {
|
|
32
|
+
"flags": ">=4"
|
|
33
|
+
},
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"@types/bun": "^1.4.2",
|
|
36
|
+
"@types/node": "^26.6.4",
|
|
37
|
+
"flags": "^4.3.1",
|
|
38
|
+
"tsdown": "^0.23.0",
|
|
39
|
+
"typescript": "^7.0.2"
|
|
40
|
+
}
|
|
41
41
|
}
|