1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-04-19 01:17:18 +02:00
unleash.unleash/frontend/src/hooks/api/actions/useFeatureApi/useFeatureApi.ts
olav 8f1900f32b feat: generate an OpenAPI client (2) (#875)
* feat: add a script that generates an OpenAPI client

* feat: generate an OpenAPI client

* feat: use the generated OpenAPI client

* refactor: add an OpenAPI section to the readme

* refactor: fix missing interface prefixes

* refactor: regenerate OpenAPI client
2022-04-26 10:53:46 +02:00

245 lines
6.3 KiB
TypeScript

import { ITag } from 'interfaces/tags';
import useAPI from '../useApi/useApi';
import { Operation } from 'fast-json-patch';
import { IConstraint } from 'interfaces/strategy';
import { CreateFeatureSchema } from 'openapi';
import { openApiAdmin } from 'utils/openapiClient';
const useFeatureApi = () => {
const { makeRequest, createRequest, errors, loading } = useAPI({
propagateErrors: true,
});
const validateFeatureToggleName = async (name: string) => {
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;
}
};
const validateConstraint = async (
constraint: IConstraint
): Promise<void> => {
const path = `api/admin/constraints/validate`;
const req = createRequest(path, {
method: 'POST',
body: JSON.stringify(constraint),
});
await makeRequest(req.caller, req.id);
};
const createFeatureToggle = async (
projectId: string,
createFeatureSchema: CreateFeatureSchema
) => {
return openApiAdmin.apiAdminProjectsProjectIdFeaturesPost({
projectId,
createFeatureSchema,
});
};
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;
}
};
const changeFeatureProject = async (
projectId: string,
featureId: string,
newProjectId: string
) => {
const path = `api/admin/projects/${projectId}/features/${featureId}/changeProject`;
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;
}
};
const addTagToFeature = async (featureId: string, tag: ITag) => {
// 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;
}
};
const deleteTagFromFeature = async (
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;
}
};
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;
}
};
const patchFeatureVariants = async (
projectId: string,
featureId: string,
patchPayload: Operation[]
) => {
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;
}
};
const cloneFeatureToggle = async (
projectId: string,
featureId: string,
payload: { name: string; replaceGroupId: boolean }
) => {
const path = `api/admin/projects/${projectId}/features/${featureId}/clone`;
const req = createRequest(path, {
method: 'POST',
body: JSON.stringify(payload),
});
try {
const res = await makeRequest(req.caller, req.id);
return res;
} catch (e) {
throw e;
}
};
return {
validateFeatureToggleName,
validateConstraint,
createFeatureToggle,
changeFeatureProject,
errors,
toggleFeatureEnvironmentOn,
toggleFeatureEnvironmentOff,
addTagToFeature,
deleteTagFromFeature,
archiveFeatureToggle,
patchFeatureToggle,
patchFeatureVariants,
cloneFeatureToggle,
loading,
};
};
export default useFeatureApi;