@sanity/personalization-plugin 2.3.0-launch-darkly.1 → 2.3.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 +2 -0
- package/dist/growthbook/index.d.mts +15 -0
- package/dist/growthbook/index.d.ts +15 -0
- package/dist/growthbook/index.js +120 -0
- package/dist/growthbook/index.js.map +1 -0
- package/dist/growthbook/index.mjs +124 -0
- package/dist/growthbook/index.mjs.map +1 -0
- package/dist/index.d.mts +171 -177
- package/dist/index.d.ts +171 -177
- package/dist/index.js +47 -107
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +50 -111
- package/dist/index.mjs.map +1 -1
- package/package.json +7 -2
- package/src/components/ExperimentContext.tsx +4 -7
- package/src/components/ExperimentField.tsx +54 -32
- package/src/components/ExperimentInput.tsx +7 -6
- package/src/fieldExperiments.tsx +1 -0
- package/src/growthbook/Components/GrowthbookContext.tsx +38 -0
- package/src/{components → growthbook/Components}/Secrets.tsx +7 -5
- package/src/growthbook/index.ts +54 -0
- package/src/growthbook/types.ts +15 -0
- package/src/growthbook/utils.ts +94 -0
- package/src/index.ts +0 -1
- package/src/types.ts +160 -169
- package/src/launchDarklyExperiments.tsx +0 -48
- package/src/utils/launchDarkly.ts +0 -54
package/src/types.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import {Dispatch, SetStateAction} from 'react'
|
|
2
1
|
import {
|
|
3
2
|
ArrayOfObjectsInputProps,
|
|
4
3
|
FieldDefinition,
|
|
@@ -38,8 +37,6 @@ export type VariantPreviewProps = Omit<PreviewProps, 'SchemaType'> & {
|
|
|
38
37
|
|
|
39
38
|
export type ExperimentContextProps = Required<FieldPluginConfig> & {
|
|
40
39
|
experiments: ExperimentType[]
|
|
41
|
-
setSecret: Dispatch<SetStateAction<string | undefined>>
|
|
42
|
-
secret: string | undefined
|
|
43
40
|
}
|
|
44
41
|
|
|
45
42
|
export type ArrayInputProps = ArrayOfObjectsInputProps & {
|
|
@@ -71,184 +68,178 @@ export type ExperimentGeneric<T> = {
|
|
|
71
68
|
| undefined
|
|
72
69
|
}
|
|
73
70
|
|
|
74
|
-
export type
|
|
71
|
+
export type GrowthbookExperiment = {
|
|
72
|
+
id: string
|
|
73
|
+
dateCreated: string
|
|
74
|
+
dateUpdated: string
|
|
75
75
|
name: string
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
76
|
+
project: string
|
|
77
|
+
hypothesis: string
|
|
78
|
+
description: string
|
|
79
|
+
tags: [string]
|
|
80
|
+
owner: string
|
|
81
|
+
archived: boolean
|
|
82
|
+
status: string
|
|
83
|
+
autoRefresh: boolean
|
|
84
|
+
hashAttribute: string
|
|
85
|
+
fallbackAttribute: string
|
|
86
|
+
hashVersion: number
|
|
87
|
+
disableStickyBucketing: boolean
|
|
88
|
+
bucketVersion: number
|
|
89
|
+
minBucketVersion: number
|
|
90
|
+
variations: [
|
|
91
|
+
{
|
|
92
|
+
variationId: string
|
|
93
|
+
key: string
|
|
94
|
+
name: string
|
|
95
|
+
description: string
|
|
96
|
+
screenshots: [string]
|
|
97
|
+
},
|
|
98
|
+
]
|
|
99
|
+
phases: [
|
|
100
|
+
{
|
|
101
|
+
name: string
|
|
102
|
+
dateStarted: string
|
|
103
|
+
dateEnded: string
|
|
104
|
+
reasonForStopping: string
|
|
105
|
+
seed: string
|
|
106
|
+
coverage: 0
|
|
107
|
+
trafficSplit: [
|
|
108
|
+
{
|
|
109
|
+
variationId: string
|
|
110
|
+
weight: 0
|
|
111
|
+
},
|
|
112
|
+
]
|
|
113
|
+
namespace: {
|
|
114
|
+
namespaceId: string
|
|
115
|
+
range: []
|
|
116
|
+
}
|
|
117
|
+
targetingCondition: string
|
|
118
|
+
savedGroupTargeting: [
|
|
119
|
+
{
|
|
120
|
+
matchType: string
|
|
121
|
+
savedGroups: [string]
|
|
122
|
+
},
|
|
123
|
+
]
|
|
124
|
+
},
|
|
125
|
+
]
|
|
126
|
+
settings: {
|
|
127
|
+
datasourceId: string
|
|
128
|
+
assignmentQueryId: string
|
|
129
|
+
experimentId: string
|
|
130
|
+
segmentId: string
|
|
131
|
+
queryFilter: string
|
|
132
|
+
inProgressConversions: string
|
|
133
|
+
attributionModel: string
|
|
134
|
+
statsEngine: string
|
|
135
|
+
regressionAdjustmentEnabled: boolean
|
|
136
|
+
goals: [
|
|
137
|
+
{
|
|
138
|
+
metricId: string
|
|
139
|
+
overrides: {
|
|
140
|
+
delayHours: 0
|
|
141
|
+
windowHours: 0
|
|
142
|
+
window: string
|
|
143
|
+
winRiskThreshold: 0
|
|
144
|
+
loseRiskThreshold: 0
|
|
145
|
+
}
|
|
146
|
+
},
|
|
147
|
+
]
|
|
148
|
+
secondaryMetrics: [
|
|
149
|
+
{
|
|
150
|
+
metricId: string
|
|
151
|
+
overrides: {
|
|
152
|
+
delayHours: 0
|
|
153
|
+
windowHours: 0
|
|
154
|
+
window: string
|
|
155
|
+
winRiskThreshold: 0
|
|
156
|
+
loseRiskThreshold: 0
|
|
116
157
|
}
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
_id: string
|
|
131
|
-
role: string
|
|
132
|
-
email: string
|
|
133
|
-
firstName: string
|
|
134
|
-
lastName: string
|
|
158
|
+
},
|
|
159
|
+
]
|
|
160
|
+
guardrails: [
|
|
161
|
+
{
|
|
162
|
+
metricId: string
|
|
163
|
+
overrides: {
|
|
164
|
+
delayHours: 0
|
|
165
|
+
windowHours: 0
|
|
166
|
+
window: string
|
|
167
|
+
winRiskThreshold: 0
|
|
168
|
+
loseRiskThreshold: 0
|
|
135
169
|
}
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
170
|
+
},
|
|
171
|
+
]
|
|
172
|
+
activationMetric: {
|
|
173
|
+
metricId: string
|
|
174
|
+
overrides: {
|
|
175
|
+
delayHours: 0
|
|
176
|
+
windowHours: 0
|
|
177
|
+
window: string
|
|
178
|
+
winRiskThreshold: 0
|
|
179
|
+
loseRiskThreshold: 0
|
|
139
180
|
}
|
|
140
|
-
}>
|
|
141
|
-
}
|
|
142
|
-
customProperties: {
|
|
143
|
-
key: {
|
|
144
|
-
name: string
|
|
145
|
-
value: string[]
|
|
146
181
|
}
|
|
147
182
|
}
|
|
183
|
+
resultSummary: {
|
|
184
|
+
status: string
|
|
185
|
+
winner: string
|
|
186
|
+
conclusions: string
|
|
187
|
+
releasedVariationId: string
|
|
188
|
+
excludeFromPayload: boolean
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
export type GrowthbookFeature = {
|
|
193
|
+
id: string
|
|
194
|
+
dateCreated: string
|
|
195
|
+
dateUpdated: string
|
|
148
196
|
archived: boolean
|
|
149
197
|
description: string
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
type: string
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
_id: string
|
|
159
|
-
role: string
|
|
160
|
-
email: string
|
|
161
|
-
firstName: string
|
|
162
|
-
lastName: string
|
|
163
|
-
}
|
|
164
|
-
maintainerTeamKey: string
|
|
165
|
-
_maintainerTeam: {
|
|
166
|
-
key: string
|
|
167
|
-
name: string
|
|
168
|
-
_links: {
|
|
169
|
-
parent: {
|
|
170
|
-
href: string
|
|
171
|
-
type: string
|
|
172
|
-
}
|
|
173
|
-
roles: {
|
|
174
|
-
href: string
|
|
175
|
-
type: string
|
|
176
|
-
}
|
|
177
|
-
self: {
|
|
178
|
-
href: string
|
|
179
|
-
type: string
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
archivedDate: number
|
|
184
|
-
deprecated: boolean
|
|
185
|
-
deprecatedDate: number
|
|
186
|
-
defaults: {
|
|
187
|
-
onVariation: number
|
|
188
|
-
offVariation: number
|
|
189
|
-
}
|
|
190
|
-
_purpose: string
|
|
191
|
-
migrationSettings: {
|
|
192
|
-
contextKind: string
|
|
193
|
-
stageCount: number
|
|
194
|
-
}
|
|
198
|
+
owner: string
|
|
199
|
+
project: string
|
|
200
|
+
valueType: string
|
|
201
|
+
defaultValue: string
|
|
202
|
+
tags: string[]
|
|
195
203
|
environments: {
|
|
196
204
|
[key: string]: {
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
+
enabled: boolean
|
|
206
|
+
defaultValue: string
|
|
207
|
+
rules: {
|
|
208
|
+
description: string
|
|
209
|
+
condition: string
|
|
210
|
+
savedGroupTargeting: {matchType: string; savedGroups: string[]}[]
|
|
211
|
+
id: string
|
|
212
|
+
enabled: boolean
|
|
205
213
|
type: string
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
values: unknown[]
|
|
225
|
-
negate: boolean
|
|
226
|
-
}>
|
|
227
|
-
trackEvents: boolean
|
|
228
|
-
}>
|
|
229
|
-
fallthrough: {
|
|
230
|
-
variation: number
|
|
231
|
-
}
|
|
232
|
-
offVariation: number
|
|
233
|
-
prerequisites: Array<{
|
|
234
|
-
key: string
|
|
235
|
-
variation: number
|
|
236
|
-
}>
|
|
237
|
-
_summary: {
|
|
238
|
-
variations: {
|
|
239
|
-
[key: string]: {
|
|
240
|
-
rules: number
|
|
241
|
-
nullRules: number
|
|
242
|
-
targets: number
|
|
243
|
-
contextTargets: number
|
|
244
|
-
isFallthrough?: boolean
|
|
245
|
-
isOff?: boolean
|
|
246
|
-
}
|
|
247
|
-
}
|
|
248
|
-
prerequisites: number
|
|
214
|
+
value: string
|
|
215
|
+
variations: {value: string; variationId: string}[]
|
|
216
|
+
}[]
|
|
217
|
+
definition: string
|
|
218
|
+
draft: {
|
|
219
|
+
enabled: boolean
|
|
220
|
+
defaultValue: string
|
|
221
|
+
rules: {
|
|
222
|
+
description: string
|
|
223
|
+
condition: string
|
|
224
|
+
savedGroupTargeting: {matchType: string; savedGroups: string[]}[]
|
|
225
|
+
id: string
|
|
226
|
+
enabled: boolean
|
|
227
|
+
type: string
|
|
228
|
+
value: string
|
|
229
|
+
variations: {value: string; variationId: string}[]
|
|
230
|
+
}[]
|
|
231
|
+
definition: string
|
|
249
232
|
}
|
|
250
233
|
}
|
|
251
234
|
}
|
|
252
|
-
|
|
253
|
-
|
|
235
|
+
prerequisites: {
|
|
236
|
+
parentId: string
|
|
237
|
+
parentCondition: string
|
|
238
|
+
}[]
|
|
239
|
+
revision: {
|
|
240
|
+
version: number
|
|
241
|
+
comment: string
|
|
242
|
+
date: string
|
|
243
|
+
publishedBy: string
|
|
244
|
+
}
|
|
254
245
|
}
|
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
import {definePlugin, FieldDefinition, isObjectInputProps} from 'sanity'
|
|
2
|
-
|
|
3
|
-
import {Secrets} from './components/Secrets'
|
|
4
|
-
import {fieldLevelExperiments} from './fieldExperiments'
|
|
5
|
-
import {flattenSchemaType} from './utils/flattenSchemaType'
|
|
6
|
-
import {getExperiments} from './utils/launchDarkly'
|
|
7
|
-
|
|
8
|
-
export type LaunchDarklyFieldLevelConfig = {
|
|
9
|
-
fields: (string | FieldDefinition)[]
|
|
10
|
-
projectKey: string
|
|
11
|
-
tags?: string[]
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export const launchDarklyFieldLevel = definePlugin<LaunchDarklyFieldLevelConfig>((config) => {
|
|
15
|
-
const {fields, projectKey, tags} = config
|
|
16
|
-
return {
|
|
17
|
-
name: 'sanity-growthbook-personalistaion-plugin-field-level-experiments',
|
|
18
|
-
plugins: [
|
|
19
|
-
fieldLevelExperiments({
|
|
20
|
-
fields,
|
|
21
|
-
experiments: (client) => getExperiments({client, projectKey, tags}),
|
|
22
|
-
}),
|
|
23
|
-
],
|
|
24
|
-
|
|
25
|
-
form: {
|
|
26
|
-
components: {
|
|
27
|
-
input: (props) => {
|
|
28
|
-
const isRootInput = props.id === 'root' && isObjectInputProps(props)
|
|
29
|
-
|
|
30
|
-
if (!isRootInput) {
|
|
31
|
-
return props.renderDefault(props)
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
const flatFieldTypeNames = flattenSchemaType(props.schemaType).map(
|
|
35
|
-
(field) => field.type.name,
|
|
36
|
-
)
|
|
37
|
-
|
|
38
|
-
const hasExperiment = flatFieldTypeNames.some((name) => name.startsWith('experiment'))
|
|
39
|
-
|
|
40
|
-
if (!hasExperiment) {
|
|
41
|
-
return props.renderDefault(props)
|
|
42
|
-
}
|
|
43
|
-
return Secrets(props, 'launchdarkly')
|
|
44
|
-
},
|
|
45
|
-
},
|
|
46
|
-
},
|
|
47
|
-
}
|
|
48
|
-
})
|
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
import {SanityClient} from 'sanity'
|
|
2
|
-
|
|
3
|
-
import {LaunchDarklyFieldLevelConfig} from '../launchDarklyExperiments'
|
|
4
|
-
import {ExperimentType, LaunchDarklyFlagItem} from '../types'
|
|
5
|
-
|
|
6
|
-
export const getExperiments = async ({
|
|
7
|
-
client,
|
|
8
|
-
projectKey,
|
|
9
|
-
tags,
|
|
10
|
-
}: Omit<LaunchDarklyFieldLevelConfig, 'fields'> & {client: SanityClient}): Promise<
|
|
11
|
-
ExperimentType[]
|
|
12
|
-
> => {
|
|
13
|
-
const query = `*[_id == 'secrets.launchdarkly'][0].secrets.apiKey`
|
|
14
|
-
|
|
15
|
-
const secret = await client.fetch(query) // secret is stored in the content lake using @sanity/studio-secrets
|
|
16
|
-
if (!secret) return []
|
|
17
|
-
|
|
18
|
-
const url = new URL(`https://app.launchdarkly.com/api/v2/flags/${projectKey}`)
|
|
19
|
-
|
|
20
|
-
if (tags) {
|
|
21
|
-
url.searchParams.set('filter', `tags:${tags.join('+')}`)
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
const featureExperiments: ExperimentType[] = []
|
|
25
|
-
let hasMore = true
|
|
26
|
-
const offset = 0
|
|
27
|
-
const limit = 10
|
|
28
|
-
|
|
29
|
-
while (hasMore) {
|
|
30
|
-
url.searchParams.set('offset', offset.toString())
|
|
31
|
-
url.searchParams.set('limit', limit.toString())
|
|
32
|
-
const responseFlags = await fetch(url, {
|
|
33
|
-
headers: {
|
|
34
|
-
Authorization: secret,
|
|
35
|
-
},
|
|
36
|
-
})
|
|
37
|
-
|
|
38
|
-
const {items} = await responseFlags.json()
|
|
39
|
-
const experiments = items.map((flag: LaunchDarklyFlagItem) => ({
|
|
40
|
-
id: flag.key,
|
|
41
|
-
label: flag.name,
|
|
42
|
-
variants: flag.variations.map((variation) => ({
|
|
43
|
-
id: variation.value,
|
|
44
|
-
label: variation.name ?? variation.value,
|
|
45
|
-
})),
|
|
46
|
-
}))
|
|
47
|
-
featureExperiments.push(...experiments)
|
|
48
|
-
if (items.length !== limit) {
|
|
49
|
-
hasMore = false
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
return featureExperiments
|
|
54
|
-
}
|