2023-04-19 20:40:29 +02:00
|
|
|
import { IFeatureToggle } from 'interfaces/featureToggle';
|
|
|
|
import { formatApiPath } from 'utils/formatPath';
|
|
|
|
|
2023-04-27 09:15:17 +02:00
|
|
|
const PROJECT = 'demo-app';
|
|
|
|
const ENVIRONMENT = 'dev';
|
|
|
|
|
2023-04-19 20:40:29 +02:00
|
|
|
export const gradualRollout = async () => {
|
|
|
|
const featureId = 'demoApp.step3';
|
|
|
|
|
|
|
|
const { environments }: IFeatureToggle = await fetch(
|
|
|
|
formatApiPath(
|
2023-04-27 09:15:17 +02:00
|
|
|
`api/admin/projects/${PROJECT}/features/${featureId}?variantEnvironments=true`
|
2023-04-19 20:40:29 +02:00
|
|
|
)
|
|
|
|
).then(res => res.json());
|
|
|
|
|
|
|
|
const strategies =
|
2023-04-27 09:15:17 +02:00
|
|
|
environments.find(({ name }) => name === ENVIRONMENT)?.strategies || [];
|
2023-04-19 20:40:29 +02:00
|
|
|
|
|
|
|
if (!strategies.find(({ name }) => name === 'flexibleRollout')) {
|
|
|
|
await fetch(
|
|
|
|
formatApiPath(
|
2023-04-27 09:15:17 +02:00
|
|
|
`api/admin/projects/${PROJECT}/features/${featureId}/environments/${ENVIRONMENT}/strategies`
|
2023-04-19 20:40:29 +02:00
|
|
|
),
|
|
|
|
{
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
},
|
|
|
|
body: JSON.stringify({
|
|
|
|
name: 'flexibleRollout',
|
|
|
|
constraints: [],
|
|
|
|
parameters: {
|
|
|
|
rollout: '50',
|
|
|
|
stickiness: 'userId',
|
|
|
|
groupId: featureId,
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export const variants = async () => {
|
|
|
|
const featureId = 'demoApp.step4';
|
|
|
|
|
|
|
|
const { variants }: IFeatureToggle = await fetch(
|
|
|
|
formatApiPath(
|
2023-04-27 09:15:17 +02:00
|
|
|
`api/admin/projects/${PROJECT}/features/${featureId}?variantEnvironments=true`
|
2023-04-19 20:40:29 +02:00
|
|
|
)
|
|
|
|
).then(res => res.json());
|
|
|
|
|
|
|
|
if (!variants.length) {
|
|
|
|
await fetch(
|
|
|
|
formatApiPath(
|
2023-04-27 09:15:17 +02:00
|
|
|
`api/admin/projects/${PROJECT}/features/${featureId}/environments/${ENVIRONMENT}/variants`
|
2023-04-19 20:40:29 +02:00
|
|
|
),
|
|
|
|
{
|
|
|
|
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',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
]),
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|