@sanity/personalization-plugin 2.2.0-launch-darkly.1 → 2.2.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/dist/index.d.mts CHANGED
@@ -1,5 +1,4 @@
1
1
  import {ArrayOfObjectsInputProps} from 'sanity'
2
- import {Dispatch} from 'react'
3
2
  import {FieldDefinition} from 'sanity'
4
3
  import {ObjectField} from 'sanity'
5
4
  import {Path} from 'sanity'
@@ -7,7 +6,6 @@ import {Plugin as Plugin_2} from 'sanity'
7
6
  import {PreviewProps} from 'sanity'
8
7
  import {SanityClient} from 'sanity'
9
8
  import {SchemaType} from 'sanity'
10
- import {SetStateAction} from 'react'
11
9
 
12
10
  export declare type ArrayInputProps = ArrayOfObjectsInputProps & {
13
11
  variantName: string
@@ -17,8 +15,6 @@ export declare type ArrayInputProps = ArrayOfObjectsInputProps & {
17
15
 
18
16
  export declare type ExperimentContextProps = Required<FieldPluginConfig> & {
19
17
  experiments: ExperimentType[]
20
- setSecret: Dispatch<SetStateAction<string | undefined>>
21
- secret: string | undefined
22
18
  }
23
19
 
24
20
  export declare type ExperimentGeneric<T> = {
@@ -60,196 +56,6 @@ export declare type FieldPluginConfig = {
60
56
  */
61
57
  export declare function flattenSchemaType(schemaType: SchemaType): ObjectFieldWithPath[]
62
58
 
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
-
253
59
  export declare type ObjectFieldWithPath = ObjectField<SchemaType> & {
254
60
  path: Path
255
61
  }
package/dist/index.d.ts CHANGED
@@ -1,5 +1,4 @@
1
1
  import {ArrayOfObjectsInputProps} from 'sanity'
2
- import {Dispatch} from 'react'
3
2
  import {FieldDefinition} from 'sanity'
4
3
  import {ObjectField} from 'sanity'
5
4
  import {Path} from 'sanity'
@@ -7,7 +6,6 @@ import {Plugin as Plugin_2} from 'sanity'
7
6
  import {PreviewProps} from 'sanity'
8
7
  import {SanityClient} from 'sanity'
9
8
  import {SchemaType} from 'sanity'
10
- import {SetStateAction} from 'react'
11
9
 
12
10
  export declare type ArrayInputProps = ArrayOfObjectsInputProps & {
13
11
  variantName: string
@@ -17,8 +15,6 @@ export declare type ArrayInputProps = ArrayOfObjectsInputProps & {
17
15
 
18
16
  export declare type ExperimentContextProps = Required<FieldPluginConfig> & {
19
17
  experiments: ExperimentType[]
20
- setSecret: Dispatch<SetStateAction<string | undefined>>
21
- secret: string | undefined
22
18
  }
23
19
 
24
20
  export declare type ExperimentGeneric<T> = {
@@ -60,196 +56,6 @@ export declare type FieldPluginConfig = {
60
56
  */
61
57
  export declare function flattenSchemaType(schemaType: SchemaType): ObjectFieldWithPath[]
62
58
 
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
-
253
59
  export declare type ObjectFieldWithPath = ObjectField<SchemaType> & {
254
60
  path: Path
255
61
  }
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"), studioSecrets = require("@sanity/studio-secrets");
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");
4
4
  function _interopDefaultCompat(e) {
5
5
  return e && typeof e == "object" && "default" in e ? e : { default: e };
6
6
  }
@@ -15,23 +15,20 @@ const CONFIG_DEFAULT = {
15
15
  experimentId: "experimentId"
16
16
  }, ExperimentContext = react.createContext({
17
17
  ...CONFIG_DEFAULT,
18
- experiments: [],
19
- setSecret: () => {
20
- },
21
- secret: void 0
18
+ experiments: []
22
19
  });
23
20
  function useExperimentContext() {
24
21
  return react.useContext(ExperimentContext);
25
22
  }
26
23
  function ExperimentProvider(props) {
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(
24
+ const { experimentFieldPluginConfig } = props, client = sanity.useClient({ apiVersion: experimentFieldPluginConfig.apiVersion }), workspace = sanity.useWorkspace(), experiments = Array.isArray(experimentFieldPluginConfig.experiments) ? experimentFieldPluginConfig.experiments : suspendReact.suspend(
28
25
  // eslint-disable-next-line require-await
29
26
  async () => typeof experimentFieldPluginConfig.experiments == "function" ? experimentFieldPluginConfig.experiments(client) : experimentFieldPluginConfig.experiments,
30
- [workspace, secret],
27
+ [workspace],
31
28
  { equal: equal__default.default }
32
29
  ), context = react.useMemo(
33
- () => ({ ...experimentFieldPluginConfig, experiments, secret, setSecret }),
34
- [experimentFieldPluginConfig, experiments, secret, setSecret]
30
+ () => ({ ...experimentFieldPluginConfig, experiments }),
31
+ [experimentFieldPluginConfig, experiments]
35
32
  );
36
33
  return /* @__PURE__ */ jsxRuntime.jsx(ExperimentContext.Provider, { value: context, children: props.renderDefault(props) });
37
34
  }
@@ -6508,7 +6505,11 @@ const useAddExperimentAction = (props) => {
6508
6505
  },
6509
6506
  [patch, subValues, aditionalChangePath]
6510
6507
  );
6511
- return experiments.length ? /* @__PURE__ */ jsxRuntime.jsx(Select, { ...props, listOptions: formatlistOptions(experiments), handleChange }) : /* @__PURE__ */ jsxRuntime.jsx(jsxRuntime.Fragment, {});
6508
+ return experiments.length ? /* @__PURE__ */ jsxRuntime.jsx(Select, { ...props, listOptions: formatlistOptions(experiments), handleChange }) : /* @__PURE__ */ jsxRuntime.jsx(ui.Card, { padding: [3, 3, 4], radius: 2, shadow: 1, tone: "caution", children: /* @__PURE__ */ jsxRuntime.jsxs(ui.Text, { align: "center", size: [2, 2, 3], children: [
6509
+ "There are no defined ",
6510
+ props.experimentNameOverride,
6511
+ "s"
6512
+ ] }) });
6512
6513
  }, VariantInput = (props) => {
6513
6514
  const defaultValue = sanity.useFormValue([props.path[0], "default"]), handleClick = () => {
6514
6515
  props.onChange(sanity.set(defaultValue, ["value"]));
@@ -6609,7 +6610,14 @@ const createExperimentType = ({
6609
6610
  name: experimentId,
6610
6611
  type: "string",
6611
6612
  components: {
6612
- input: (props) => /* @__PURE__ */ jsxRuntime.jsx(ExperimentInput, { ...props, variantNameOverride })
6613
+ input: (props) => /* @__PURE__ */ jsxRuntime.jsx(
6614
+ ExperimentInput,
6615
+ {
6616
+ ...props,
6617
+ experimentNameOverride,
6618
+ variantNameOverride
6619
+ }
6620
+ )
6613
6621
  },
6614
6622
  hidden: ({ parent }) => !parent?.active
6615
6623
  }),
@@ -6745,79 +6753,7 @@ const createExperimentType = ({
6745
6753
  }
6746
6754
  }
6747
6755
  };
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
- };
6819
6756
  });
6820
6757
  exports.fieldLevelExperiments = fieldLevelExperiments;
6821
6758
  exports.flattenSchemaType = flattenSchemaType;
6822
- exports.launchDarklyFieldLevel = launchDarklyFieldLevel;
6823
6759
  //# sourceMappingURL=index.js.map