/
demo-setup.ts
112 lines (104 loc) · 3.67 KB
/
demo-setup.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import { IFeatureToggle } from 'interfaces/featureToggle';
import { formatApiPath } from 'utils/formatPath';
const PROJECT = 'demo-app';
const ENVIRONMENT = 'dev';
export const gradualRollout = async () => {
const featureId = 'demoApp.step3';
const { environments }: IFeatureToggle = await fetch(
formatApiPath(
`api/admin/projects/${PROJECT}/features/${featureId}?variantEnvironments=true`
)
).then(res => res.json());
const strategies =
environments.find(({ name }) => name === ENVIRONMENT)?.strategies || [];
if (!strategies.find(({ name }) => name === 'flexibleRollout')) {
await fetch(
formatApiPath(
`api/admin/projects/${PROJECT}/features/${featureId}/environments/${ENVIRONMENT}/strategies`
),
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
name: 'flexibleRollout',
constraints: [],
parameters: {
rollout: '50',
stickiness: 'default',
groupId: featureId,
},
}),
}
);
}
};
export const variants = async () => {
const featureId = 'demoApp.step4';
const { variants }: IFeatureToggle = await fetch(
formatApiPath(
`api/admin/projects/${PROJECT}/features/${featureId}?variantEnvironments=true`
)
).then(res => res.json());
if (!variants.length) {
await fetch(
formatApiPath(
`api/admin/projects/${PROJECT}/features/${featureId}/environments/${ENVIRONMENT}/variants`
),
{
method: 'PATCH',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify([
{
op: 'add',
path: '/0',
value: {
name: 'red',
weightType: 'variable',
weight: 333,
overrides: [],
stickiness: 'default',
payload: {
type: 'string',
value: 'red',
},
},
},
{
op: 'add',
path: '/1',
value: {
name: 'green',
weightType: 'variable',
weight: 333,
overrides: [],
stickiness: 'default',
payload: {
type: 'string',
value: 'green',
},
},
},
{
op: 'add',
path: '/2',
value: {
name: 'blue',
weightType: 'variable',
weight: 333,
overrides: [],
stickiness: 'default',
payload: {
type: 'string',
value: 'blue',
},
},
},
]),
}
);
}
};