@sanity/personalization-plugin 2.2.0 → 2.3.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 +194 -0
- package/dist/index.d.ts +194 -0
- package/dist/index.js +81 -6
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +84 -8
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -1
- package/src/components/ExperimentContext.tsx +7 -4
- package/src/components/Secrets.tsx +45 -0
- package/src/index.ts +1 -0
- package/src/launchDarklyExperiments.tsx +48 -0
- package/src/types.ts +185 -0
- package/src/utils/launchDarkly.ts +54 -0
package/dist/index.mjs
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
|
-
import { jsx, jsxs } from "react/jsx-runtime";
|
|
1
|
+
import { jsx, jsxs, Fragment } from "react/jsx-runtime";
|
|
2
2
|
import { useClient, useWorkspace, useFormValue, defineDocumentFieldAction, set, unset, useDocumentOperation, isReference, isImage, isDocumentSchemaType, definePlugin, isObjectInputProps, defineType, defineField } from "sanity";
|
|
3
3
|
import { Stack, Inline, Button, Select as Select$1, Card, Text } from "@sanity/ui";
|
|
4
4
|
import { uuid } from "@sanity/uuid";
|
|
5
|
-
import { createContext, useMemo, useContext, useCallback, forwardRef,
|
|
5
|
+
import { createContext, useState, useMemo, useContext, useCallback, forwardRef, useEffect } from "react";
|
|
6
6
|
import equal from "fast-deep-equal";
|
|
7
7
|
import { suspend } from "suspend-react";
|
|
8
8
|
import { GiSoapExperiment } from "react-icons/gi";
|
|
9
|
+
import { useSecrets, SettingsView } from "@sanity/studio-secrets";
|
|
9
10
|
const CONFIG_DEFAULT = {
|
|
10
11
|
fields: [],
|
|
11
12
|
apiVersion: "2024-11-07",
|
|
@@ -16,20 +17,23 @@ const CONFIG_DEFAULT = {
|
|
|
16
17
|
experimentId: "experimentId"
|
|
17
18
|
}, ExperimentContext = createContext({
|
|
18
19
|
...CONFIG_DEFAULT,
|
|
19
|
-
experiments: []
|
|
20
|
+
experiments: [],
|
|
21
|
+
setSecret: () => {
|
|
22
|
+
},
|
|
23
|
+
secret: void 0
|
|
20
24
|
});
|
|
21
25
|
function useExperimentContext() {
|
|
22
26
|
return useContext(ExperimentContext);
|
|
23
27
|
}
|
|
24
28
|
function ExperimentProvider(props) {
|
|
25
|
-
const { experimentFieldPluginConfig } = props, client = useClient({ apiVersion: experimentFieldPluginConfig.apiVersion }), workspace = useWorkspace(), experiments = Array.isArray(experimentFieldPluginConfig.experiments) ? experimentFieldPluginConfig.experiments : suspend(
|
|
29
|
+
const { experimentFieldPluginConfig } = props, [secret, setSecret] = useState(), client = useClient({ apiVersion: experimentFieldPluginConfig.apiVersion }), workspace = useWorkspace(), experiments = Array.isArray(experimentFieldPluginConfig.experiments) ? experimentFieldPluginConfig.experiments : suspend(
|
|
26
30
|
// eslint-disable-next-line require-await
|
|
27
31
|
async () => typeof experimentFieldPluginConfig.experiments == "function" ? experimentFieldPluginConfig.experiments(client) : experimentFieldPluginConfig.experiments,
|
|
28
|
-
[workspace],
|
|
32
|
+
[workspace, secret],
|
|
29
33
|
{ equal }
|
|
30
34
|
), context = useMemo(
|
|
31
|
-
() => ({ ...experimentFieldPluginConfig, experiments }),
|
|
32
|
-
[experimentFieldPluginConfig, experiments]
|
|
35
|
+
() => ({ ...experimentFieldPluginConfig, experiments, secret, setSecret }),
|
|
36
|
+
[experimentFieldPluginConfig, experiments, secret, setSecret]
|
|
33
37
|
);
|
|
34
38
|
return /* @__PURE__ */ jsx(ExperimentContext.Provider, { value: context, children: props.renderDefault(props) });
|
|
35
39
|
}
|
|
@@ -6754,9 +6758,81 @@ const createExperimentType = ({
|
|
|
6754
6758
|
}
|
|
6755
6759
|
}
|
|
6756
6760
|
};
|
|
6761
|
+
}), pluginConfigKeys = [
|
|
6762
|
+
{
|
|
6763
|
+
key: "apiKey",
|
|
6764
|
+
title: "Your secret API key"
|
|
6765
|
+
}
|
|
6766
|
+
], Secrets = (props, namespace) => {
|
|
6767
|
+
const { secrets, loading } = useSecrets(namespace), { setSecret } = useExperimentContext(), [showSettings, setShowSettings] = useState(!1);
|
|
6768
|
+
return useEffect(() => {
|
|
6769
|
+
if (!loading)
|
|
6770
|
+
return !secrets && !loading ? (setSecret(void 0), setShowSettings(!0)) : (setSecret(secrets.apiKey), setShowSettings(!1));
|
|
6771
|
+
}, [secrets, loading, setSecret]), showSettings ? /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
6772
|
+
/* @__PURE__ */ jsx(
|
|
6773
|
+
SettingsView,
|
|
6774
|
+
{
|
|
6775
|
+
title: `${namespace} api key`,
|
|
6776
|
+
namespace,
|
|
6777
|
+
keys: pluginConfigKeys,
|
|
6778
|
+
onClose: () => {
|
|
6779
|
+
setShowSettings(!1);
|
|
6780
|
+
}
|
|
6781
|
+
}
|
|
6782
|
+
),
|
|
6783
|
+
props.renderDefault(props)
|
|
6784
|
+
] }) : props.renderDefault(props);
|
|
6785
|
+
}, getExperiments = async ({
|
|
6786
|
+
client,
|
|
6787
|
+
projectKey,
|
|
6788
|
+
tags
|
|
6789
|
+
}) => {
|
|
6790
|
+
const secret = await client.fetch("*[_id == 'secrets.launchdarkly'][0].secrets.apiKey");
|
|
6791
|
+
if (!secret) return [];
|
|
6792
|
+
const url = new URL(`https://app.launchdarkly.com/api/v2/flags/${projectKey}`);
|
|
6793
|
+
tags && url.searchParams.set("filter", `tags:${tags.join("+")}`);
|
|
6794
|
+
const featureExperiments = [];
|
|
6795
|
+
let hasMore = !0;
|
|
6796
|
+
const offset = 0, limit = 10;
|
|
6797
|
+
for (; hasMore; ) {
|
|
6798
|
+
url.searchParams.set("offset", offset.toString()), url.searchParams.set("limit", limit.toString());
|
|
6799
|
+
const responseFlags = await fetch(url, {
|
|
6800
|
+
headers: {
|
|
6801
|
+
Authorization: secret
|
|
6802
|
+
}
|
|
6803
|
+
}), { items } = await responseFlags.json(), experiments = items.map((flag) => ({
|
|
6804
|
+
id: flag.key,
|
|
6805
|
+
label: flag.name,
|
|
6806
|
+
variants: flag.variations.map((variation) => ({
|
|
6807
|
+
id: variation.value,
|
|
6808
|
+
label: variation.name ?? variation.value
|
|
6809
|
+
}))
|
|
6810
|
+
}));
|
|
6811
|
+
featureExperiments.push(...experiments), items.length !== limit && (hasMore = !1);
|
|
6812
|
+
}
|
|
6813
|
+
return featureExperiments;
|
|
6814
|
+
}, launchDarklyFieldLevel = definePlugin((config) => {
|
|
6815
|
+
const { fields, projectKey, tags } = config;
|
|
6816
|
+
return {
|
|
6817
|
+
name: "sanity-growthbook-personalistaion-plugin-field-level-experiments",
|
|
6818
|
+
plugins: [
|
|
6819
|
+
fieldLevelExperiments({
|
|
6820
|
+
fields,
|
|
6821
|
+
experiments: (client) => getExperiments({ client, projectKey, tags })
|
|
6822
|
+
})
|
|
6823
|
+
],
|
|
6824
|
+
form: {
|
|
6825
|
+
components: {
|
|
6826
|
+
input: (props) => !(props.id === "root" && isObjectInputProps(props)) || !flattenSchemaType(props.schemaType).map(
|
|
6827
|
+
(field) => field.type.name
|
|
6828
|
+
).some((name) => name.startsWith("experiment")) ? props.renderDefault(props) : Secrets(props, "launchdarkly")
|
|
6829
|
+
}
|
|
6830
|
+
}
|
|
6831
|
+
};
|
|
6757
6832
|
});
|
|
6758
6833
|
export {
|
|
6759
6834
|
fieldLevelExperiments,
|
|
6760
|
-
flattenSchemaType
|
|
6835
|
+
flattenSchemaType,
|
|
6836
|
+
launchDarklyFieldLevel
|
|
6761
6837
|
};
|
|
6762
6838
|
//# sourceMappingURL=index.mjs.map
|