@growthbook/proxy-eval 1.0.0 → 1.0.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/dist/index.d.ts +6 -2
- package/dist/index.js +72 -54
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/src/index.ts +18 -2
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
1
|
-
|
|
1
|
+
import { StickyBucketService } from "@growthbook/growthbook";
|
|
2
|
+
export declare function evaluateFeatures({ payload, attributes, forcedVariations, forcedFeatures, url, stickyBucketService, ctx, }: {
|
|
2
3
|
payload: any;
|
|
3
4
|
attributes: Record<string, any>;
|
|
4
5
|
forcedVariations?: Record<string, number>;
|
|
5
6
|
forcedFeatures?: Map<string, any>;
|
|
6
7
|
url?: string;
|
|
8
|
+
stickyBucketService?: (StickyBucketService & {
|
|
9
|
+
onEvaluate?: () => Promise<void>;
|
|
10
|
+
}) | null;
|
|
7
11
|
ctx?: any;
|
|
8
|
-
}): any
|
|
12
|
+
}): Promise<any>;
|
package/dist/index.js
CHANGED
|
@@ -1,68 +1,86 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
2
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
12
|
exports.evaluateFeatures = void 0;
|
|
4
13
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
5
14
|
const growthbook_1 = require("@growthbook/growthbook");
|
|
6
|
-
function evaluateFeatures({ payload, attributes, forcedVariations, forcedFeatures, url, ctx, }) {
|
|
7
|
-
var _a;
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
if (experiments) {
|
|
17
|
-
context.experiments = experiments;
|
|
18
|
-
}
|
|
19
|
-
if (forcedVariations) {
|
|
20
|
-
context.forcedVariations = forcedVariations;
|
|
21
|
-
}
|
|
22
|
-
if (url !== undefined) {
|
|
23
|
-
context.url = url;
|
|
24
|
-
}
|
|
25
|
-
if (features || experiments) {
|
|
26
|
-
const gb = new growthbook_1.GrowthBook(context);
|
|
27
|
-
if (forcedFeatures) {
|
|
28
|
-
gb.setForcedFeatures(forcedFeatures);
|
|
15
|
+
function evaluateFeatures({ payload, attributes, forcedVariations, forcedFeatures, url, stickyBucketService = null, ctx, }) {
|
|
16
|
+
var _a, _b;
|
|
17
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
18
|
+
const evaluatedFeatures = {};
|
|
19
|
+
const evaluatedExperiments = [];
|
|
20
|
+
const features = payload === null || payload === void 0 ? void 0 : payload.features;
|
|
21
|
+
const experiments = payload === null || payload === void 0 ? void 0 : payload.experiments;
|
|
22
|
+
const context = { attributes };
|
|
23
|
+
if (features) {
|
|
24
|
+
context.features = features;
|
|
29
25
|
}
|
|
30
|
-
if (
|
|
31
|
-
|
|
26
|
+
if (experiments) {
|
|
27
|
+
context.experiments = experiments;
|
|
32
28
|
}
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
29
|
+
if (forcedVariations) {
|
|
30
|
+
context.forcedVariations = forcedVariations;
|
|
31
|
+
}
|
|
32
|
+
if (url !== undefined) {
|
|
33
|
+
context.url = url;
|
|
34
|
+
}
|
|
35
|
+
if (stickyBucketService) {
|
|
36
|
+
context.stickyBucketService = stickyBucketService;
|
|
37
|
+
}
|
|
38
|
+
if (features || experiments) {
|
|
39
|
+
const gb = new growthbook_1.GrowthBook(context);
|
|
40
|
+
if (forcedFeatures) {
|
|
41
|
+
gb.setForcedFeatures(forcedFeatures);
|
|
42
|
+
}
|
|
43
|
+
if (ctx === null || ctx === void 0 ? void 0 : ctx.verboseDebugging) {
|
|
44
|
+
gb.debug = true;
|
|
45
|
+
}
|
|
46
|
+
if (stickyBucketService) {
|
|
47
|
+
yield gb.refreshStickyBuckets();
|
|
48
|
+
}
|
|
49
|
+
const gbFeatures = gb.getFeatures();
|
|
50
|
+
for (const key in gbFeatures) {
|
|
51
|
+
const result = gb.evalFeature(key);
|
|
52
|
+
if (result.on || result.experiment) {
|
|
53
|
+
// reduced feature definition
|
|
54
|
+
evaluatedFeatures[key] = {
|
|
55
|
+
defaultValue: result.value,
|
|
56
|
+
};
|
|
57
|
+
if (result.source === "experiment") {
|
|
58
|
+
// reduced experiment definition for tracking
|
|
59
|
+
const scrubbedResultExperiment = ((_a = result === null || result === void 0 ? void 0 : result.experimentResult) === null || _a === void 0 ? void 0 : _a.variationId) !== undefined
|
|
60
|
+
? scrubExperiment(result.experiment, result.experimentResult.variationId)
|
|
61
|
+
: result.experiment;
|
|
62
|
+
evaluatedFeatures[key].rules = [
|
|
63
|
+
{
|
|
64
|
+
force: result.value,
|
|
65
|
+
tracks: [{ experiment: scrubbedResultExperiment, result }],
|
|
66
|
+
},
|
|
67
|
+
];
|
|
68
|
+
}
|
|
52
69
|
}
|
|
53
70
|
}
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
71
|
+
const gbExperiments = gb.getExperiments();
|
|
72
|
+
for (const experiment of gbExperiments) {
|
|
73
|
+
const result = gb.run(experiment);
|
|
74
|
+
if (result.inExperiment) {
|
|
75
|
+
// reduced experiment definition
|
|
76
|
+
const evaluatedExperiment = scrubExperiment(experiment, result.variationId);
|
|
77
|
+
evaluatedExperiments.push(evaluatedExperiment);
|
|
78
|
+
}
|
|
62
79
|
}
|
|
63
80
|
}
|
|
64
|
-
|
|
65
|
-
|
|
81
|
+
(_b = stickyBucketService === null || stickyBucketService === void 0 ? void 0 : stickyBucketService.onEvaluate) === null || _b === void 0 ? void 0 : _b.call(stickyBucketService);
|
|
82
|
+
return Object.assign(Object.assign({}, payload), { features: evaluatedFeatures, experiments: evaluatedExperiments });
|
|
83
|
+
});
|
|
66
84
|
}
|
|
67
85
|
exports.evaluateFeatures = evaluateFeatures;
|
|
68
86
|
function scrubExperiment(experiment, allowedVariation) {
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,uDAAuD;AACvD,uDAIgC;AAEhC,SAAsB,gBAAgB,CAAC,EACrC,OAAO,EACP,UAAU,EACV,gBAAgB,EAChB,cAAc,EACd,GAAG,EACH,mBAAmB,GAAG,IAAI,EAC1B,GAAG,GAWJ;;;QACC,MAAM,iBAAiB,GAAwB,EAAE,CAAC;QAClD,MAAM,oBAAoB,GAAU,EAAE,CAAC;QAEvC,MAAM,QAAQ,GAAG,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,QAAQ,CAAC;QACnC,MAAM,WAAW,GAAG,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,WAAW,CAAC;QACzC,MAAM,OAAO,GAAc,EAAE,UAAU,EAAE,CAAC;QAC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AkB,CAAC;AAC5B,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@growthbook/proxy-eval",
|
|
3
3
|
"description": "Remote evaluation service for proxies, edge workers, or backend APIs",
|
|
4
|
-
"version": "1.0.
|
|
4
|
+
"version": "1.0.2",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"repository": {
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
"dev": "tsc --watch"
|
|
19
19
|
},
|
|
20
20
|
"dependencies": {
|
|
21
|
-
"@growthbook/growthbook": "^0.
|
|
21
|
+
"@growthbook/growthbook": "^0.34.0"
|
|
22
22
|
},
|
|
23
23
|
"devDependencies": {
|
|
24
24
|
"rimraf": "^5.0.5",
|
package/src/index.ts
CHANGED
|
@@ -1,12 +1,17 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
GrowthBook,
|
|
4
|
+
Context as GBContext,
|
|
5
|
+
StickyBucketService,
|
|
6
|
+
} from "@growthbook/growthbook";
|
|
3
7
|
|
|
4
|
-
export function evaluateFeatures({
|
|
8
|
+
export async function evaluateFeatures({
|
|
5
9
|
payload,
|
|
6
10
|
attributes,
|
|
7
11
|
forcedVariations,
|
|
8
12
|
forcedFeatures,
|
|
9
13
|
url,
|
|
14
|
+
stickyBucketService = null,
|
|
10
15
|
ctx,
|
|
11
16
|
}: {
|
|
12
17
|
payload: any;
|
|
@@ -14,6 +19,9 @@ export function evaluateFeatures({
|
|
|
14
19
|
forcedVariations?: Record<string, number>;
|
|
15
20
|
forcedFeatures?: Map<string, any>;
|
|
16
21
|
url?: string;
|
|
22
|
+
stickyBucketService?:
|
|
23
|
+
| (StickyBucketService & { onEvaluate?: () => Promise<void> })
|
|
24
|
+
| null;
|
|
17
25
|
ctx?: any;
|
|
18
26
|
}) {
|
|
19
27
|
const evaluatedFeatures: Record<string, any> = {};
|
|
@@ -34,6 +42,9 @@ export function evaluateFeatures({
|
|
|
34
42
|
if (url !== undefined) {
|
|
35
43
|
context.url = url;
|
|
36
44
|
}
|
|
45
|
+
if (stickyBucketService) {
|
|
46
|
+
context.stickyBucketService = stickyBucketService;
|
|
47
|
+
}
|
|
37
48
|
|
|
38
49
|
if (features || experiments) {
|
|
39
50
|
const gb = new GrowthBook(context);
|
|
@@ -43,6 +54,9 @@ export function evaluateFeatures({
|
|
|
43
54
|
if (ctx?.verboseDebugging) {
|
|
44
55
|
gb.debug = true;
|
|
45
56
|
}
|
|
57
|
+
if (stickyBucketService) {
|
|
58
|
+
await gb.refreshStickyBuckets();
|
|
59
|
+
}
|
|
46
60
|
|
|
47
61
|
const gbFeatures = gb.getFeatures();
|
|
48
62
|
for (const key in gbFeatures) {
|
|
@@ -85,6 +99,8 @@ export function evaluateFeatures({
|
|
|
85
99
|
}
|
|
86
100
|
}
|
|
87
101
|
|
|
102
|
+
stickyBucketService?.onEvaluate?.();
|
|
103
|
+
|
|
88
104
|
return {
|
|
89
105
|
...payload,
|
|
90
106
|
features: evaluatedFeatures,
|