@sanity/personalization-plugin 2.5.0-launch-darkly.1 → 3.0.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.
Files changed (48) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +640 -109
  3. package/dist/growthbook/index.d.ts +12 -15
  4. package/dist/growthbook/index.d.ts.map +1 -0
  5. package/dist/growthbook/index.js +104 -68
  6. package/dist/growthbook/index.js.map +1 -1
  7. package/dist/index.d.ts +212 -252
  8. package/dist/index.d.ts.map +1 -0
  9. package/dist/index.js +417 -7195
  10. package/dist/index.js.map +1 -1
  11. package/dist/launchDarkly/index.d.ts +9 -12
  12. package/dist/launchDarkly/index.d.ts.map +1 -0
  13. package/dist/launchDarkly/index.js +74 -46
  14. package/dist/launchDarkly/index.js.map +1 -1
  15. package/package.json +36 -78
  16. package/dist/growthbook/index.d.mts +0 -15
  17. package/dist/growthbook/index.mjs +0 -124
  18. package/dist/growthbook/index.mjs.map +0 -1
  19. package/dist/index.d.mts +0 -267
  20. package/dist/index.mjs +0 -7347
  21. package/dist/index.mjs.map +0 -1
  22. package/dist/launchDarkly/index.d.mts +0 -12
  23. package/dist/launchDarkly/index.mjs +0 -107
  24. package/dist/launchDarkly/index.mjs.map +0 -1
  25. package/sanity.json +0 -8
  26. package/src/components/Array.tsx +0 -68
  27. package/src/components/ExperimentContext.tsx +0 -65
  28. package/src/components/ExperimentField.tsx +0 -138
  29. package/src/components/ExperimentInput.tsx +0 -75
  30. package/src/components/Select.tsx +0 -43
  31. package/src/components/VariantInput.tsx +0 -18
  32. package/src/components/VariantPreview.tsx +0 -75
  33. package/src/fieldExperiments.tsx +0 -264
  34. package/src/growthbook/Components/GrowthbookContext.tsx +0 -38
  35. package/src/growthbook/Components/Secrets.tsx +0 -47
  36. package/src/growthbook/index.ts +0 -54
  37. package/src/growthbook/types.ts +0 -15
  38. package/src/growthbook/utils.ts +0 -94
  39. package/src/index.ts +0 -3
  40. package/src/launchDarkly/components/LaunchDarklyContext.tsx +0 -36
  41. package/src/launchDarkly/components/Secrets.tsx +0 -46
  42. package/src/launchDarkly/index.ts +0 -52
  43. package/src/launchDarkly/launchdarkly.md +0 -76
  44. package/src/launchDarkly/types.ts +0 -193
  45. package/src/launchDarkly/utils.ts +0 -54
  46. package/src/types.ts +0 -245
  47. package/src/utils/flattenSchemaType.ts +0 -47
  48. package/v2-incompatible.js +0 -11
