@sanity/personalization-plugin 2.3.0-growthbook.1 → 2.3.0-launch-darkly.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.
@@ -1,94 +0,0 @@
1
- import {SanityClient} from 'sanity'
2
-
3
- import {ExperimentType, GrowthbookFeature, VariantType} from '../types'
4
- import {namespace, pluginConfigKeys} from './Components/Secrets'
5
- import {GrowthbookABConfig} from './types'
6
-
7
- const getBooleanConversion = (value: string) => {
8
- // control is false
9
- if (value === 'true') {
10
- return 'variant'
11
- } else if (value === 'false') {
12
- return 'control'
13
- }
14
- return value
15
- }
16
-
17
- export const getExperiments = async ({
18
- client,
19
- environment,
20
- baseUrl,
21
- project,
22
- convertBooleans,
23
- tags,
24
- }: Omit<GrowthbookABConfig, 'fields' | 'baseUrl'> & {
25
- client: SanityClient
26
- baseUrl: string
27
- }): Promise<ExperimentType[]> => {
28
- const query = `*[_id == 'secrets.${namespace}'][0].secrets.${pluginConfigKeys[0].key}`
29
-
30
- const secret = await client.fetch(query) // secret is stored in the content lake using @sanity/studio-secrets
31
- if (!secret) return []
32
-
33
- const featureExperiments: ExperimentType[] = []
34
- let hasMore = true
35
- let offset = 0
36
- const url = new URL(`${baseUrl}/features`)
37
- if (project) {
38
- url.searchParams.set('projectId', project)
39
- }
40
-
41
- while (hasMore) {
42
- url.searchParams.set('offset', offset.toString())
43
- const response = await fetch(url, {
44
- headers: {
45
- Authorization: `Bearer ${secret}`,
46
- },
47
- })
48
-
49
- const {features, hasMore: responseHasMore, nextOffset} = await response.json()
50
-
51
- hasMore = responseHasMore
52
- offset = nextOffset
53
- if (!features) continue
54
-
55
- features.forEach((feature: GrowthbookFeature) => {
56
- if (feature.archived) {
57
- return undefined
58
- }
59
- if (tags && feature.tags && !feature.tags.some((tag) => tags.includes(tag))) {
60
- return undefined
61
- }
62
-
63
- const experiments = feature.environments[environment]?.rules.filter(
64
- (experiment) => experiment.type === 'experiment-ref' || experiment.type === 'experiment',
65
- )
66
-
67
- if (!experiments) {
68
- return undefined
69
- }
70
-
71
- const variations: VariantType[] = []
72
- const uniqueValues = new Set<string>()
73
-
74
- experiments.forEach((experiment) => {
75
- experiment?.variations.forEach((variant) => {
76
- const value = convertBooleans ? getBooleanConversion(variant.value) : variant.value
77
- if (!uniqueValues.has(value)) {
78
- uniqueValues.add(value)
79
- variations.push({
80
- id: value,
81
- label: value,
82
- })
83
- }
84
- })
85
- })
86
- const value = {id: feature.id, label: feature.id, variants: variations}
87
-
88
- featureExperiments.push(value)
89
- return undefined
90
- })
91
- }
92
- const sortedFeatureExperiments = featureExperiments.sort((a, b) => a.id.localeCompare(b.id))
93
- return sortedFeatureExperiments
94
- }