@dif.sh/openfeature 0.6.2
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 +211 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -0
- package/dist/provider.d.ts +44 -0
- package/dist/provider.d.ts.map +1 -0
- package/dist/provider.js +136 -0
- package/dist/provider.js.map +1 -0
- package/package.json +56 -0
package/README.md
ADDED
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
# @dif.sh/openfeature
|
|
2
|
+
|
|
3
|
+
[OpenFeature](https://openfeature.dev/) server provider for [dif.sh](https://www.dif.sh/).
|
|
4
|
+
|
|
5
|
+
It resolves `OpenFeature.getClient()` evaluations against the experiments that
|
|
6
|
+
`dif build` registers in `dif/generated/client.ts`. Every decision goes through
|
|
7
|
+
the same `assign()` function `@dif.sh/sdk` uses, so a user gets the same variant
|
|
8
|
+
from this provider as from `dif()`.
|
|
9
|
+
|
|
10
|
+
The typed `dif()` call in `@dif.sh/sdk` is still the main way to use dif. This
|
|
11
|
+
package is for codebases that already evaluate flags through OpenFeature. See
|
|
12
|
+
[When to use this vs `@dif.sh/sdk` directly](#when-to-use-this-vs-difshsdk-directly).
|
|
13
|
+
|
|
14
|
+
```sh
|
|
15
|
+
npm i @dif.sh/sdk @dif.sh/openfeature @openfeature/server-sdk
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
Peers: `@dif.sh/sdk` `^0.6.1` and `@openfeature/server-sdk` `^1.18.0`. Node
|
|
19
|
+
20.6+, ESM only.
|
|
20
|
+
|
|
21
|
+
## Usage
|
|
22
|
+
|
|
23
|
+
```ts
|
|
24
|
+
import "./dif/generated/client"; // side effect: registers every active experiment
|
|
25
|
+
import { OpenFeature } from "@openfeature/server-sdk";
|
|
26
|
+
import { DifOpenFeatureProvider } from "@dif.sh/openfeature";
|
|
27
|
+
|
|
28
|
+
OpenFeature.setProvider(new DifOpenFeatureProvider());
|
|
29
|
+
const client = OpenFeature.getClient();
|
|
30
|
+
|
|
31
|
+
const newCheckout = await client.getBooleanValue("new-checkout", false, {
|
|
32
|
+
targetingKey: user.id,
|
|
33
|
+
device_type: "mobile",
|
|
34
|
+
});
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
Import `dif/generated/client.ts` before the first evaluation. Without it the
|
|
38
|
+
registry is empty, so every call returns your default value with
|
|
39
|
+
`errorCode: FLAG_NOT_FOUND`. Run `dif build` after you add, rename, or conclude a
|
|
40
|
+
file in `dif/experiments/active/`.
|
|
41
|
+
|
|
42
|
+
The provider has no `initialize()` step because the specs are already in memory
|
|
43
|
+
once the generated client is imported. `setProvider` and `setProviderAndWait`
|
|
44
|
+
behave the same.
|
|
45
|
+
|
|
46
|
+
### Evaluation context
|
|
47
|
+
|
|
48
|
+
| Context field | Becomes |
|
|
49
|
+
| --- | --- |
|
|
50
|
+
| `targetingKey` | The dif user id, hashed with the experiment's salt to pick a bucket. Missing or `""` resolves to the first declared variant with no exposure. |
|
|
51
|
+
| Any other top-level string, number, boolean, or `null` | An entry in the attribute bag the experiment's `audience:` block is evaluated against (`device_type`, `plan`, …). |
|
|
52
|
+
| Objects, arrays, `Date`s | Skipped. `audience:` rules only compare scalars. |
|
|
53
|
+
|
|
54
|
+
### What each method returns
|
|
55
|
+
|
|
56
|
+
The first entry under `variants:` in the experiment's `.md` file is the control.
|
|
57
|
+
|
|
58
|
+
| Method | `value` | `variant` |
|
|
59
|
+
| --- | --- | --- |
|
|
60
|
+
| `getStringValue` / `getStringDetails` | The variant id, e.g. `"variant_a"` | The variant id |
|
|
61
|
+
| `getBooleanValue` / `getBooleanDetails` | `false` for the first declared variant, `true` for any other | The variant id |
|
|
62
|
+
| `getNumberValue` / `getObjectValue` | Your default value, with `errorCode: TYPE_MISMATCH` | none |
|
|
63
|
+
|
|
64
|
+
A flag declared as `variants: [{ id: "off" }, { id: "on" }]` gives `false` for
|
|
65
|
+
`off` and `true` for `on`. An experiment declared as `control` / `variant_a`
|
|
66
|
+
gives `false` for `control`. With three or more variants, every non-control
|
|
67
|
+
variant is `true`, so use `getStringValue` to tell them apart.
|
|
68
|
+
|
|
69
|
+
`reason` on the details object:
|
|
70
|
+
|
|
71
|
+
| `reason` | When |
|
|
72
|
+
| --- | --- |
|
|
73
|
+
| `SPLIT` | `targetingKey` was set, the `audience:` block matched, and the user was bucketed. `onExposure` runs. |
|
|
74
|
+
| `STATIC` | An `overrides` entry forced the variant. `onExposure` does not run. |
|
|
75
|
+
| `DEFAULT` | No `targetingKey`, the `audience:` block didn't match, or another experiment in the same `exclusion_group:` won. The value is the first declared variant. |
|
|
76
|
+
| `ERROR` | `FLAG_NOT_FOUND` (id not registered) or `TYPE_MISMATCH` (number or object evaluation). The value is your default. |
|
|
77
|
+
|
|
78
|
+
`flagMetadata` carries `surface` (the experiment's `surface:` field) and, on
|
|
79
|
+
`SPLIT` only, `bucket` (`0..9999`).
|
|
80
|
+
|
|
81
|
+
## Options
|
|
82
|
+
|
|
83
|
+
```ts
|
|
84
|
+
new DifOpenFeatureProvider({
|
|
85
|
+
overrides, // QA forces: experiment id → variant id
|
|
86
|
+
onExposure, // ({ flagKey, variant, bucket, userId }) => void
|
|
87
|
+
});
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
### `onExposure`
|
|
91
|
+
|
|
92
|
+
Called for every evaluation that resolves with reason `SPLIT`. It is not
|
|
93
|
+
deduplicated. The browser `dif()` call dedupes per `(experiment, user)`, but
|
|
94
|
+
this provider holds no per-user state, so one request that evaluates
|
|
95
|
+
`new-checkout` twice calls `onExposure` twice. Errors thrown inside it are
|
|
96
|
+
caught and ignored, and the evaluation result doesn't change.
|
|
97
|
+
|
|
98
|
+
The provider does not send exposures anywhere itself. `DifServer` in
|
|
99
|
+
`@dif.sh/sdk/server` sends metrics (`track()`), not exposures, so forward
|
|
100
|
+
`onExposure` to your own analytics pipeline or warehouse.
|
|
101
|
+
|
|
102
|
+
### `overrides`
|
|
103
|
+
|
|
104
|
+
A fixed map, or a function of the evaluation context. A forced evaluation
|
|
105
|
+
resolves with reason `STATIC` and never calls `onExposure`. An override that
|
|
106
|
+
names a variant the spec doesn't declare is ignored.
|
|
107
|
+
|
|
108
|
+
The browser SDK's `syncOverrides()` stores `?_dif=` links in a `_dif` cookie
|
|
109
|
+
using the same `id=variant,id=variant` format as `dif qa --preview-url`. To
|
|
110
|
+
honour that cookie on the server, put it on the context and parse it with
|
|
111
|
+
`parseOverrides` from `@dif.sh/sdk`:
|
|
112
|
+
|
|
113
|
+
```ts
|
|
114
|
+
import { parseOverrides } from "@dif.sh/sdk";
|
|
115
|
+
|
|
116
|
+
new DifOpenFeatureProvider({
|
|
117
|
+
overrides: (ctx) =>
|
|
118
|
+
typeof ctx.dif_preview === "string"
|
|
119
|
+
? (parseOverrides(ctx.dif_preview) ?? undefined)
|
|
120
|
+
: undefined,
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
// at the call site
|
|
124
|
+
await client.getStringValue("checkout-cta-v2", "control", {
|
|
125
|
+
targetingKey: user.id,
|
|
126
|
+
dif_preview: cookies.get("_dif"),
|
|
127
|
+
});
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
Return `undefined` from the function to turn forcing off, for example in
|
|
131
|
+
production.
|
|
132
|
+
|
|
133
|
+
## Next.js (App Router)
|
|
134
|
+
|
|
135
|
+
**`lib/flags.ts`**: import the generated client and set the provider once per
|
|
136
|
+
server process.
|
|
137
|
+
|
|
138
|
+
```ts
|
|
139
|
+
import "../dif/generated/client";
|
|
140
|
+
import { OpenFeature } from "@openfeature/server-sdk";
|
|
141
|
+
import { DifOpenFeatureProvider } from "@dif.sh/openfeature";
|
|
142
|
+
import { logExposure } from "./analytics"; // your pipeline
|
|
143
|
+
|
|
144
|
+
OpenFeature.setProvider(
|
|
145
|
+
new DifOpenFeatureProvider({
|
|
146
|
+
onExposure: ({ flagKey, variant, bucket, userId }) =>
|
|
147
|
+
logExposure({ experiment: flagKey, variant, bucket, user_id: userId }),
|
|
148
|
+
}),
|
|
149
|
+
);
|
|
150
|
+
|
|
151
|
+
export const flags = OpenFeature.getClient();
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
**`app/checkout/page.tsx`**: evaluate in a server component.
|
|
155
|
+
|
|
156
|
+
```tsx
|
|
157
|
+
import { headers } from "next/headers";
|
|
158
|
+
import { flags } from "@/lib/flags";
|
|
159
|
+
import { getSession } from "@/lib/auth"; // your auth
|
|
160
|
+
import { LegacyCheckout, NewCheckout } from "./checkout";
|
|
161
|
+
|
|
162
|
+
export default async function CheckoutPage() {
|
|
163
|
+
const session = await getSession();
|
|
164
|
+
const ua = (await headers()).get("user-agent") ?? "";
|
|
165
|
+
|
|
166
|
+
const newCheckout = await flags.getBooleanValue("new-checkout", false, {
|
|
167
|
+
targetingKey: session?.user.id,
|
|
168
|
+
device_type: /Mobi/i.test(ua) ? "mobile" : "desktop",
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
return newCheckout ? <NewCheckout /> : <LegacyCheckout />;
|
|
172
|
+
}
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
## When to use this vs `@dif.sh/sdk` directly
|
|
176
|
+
|
|
177
|
+
Use `dif()` from `@dif.sh/sdk` if you can. It checks more at build time and does
|
|
178
|
+
more at runtime:
|
|
179
|
+
|
|
180
|
+
- **Typed branches.** `dif("new-checkout", { off: () => …, on: () => … })` takes
|
|
181
|
+
its branch keys from the experiment's `variants:`, so a renamed variant is a
|
|
182
|
+
type error. In `client.getBooleanValue("new-checkout", false)`, the flag key is
|
|
183
|
+
a plain string that TypeScript can't check.
|
|
184
|
+
- **Orphan checks.** `dif validate` warns with `W001` when a `dif("<id>", …)` call
|
|
185
|
+
site names an experiment that isn't active. It only matches `dif("…")` calls,
|
|
186
|
+
so it doesn't see OpenFeature `get*Value` calls.
|
|
187
|
+
- **Exposure delivery.** In the browser, `dif()` dedupes exposures and delivers
|
|
188
|
+
them through the `events` config `dif build` writes to
|
|
189
|
+
`dif/generated/events.ts`. With this provider you get `onExposure` and deliver
|
|
190
|
+
the event yourself.
|
|
191
|
+
- **Framework adapters.** `@dif.sh/react` and `@dif.sh/svelte` assign on the
|
|
192
|
+
server and reuse that decision on the client, so the variant doesn't change
|
|
193
|
+
after hydration.
|
|
194
|
+
|
|
195
|
+
Use `@dif.sh/openfeature` when:
|
|
196
|
+
|
|
197
|
+
- your services already call `OpenFeature.getClient()`, and you're moving flags
|
|
198
|
+
into `dif/experiments/active/*.md` without rewriting those call sites;
|
|
199
|
+
- dif is one of several providers behind OpenFeature, for example alongside
|
|
200
|
+
another vendor during a migration;
|
|
201
|
+
- you already rely on OpenFeature hooks (logging, OpenTelemetry) for evaluation
|
|
202
|
+
telemetry.
|
|
203
|
+
|
|
204
|
+
Both read the same registry, so they can run side by side. A `dif()` call and a
|
|
205
|
+
`getStringValue` call for the same id and user return the same variant.
|
|
206
|
+
|
|
207
|
+
## Docs
|
|
208
|
+
|
|
209
|
+
- SDK and generated client: <https://www.dif.sh/docs/sdk/>
|
|
210
|
+
- Experiment file format: <https://www.dif.sh/docs/format/>
|
|
211
|
+
- CLI (`dif build`, `dif validate`, `dif qa`): <https://www.dif.sh/docs/cli/>
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,sBAAsB,EAAE,MAAM,eAAe,CAAC;AACvD,YAAY,EAAE,6BAA6B,EAAE,WAAW,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,sBAAsB,EAAE,MAAM,eAAe,CAAC"}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { type EvaluationContext, type JsonValue, type Provider, type ResolutionDetails } from "@openfeature/server-sdk";
|
|
2
|
+
/** Payload passed to {@link DifOpenFeatureProviderOptions.onExposure}. */
|
|
3
|
+
export interface DifExposure {
|
|
4
|
+
/** Experiment id (the OpenFeature flag key). */
|
|
5
|
+
flagKey: string;
|
|
6
|
+
/** Variant id the user was bucketed into. */
|
|
7
|
+
variant: string;
|
|
8
|
+
/** Bucket `0..9999`. */
|
|
9
|
+
bucket: number;
|
|
10
|
+
/** The evaluation context's `targetingKey`. */
|
|
11
|
+
userId: string;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* QA/preview forces (experiment id → variant id). Either a fixed map or a
|
|
15
|
+
* function of the evaluation context, e.g. to read a `_dif` value you put on
|
|
16
|
+
* the context from a request cookie.
|
|
17
|
+
*/
|
|
18
|
+
export type DifOverrides = Record<string, string> | ((context: EvaluationContext) => Record<string, string> | undefined);
|
|
19
|
+
export interface DifOpenFeatureProviderOptions {
|
|
20
|
+
/** QA/preview forces. A valid force resolves with reason `STATIC` and never
|
|
21
|
+
* calls `onExposure`. A variant id the spec doesn't declare is ignored. */
|
|
22
|
+
overrides?: DifOverrides;
|
|
23
|
+
/** Called when an evaluation bucketed a real user into a variant (reason
|
|
24
|
+
* `SPLIT`). Called on every such evaluation — not deduped. Errors thrown
|
|
25
|
+
* here are swallowed so they never change an evaluation result. */
|
|
26
|
+
onExposure?: (exposure: DifExposure) => void;
|
|
27
|
+
}
|
|
28
|
+
export declare class DifOpenFeatureProvider implements Provider {
|
|
29
|
+
#private;
|
|
30
|
+
readonly metadata: {
|
|
31
|
+
readonly name: "dif.sh";
|
|
32
|
+
};
|
|
33
|
+
readonly runsOn = "server";
|
|
34
|
+
constructor(options?: DifOpenFeatureProviderOptions);
|
|
35
|
+
/** `value` and `variant` are the assigned variant id. */
|
|
36
|
+
resolveStringEvaluation(flagKey: string, defaultValue: string, context?: EvaluationContext): Promise<ResolutionDetails<string>>;
|
|
37
|
+
/** `value` is `true` for any variant other than the first declared one
|
|
38
|
+
* (the control), so `off`/`on` and `control`/`variant_a` both map to
|
|
39
|
+
* `false`/`true`. `variant` is the variant id. */
|
|
40
|
+
resolveBooleanEvaluation(flagKey: string, defaultValue: boolean, context?: EvaluationContext): Promise<ResolutionDetails<boolean>>;
|
|
41
|
+
resolveNumberEvaluation(flagKey: string, defaultValue: number): Promise<ResolutionDetails<number>>;
|
|
42
|
+
resolveObjectEvaluation<T extends JsonValue>(flagKey: string, defaultValue: T): Promise<ResolutionDetails<T>>;
|
|
43
|
+
}
|
|
44
|
+
//# sourceMappingURL=provider.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"provider.d.ts","sourceRoot":"","sources":["../src/provider.ts"],"names":[],"mappings":"AAaA,OAAO,EAGL,KAAK,iBAAiB,EAEtB,KAAK,SAAS,EACd,KAAK,QAAQ,EACb,KAAK,iBAAiB,EACvB,MAAM,yBAAyB,CAAC;AAUjC,0EAA0E;AAC1E,MAAM,WAAW,WAAW;IAC1B,gDAAgD;IAChD,OAAO,EAAE,MAAM,CAAC;IAChB,6CAA6C;IAC7C,OAAO,EAAE,MAAM,CAAC;IAChB,wBAAwB;IACxB,MAAM,EAAE,MAAM,CAAC;IACf,+CAA+C;IAC/C,MAAM,EAAE,MAAM,CAAC;CAChB;AAED;;;;GAIG;AACH,MAAM,MAAM,YAAY,GACpB,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GACtB,CAAC,CAAC,OAAO,EAAE,iBAAiB,KAAK,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,SAAS,CAAC,CAAC;AAEzE,MAAM,WAAW,6BAA6B;IAC5C;gFAC4E;IAC5E,SAAS,CAAC,EAAE,YAAY,CAAC;IACzB;;wEAEoE;IACpE,UAAU,CAAC,EAAE,CAAC,QAAQ,EAAE,WAAW,KAAK,IAAI,CAAC;CAC9C;AAOD,qBAAa,sBAAuB,YAAW,QAAQ;;IACrD,QAAQ,CAAC,QAAQ;;MAA+B;IAChD,QAAQ,CAAC,MAAM,YAAY;gBAKf,OAAO,GAAE,6BAAkC;IAKvD,yDAAyD;IACnD,uBAAuB,CAC3B,OAAO,EAAE,MAAM,EACf,YAAY,EAAE,MAAM,EACpB,OAAO,GAAE,iBAAsB,GAC9B,OAAO,CAAC,iBAAiB,CAAC,MAAM,CAAC,CAAC;IAMrC;;uDAEmD;IAC7C,wBAAwB,CAC5B,OAAO,EAAE,MAAM,EACf,YAAY,EAAE,OAAO,EACrB,OAAO,GAAE,iBAAsB,GAC9B,OAAO,CAAC,iBAAiB,CAAC,OAAO,CAAC,CAAC;IAMhC,uBAAuB,CAC3B,OAAO,EAAE,MAAM,EACf,YAAY,EAAE,MAAM,GACnB,OAAO,CAAC,iBAAiB,CAAC,MAAM,CAAC,CAAC;IAI/B,uBAAuB,CAAC,CAAC,SAAS,SAAS,EAC/C,OAAO,EAAE,MAAM,EACf,YAAY,EAAE,CAAC,GACd,OAAO,CAAC,iBAAiB,CAAC,CAAC,CAAC,CAAC;CAqCjC"}
|
package/dist/provider.js
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
// OpenFeature server provider for dif.sh.
|
|
2
|
+
//
|
|
3
|
+
// A bridge, not the primary API: the typed `dif()` call from `@dif.sh/sdk` is
|
|
4
|
+
// still how dif is meant to be used. This provider lets a team that has
|
|
5
|
+
// standardised on OpenFeature evaluate the same experiments through
|
|
6
|
+
// `OpenFeature.getClient()`.
|
|
7
|
+
//
|
|
8
|
+
// Every decision is delegated to the SDK's pure `assign()` — the same function
|
|
9
|
+
// `@dif.sh/svelte` uses on the server — so a user gets the same variant here as
|
|
10
|
+
// from `dif()`. The spec registry is filled by the generated
|
|
11
|
+
// `dif/generated/client.ts` (side-effect import), so that file must be imported
|
|
12
|
+
// before the first evaluation.
|
|
13
|
+
import { ErrorCode, StandardResolutionReasons, } from "@openfeature/server-sdk";
|
|
14
|
+
import { assign, getSpec, } from "@dif.sh/sdk";
|
|
15
|
+
export class DifOpenFeatureProvider {
|
|
16
|
+
metadata = { name: "dif.sh" };
|
|
17
|
+
runsOn = "server";
|
|
18
|
+
#overrides;
|
|
19
|
+
#onExposure;
|
|
20
|
+
constructor(options = {}) {
|
|
21
|
+
this.#overrides = options.overrides;
|
|
22
|
+
this.#onExposure = options.onExposure;
|
|
23
|
+
}
|
|
24
|
+
/** `value` and `variant` are the assigned variant id. */
|
|
25
|
+
async resolveStringEvaluation(flagKey, defaultValue, context = {}) {
|
|
26
|
+
const result = this.#evaluate(flagKey, context);
|
|
27
|
+
if (!result)
|
|
28
|
+
return flagNotFound(flagKey, defaultValue);
|
|
29
|
+
return details(result, result.assignment.variant);
|
|
30
|
+
}
|
|
31
|
+
/** `value` is `true` for any variant other than the first declared one
|
|
32
|
+
* (the control), so `off`/`on` and `control`/`variant_a` both map to
|
|
33
|
+
* `false`/`true`. `variant` is the variant id. */
|
|
34
|
+
async resolveBooleanEvaluation(flagKey, defaultValue, context = {}) {
|
|
35
|
+
const result = this.#evaluate(flagKey, context);
|
|
36
|
+
if (!result)
|
|
37
|
+
return flagNotFound(flagKey, defaultValue);
|
|
38
|
+
return details(result, result.assignment.variant !== result.spec.variants[0]);
|
|
39
|
+
}
|
|
40
|
+
async resolveNumberEvaluation(flagKey, defaultValue) {
|
|
41
|
+
return typeMismatch(flagKey, defaultValue, "number");
|
|
42
|
+
}
|
|
43
|
+
async resolveObjectEvaluation(flagKey, defaultValue) {
|
|
44
|
+
return typeMismatch(flagKey, defaultValue, "object");
|
|
45
|
+
}
|
|
46
|
+
#evaluate(flagKey, context) {
|
|
47
|
+
const spec = getSpec(flagKey);
|
|
48
|
+
if (!spec)
|
|
49
|
+
return null;
|
|
50
|
+
const userId = userIdFrom(context);
|
|
51
|
+
const ctx = { userId, attributes: attributesFrom(context) };
|
|
52
|
+
const overrides = typeof this.#overrides === "function" ? this.#overrides(context) : this.#overrides;
|
|
53
|
+
if (overrides)
|
|
54
|
+
ctx.overrides = overrides;
|
|
55
|
+
const assignment = assign(flagKey, ctx);
|
|
56
|
+
if (!assignment)
|
|
57
|
+
return null;
|
|
58
|
+
if (this.#onExposure &&
|
|
59
|
+
assignment.exposed &&
|
|
60
|
+
assignment.bucket !== null &&
|
|
61
|
+
userId !== null) {
|
|
62
|
+
try {
|
|
63
|
+
this.#onExposure({
|
|
64
|
+
flagKey,
|
|
65
|
+
variant: assignment.variant,
|
|
66
|
+
bucket: assignment.bucket,
|
|
67
|
+
userId,
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
catch {
|
|
71
|
+
// An exposure sink must never change what the caller renders.
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return { spec, assignment };
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
/** `targetingKey` → dif user id. Missing, empty, or non-string ⇒ `null`
|
|
78
|
+
* (control, no exposure) — an empty string would otherwise put every
|
|
79
|
+
* anonymous request into one shared bucket. */
|
|
80
|
+
function userIdFrom(context) {
|
|
81
|
+
const key = context.targetingKey;
|
|
82
|
+
return typeof key === "string" && key !== "" ? key : null;
|
|
83
|
+
}
|
|
84
|
+
/** Every other top-level scalar (string / number / boolean / null) becomes an
|
|
85
|
+
* audience attribute. Dates, objects, and arrays are skipped: dif audience
|
|
86
|
+
* predicates only compare scalars. */
|
|
87
|
+
function attributesFrom(context) {
|
|
88
|
+
const attrs = {};
|
|
89
|
+
for (const [key, value] of Object.entries(context)) {
|
|
90
|
+
if (key === "targetingKey")
|
|
91
|
+
continue;
|
|
92
|
+
if (value === null ||
|
|
93
|
+
typeof value === "string" ||
|
|
94
|
+
typeof value === "number" ||
|
|
95
|
+
typeof value === "boolean") {
|
|
96
|
+
attrs[key] = value;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
return attrs;
|
|
100
|
+
}
|
|
101
|
+
function details({ spec, assignment }, value) {
|
|
102
|
+
const flagMetadata = { surface: spec.surface };
|
|
103
|
+
if (assignment.bucket !== null)
|
|
104
|
+
flagMetadata.bucket = assignment.bucket;
|
|
105
|
+
return {
|
|
106
|
+
value,
|
|
107
|
+
variant: assignment.variant,
|
|
108
|
+
reason: assignment.forced
|
|
109
|
+
? StandardResolutionReasons.STATIC
|
|
110
|
+
: assignment.exposed
|
|
111
|
+
? StandardResolutionReasons.SPLIT
|
|
112
|
+
: StandardResolutionReasons.DEFAULT,
|
|
113
|
+
flagMetadata,
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
function flagNotFound(flagKey, defaultValue) {
|
|
117
|
+
return {
|
|
118
|
+
value: defaultValue,
|
|
119
|
+
reason: StandardResolutionReasons.ERROR,
|
|
120
|
+
errorCode: ErrorCode.FLAG_NOT_FOUND,
|
|
121
|
+
errorMessage: `dif: no experiment "${flagKey}" is registered. Import the generated client ` +
|
|
122
|
+
`(import "./dif/generated/client") before evaluating, and run \`dif build\` ` +
|
|
123
|
+
`after adding or renaming a dif/experiments/active/*.md file.`,
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
function typeMismatch(flagKey, defaultValue, requested) {
|
|
127
|
+
return {
|
|
128
|
+
value: defaultValue,
|
|
129
|
+
reason: StandardResolutionReasons.ERROR,
|
|
130
|
+
errorCode: ErrorCode.TYPE_MISMATCH,
|
|
131
|
+
errorMessage: `dif: "${flagKey}" can't resolve as ${requested === "number" ? "a number" : "an object"}. ` +
|
|
132
|
+
`dif flags resolve to a variant id (getStringValue) or a boolean ` +
|
|
133
|
+
`(getBooleanValue: true for any variant other than the first declared).`,
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
//# sourceMappingURL=provider.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"provider.js","sourceRoot":"","sources":["../src/provider.ts"],"names":[],"mappings":"AAAA,0CAA0C;AAC1C,EAAE;AACF,8EAA8E;AAC9E,wEAAwE;AACxE,oEAAoE;AACpE,6BAA6B;AAC7B,EAAE;AACF,+EAA+E;AAC/E,gFAAgF;AAChF,6DAA6D;AAC7D,gFAAgF;A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AAe,EAAE,YAAe;IACvD,OAAO;QACL,KAAK,EAAE,YAAY;QACnB,MAAM,EAAE,yBAAyB,CAAC,KAAK;QACvC,SAAS,EAAE,SAAS,CAAC,cAAc;QACnC,YAAY,EACV,uBAAuB,OAAO,+CAA+C;YAC7E,6EAA6E;YAC7E,8DAA8D;KACjE,CAAC;AACJ,CAAC;AAED,SAAS,YAAY,CACnB,OAAe,EACf,YAAe,EACf,SAA8B;IAE9B,OAAO;QACL,KAAK,EAAE,YAAY;QACnB,MAAM,EAAE,yBAAyB,CAAC,KAAK;QACvC,SAAS,EAAE,SAAS,CAAC,aAAa;QAClC,YAAY,EACV,SAAS,OAAO,sBAAsB,SAAS,KAAK,QAAQ,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC,WAAW,IAAI;YAC3F,kEAAkE;YAClE,wEAAwE;KAC3E,CAAC;AACJ,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@dif.sh/openfeature",
|
|
3
|
+
"version": "0.6.2",
|
|
4
|
+
"description": "OpenFeature server provider for dif.sh — resolves OpenFeature evaluations against the experiments registered by dif/generated/client.ts.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist",
|
|
17
|
+
"README.md"
|
|
18
|
+
],
|
|
19
|
+
"sideEffects": false,
|
|
20
|
+
"scripts": {
|
|
21
|
+
"build": "tsc -p tsconfig.json",
|
|
22
|
+
"lint": "tsc --noEmit",
|
|
23
|
+
"test": "node --test --import tsx src/*.test.ts",
|
|
24
|
+
"prepublishOnly": "npm run lint && npm test && npm run build"
|
|
25
|
+
},
|
|
26
|
+
"engines": {
|
|
27
|
+
"node": ">=20.6"
|
|
28
|
+
},
|
|
29
|
+
"publishConfig": {
|
|
30
|
+
"access": "public"
|
|
31
|
+
},
|
|
32
|
+
"repository": {
|
|
33
|
+
"type": "git",
|
|
34
|
+
"url": "git+https://github.com/dif-sh/dif.git",
|
|
35
|
+
"directory": "cli/packages/openfeature"
|
|
36
|
+
},
|
|
37
|
+
"keywords": [
|
|
38
|
+
"experimentation",
|
|
39
|
+
"ab-testing",
|
|
40
|
+
"feature-flags",
|
|
41
|
+
"openfeature",
|
|
42
|
+
"openfeature-provider",
|
|
43
|
+
"dif.sh"
|
|
44
|
+
],
|
|
45
|
+
"peerDependencies": {
|
|
46
|
+
"@dif.sh/sdk": "^0.6.2",
|
|
47
|
+
"@openfeature/server-sdk": "^1.18.0"
|
|
48
|
+
},
|
|
49
|
+
"devDependencies": {
|
|
50
|
+
"@dif.sh/sdk": "file:../sdk",
|
|
51
|
+
"@openfeature/core": "^1.12.0",
|
|
52
|
+
"@openfeature/server-sdk": "^1.23.0",
|
|
53
|
+
"tsx": "^4.7.0",
|
|
54
|
+
"typescript": "^5.4.0"
|
|
55
|
+
}
|
|
56
|
+
}
|