@@ -1,15 +1,12 @@
1
- import {FieldDefinition} from 'sanity'
2
- import {Plugin as Plugin_2} from 'sanity'
3
-
4
- export declare const fieldLevelExperiments: Plugin_2<GrowthbookExperimentFieldPluginConfig>
5
-
6
- declare type GrowthbookExperimentFieldPluginConfig = {
7
- fields: (string | FieldDefinition)[]
8
- environment: string
9
- baseUrl?: string
10
- project?: string
11
- convertBooleans?: boolean
12
- tags?: string[]
13
- }
14
-
15
- export {}
1
+ import { FieldDefinition } from "sanity";
2
+ type GrowthbookExperimentFieldPluginConfig = {
3
+ fields: (string | FieldDefinition)[];
4
+ environment: string;
5
+ baseUrl?: string;
6
+ project?: string;
7
+ convertBooleans?: boolean;
8
+ tags?: string[];
9
+ };
10
+ declare const fieldLevelExperiments: import("sanity").Plugin<GrowthbookExperimentFieldPluginConfig>;
11
+ export { fieldLevelExperiments };
12
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","names":[],"sources":["../../src/growthbook/types.ts","../../src/growthbook/index.ts"],"mappings":";KAEY,qCAAA;EACV,MAAA,YAAkB,eAAe;EACjC,WAAA;EACA,OAAA;EACA,OAAA;EACA,eAAA;EACA,IAAA;AAAA;AAAA,cCAW,qBAAA,mBAAqB,MAAA,CAAA,qCAAA"}
@@ -1,46 +1,63 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: !0 });
3
- var sanity = require("sanity"), index = require("../index.js"), jsxRuntime = require("react/jsx-runtime"), react = require("react"), studioSecrets = require("@sanity/studio-secrets");
4
- const namespace = "growthbook", pluginConfigKeys = [
5
- {
6
- key: "apiKey",
7
- title: "Your secret API key"
8
- }
9
- ], Secrets = (props) => {
10
- const { secrets, loading } = studioSecrets.useSecrets(namespace), { setSecret } = useGrowthbookContext(), [showSettings, setShowSettings] = react.useState(!1);
11
- return react.useEffect(() => {
1
+ import { definePlugin, isObjectInputProps } from "sanity";
2
+ import { flattenSchemaType, fieldLevelExperiments as fieldLevelExperiments$1 } from "../index.js";
3
+ import { jsxs, Fragment, jsx } from "react/jsx-runtime";
4
+ import { c } from "react/compiler-runtime";
5
+ import { useState, useEffect, createContext, useContext } from "react";
6
+ import { useSecrets, SettingsView } from "@sanity/studio-secrets";
7
+ const namespace = "growthbook", apiKeyName = "apiKey", pluginConfigKeys = [{
8
+ key: apiKeyName,
9
+ title: "Your secret API key"
10
+ }], Secrets = (props) => {
11
+ const $ = c(12), {
12
+ secrets,
13
+ loading
14
+ } = useSecrets(namespace), {
15
+ setSecret
16
+ } = useGrowthbookContext(), [showSettings, setShowSettings] = useState(!1);
17
+ let t0, t1;
18
+ if ($[0] !== loading || $[1] !== secrets || $[2] !== setSecret ? (t0 = () => {
12
19
  if (!loading)
13
20
  return !secrets && !loading ? (setSecret(void 0), setShowSettings(!0)) : (setSecret(secrets.apiKey), setShowSettings(!1));
14
- }, [secrets, loading, setSecret]), showSettings ? /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
15
- /* @__PURE__ */ jsxRuntime.jsx(
16
- studioSecrets.SettingsView,
17
- {
18
- title: "Growthbook secret",
19
- namespace,
20
- keys: pluginConfigKeys,
21
- onClose: () => {
22
- setShowSettings(!1);
23
- }
24
- }
25
- ),
26
- props.renderDefault(props)
27
- ] }) : props.renderDefault(props);
21
+ }, t1 = [secrets, loading, setSecret], $[0] = loading, $[1] = secrets, $[2] = setSecret, $[3] = t0, $[4] = t1) : (t0 = $[3], t1 = $[4]), useEffect(t0, t1), !showSettings) {
22
+ let t22;
23
+ return $[5] !== props ? (t22 = props.renderDefault(props), $[5] = props, $[6] = t22) : t22 = $[6], t22;
24
+ }
25
+ let t2;
26
+ $[7] === /* @__PURE__ */ Symbol.for("react.memo_cache_sentinel") ? (t2 = /* @__PURE__ */ jsx(SettingsView, { title: "Growthbook secret", namespace, keys: pluginConfigKeys, onClose: () => {
27
+ setShowSettings(!1);
28
+ } }), $[7] = t2) : t2 = $[7];
29
+ let t3;
30
+ $[8] !== props ? (t3 = props.renderDefault(props), $[8] = props, $[9] = t3) : t3 = $[9];
31
+ let t4;
32
+ return $[10] !== t3 ? (t4 = /* @__PURE__ */ jsxs(Fragment, { children: [
33
+ t2,
34
+ t3
35
+ ] }), $[10] = t3, $[11] = t4) : t4 = $[11], t4;
28
36
  }, GROWTHBOOK_CONFIG_DEFAULT = {
29
37
  baseUrl: "https://api.growthbook.io/api/v1"
30
- }, GrowthbookContext = react.createContext({
38
+ }, GrowthbookContext = createContext({
31
39
  setSecret: () => {
32
40
  },
33
41
  secret: void 0
34
42
  });
35
43
  function useGrowthbookContext() {
36
- return react.useContext(GrowthbookContext);
44
+ return useContext(GrowthbookContext);
37
45
  }
38
46
  function GrowthbookProvider(props) {
39
- const { growthbookFieldPluginConfig } = props, [secret, setSecret] = react.useState(), context = react.useMemo(
40
- () => ({ ...growthbookFieldPluginConfig, secret, setSecret }),
41
- [growthbookFieldPluginConfig, secret, setSecret]
42
- );
43
- return /* @__PURE__ */ jsxRuntime.jsx(GrowthbookContext.Provider, { value: context, children: /* @__PURE__ */ jsxRuntime.jsx(Secrets, { ...props }) });
47
+ const $ = c(8), {
48
+ growthbookFieldPluginConfig
49
+ } = props, [secret, setSecret] = useState();
50
+ let t0;
51
+ $[0] !== growthbookFieldPluginConfig || $[1] !== secret ? (t0 = {
52
+ ...growthbookFieldPluginConfig,
53
+ secret,
54
+ setSecret
55
+ }, $[0] = growthbookFieldPluginConfig, $[1] = secret, $[2] = t0) : t0 = $[2];
56
+ const context = t0;
57
+ let t1;
58
+ $[3] !== props ? (t1 = /* @__PURE__ */ jsx(Secrets, { ...props }), $[3] = props, $[4] = t1) : t1 = $[4];
59
+ let t2;
60
+ return $[5] !== context || $[6] !== t1 ? (t2 = /* @__PURE__ */ jsx(GrowthbookContext.Provider, { value: context, children: t1 }), $[5] = context, $[6] = t1, $[7] = t2) : t2 = $[7], t2;
44
61
  }
45
62
  const getBooleanConversion = (value) => value === "true" ? "variant" : value === "false" ? "control" : value, getExperiments = async ({
46
63
  client,
@@ -50,7 +67,7 @@ const getBooleanConversion = (value) => value === "true" ? "variant" : value ===
50
67
  convertBooleans,
51
68
  tags
52
69
  }) => {
53
- const query = `*[_id == 'secrets.${namespace}'][0].secrets.${pluginConfigKeys[0].key}`, secret = await client.fetch(query);
70
+ const query = `*[_id == 'secrets.${namespace}'][0].secrets.${apiKeyName}`, secret = await client.fetch(query);
54
71
  if (!secret) return [];
55
72
  const featureExperiments = [];
56
73
  let hasMore = !0, offset = 0;
@@ -61,13 +78,15 @@ const getBooleanConversion = (value) => value === "true" ? "variant" : value ===
61
78
  headers: {
62
79
  Authorization: `Bearer ${secret}`
63
80
  }
64
- }), { features, hasMore: responseHasMore, nextOffset } = await response.json();
81
+ }), {
82
+ features,
83
+ hasMore: responseHasMore,
84
+ nextOffset
85
+ } = await response.json();
65
86
  hasMore = responseHasMore, offset = nextOffset, features && features.forEach((feature) => {
66
87
  if (feature.archived || tags && feature.tags && !feature.tags.some((tag) => tags.includes(tag)))
67
88
  return;
68
- const experiments = feature.environments[environment]?.rules.filter(
69
- (experiment) => experiment.type === "experiment-ref" || experiment.type === "experiment"
70
- );
89
+ const experiments = feature.environments[environment]?.rules.filter((experiment) => experiment.type === "experiment-ref" || experiment.type === "experiment");
71
90
  if (!experiments)
72
91
  return;
73
92
  const variations = [], uniqueValues = /* @__PURE__ */ new Set();
@@ -80,41 +99,58 @@ const getBooleanConversion = (value) => value === "true" ? "variant" : value ===
80
99
  }));
