@sanity/personalization-plugin 2.1.0 → 2.2.0-launch-darkly.1

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.mts CHANGED
@@ -1,4 +1,5 @@
1
1
  import {ArrayOfObjectsInputProps} from 'sanity'
2
+ import {Dispatch} from 'react'
2
3
  import {FieldDefinition} from 'sanity'
3
4
  import {ObjectField} from 'sanity'
4
5
  import {Path} from 'sanity'
@@ -6,6 +7,7 @@ import {Plugin as Plugin_2} from 'sanity'
6
7
  import {PreviewProps} from 'sanity'
7
8
  import {SanityClient} from 'sanity'
8
9
  import {SchemaType} from 'sanity'
10
+ import {SetStateAction} from 'react'
9
11
 
10
12
  export declare type ArrayInputProps = ArrayOfObjectsInputProps & {
11
13
  variantName: string
@@ -15,6 +17,8 @@ export declare type ArrayInputProps = ArrayOfObjectsInputProps & {
15
17
 
16
18
  export declare type ExperimentContextProps = Required<FieldPluginConfig> & {
17
19
  experiments: ExperimentType[]
20
+ setSecret: Dispatch<SetStateAction<string | undefined>>
21
+ secret: string | undefined
18
22
  }
19
23
 
20
24
  export declare type ExperimentGeneric<T> = {
@@ -56,6 +60,196 @@ export declare type FieldPluginConfig = {
56
60
  */
57
61
  export declare function flattenSchemaType(schemaType: SchemaType): ObjectFieldWithPath[]
58
62
 
63
+ export declare const launchDarklyFieldLevel: Plugin_2<LaunchDarklyFieldLevelConfig>
64
+
65
+ export declare type LaunchDarklyFieldLevelConfig = {
66
+ fields: (string | FieldDefinition)[]
67
+ projectKey: string
68
+ tags?: string[]
69
+ }
70
+
71
+ export declare type LaunchDarklyFlagItem = {
72
+ name: string
73
+ kind: string
74
+ key: string
75
+ _version: number
76
+ creationDate: number
77
+ variations: Array<{
78
+ value: boolean
79
+ _id: string
80
+ name: string
81
+ }>
82
+ temporary: boolean
83
+ tags: string[]
84
+ _links: {
85
+ parent: {
86
+ href: string
87
+ type: string
88
+ }
89
+ self: {
90
+ href: string
91
+ type: string
92
+ }
93
+ }
94
+ experiments: {
95
+ baselineIdx: number
96
+ items: Array<{
97
+ metricKey: string
98
+ _metric: {
99
+ _id: string
100
+ _versionId: string
101
+ key: string
102
+ name: string
103
+ kind: string
104
+ _links: {
105
+ parent: {
106
+ href: string
107
+ type: string
108
+ }
109
+ self: {
110
+ href: string
111
+ type: string
112
+ }
113
+ }
114
+ tags: string[]
115
+ _creationDate: number
116
+ experimentCount: number
117
+ metricGroupCount: number
118
+ _attachedFlagCount: number
119
+ maintainerId: string
120
+ _maintainer: {
121
+ _links: {
122
+ self: {
123
+ href: string
124
+ type: string
125
+ }
126
+ }
127
+ _id: string
128
+ role: string
129
+ email: string
130
+ firstName: string
131
+ lastName: string
132
+ }
133
+ category: string
134
+ isNumeric: boolean
135
+ percentileValue: number
136
+ }
137
+ }>
138
+ }
139
+ customProperties: {
140
+ key: {
141
+ name: string
142
+ value: string[]
143
+ }
144
+ }
145
+ archived: boolean
146
+ description: string
147
+ maintainerId: string
148
+ _maintainer: {
149
+ _links: {
150
+ self: {
151
+ href: string
152
+ type: string
153
+ }
154
+ }
155
+ _id: string
156
+ role: string
157
+ email: string
158
+ firstName: string
159
+ lastName: string
160
+ }
161
+ maintainerTeamKey: string
162
+ _maintainerTeam: {
163
+ key: string
164
+ name: string
165
+ _links: {
166
+ parent: {
167
+ href: string
168
+ type: string
169
+ }
170
+ roles: {
171
+ href: string
172
+ type: string
173
+ }
174
+ self: {
175
+ href: string
176
+ type: string
177
+ }
178
+ }
179
+ }
180
+ archivedDate: number
181
+ deprecated: boolean
182
+ deprecatedDate: number
183
+ defaults: {
184
+ onVariation: number
185
+ offVariation: number
186
+ }
187
+ _purpose: string
188
+ migrationSettings: {
189
+ contextKind: string
190
+ stageCount: number
191
+ }
192
+ environments: {
193
+ [key: string]: {
194
+ on: boolean
195
+ archived: boolean
196
+ salt: string
197
+ sel: string
198
+ lastModified: number
199
+ version: number
200
+ _site: {
201
+ href: string
202
+ type: string
203
+ }
204
+ _environmentName: string
205
+ trackEvents: boolean
206
+ trackEventsFallthrough: boolean
207
+ targets: Array<{
208
+ values: string[]
209
+ variation: number
210
+ contextKind: string
211
+ }>
212
+ contextTargets: Array<{
213
+ values: string[]
214
+ variation: number
215
+ contextKind: string
216
+ }>
217
+ rules: Array<{
218
+ clauses: Array<{
219
+ attribute: string
220
+ op: string
221
+ values: unknown[]
222
+ negate: boolean
223
+ }>
224
+ trackEvents: boolean
225
+ }>
226
+ fallthrough: {
227
+ variation: number
228
+ }
229
+ offVariation: number
230
+ prerequisites: Array<{
231
+ key: string
232
+ variation: number
233
+ }>
234
+ _summary: {
235
+ variations: {
236
+ [key: string]: {
237
+ rules: number
238
+ nullRules: number
239
+ targets: number
240
+ contextTargets: number
241
+ isFallthrough?: boolean
242
+ isOff?: boolean
243
+ }
244
+ }
245
+ prerequisites: number
246
+ }
247
+ }
248
+ }
249
+ includeInSnippet: boolean
250
+ goalIds: string[]
251
+ }
252
+
59
253
  export declare type ObjectFieldWithPath = ObjectField<SchemaType> & {
60
254
  path: Path
61
255
  }
package/dist/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import {ArrayOfObjectsInputProps} from 'sanity'
2
+ import {Dispatch} from 'react'
2
3
  import {FieldDefinition} from 'sanity'
3
4
  import {ObjectField} from 'sanity'
4
5
  import {Path} from 'sanity'
@@ -6,6 +7,7 @@ import {Plugin as Plugin_2} from 'sanity'
6
7
  import {PreviewProps} from 'sanity'
7
8
  import {SanityClient} from 'sanity'
8
9
  import {SchemaType} from 'sanity'
10
+ import {SetStateAction} from 'react'
9
11
 
10
12
  export declare type ArrayInputProps = ArrayOfObjectsInputProps & {
11
13
  variantName: string
@@ -15,6 +17,8 @@ export declare type ArrayInputProps = ArrayOfObjectsInputProps & {
15
17
 
16
18
  export declare type ExperimentContextProps = Required<FieldPluginConfig> & {
17
19
  experiments: ExperimentType[]
20
+ setSecret: Dispatch<SetStateAction<string | undefined>>
21
+ secret: string | undefined
18
22
  }
19
23
 
20
24
  export declare type ExperimentGeneric<T> = {
@@ -56,6 +60,196 @@ export declare type FieldPluginConfig = {
56
60
  */
57
61
  export declare function flattenSchemaType(schemaType: SchemaType): ObjectFieldWithPath[]
58
62
 
63
+ export declare const launchDarklyFieldLevel: Plugin_2<LaunchDarklyFieldLevelConfig>
64
+
65
+ export declare type LaunchDarklyFieldLevelConfig = {
66
+ fields: (string | FieldDefinition)[]
67
+ projectKey: string
68
+ tags?: string[]
69
+ }
70
+
71
+ export declare type LaunchDarklyFlagItem = {
72
+ name: string
73
+ kind: string
74
+ key: string
75
+ _version: number
76
+ creationDate: number
77
+ variations: Array<{
78
+ value: boolean
79
+ _id: string
80
+ name: string
81
+ }>
82
+ temporary: boolean
83
+ tags: string[]
84
+ _links: {
85
+ parent: {
86
+ href: string
87
+ type: string
88
+ }
89
+ self: {
90
+ href: string
91
+ type: string
92
+ }
93
+ }
94
+ experiments: {
95
+ baselineIdx: number
96
+ items: Array<{
97
+ metricKey: string
98
+ _metric: {
99
+ _id: string
100
+ _versionId: string
101
+ key: string
102
+ name: string
103
+ kind: string
104
+ _links: {
105
+ parent: {
106
+ href: string
107
+ type: string
108
+ }
109
+ self: {
110
+ href: string
111
+ type: string
112
+ }
113
+ }
114
+ tags: string[]
115
+ _creationDate: number
116
+ experimentCount: number
117
+ metricGroupCount: number
118
+ _attachedFlagCount: number
119
+ maintainerId: string
120
+ _maintainer: {
121
+ _links: {
122
+ self: {
123
+ href: string
124
+ type: string
125
+ }
126
+ }
127
+ _id: string
128
+ role: string
129
+ email: string
130
+ firstName: string
131
+ lastName: string
132
+ }
133
+ category: string
134
+ isNumeric: boolean
135
+ percentileValue: number
136
+ }
137
+ }>
138
+ }
139
+ customProperties: {
140
+ key: {
141
+ name: string
142
+ value: string[]
143
+ }
144
+ }
145
+ archived: boolean
146
+ description: string
147
+ maintainerId: string
148
+ _maintainer: {
149
+ _links: {
150
+ self: {
151
+ href: string
152
+ type: string
153
+ }
154
+ }
155
+ _id: string
156
+ role: string
157
+ email: string
158
+ firstName: string
159
+ lastName: string
160
+ }
161
+ maintainerTeamKey: string
162
+ _maintainerTeam: {
163
+ key: string
164
+ name: string
165
+ _links: {
166
+ parent: {
167
+ href: string
168
+ type: string
169
+ }
170
+ roles: {
171
+ href: string
172
+ type: string
173
+ }
174
+ self: {
175
+ href: string
176
+ type: string
177
+ }
178
+ }
179
+ }
180
+ archivedDate: number
181
+ deprecated: boolean
182
+ deprecatedDate: number
183
+ defaults: {
184
+ onVariation: number
185
+ offVariation: number
186
+ }
187
+ _purpose: string
188
+ migrationSettings: {
189
+ contextKind: string
190
+ stageCount: number
191
+ }
192
+ environments: {
193
+ [key: string]: {
194
+ on: boolean
195
+ archived: boolean
196
+ salt: string
197
+ sel: string
198
+ lastModified: number
199
+ version: number
200
+ _site: {
201
+ href: string
202
+ type: string
203
+ }
204
+ _environmentName: string
205
+ trackEvents: boolean
206
+ trackEventsFallthrough: boolean
207
+ targets: Array<{
208
+ values: string[]
209
+ variation: number
210
+ contextKind: string
211
+ }>
212
+ contextTargets: Array<{
213
+ values: string[]
214
+ variation: number
215
+ contextKind: string
216
+ }>
217
+ rules: Array<{
218
+ clauses: Array<{
219
+ attribute: string
220
+ op: string
221
+ values: unknown[]
222
+ negate: boolean
223
+ }>
224
+ trackEvents: boolean
225
+ }>
226
+ fallthrough: {
227
+ variation: number
228
+ }
229
+ offVariation: number
230
+ prerequisites: Array<{
231
+ key: string
232
+ variation: number
233
+ }>
234
+ _summary: {
235
+ variations: {
236
+ [key: string]: {
237
+ rules: number
238
+ nullRules: number
239
+ targets: number
240
+ contextTargets: number
241
+ isFallthrough?: boolean
242
+ isOff?: boolean
243
+ }
244
+ }
245
+ prerequisites: number
246
+ }
247
+ }
248
+ }
249
+ includeInSnippet: boolean
250
+ goalIds: string[]
251
+ }
252
+
59
253
  export declare type ObjectFieldWithPath = ObjectField<SchemaType> & {
60
254
  path: Path
61
255
  }
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: !0 });
3
- var jsxRuntime = require("react/jsx-runtime"), sanity = require("sanity"), ui = require("@sanity/ui"), uuid = require("@sanity/uuid"), react = require("react"), equal = require("fast-deep-equal"), suspendReact = require("suspend-react"), gi = require("react-icons/gi");
3
+ var jsxRuntime = require("react/jsx-runtime"), sanity = require("sanity"), ui = require("@sanity/ui"), uuid = require("@sanity/uuid"), react = require("react"), equal = require("fast-deep-equal"), suspendReact = require("suspend-react"), gi = require("react-icons/gi"), studioSecrets = require("@sanity/studio-secrets");
4
4
  function _interopDefaultCompat(e) {
5
5
  return e && typeof e == "object" && "default" in e ? e : { default: e };
6
6
  }
@@ -15,20 +15,23 @@ const CONFIG_DEFAULT = {
15
15
  experimentId: "experimentId"
16
16
  }, ExperimentContext = react.createContext({
17
17
  ...CONFIG_DEFAULT,
18
- experiments: []
18
+ experiments: [],
19
+ setSecret: () => {
20
+ },
21
+ secret: void 0
19
22
  });
20
23
  function useExperimentContext() {
21
24
  return react.useContext(ExperimentContext);
22
25
  }
23
26
  function ExperimentProvider(props) {
24
- const { experimentFieldPluginConfig } = props, client = sanity.useClient({ apiVersion: experimentFieldPluginConfig.apiVersion }), workspace = sanity.useWorkspace(), experiments = Array.isArray(experimentFieldPluginConfig.experiments) ? experimentFieldPluginConfig.experiments : suspendReact.suspend(
27
+ const { experimentFieldPluginConfig } = props, [secret, setSecret] = react.useState(), client = sanity.useClient({ apiVersion: experimentFieldPluginConfig.apiVersion }), workspace = sanity.useWorkspace(), experiments = Array.isArray(experimentFieldPluginConfig.experiments) ? experimentFieldPluginConfig.experiments : suspendReact.suspend(
25
28
  // eslint-disable-next-line require-await
26
29
  async () => typeof experimentFieldPluginConfig.experiments == "function" ? experimentFieldPluginConfig.experiments(client) : experimentFieldPluginConfig.experiments,
27
- [workspace],
30
+ [workspace, secret],
28
31
  { equal: equal__default.default }
29
32
  ), context = react.useMemo(
30
- () => ({ ...experimentFieldPluginConfig, experiments }),
31
- [experimentFieldPluginConfig, experiments]
33
+ () => ({ ...experimentFieldPluginConfig, experiments, secret, setSecret }),
34
+ [experimentFieldPluginConfig, experiments, secret, setSecret]
32
35
  );
33
36
  return /* @__PURE__ */ jsxRuntime.jsx(ExperimentContext.Provider, { value: context, children: props.renderDefault(props) });
34
37
  }
@@ -6742,7 +6745,79 @@ const createExperimentType = ({
6742
6745
  }
6743
6746
  }
6744
6747
  };
6748
+ }), pluginConfigKeys = [
6749
+ {
6750
+ key: "apiKey",
6751
+ title: "Your secret API key"
6752
+ }
6753
+ ], Secrets = (props, namespace) => {
6754
+ const { secrets, loading } = studioSecrets.useSecrets(namespace), { setSecret } = useExperimentContext(), [showSettings, setShowSettings] = react.useState(!1);
6755
+ return react.useEffect(() => {
6756
+ if (!loading)
6757
+ return !secrets && !loading ? (setSecret(void 0), setShowSettings(!0)) : (setSecret(secrets.apiKey), setShowSettings(!1));
6758
+ }, [secrets, loading, setSecret]), showSettings ? /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
6759
+ /* @__PURE__ */ jsxRuntime.jsx(
6760
+ studioSecrets.SettingsView,
6761
+ {
6762
+ title: `${namespace} api key`,
6763
+ namespace,
6764
+ keys: pluginConfigKeys,
6765
+ onClose: () => {
6766
+ setShowSettings(!1);
6767
+ }
6768
+ }
6769
+ ),
6770
+ props.renderDefault(props)
6771
+ ] }) : props.renderDefault(props);
6772
+ }, getExperiments = async ({
6773
+ client,
6774
+ projectKey,
6775
+ tags
6776
+ }) => {
6777
+ const secret = await client.fetch("*[_id == 'secrets.launchdarkly'][0].secrets.apiKey");
6778
+ if (!secret) return [];
6779
+ const url = new URL(`https://app.launchdarkly.com/api/v2/flags/${projectKey}`);
6780
+ tags && url.searchParams.set("filter", `tags:${tags.join("+")}`);
6781
+ const featureExperiments = [];
6782
+ let hasMore = !0;
6783
+ const offset = 0, limit = 10;
6784
+ for (; hasMore; ) {
6785
+ url.searchParams.set("offset", offset.toString()), url.searchParams.set("limit", limit.toString());
6786
+ const responseFlags = await fetch(url, {
6787
+ headers: {
6788
+ Authorization: secret
6789
+ }
6790
+ }), { items } = await responseFlags.json(), experiments = items.map((flag) => ({
6791
+ id: flag.key,
6792
+ label: flag.name,
6793
+ variants: flag.variations.map((variation) => ({
6794
+ id: variation.value,
6795
+ label: variation.name
6796
+ }))
6797
+ }));
6798
+ featureExperiments.push(...experiments), items.length !== limit && (hasMore = !1);
6799
+ }
6800
+ return featureExperiments;
6801
+ }, launchDarklyFieldLevel = sanity.definePlugin((config) => {
6802
+ const { fields, projectKey, tags } = config;
6803
+ return {
6804
+ name: "sanity-growthbook-personalistaion-plugin-field-level-experiments",
6805
+ plugins: [
6806
+ fieldLevelExperiments({
6807
+ fields,
6808
+ experiments: (client) => getExperiments({ client, projectKey, tags })
6809
+ })
6810
+ ],
6811
+ form: {
6812
+ components: {
6813
+ input: (props) => !(props.id === "root" && sanity.isObjectInputProps(props)) || !flattenSchemaType(props.schemaType).map(
6814
+ (field) => field.type.name
6815
+ ).some((name) => name.startsWith("experiment")) ? props.renderDefault(props) : Secrets(props, "launchdarkly")
6816
+ }
6817
+ }
6818
+ };
6745
6819
  });
6746
6820
  exports.fieldLevelExperiments = fieldLevelExperiments;
6747
6821
  exports.flattenSchemaType = flattenSchemaType;
6822
+ exports.launchDarklyFieldLevel = launchDarklyFieldLevel;
6748
6823
  //# sourceMappingURL=index.js.map