@sanity/personalization-plugin 2.1.0-growthbook.1 → 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/README.md +60 -6
- package/dist/index.d.mts +190 -191
- package/dist/index.d.ts +190 -191
- package/dist/index.js +188 -106
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +188 -106
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
- package/src/components/Array.tsx +11 -12
- package/src/components/ExperimentContext.tsx +6 -1
- package/src/components/ExperimentField.tsx +71 -39
- package/src/components/ExperimentInput.tsx +5 -2
- package/src/components/Secrets.tsx +2 -4
- package/src/components/VariantInput.tsx +15 -68
- package/src/fieldExperiments.tsx +99 -34
- package/src/index.ts +1 -1
- package/src/{growthbookFieldExperiments.tsx → launchDarklyExperiments.tsx} +8 -11
- package/src/types.ts +186 -173
- package/src/utils/launchDarkly.ts +54 -0
- package/src/utils/growthbook.ts +0 -78
package/dist/index.js
CHANGED
|
@@ -7,7 +7,12 @@ function _interopDefaultCompat(e) {
|
|
|
7
7
|
var equal__default = /* @__PURE__ */ _interopDefaultCompat(equal);
|
|
8
8
|
const CONFIG_DEFAULT = {
|
|
9
9
|
fields: [],
|
|
10
|
-
apiVersion: "2024-11-07"
|
|
10
|
+
apiVersion: "2024-11-07",
|
|
11
|
+
experimentNameOverride: "experiment",
|
|
12
|
+
variantNameOverride: "variant",
|
|
13
|
+
variantId: "variantId",
|
|
14
|
+
variantArrayName: "variants",
|
|
15
|
+
experimentId: "experimentId"
|
|
11
16
|
}, ExperimentContext = react.createContext({
|
|
12
17
|
...CONFIG_DEFAULT,
|
|
13
18
|
experiments: [],
|
|
@@ -21,7 +26,7 @@ function useExperimentContext() {
|
|
|
21
26
|
function ExperimentProvider(props) {
|
|
22
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(
|
|
23
28
|
// eslint-disable-next-line require-await
|
|
24
|
-
async () => typeof experimentFieldPluginConfig.experiments == "function" ? experimentFieldPluginConfig.experiments(client
|
|
29
|
+
async () => typeof experimentFieldPluginConfig.experiments == "function" ? experimentFieldPluginConfig.experiments(client) : experimentFieldPluginConfig.experiments,
|
|
25
30
|
[workspace, secret],
|
|
26
31
|
{ equal: equal__default.default }
|
|
27
32
|
), context = react.useMemo(
|
|
@@ -31,18 +36,18 @@ function ExperimentProvider(props) {
|
|
|
31
36
|
return /* @__PURE__ */ jsxRuntime.jsx(ExperimentContext.Provider, { value: context, children: props.renderDefault(props) });
|
|
32
37
|
}
|
|
33
38
|
const ArrayInput = (props) => {
|
|
34
|
-
const fieldPath = props.path.slice(0, -1), experimentId = sanity.useFormValue([...fieldPath,
|
|
39
|
+
const fieldPath = props.path.slice(0, -1), { onItemAppend, variantName, variantId, experimentId } = props, experimentValue = sanity.useFormValue([...fieldPath, experimentId]), { experiments } = useExperimentContext(), handleClick = react.useCallback(
|
|
35
40
|
async (variant) => {
|
|
36
41
|
const item = {
|
|
37
42
|
_key: uuid.uuid(),
|
|
38
|
-
variantId: variant.id,
|
|
39
|
-
experimentId,
|
|
40
|
-
_type:
|
|
43
|
+
[variantId]: variant.id,
|
|
44
|
+
[experimentId]: experimentValue,
|
|
45
|
+
_type: variantName
|
|
41
46
|
};
|
|
42
47
|
onItemAppend(item);
|
|
43
48
|
},
|
|
44
|
-
[experimentId,
|
|
45
|
-
), filteredVariants = experiments.find((option) => option.id ===
|
|
49
|
+
[variantId, experimentId, experimentValue, variantName, onItemAppend]
|
|
50
|
+
), filteredVariants = experiments.find((option) => option.id === experimentValue)?.variants || [], usedVariants = (props.value || [])?.map((variant) => variant[variantId]);
|
|
46
51
|
return /* @__PURE__ */ jsxRuntime.jsxs(ui.Stack, { space: 3, children: [
|
|
47
52
|
props.renderDefault({ ...props, arrayFunctions: () => null }),
|
|
48
53
|
/* @__PURE__ */ jsxRuntime.jsx(ui.Inline, { space: 1, children: filteredVariants.map((variant) => /* @__PURE__ */ jsxRuntime.jsx(
|
|
@@ -53,7 +58,7 @@ const ArrayInput = (props) => {
|
|
|
53
58
|
disabled: usedVariants?.includes(variant.id),
|
|
54
59
|
onClick: () => handleClick(variant)
|
|
55
60
|
},
|
|
56
|
-
`${
|
|
61
|
+
`${experimentValue}-${variant.id}`
|
|
57
62
|
)) })
|
|
58
63
|
] });
|
|
59
64
|
}, AccessDeniedIcon = react.forwardRef(function(props, ref) {
|
|
@@ -6403,40 +6408,60 @@ const icons = {
|
|
|
6403
6408
|
});
|
|
6404
6409
|
Icon.displayName = "ForwardRef(Icon)";
|
|
6405
6410
|
const useAddExperimentAction = (props) => {
|
|
6406
|
-
const
|
|
6407
|
-
|
|
6408
|
-
}
|
|
6411
|
+
const { onChange, experimentNameOverride } = props, handleAddAction = () => {
|
|
6412
|
+
onChange([sanity.set(!0, ["active"])]);
|
|
6413
|
+
};
|
|
6409
6414
|
return {
|
|
6410
|
-
title:
|
|
6415
|
+
title: `Add ${experimentNameOverride}`,
|
|
6411
6416
|
type: "action",
|
|
6412
6417
|
icon: gi.GiSoapExperiment,
|
|
6413
|
-
onAction:
|
|
6418
|
+
onAction: handleAddAction,
|
|
6414
6419
|
renderAsButton: !0
|
|
6415
6420
|
};
|
|
6416
6421
|
}, useRemoveExperimentAction = (props) => {
|
|
6417
|
-
const
|
|
6418
|
-
const
|
|
6419
|
-
return [sanity.unset(
|
|
6420
|
-
},
|
|
6421
|
-
|
|
6422
|
-
|
|
6422
|
+
const { onChange, experimentId, experimentNameOverride } = props, patchActiveFalseEvent = () => sanity.set(!1, ["active"]), patchClearEvent = () => {
|
|
6423
|
+
const experiment = [experimentId], variants = [experimentNameOverride];
|
|
6424
|
+
return [sanity.unset(experiment), sanity.unset(variants)];
|
|
6425
|
+
}, handleClearAction = () => {
|
|
6426
|
+
const clearEvents = patchClearEvent(), activeEvent = patchActiveFalseEvent();
|
|
6427
|
+
onChange([activeEvent, ...clearEvents]);
|
|
6428
|
+
};
|
|
6423
6429
|
return {
|
|
6424
|
-
title:
|
|
6430
|
+
title: `Remove ${experimentNameOverride}`,
|
|
6425
6431
|
type: "action",
|
|
6426
6432
|
icon: CloseIcon,
|
|
6427
|
-
onAction:
|
|
6433
|
+
onAction: handleClearAction,
|
|
6428
6434
|
renderAsButton: !0
|
|
6429
6435
|
};
|
|
6430
|
-
}, newActions = ({
|
|
6431
|
-
|
|
6432
|
-
|
|
6433
|
-
|
|
6434
|
-
|
|
6435
|
-
|
|
6436
|
-
})
|
|
6437
|
-
const
|
|
6436
|
+
}, newActions = ({
|
|
6437
|
+
onChange,
|
|
6438
|
+
inputId,
|
|
6439
|
+
active,
|
|
6440
|
+
experimentNameOverride,
|
|
6441
|
+
experimentId
|
|
6442
|
+
}) => {
|
|
6443
|
+
const removeAction = sanity.defineDocumentFieldAction({
|
|
6444
|
+
name: `Remove ${experimentNameOverride}`,
|
|
6445
|
+
useAction: (props) => useRemoveExperimentAction({
|
|
6446
|
+
onChange,
|
|
6447
|
+
experimentNameOverride,
|
|
6448
|
+
experimentId
|
|
6449
|
+
})
|
|
6450
|
+
}), addAction = sanity.defineDocumentFieldAction({
|
|
6451
|
+
name: `Add ${experimentNameOverride}`,
|
|
6452
|
+
useAction: (props) => useAddExperimentAction({
|
|
6453
|
+
onChange,
|
|
6454
|
+
experimentNameOverride
|
|
6455
|
+
})
|
|
6456
|
+
});
|
|
6457
|
+
return active ? removeAction : addAction;
|
|
6458
|
+
}, ExperimentField = (props) => {
|
|
6459
|
+
const { onChange } = props.inputProps, { inputId, experimentNameOverride, experimentId } = props, active = props.value?.active, oldActions = props.actions || [], withActionProps = {
|
|
6438
6460
|
...props,
|
|
6439
|
-
actions: [
|
|
6461
|
+
actions: [
|
|
6462
|
+
newActions({ onChange, inputId, active, experimentNameOverride, experimentId }),
|
|
6463
|
+
...oldActions
|
|
6464
|
+
]
|
|
6440
6465
|
};
|
|
6441
6466
|
return props.renderDefault(withActionProps);
|
|
6442
6467
|
}, Select = (props) => {
|
|
@@ -6468,7 +6493,10 @@ const useAddExperimentAction = (props) => {
|
|
|
6468
6493
|
title: experiment.label,
|
|
6469
6494
|
value: experiment.id
|
|
6470
6495
|
})), ExperimentInput = (props) => {
|
|
6471
|
-
const { experiments } = useExperimentContext(), id = sanity.useFormValue(["_id"]), aditionalChangePath = react.useMemo(
|
|
6496
|
+
const { experiments } = useExperimentContext(), id = sanity.useFormValue(["_id"]), aditionalChangePath = react.useMemo(
|
|
6497
|
+
() => [...props.path.slice(0, -1), props.variantNameOverride],
|
|
6498
|
+
[props.variantNameOverride, props.path]
|
|
6499
|
+
), subValues = sanity.useFormValue(aditionalChangePath), { patch } = sanity.useDocumentOperation(id.replace("drafts.", ""), props.schemaType.name), handleChange = react.useCallback(
|
|
6472
6500
|
(event, onChange) => {
|
|
6473
6501
|
const inputValue = event.currentTarget.value;
|
|
6474
6502
|
if (onChange(inputValue ? sanity.set(inputValue) : sanity.unset()), subValues) {
|
|
@@ -6481,6 +6509,14 @@ const useAddExperimentAction = (props) => {
|
|
|
6481
6509
|
[patch, subValues, aditionalChangePath]
|
|
6482
6510
|
);
|
|
6483
6511
|
return experiments.length ? /* @__PURE__ */ jsxRuntime.jsx(Select, { ...props, listOptions: formatlistOptions(experiments), handleChange }) : /* @__PURE__ */ jsxRuntime.jsx(jsxRuntime.Fragment, {});
|
|
6512
|
+
}, VariantInput = (props) => {
|
|
6513
|
+
const defaultValue = sanity.useFormValue([props.path[0], "default"]), handleClick = () => {
|
|
6514
|
+
props.onChange(sanity.set(defaultValue, ["value"]));
|
|
6515
|
+
};
|
|
6516
|
+
return /* @__PURE__ */ jsxRuntime.jsxs(ui.Stack, { space: 3, children: [
|
|
6517
|
+
props.renderDefault(props),
|
|
6518
|
+
/* @__PURE__ */ jsxRuntime.jsx(ui.Inline, { space: 1, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { text: "Copy default", mode: "ghost", onClick: () => handleClick() }) })
|
|
6519
|
+
] });
|
|
6484
6520
|
}, VariantPreview = (props) => {
|
|
6485
6521
|
const [subtitle, setSubtitle] = react.useState(void 0), [title, setTitle] = react.useState(void 0), [media, setMedia] = react.useState(void 0), client = sanity.useClient({ apiVersion: "2025-01-01" }), { experiments } = useExperimentContext(), { experiment, variant, value } = props, selectedExperiment = experiments.find((experimentItem) => experimentItem.id === experiment), selectedVariant = selectedExperiment?.variants.find((variantItem) => variantItem.id === variant);
|
|
6486
6522
|
react.useEffect(() => {
|
|
@@ -6528,15 +6564,27 @@ function extractInnerFields(fields, path, maxDepth) {
|
|
|
6528
6564
|
return [...acc, thisFieldWithPath];
|
|
6529
6565
|
}, []);
|
|
6530
6566
|
}
|
|
6531
|
-
const
|
|
6532
|
-
field
|
|
6567
|
+
const createExperimentType = ({
|
|
6568
|
+
field,
|
|
6569
|
+
experimentNameOverride,
|
|
6570
|
+
variantNameOverride,
|
|
6571
|
+
variantId,
|
|
6572
|
+
variantArrayName,
|
|
6573
|
+
experimentId
|
|
6533
6574
|
}) => {
|
|
6534
|
-
const typeName = typeof field == "string" ? field : field.name, usedName = String(typeName[0]).toUpperCase() + String(typeName).slice(1),
|
|
6575
|
+
const typeName = typeof field == "string" ? field : field.name, usedName = String(typeName[0]).toUpperCase() + String(typeName).slice(1), variantName = `${variantNameOverride}${usedName}`;
|
|
6535
6576
|
return sanity.defineType({
|
|
6536
|
-
name:
|
|
6577
|
+
name: `${experimentNameOverride}${usedName}`,
|
|
6537
6578
|
type: "object",
|
|
6538
6579
|
components: {
|
|
6539
|
-
field:
|
|
6580
|
+
field: (props) => /* @__PURE__ */ jsxRuntime.jsx(
|
|
6581
|
+
ExperimentField,
|
|
6582
|
+
{
|
|
6583
|
+
...props,
|
|
6584
|
+
experimentId,
|
|
6585
|
+
experimentNameOverride
|
|
6586
|
+
}
|
|
6587
|
+
)
|
|
6540
6588
|
},
|
|
6541
6589
|
fields: [
|
|
6542
6590
|
typeof field == "string" ? (
|
|
@@ -6558,105 +6606,151 @@ const createFieldType = ({
|
|
|
6558
6606
|
hidden: !0
|
|
6559
6607
|
}),
|
|
6560
6608
|
sanity.defineField({
|
|
6561
|
-
|
|
6562
|
-
name: "experimentId",
|
|
6609
|
+
name: experimentId,
|
|
6563
6610
|
type: "string",
|
|
6564
6611
|
components: {
|
|
6565
|
-
input: ExperimentInput
|
|
6612
|
+
input: (props) => /* @__PURE__ */ jsxRuntime.jsx(ExperimentInput, { ...props, variantNameOverride })
|
|
6566
6613
|
},
|
|
6567
6614
|
hidden: ({ parent }) => !parent?.active
|
|
6568
6615
|
}),
|
|
6569
6616
|
sanity.defineField({
|
|
6570
|
-
name:
|
|
6617
|
+
name: variantArrayName,
|
|
6571
6618
|
type: "array",
|
|
6572
|
-
hidden: ({ parent }) => !parent?.experimentId,
|
|
6619
|
+
hidden: ({ parent }) => !parent?.[experimentId],
|
|
6573
6620
|
components: {
|
|
6574
|
-
input: (props) => /* @__PURE__ */ jsxRuntime.jsx(
|
|
6621
|
+
input: (props) => /* @__PURE__ */ jsxRuntime.jsx(
|
|
6622
|
+
ArrayInput,
|
|
6623
|
+
{
|
|
6624
|
+
...props,
|
|
6625
|
+
variantName,
|
|
6626
|
+
variantId,
|
|
6627
|
+
experimentId
|
|
6628
|
+
}
|
|
6629
|
+
)
|
|
6575
6630
|
},
|
|
6576
6631
|
of: [
|
|
6577
6632
|
sanity.defineField({
|
|
6578
|
-
name:
|
|
6579
|
-
type:
|
|
6633
|
+
name: variantName,
|
|
6634
|
+
type: variantName
|
|
6580
6635
|
})
|
|
6581
6636
|
]
|
|
6582
6637
|
})
|
|
6583
6638
|
]
|
|
6584
6639
|
});
|
|
6585
|
-
},
|
|
6586
|
-
field
|
|
6640
|
+
}, createVariantType = ({
|
|
6641
|
+
field,
|
|
6642
|
+
variantNameOverride,
|
|
6643
|
+
variantId,
|
|
6644
|
+
experimentId
|
|
6587
6645
|
}) => {
|
|
6588
6646
|
const typeName = typeof field == "string" ? field : field.name, usedName = String(typeName[0]).toUpperCase() + String(typeName).slice(1);
|
|
6589
6647
|
return sanity.defineType({
|
|
6590
|
-
name:
|
|
6591
|
-
title:
|
|
6648
|
+
name: `${variantNameOverride}${usedName}`,
|
|
6649
|
+
title: `${variantNameOverride} array ${usedName}`,
|
|
6592
6650
|
type: "object",
|
|
6593
6651
|
components: {
|
|
6594
|
-
preview: VariantPreview
|
|
6652
|
+
preview: VariantPreview,
|
|
6653
|
+
input: VariantInput
|
|
6595
6654
|
},
|
|
6596
6655
|
fields: [
|
|
6597
6656
|
{
|
|
6598
6657
|
type: "string",
|
|
6599
|
-
name:
|
|
6658
|
+
name: variantId,
|
|
6600
6659
|
readOnly: !0
|
|
6601
6660
|
},
|
|
6602
6661
|
{
|
|
6603
6662
|
type: "string",
|
|
6604
|
-
name:
|
|
6663
|
+
name: experimentId,
|
|
6605
6664
|
hidden: !0
|
|
6606
6665
|
},
|
|
6607
6666
|
typeof field == "string" ? (
|
|
6608
6667
|
// Define a simple field if all we have is the name as a string
|
|
6609
6668
|
sanity.defineField({
|
|
6610
6669
|
name: "value",
|
|
6611
|
-
type: field
|
|
6612
|
-
hidden: ({
|
|
6670
|
+
type: field
|
|
6671
|
+
// hidden: ({parent}) => !parent?.[`${objectNameOverride}Id`],
|
|
6613
6672
|
})
|
|
6614
6673
|
) : (
|
|
6615
6674
|
// Pass in the configured options, but overwrite the name
|
|
6616
6675
|
{
|
|
6617
6676
|
...field,
|
|
6618
|
-
name: "value"
|
|
6619
|
-
hidden: ({
|
|
6677
|
+
name: "value"
|
|
6678
|
+
// hidden: ({parent}) => !parent?.[`${objectNameOverride}Id`],
|
|
6620
6679
|
}
|
|
6621
6680
|
)
|
|
6622
6681
|
],
|
|
6623
6682
|
preview: {
|
|
6624
6683
|
select: {
|
|
6625
|
-
variant:
|
|
6626
|
-
experiment:
|
|
6684
|
+
variant: variantId,
|
|
6685
|
+
experiment: experimentId,
|
|
6627
6686
|
value: "value"
|
|
6628
6687
|
}
|
|
6629
6688
|
}
|
|
6630
6689
|
});
|
|
6631
|
-
}, fieldSchema = ({
|
|
6632
|
-
|
|
6633
|
-
|
|
6690
|
+
}, fieldSchema = ({
|
|
6691
|
+
fields,
|
|
6692
|
+
experimentNameOverride,
|
|
6693
|
+
variantNameOverride,
|
|
6694
|
+
variantId,
|
|
6695
|
+
variantArrayName,
|
|
6696
|
+
experimentId
|
|
6697
|
+
}) => [
|
|
6698
|
+
...fields.map(
|
|
6699
|
+
(field) => createVariantType({ field, variantNameOverride, variantId, experimentId })
|
|
6700
|
+
),
|
|
6701
|
+
...fields.map(
|
|
6702
|
+
(field) => createExperimentType({
|
|
6703
|
+
field,
|
|
6704
|
+
experimentNameOverride,
|
|
6705
|
+
variantNameOverride,
|
|
6706
|
+
variantId,
|
|
6707
|
+
variantArrayName,
|
|
6708
|
+
experimentId
|
|
6709
|
+
})
|
|
6710
|
+
)
|
|
6634
6711
|
], fieldLevelExperiments = sanity.definePlugin((config) => {
|
|
6635
|
-
const pluginConfig = { ...CONFIG_DEFAULT, ...config }, { fields,
|
|
6712
|
+
const pluginConfig = { ...CONFIG_DEFAULT, ...config }, { fields, experimentNameOverride, variantNameOverride } = pluginConfig, experimentId = `${experimentNameOverride}Id`, variantArrayName = `${variantNameOverride}s`, variantId = `${variantNameOverride}Id`;
|
|
6636
6713
|
return {
|
|
6637
6714
|
name: "sanity-personalistaion-plugin-field-level-experiments",
|
|
6638
6715
|
schema: {
|
|
6639
|
-
types: fieldSchema({
|
|
6716
|
+
types: fieldSchema({
|
|
6717
|
+
fields,
|
|
6718
|
+
experimentNameOverride,
|
|
6719
|
+
variantNameOverride,
|
|
6720
|
+
variantId,
|
|
6721
|
+
variantArrayName,
|
|
6722
|
+
experimentId
|
|
6723
|
+
})
|
|
6640
6724
|
},
|
|
6641
6725
|
form: {
|
|
6642
6726
|
components: {
|
|
6643
6727
|
input: (props) => {
|
|
6644
6728
|
if (!(props.id === "root" && sanity.isObjectInputProps(props)) || !flattenSchemaType(props.schemaType).map(
|
|
6645
6729
|
(field) => field.type.name
|
|
6646
|
-
).some(
|
|
6730
|
+
).some(
|
|
6731
|
+
(name) => name.startsWith(experimentNameOverride)
|
|
6732
|
+
))
|
|
6647
6733
|
return props.renderDefault(props);
|
|
6648
|
-
const providerProps = {
|
|
6734
|
+
const providerProps = {
|
|
6735
|
+
...props,
|
|
6736
|
+
experimentFieldPluginConfig: {
|
|
6737
|
+
...pluginConfig,
|
|
6738
|
+
variantId,
|
|
6739
|
+
variantArrayName,
|
|
6740
|
+
experimentId
|
|
6741
|
+
}
|
|
6742
|
+
};
|
|
6649
6743
|
return ExperimentProvider(providerProps);
|
|
6650
6744
|
}
|
|
6651
6745
|
}
|
|
6652
6746
|
}
|
|
6653
6747
|
};
|
|
6654
|
-
}),
|
|
6748
|
+
}), pluginConfigKeys = [
|
|
6655
6749
|
{
|
|
6656
6750
|
key: "apiKey",
|
|
6657
6751
|
title: "Your secret API key"
|
|
6658
6752
|
}
|
|
6659
|
-
], Secrets = (props) => {
|
|
6753
|
+
], Secrets = (props, namespace) => {
|
|
6660
6754
|
const { secrets, loading } = studioSecrets.useSecrets(namespace), { setSecret } = useExperimentContext(), [showSettings, setShowSettings] = react.useState(!1);
|
|
6661
6755
|
return react.useEffect(() => {
|
|
6662
6756
|
if (!loading)
|
|
@@ -6665,7 +6759,7 @@ const createFieldType = ({
|
|
|
6665
6759
|
/* @__PURE__ */ jsxRuntime.jsx(
|
|
6666
6760
|
studioSecrets.SettingsView,
|
|
6667
6761
|
{
|
|
6668
|
-
title:
|
|
6762
|
+
title: `${namespace} api key`,
|
|
6669
6763
|
namespace,
|
|
6670
6764
|
keys: pluginConfigKeys,
|
|
6671
6765
|
onClose: () => {
|
|
@@ -6675,67 +6769,55 @@ const createFieldType = ({
|
|
|
6675
6769
|
),
|
|
6676
6770
|
props.renderDefault(props)
|
|
6677
6771
|
] }) : props.renderDefault(props);
|
|
6678
|
-
},
|
|
6772
|
+
}, getExperiments = async ({
|
|
6679
6773
|
client,
|
|
6680
|
-
|
|
6681
|
-
|
|
6682
|
-
project,
|
|
6683
|
-
convertBooleans
|
|
6774
|
+
projectKey,
|
|
6775
|
+
tags
|
|
6684
6776
|
}) => {
|
|
6685
|
-
const secret = await client.fetch("*[_id == 'secrets.
|
|
6777
|
+
const secret = await client.fetch("*[_id == 'secrets.launchdarkly'][0].secrets.apiKey");
|
|
6686
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("+")}`);
|
|
6687
6781
|
const featureExperiments = [];
|
|
6688
|
-
let hasMore = !0
|
|
6689
|
-
const
|
|
6690
|
-
for (
|
|
6691
|
-
url.searchParams.set("offset", offset.toString());
|
|
6692
|
-
const
|
|
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, {
|
|
6693
6787
|
headers: {
|
|
6694
|
-
Authorization:
|
|
6788
|
+
Authorization: secret
|
|
6695
6789
|
}
|
|
6696
|
-
}), {
|
|
6697
|
-
|
|
6698
|
-
|
|
6699
|
-
|
|
6700
|
-
|
|
6701
|
-
|
|
6702
|
-
)
|
|
6703
|
-
|
|
6704
|
-
|
|
6705
|
-
const variations = /* @__PURE__ */ new Set();
|
|
6706
|
-
experiments.forEach((experiment) => {
|
|
6707
|
-
experiment?.variations.forEach((variant) => {
|
|
6708
|
-
variations.add({
|
|
6709
|
-
id: convertBooleans ? getBooleanConversion(variant.value) : variant.value,
|
|
6710
|
-
label: convertBooleans ? getBooleanConversion(variant.value) : variant.value
|
|
6711
|
-
});
|
|
6712
|
-
});
|
|
6713
|
-
});
|
|
6714
|
-
const value = { id: feature.id, label: feature.id, variants: [...variations] };
|
|
6715
|
-
featureExperiments.push(value);
|
|
6716
|
-
});
|
|
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);
|
|
6717
6799
|
}
|
|
6718
6800
|
return featureExperiments;
|
|
6719
|
-
},
|
|
6720
|
-
const { fields,
|
|
6801
|
+
}, launchDarklyFieldLevel = sanity.definePlugin((config) => {
|
|
6802
|
+
const { fields, projectKey, tags } = config;
|
|
6721
6803
|
return {
|
|
6722
6804
|
name: "sanity-growthbook-personalistaion-plugin-field-level-experiments",
|
|
6723
6805
|
plugins: [
|
|
6724
6806
|
fieldLevelExperiments({
|
|
6725
6807
|
fields,
|
|
6726
|
-
experiments: (client) => getExperiments({ client,
|
|
6808
|
+
experiments: (client) => getExperiments({ client, projectKey, tags })
|
|
6727
6809
|
})
|
|
6728
6810
|
],
|
|
6729
6811
|
form: {
|
|
6730
6812
|
components: {
|
|
6731
6813
|
input: (props) => !(props.id === "root" && sanity.isObjectInputProps(props)) || !flattenSchemaType(props.schemaType).map(
|
|
6732
6814
|
(field) => field.type.name
|
|
6733
|
-
).some((name) => name.startsWith("experiment")) ? props.renderDefault(props) : Secrets(props)
|
|
6815
|
+
).some((name) => name.startsWith("experiment")) ? props.renderDefault(props) : Secrets(props, "launchdarkly")
|
|
6734
6816
|
}
|
|
6735
6817
|
}
|
|
6736
6818
|
};
|
|
6737
6819
|
});
|
|
6738
6820
|
exports.fieldLevelExperiments = fieldLevelExperiments;
|
|
6739
6821
|
exports.flattenSchemaType = flattenSchemaType;
|
|
6740
|
-
exports.
|
|
6822
|
+
exports.launchDarklyFieldLevel = launchDarklyFieldLevel;
|
|
6741
6823
|
//# sourceMappingURL=index.js.map
|