81
100
  });
82
101
  });
83
- const value = { id: feature.id, label: feature.id, variants: variations };
102
+ const value = {
103
+ id: feature.id,
104
+ label: feature.id,
105
+ variants: variations
106
+ };
84
107
  featureExperiments.push(value);
85
108
  });
86
109
  }
87
110
  return featureExperiments.sort((a, b) => a.id.localeCompare(b.id));
88
- }, fieldLevelExperiments = sanity.definePlugin(
89
- (config) => {
90
- const pluginConfig = { ...GROWTHBOOK_CONFIG_DEFAULT, ...config }, { fields, environment, project, convertBooleans, baseUrl, tags } = pluginConfig;
91
- return {
92
- name: "sanity-growthbook-personalistaion-plugin-field-level-experiments",
93
- plugins: [
94
- index.fieldLevelExperiments({
95
- fields,
96
- experiments: (client) => getExperiments({ client, environment, baseUrl, project, convertBooleans, tags })
97
- })
98
- ],
99
- form: {
100
- components: {
101
- input: (props) => {
102
- if (!(props.id === "root" && sanity.isObjectInputProps(props)) || !index.flattenSchemaType(props.schemaType).map(
103
- (field) => field.type.name
104
- ).some((name) => name.startsWith("experiment")))
105
- return props.renderDefault(props);
106
- const providerProps = {
107
- ...props,
108
- growthbookFieldPluginConfig: {
109
- ...pluginConfig
110
- }
111
- };
112
- return GrowthbookProvider(providerProps);
113
- }
111
+ }, fieldLevelExperiments = definePlugin((config) => {
112
+ const pluginConfig = {
113
+ ...GROWTHBOOK_CONFIG_DEFAULT,
114
+ ...config
115
+ }, {
116
+ fields,
117
+ environment,
118
+ project,
119
+ convertBooleans,
120
+ baseUrl,
121
+ tags
122
+ } = pluginConfig;
123
+ return {
124
+ name: "sanity-growthbook-personalistaion-plugin-field-level-experiments",
125
+ plugins: [fieldLevelExperiments$1({
126
+ fields,
127
+ experiments: (client) => getExperiments({
128
+ client,
129
+ environment,
130
+ baseUrl,
131
+ project,
132
+ convertBooleans,
133
+ tags
134
+ })
135
+ })],
136
+ form: {
137
+ components: {
138
+ input: (props) => {
139
+ if (!(props.id === "root" && isObjectInputProps(props)) || !flattenSchemaType(props.schemaType).map((field) => field.type.name).some((name) => name.startsWith("experiment")))
140
+ return props.renderDefault(props);
141
+ const providerProps = {
142
+ ...props,
143
+ growthbookFieldPluginConfig: {
144
+ ...pluginConfig
145
+ }
146
+ };
147
+ return GrowthbookProvider(providerProps);
114
148
  }
115
149
  }
116
- };
117
- }
118
- );
119
- exports.fieldLevelExperiments = fieldLevelExperiments;
150
+ }
151
+ };
152
+ });
153
+ export {
154
+ fieldLevelExperiments
155
+ };
120
156
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sources":["../../src/growthbook/Components/Secrets.tsx","../../src/growthbook/Components/GrowthbookContext.tsx","../../src/growthbook/utils.ts","../../src/growthbook/index.ts"],"sourcesContent":["import {SettingsView, useSecrets} from '@sanity/studio-secrets'\nimport {useEffect, useState} from 'react'\nimport {ObjectInputProps} from 'sanity'\n\nimport {useGrowthbookContext} from './GrowthbookContext'\n\nexport const namespace = 'growthbook'\n\nexport const pluginConfigKeys = [\n {\n key: 'apiKey',\n title: 'Your secret API key',\n },\n]\n\nexport const Secrets = (props: ObjectInputProps) => {\n const {secrets, loading} = useSecrets(namespace) as {secrets: {apiKey: string}; loading: boolean}\n const {setSecret} = useGrowthbookContext()\n const [showSettings, setShowSettings] = useState<boolean>(false)\n\n useEffect(() => {\n if (loading) return undefined\n if (!secrets && !loading) {\n setSecret(undefined)\n return setShowSettings(true)\n }\n setSecret(secrets.apiKey)\n return setShowSettings(false)\n }, [secrets, loading, setSecret])\n\n if (!showSettings) {\n return props.renderDefault(props)\n }\n return (\n <>\n <SettingsView\n title={'Growthbook secret'}\n namespace={namespace}\n keys={pluginConfigKeys}\n onClose={() => {\n setShowSettings(false)\n }}\n />\n {props.renderDefault(props)}\n </>\n )\n}\n","import {createContext, useContext, useMemo, useState} from 'react'\nimport {ObjectInputProps} from 'sanity'\n\nimport {GrowthbookContextProps, GrowthbookExperimentFieldPluginConfig} from '../types'\nimport {Secrets} from './Secrets'\n\nexport const GROWTHBOOK_CONFIG_DEFAULT = {\n baseUrl: 'https://api.growthbook.io/api/v1',\n}\n\nexport const GrowthbookContext = createContext<GrowthbookContextProps>({\n setSecret: () => undefined,\n secret: undefined,\n})\n\nexport function useGrowthbookContext() {\n return useContext(GrowthbookContext)\n}\n\ntype GrowthbookProps = ObjectInputProps & {\n growthbookFieldPluginConfig: GrowthbookExperimentFieldPluginConfig\n}\n\nexport function GrowthbookProvider(props: GrowthbookProps) {\n const {growthbookFieldPluginConfig} = props\n const [secret, setSecret] = useState<string | undefined>()\n\n const context = useMemo(\n () => ({...growthbookFieldPluginConfig, secret, setSecret}),\n [growthbookFieldPluginConfig, secret, setSecret],\n )\n\n return (\n <GrowthbookContext.Provider value={context}>\n <Secrets {...props} />\n </GrowthbookContext.Provider>\n )\n}\n","import {SanityClient} from 'sanity'\n\nimport {ExperimentType, GrowthbookFeature, VariantType} from '../types'\nimport {namespace, pluginConfigKeys} from './Components/Secrets'\nimport {GrowthbookExperimentFieldPluginConfig} from './types'\n\nconst getBooleanConversion = (value: string) => {\n // control is false\n if (value === 'true') {\n return 'variant'\n } else if (value === 'false') {\n return 'control'\n }\n return value\n}\n\nexport const getExperiments = async ({\n client,\n environment,\n baseUrl,\n project,\n convertBooleans,\n tags,\n}: Omit<GrowthbookExperimentFieldPluginConfig, 'fields' | 'baseUrl'> & {\n client: SanityClient\n baseUrl: string\n}): Promise<ExperimentType[]> => {\n const query = `*[_id == 'secrets.${namespace}'][0].secrets.${pluginConfigKeys[0].key}`\n\n const secret = await client.fetch(query) // secret is stored in the content lake using @sanity/studio-secrets\n if (!secret) return []\n\n const featureExperiments: ExperimentType[] = []\n let hasMore = true\n let offset = 0\n const url = new URL(`${baseUrl}/features`)\n if (project) {\n url.searchParams.set('projectId', project)\n }\n\n while (hasMore) {\n url.searchParams.set('offset', offset.toString())\n const response = await fetch(url, {\n headers: {\n Authorization: `Bearer ${secret}`,\n },\n })\n\n const {features, hasMore: responseHasMore, nextOffset} = await response.json()\n\n hasMore = responseHasMore\n offset = nextOffset\n if (!features) continue\n\n features.forEach((feature: GrowthbookFeature) => {\n if (feature.archived) {\n return undefined\n }\n if (tags && feature.tags && !feature.tags.some((tag) => tags.includes(tag))) {\n return undefined\n }\n\n const experiments = feature.environments[environment]?.rules.filter(\n (experiment) => experiment.type === 'experiment-ref' || experiment.type === 'experiment',\n )\n\n if (!experiments) {\n return undefined\n }\n\n const variations: VariantType[] = []\n const uniqueValues = new Set<string>()\n\n experiments.forEach((experiment) => {\n experiment?.variations.forEach((variant) => {\n const value = convertBooleans ? getBooleanConversion(variant.value) : variant.value\n if (!uniqueValues.has(value)) {\n uniqueValues.add(value)\n variations.push({\n id: value,\n label: value,\n })\n }\n })\n })\n const value = {id: feature.id, label: feature.id, variants: variations}\n\n featureExperiments.push(value)\n return undefined\n })\n }\n const sortedFeatureExperiments = featureExperiments.sort((a, b) => a.id.localeCompare(b.id))\n return sortedFeatureExperiments\n}\n","import {definePlugin, isObjectInputProps} from 'sanity'\n\nimport {fieldLevelExperiments as baseFieldLevelExperiments} from '../fieldExperiments'\nimport {flattenSchemaType} from '../utils/flattenSchemaType'\nimport {GROWTHBOOK_CONFIG_DEFAULT, GrowthbookProvider} from './Components/GrowthbookContext'\nimport {GrowthbookExperimentFieldPluginConfig} from './types'\nimport {getExperiments} from './utils'\n\nexport const fieldLevelExperiments = definePlugin<GrowthbookExperimentFieldPluginConfig>(\n (config) => {\n const pluginConfig = {...GROWTHBOOK_CONFIG_DEFAULT, ...config}\n const {fields, environment, project, convertBooleans, baseUrl, tags} = pluginConfig\n return {\n name: 'sanity-growthbook-personalistaion-plugin-field-level-experiments',\n plugins: [\n baseFieldLevelExperiments({\n fields,\n experiments: (client) =>\n getExperiments({client, environment, baseUrl, project, convertBooleans, tags}),\n }),\n ],\n\n form: {\n components: {\n input: (props) => {\n const isRootInput = props.id === 'root' && isObjectInputProps(props)\n\n if (!isRootInput) {\n return props.renderDefault(props)\n }\n\n const flatFieldTypeNames = flattenSchemaType(props.schemaType).map(\n (field) => field.type.name,\n )\n\n const hasExperiment = flatFieldTypeNames.some((name) => name.startsWith('experiment'))\n\n if (!hasExperiment) {\n return props.renderDefault(props)\n }\n\n const providerProps = {\n ...props,\n growthbookFieldPluginConfig: {\n ...pluginConfig,\n },\n }\n return GrowthbookProvider(providerProps)\n },\n },\n },\n }\n },\n)\n"],"names":["useSecrets","useState","useEffect","jsxs","Fragment","jsx","SettingsView","createContext","useContext","useMemo","value","definePlugin","baseFieldLevelExperiments","isObjectInputProps","flattenSchemaType"],"mappings":";;;AAMO,MAAM,YAAY,cAEZ,mBAAmB;AAAA,EAC9B;AAAA,IACE,KAAK;AAAA,IACL,OAAO;AAAA,EAAA;AAEX,GAEa,UAAU,CAAC,UAA4B;AAClD,QAAM,EAAC,SAAS,QAAA,IAAWA,cAAAA,WAAW,SAAS,GACzC,EAAC,UAAA,IAAa,wBACd,CAAC,cAAc,eAAe,IAAIC,MAAAA,SAAkB,EAAK;AAY/D,SAVAC,MAAAA,UAAU,MAAM;AACd,QAAI,CAAA;AACJ,aAAI,CAAC,WAAW,CAAC,WACf,UAAU,MAAS,GACZ,gBAAgB,EAAI,MAE7B,UAAU,QAAQ,MAAM,GACjB,gBAAgB,EAAK;AAAA,EAC9B,GAAG,CAAC,SAAS,SAAS,SAAS,CAAC,GAE3B,eAIHC,2BAAAA,KAAAC,qBAAA,EACE,UAAA;AAAA,IAAAC,2BAAAA;AAAAA,MAACC,cAAAA;AAAAA,MAAA;AAAA,QACC,OAAO;AAAA,QACP;AAAA,QACA,MAAM;AAAA,QACN,SAAS,MAAM;AACb,0BAAgB,EAAK;AAAA,QACvB;AAAA,MAAA;AAAA,IAAA;AAAA,IAED,MAAM,cAAc,KAAK;AAAA,EAAA,EAAA,CAC5B,IAbO,MAAM,cAAc,KAAK;AAepC,GCxCa,4BAA4B;AAAA,EACvC,SAAS;AACX,GAEa,oBAAoBC,MAAAA,cAAsC;AAAA,EACrE,WAAW,MAAG;AAAA,EAAA;AAAA,EACd,QAAQ;AACV,CAAC;AAEM,SAAS,uBAAuB;AACrC,SAAOC,MAAAA,WAAW,iBAAiB;AACrC;AAMO,S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AAU,MAAM,KAAK;AAAA,YAAA,EAGiB,KAAK,CAAC,SAAS,KAAK,WAAW,YAAY,CAAC;AAGnF,qBAAO,MAAM,cAAc,KAAK;AAGlC,kBAAM,gBAAgB;AAAA,cACpB,GAAG;AAAA,cACH,6BAA6B;AAAA,gBAC3B,GAAG;AAAA,cAAA;AAAA,YACL;AAEF,mBAAO,mBAAmB,aAAa;AAAA,UACzC;AAAA,QAAA;AAAA,MACF;AAAA,IACF;AAAA,EAEJ;AACF;;"}
1
+ {"version":3,"file":"index.js","sources":["../../src/growthbook/Components/Secrets.tsx","../../src/growthbook/Components/GrowthbookContext.tsx","../../src/growthbook/utils.ts","../../src/growthbook/index.ts"],"sourcesContent":["import {SettingsView, useSecrets} from '@sanity/studio-secrets'\nimport {useEffect, useState} from 'react'\nimport type {ObjectInputProps} from 'sanity'\n\nimport {useGrowthbookContext} from './GrowthbookContext'\n\nexport const namespace = 'growthbook'\nexport const apiKeyName = 'apiKey'\n\nconst pluginConfigKeys = [\n {\n key: apiKeyName,\n title: 'Your secret API key',\n },\n]\n\nexport const Secrets = (props: ObjectInputProps) => {\n const {secrets, loading} = useSecrets(namespace) as {secrets: {apiKey: string}; loading: boolean}\n const {setSecret} = useGrowthbookContext()\n const [showSettings, setShowSettings] = useState<boolean>(false)\n\n useEffect(() => {\n if (loading) return undefined\n if (!secrets && !loading) {\n setSecret(undefined)\n // oxlint-disable-next-line react/react-compiler\n return setShowSettings(true)\n }\n setSecret(secrets.apiKey)\n return setShowSettings(false)\n }, [secrets, loading, setSecret])\n\n if (!showSettings) {\n return props.renderDefault(props)\n }\n return (\n <>\n <SettingsView\n title={'Growthbook secret'}\n namespace={namespace}\n keys={pluginConfigKeys}\n onClose={() => {\n setShowSettings(false)\n }}\n />\n {props.renderDefault(props)}\n </>\n )\n}\n","import {createContext, useContext, useMemo, useState} from 'react'\nimport type {ObjectInputProps} from 'sanity'\n\nimport type {GrowthbookContextProps, GrowthbookExperimentFieldPluginConfig} from '../types'\nimport {Secrets} from './Secrets'\n\nexport const GROWTHBOOK_CONFIG_DEFAULT = {\n baseUrl: 'https://api.growthbook.io/api/v1',\n}\n\nconst GrowthbookContext = createContext<GrowthbookContextProps>({\n setSecret: () => undefined,\n secret: undefined,\n})\n\nexport function useGrowthbookContext() {\n return useContext(GrowthbookContext)\n}\n\ntype GrowthbookProps = ObjectInputProps & {\n growthbookFieldPluginConfig: GrowthbookExperimentFieldPluginConfig\n}\n\nexport function GrowthbookProvider(props: GrowthbookProps) {\n const {growthbookFieldPluginConfig} = props\n const [secret, setSecret] = useState<string | undefined>()\n\n const context = useMemo(\n () => ({...growthbookFieldPluginConfig, secret, setSecret}),\n [growthbookFieldPluginConfig, secret, setSecret],\n )\n\n return (\n <GrowthbookContext.Provider value={context}>\n <Secrets {...props} />\n </GrowthbookContext.Provider>\n )\n}\n","import type {SanityClient} from 'sanity'\n\nimport type {ExperimentType, GrowthbookFeature, VariantType} from '../types'\nimport {apiKeyName, namespace} from './Components/Secrets'\nimport type {GrowthbookExperimentFieldPluginConfig} from './types'\n\nconst getBooleanConversion = (value: string) => {\n // control is false\n if (value === 'true') {\n return 'variant'\n } else if (value === 'false') {\n return 'control'\n }\n return value\n}\n\nexport const getExperiments = async ({\n client,\n environment,\n baseUrl,\n project,\n convertBooleans,\n tags,\n}: Omit<GrowthbookExperimentFieldPluginConfig, 'fields' | 'baseUrl'> & {\n client: SanityClient\n baseUrl: string\n}): Promise<ExperimentType[]> => {\n const query = `*[_id == 'secrets.${namespace}'][0].secrets.${apiKeyName}`\n\n const secret = await client.fetch(query) // secret is stored in the content lake using @sanity/studio-secrets\n if (!secret) return []\n\n const featureExperiments: ExperimentType[] = []\n let hasMore = true\n let offset = 0\n const url = new URL(`${baseUrl}/features`)\n if (project) {\n url.searchParams.set('projectId', project)\n }\n\n while (hasMore) {\n url.searchParams.set('offset', offset.toString())\n const response = await fetch(url, {\n headers: {\n Authorization: `Bearer ${secret}`,\n },\n })\n\n const {features, hasMore: responseHasMore, nextOffset} = await response.json()\n\n hasMore = responseHasMore\n offset = nextOffset\n if (!features) continue\n\n features.forEach((feature: GrowthbookFeature) => {\n if (feature.archived) {\n return undefined\n }\n if (tags && feature.tags && !feature.tags.some((tag) => tags.includes(tag))) {\n return undefined\n }\n\n const experiments = feature.environments[environment]?.rules.filter(\n (experiment) => experiment.type === 'experiment-ref' || experiment.type === 'experiment',\n )\n\n if (!experiments) {\n return undefined\n }\n\n const variations: VariantType[] = []\n const uniqueValues = new Set<string>()\n\n experiments.forEach((experiment) => {\n experiment?.variations.forEach((variant) => {\n const value = convertBooleans ? getBooleanConversion(variant.value) : variant.value\n if (!uniqueValues.has(value)) {\n uniqueValues.add(value)\n variations.push({\n id: value,\n label: value,\n })\n }\n })\n })\n const value = {id: feature.id, label: feature.id, variants: variations}\n\n featureExperiments.push(value)\n return undefined\n })\n }\n const sortedFeatureExperiments = featureExperiments.sort((a, b) => a.id.localeCompare(b.id))\n return sortedFeatureExperiments\n}\n","import {definePlugin, isObjectInputProps} from 'sanity'\n\nimport {fieldLevelExperiments as baseFieldLevelExperiments} from '../fieldExperiments'\nimport {flattenSchemaType} from '../utils/flattenSchemaType'\nimport {GROWTHBOOK_CONFIG_DEFAULT, GrowthbookProvider} from './Components/GrowthbookContext'\nimport type {GrowthbookExperimentFieldPluginConfig} from './types'\nimport {getExperiments} from './utils'\n\nexport const fieldLevelExperiments = definePlugin<GrowthbookExperimentFieldPluginConfig>(\n (config) => {\n const pluginConfig = {...GROWTHBOOK_CONFIG_DEFAULT, ...config}\n const {fields, environment, project, convertBooleans, baseUrl, tags} = pluginConfig\n return {\n name: 'sanity-growthbook-personalistaion-plugin-field-level-experiments',\n plugins: [\n baseFieldLevelExperiments({\n fields,\n experiments: (client) =>\n getExperiments({client, environment, baseUrl, project, convertBooleans, tags}),\n }),\n ],\n\n form: {\n components: {\n input: (props) => {\n const isRootInput = props.id === 'root' && isObjectInputProps(props)\n\n if (!isRootInput) {\n return props.renderDefault(props)\n }\n\n const flatFieldTypeNames = flattenSchemaType(props.schemaType).map(\n (field) => field.type.name,\n )\n\n const hasExperiment = flatFieldTypeNames.some((name) => name.startsWith('experiment'))\n\n if (!hasExperiment) {\n return props.renderDefault(props)\n }\n\n const providerProps = {\n ...props,\n growthbookFieldPluginConfig: {\n ...pluginConfig,\n },\n }\n return GrowthbookProvider(providerProps)\n },\n },\n },\n }\n },\n)\n"],"names":["namespace","apiKeyName","pluginConfigKeys","key","title","Secrets","props","$","_c","secrets","loading","useSecrets","setSecret","useGrowthbookContext","showSettings","setShowSettings","useState","t0","t1","undefined","apiKey","useEffect","t2","renderDefault","Symbol","for","t3","t4","GROWTHBOOK_CONFIG_DEFAULT","baseUrl","GrowthbookContext","createContext","secret","useContext","GrowthbookProvider","growthbookFieldPluginConfig","context","getBooleanConversion","value","getExperiments","client","environment","project","convertBooleans","tags","query","fetch","featureExperiments","hasMore","offset","url","URL","searchParams","set","toString","response","headers","Authorization","features","responseHasMore","nextOffset","json","forEach","feature","archived","some","tag","includes","experiments","environments","rules","filter","experiment","type","variations","uniqueValues","Set","variant","has","add","push","id","label","variants","sort","a","b","localeCompare","fieldLevelExperiments","definePlugin","config","pluginConfig","fields","name","plugins","baseFieldLevelExperiments","form","components","input","isObjectInputProps","flattenSchemaType","schemaType","map","field","startsWith","providerProps"],"mappings":";;;;;;AAMO,MAAMA,YAAY,cACZC,aAAa,UAEpBC,mBAAmB,CACvB;AAAA,EACEC,KAAKF;AAAAA,EACLG,OAAO;AACT,CAAC,GAGUC,UAAUC,CAAAA,UAAA;AAAA,QAAAC,IAAAC,EAAA,EAAA,GACrB;AAAA,IAAAC;AAAAA,IAAAC;AAAAA,EAAAA,IAA2BC,WAAWX,SAAS,GAC/C;AAAA,IAAAY;AAAAA,EAAAA,IAAoBC,wBACpB,CAAAC,cAAAC,eAAA,IAAwCC,SAAkB,EAAK;AAAC,MAAAC,IAAAC;AAahE,MAbgEX,EAAA,CAAA,MAAAG,WAAAH,SAAAE,WAAAF,EAAA,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