2022-02-03 12:39:43 +01:00
|
|
|
import { IFeatureTogglePayload } from '../../../../interfaces/featureToggle';
|
2021-10-01 13:49:18 +02:00
|
|
|
import { ITag } from '../../../../interfaces/tags';
|
2021-08-25 13:37:22 +02:00
|
|
|
import useAPI from '../useApi/useApi';
|
2021-11-25 14:05:44 +01:00
|
|
|
import { Operation } from 'fast-json-patch';
|
2022-03-04 17:29:51 +01:00
|
|
|
import { IConstraint } from 'interfaces/strategy';
|
2021-08-25 13:37:22 +02:00
|
|
|
|
|
|
|
const useFeatureApi = () => {
|
2021-10-08 16:19:06 +02:00
|
|
|
const { makeRequest, createRequest, errors, loading } = useAPI({
|
2021-08-25 13:37:22 +02:00
|
|
|
propagateErrors: true,
|
|
|
|
});
|
|
|
|
|
2022-01-24 15:17:27 +01:00
|
|
|
const validateFeatureToggleName = async (name: string) => {
|
2021-10-15 14:16:08 +02:00
|
|
|
const path = `api/admin/features/validate`;
|
|
|
|
const req = createRequest(path, {
|
|
|
|
method: 'POST',
|
|
|
|
body: JSON.stringify({ name }),
|
|
|
|
});
|
|
|
|
|
|
|
|
try {
|
|
|
|
const res = await makeRequest(req.caller, req.id);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
} catch (e) {
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-03-04 17:29:51 +01:00
|
|
|
const validateConstraint = async (
|
|
|
|
projectId: string,
|
|
|
|
featureName: string,
|
|
|
|
constraint: IConstraint
|
|
|
|
) => {
|
|
|
|
const path = `api/admin/projects/${projectId}/features/${featureName}/constraint/validate`;
|
|
|
|
const req = createRequest(path, {
|
|
|
|
method: 'POST',
|
|
|
|
body: JSON.stringify(constraint),
|
|
|
|
});
|
|
|
|
|
|
|
|
try {
|
|
|
|
const res = await makeRequest(req.caller, req.id);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
} catch (e) {
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-10-15 14:16:08 +02:00
|
|
|
const createFeatureToggle = async (
|
|
|
|
projectId: string,
|
2022-02-03 12:39:43 +01:00
|
|
|
featureToggle: IFeatureTogglePayload
|
2021-10-15 14:16:08 +02:00
|
|
|
) => {
|
|
|
|
const path = `api/admin/projects/${projectId}/features`;
|
|
|
|
const req = createRequest(path, {
|
|
|
|
method: 'POST',
|
|
|
|
body: JSON.stringify(featureToggle),
|
|
|
|
});
|
|
|
|
|
|
|
|
try {
|
|
|
|
const res = await makeRequest(req.caller, req.id);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
} catch (e) {
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-10-01 13:49:18 +02:00
|
|
|
const toggleFeatureEnvironmentOn = async (
|
|
|
|
projectId: string,
|
|
|
|
featureId: string,
|
|
|
|
environmentId: string
|
|
|
|
) => {
|
|
|
|
const path = `api/admin/projects/${projectId}/features/${featureId}/environments/${environmentId}/on`;
|
|
|
|
const req = createRequest(
|
|
|
|
path,
|
|
|
|
{ method: 'POST' },
|
|
|
|
'toggleFeatureEnvironmentOn'
|
|
|
|
);
|
|
|
|
|
|
|
|
try {
|
|
|
|
const res = await makeRequest(req.caller, req.id);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
} catch (e) {
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const toggleFeatureEnvironmentOff = async (
|
|
|
|
projectId: string,
|
|
|
|
featureId: string,
|
|
|
|
environmentId: string
|
|
|
|
) => {
|
|
|
|
const path = `api/admin/projects/${projectId}/features/${featureId}/environments/${environmentId}/off`;
|
|
|
|
const req = createRequest(
|
|
|
|
path,
|
|
|
|
{ method: 'POST' },
|
|
|
|
'toggleFeatureEnvironmentOff'
|
|
|
|
);
|
|
|
|
|
|
|
|
try {
|
|
|
|
const res = await makeRequest(req.caller, req.id);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
} catch (e) {
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-08-25 13:37:22 +02:00
|
|
|
const changeFeatureProject = async (
|
|
|
|
projectId: string,
|
2021-10-01 13:49:18 +02:00
|
|
|
featureId: string,
|
2021-08-25 13:37:22 +02:00
|
|
|
newProjectId: string
|
|
|
|
) => {
|
2021-10-01 13:49:18 +02:00
|
|
|
const path = `api/admin/projects/${projectId}/features/${featureId}/changeProject`;
|
2021-08-25 13:37:22 +02:00
|
|
|
const req = createRequest(path, {
|
|
|
|
method: 'POST',
|
|
|
|
body: JSON.stringify({ newProjectId }),
|
|
|
|
});
|
|
|
|
|
|
|
|
try {
|
|
|
|
const res = await makeRequest(req.caller, req.id);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
} catch (e) {
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-10-08 11:23:29 +02:00
|
|
|
const addTagToFeature = async (featureId: string, tag: ITag) => {
|
2021-10-01 13:49:18 +02:00
|
|
|
// TODO: Change this path to the new API when moved.
|
|
|
|
const path = `api/admin/features/${featureId}/tags`;
|
|
|
|
const req = createRequest(path, {
|
|
|
|
method: 'POST',
|
|
|
|
body: JSON.stringify({ ...tag }),
|
|
|
|
});
|
|
|
|
|
|
|
|
try {
|
|
|
|
const res = await makeRequest(req.caller, req.id);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
} catch (e) {
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-10-08 11:23:29 +02:00
|
|
|
const deleteTagFromFeature = async (
|
2021-10-01 13:49:18 +02:00
|
|
|
featureId: string,
|
|
|
|
type: string,
|
|
|
|
value: string
|
|
|
|
) => {
|
|
|
|
// TODO: Change this path to the new API when moved.
|
|
|
|
const path = `api/admin/features/${featureId}/tags/${type}/${value}`;
|
|
|
|
const req = createRequest(path, {
|
|
|
|
method: 'DELETE',
|
|
|
|
});
|
|
|
|
|
|
|
|
try {
|
|
|
|
const res = await makeRequest(req.caller, req.id);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
} catch (e) {
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-10-08 11:23:29 +02:00
|
|
|
const archiveFeatureToggle = async (
|
|
|
|
projectId: string,
|
|
|
|
featureId: string
|
|
|
|
) => {
|
|
|
|
const path = `api/admin/projects/${projectId}/features/${featureId}`;
|
|
|
|
const req = createRequest(path, {
|
|
|
|
method: 'DELETE',
|
|
|
|
});
|
|
|
|
|
|
|
|
try {
|
|
|
|
const res = await makeRequest(req.caller, req.id);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
} catch (e) {
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const patchFeatureToggle = async (
|
|
|
|
projectId: string,
|
|
|
|
featureId: string,
|
|
|
|
patchPayload: any
|
|
|
|
) => {
|
|
|
|
const path = `api/admin/projects/${projectId}/features/${featureId}`;
|
|
|
|
const req = createRequest(path, {
|
|
|
|
method: 'PATCH',
|
|
|
|
body: JSON.stringify(patchPayload),
|
|
|
|
});
|
|
|
|
|
|
|
|
try {
|
|
|
|
const res = await makeRequest(req.caller, req.id);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
} catch (e) {
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-01-24 15:17:27 +01:00
|
|
|
const patchFeatureVariants = async (
|
|
|
|
projectId: string,
|
|
|
|
featureId: string,
|
|
|
|
patchPayload: Operation[]
|
|
|
|
) => {
|
2021-11-25 14:05:44 +01:00
|
|
|
const path = `api/admin/projects/${projectId}/features/${featureId}/variants`;
|
|
|
|
const req = createRequest(path, {
|
|
|
|
method: 'PATCH',
|
|
|
|
body: JSON.stringify(patchPayload),
|
|
|
|
});
|
|
|
|
|
|
|
|
try {
|
|
|
|
const res = await makeRequest(req.caller, req.id);
|
|
|
|
return res;
|
|
|
|
} catch (e) {
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-10-07 23:04:14 +02:00
|
|
|
const cloneFeatureToggle = async (
|
|
|
|
projectId: string,
|
|
|
|
featureId: string,
|
2021-10-08 16:19:06 +02:00
|
|
|
payload: { name: string; replaceGroupId: boolean }
|
2021-10-07 23:04:14 +02:00
|
|
|
) => {
|
|
|
|
const path = `api/admin/projects/${projectId}/features/${featureId}/clone`;
|
2021-10-08 16:19:06 +02:00
|
|
|
const req = createRequest(path, {
|
|
|
|
method: 'POST',
|
|
|
|
body: JSON.stringify(payload),
|
|
|
|
});
|
2021-10-07 23:04:14 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
const res = await makeRequest(req.caller, req.id);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
} catch (e) {
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-10-01 13:49:18 +02:00
|
|
|
return {
|
2021-10-15 14:16:08 +02:00
|
|
|
validateFeatureToggleName,
|
2022-03-04 17:29:51 +01:00
|
|
|
validateConstraint,
|
2021-10-15 14:16:08 +02:00
|
|
|
createFeatureToggle,
|
2021-10-01 13:49:18 +02:00
|
|
|
changeFeatureProject,
|
|
|
|
errors,
|
|
|
|
toggleFeatureEnvironmentOn,
|
|
|
|
toggleFeatureEnvironmentOff,
|
2021-10-08 11:23:29 +02:00
|
|
|
addTagToFeature,
|
|
|
|
deleteTagFromFeature,
|
|
|
|
archiveFeatureToggle,
|
|
|
|
patchFeatureToggle,
|
2021-11-25 14:05:44 +01:00
|
|
|
patchFeatureVariants,
|
2021-10-08 16:19:06 +02:00
|
|
|
cloneFeatureToggle,
|
|
|
|
loading,
|
2021-10-01 13:49:18 +02:00
|
|
|
};
|
2021-08-25 13:37:22 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
export default useFeatureApi;
|