mirror of
https://github.com/Unleash/unleash.git
synced 2025-10-13 11:17:26 +02:00
<!-- Thanks for creating a PR! To make it easier for reviewers and everyone else to understand what your changes relate to, please add some relevant content to the headings below. Feel free to ignore or delete sections that you don't think are relevant. Thank you! ❤️ --> ## About the changes <!-- Describe the changes introduced. What are they and why are they being introduced? Feel free to also add screenshots or steps to view the changes if they're visual. --> <!-- Does it close an issue? Multiple? --> Closes # <!-- (For internal contributors): Does it relate to an issue on public roadmap? --> <!-- Relates to [roadmap](https://github.com/orgs/Unleash/projects/10) item: # --> ### Important files <!-- PRs can contain a lot of changes, but not all changes are equally important. Where should a reviewer start looking to get an overview of the changes? Are any files particularly important? --> ## Discussion points <!-- Anything about the PR you'd like to discuss before it gets merged? Got any questions or doubts? --> --------- Signed-off-by: andreas-unleash <andreas@getunleash.ai> Co-authored-by: Tymoteusz Czech <2625371+Tymek@users.noreply.github.com>
288 lines
7.4 KiB
TypeScript
288 lines
7.4 KiB
TypeScript
import type { BatchStaleSchema } from 'openapi';
|
|
import useAPI from '../useApi/useApi';
|
|
|
|
interface ICreatePayload {
|
|
id: string;
|
|
name: string;
|
|
description: string;
|
|
mode: 'open' | 'protected';
|
|
defaultStickiness: string;
|
|
}
|
|
|
|
interface IAccessesPayload {
|
|
users: { id: number }[];
|
|
groups: { id: number }[];
|
|
}
|
|
|
|
const useProjectApi = () => {
|
|
const { makeRequest, createRequest, errors, loading } = useAPI({
|
|
propagateErrors: true,
|
|
});
|
|
|
|
const createProject = async (payload: ICreatePayload) => {
|
|
const path = `api/admin/projects`;
|
|
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;
|
|
}
|
|
};
|
|
|
|
const validateId = async (id: ICreatePayload['id']) => {
|
|
const path = `api/admin/projects/validate`;
|
|
const req = createRequest(path, {
|
|
method: 'POST',
|
|
body: JSON.stringify({ id }),
|
|
});
|
|
try {
|
|
const res = await makeRequest(req.caller, req.id);
|
|
|
|
return res;
|
|
} catch (e) {
|
|
throw e;
|
|
}
|
|
};
|
|
|
|
const editProject = async (id: string, payload: ICreatePayload) => {
|
|
const path = `api/admin/projects/${id}`;
|
|
const req = createRequest(path, {
|
|
method: 'PUT',
|
|
body: JSON.stringify(payload),
|
|
});
|
|
|
|
try {
|
|
const res = await makeRequest(req.caller, req.id);
|
|
|
|
return res;
|
|
} catch (e) {
|
|
throw e;
|
|
}
|
|
};
|
|
|
|
const deleteProject = async (projectId: string) => {
|
|
const path = `api/admin/projects/${projectId}`;
|
|
const req = createRequest(path, { method: 'DELETE' });
|
|
|
|
try {
|
|
const res = await makeRequest(req.caller, req.id);
|
|
|
|
return res;
|
|
} catch (e) {
|
|
throw e;
|
|
}
|
|
};
|
|
|
|
const addEnvironmentToProject = async (
|
|
projectId: string,
|
|
environment: string
|
|
) => {
|
|
const path = `api/admin/projects/${projectId}/environments`;
|
|
const req = createRequest(path, {
|
|
method: 'POST',
|
|
body: JSON.stringify({ environment }),
|
|
});
|
|
|
|
try {
|
|
const res = await makeRequest(req.caller, req.id);
|
|
|
|
return res;
|
|
} catch (e) {
|
|
throw e;
|
|
}
|
|
};
|
|
|
|
const removeEnvironmentFromProject = async (
|
|
projectId: string,
|
|
environment: string
|
|
) => {
|
|
const path = `api/admin/projects/${projectId}/environments/${environment}`;
|
|
const req = createRequest(path, { method: 'DELETE' });
|
|
|
|
try {
|
|
const res = await makeRequest(req.caller, req.id);
|
|
|
|
return res;
|
|
} catch (e) {
|
|
throw e;
|
|
}
|
|
};
|
|
|
|
const addAccessToProject = async (
|
|
projectId: string,
|
|
roleId: number,
|
|
accesses: IAccessesPayload
|
|
) => {
|
|
const path = `api/admin/projects/${projectId}/role/${roleId}/access`;
|
|
const req = createRequest(path, {
|
|
method: 'POST',
|
|
body: JSON.stringify(accesses),
|
|
});
|
|
|
|
try {
|
|
const res = await makeRequest(req.caller, req.id);
|
|
|
|
return res;
|
|
} catch (e) {
|
|
throw e;
|
|
}
|
|
};
|
|
|
|
const removeUserFromRole = async (
|
|
projectId: string,
|
|
roleId: number,
|
|
userId: number
|
|
) => {
|
|
const path = `api/admin/projects/${projectId}/users/${userId}/roles/${roleId}`;
|
|
const req = createRequest(path, { method: 'DELETE' });
|
|
|
|
try {
|
|
const res = await makeRequest(req.caller, req.id);
|
|
|
|
return res;
|
|
} catch (e) {
|
|
throw e;
|
|
}
|
|
};
|
|
|
|
const removeGroupFromRole = async (
|
|
projectId: string,
|
|
roleId: number,
|
|
groupId: number
|
|
) => {
|
|
const path = `api/admin/projects/${projectId}/groups/${groupId}/roles/${roleId}`;
|
|
const req = createRequest(path, { method: 'DELETE' });
|
|
|
|
try {
|
|
const res = await makeRequest(req.caller, req.id);
|
|
|
|
return res;
|
|
} catch (e) {
|
|
throw e;
|
|
}
|
|
};
|
|
|
|
const searchProjectUser = async (query: string): Promise<Response> => {
|
|
const path = `api/admin/user-admin/search?q=${query}`;
|
|
|
|
const req = createRequest(path, { method: 'GET' });
|
|
|
|
try {
|
|
const res = await makeRequest(req.caller, req.id);
|
|
|
|
return res;
|
|
} catch (e) {
|
|
throw e;
|
|
}
|
|
};
|
|
|
|
const changeUserRole = (
|
|
projectId: string,
|
|
roleId: number,
|
|
userId: number
|
|
) => {
|
|
const path = `api/admin/projects/${projectId}/users/${userId}/roles/${roleId}`;
|
|
const req = createRequest(path, { method: 'PUT' });
|
|
|
|
return makeRequest(req.caller, req.id);
|
|
};
|
|
|
|
const changeGroupRole = (
|
|
projectId: string,
|
|
roleId: number,
|
|
groupId: number
|
|
) => {
|
|
const path = `api/admin/projects/${projectId}/groups/${groupId}/roles/${roleId}`;
|
|
const req = createRequest(path, { method: 'PUT' });
|
|
|
|
return makeRequest(req.caller, req.id);
|
|
};
|
|
const archiveFeatures = async (projectId: string, featureIds: string[]) => {
|
|
const path = `api/admin/projects/${projectId}/archive`;
|
|
const req = createRequest(path, {
|
|
method: 'POST',
|
|
body: JSON.stringify({ features: featureIds }),
|
|
});
|
|
|
|
return makeRequest(req.caller, req.id);
|
|
};
|
|
|
|
const reviveFeatures = async (projectId: string, featureIds: string[]) => {
|
|
const path = `api/admin/projects/${projectId}/revive`;
|
|
const req = createRequest(path, {
|
|
method: 'POST',
|
|
body: JSON.stringify({ features: featureIds }),
|
|
});
|
|
|
|
return makeRequest(req.caller, req.id);
|
|
};
|
|
|
|
const deleteFeature = async (featureId: string) => {
|
|
const path = `api/admin/archive/${featureId}`;
|
|
const req = createRequest(path, {
|
|
method: 'DELETE',
|
|
});
|
|
|
|
return makeRequest(req.caller, req.id);
|
|
};
|
|
|
|
const deleteFeatures = async (projectId: string, featureIds: string[]) => {
|
|
const path = `api/admin/projects/${projectId}/delete`;
|
|
const req = createRequest(path, {
|
|
method: 'POST',
|
|
body: JSON.stringify({ features: featureIds }),
|
|
});
|
|
|
|
return makeRequest(req.caller, req.id);
|
|
};
|
|
|
|
const staleFeatures = async (
|
|
projectId: string,
|
|
featureIds: string[],
|
|
stale = true
|
|
) => {
|
|
const payload: BatchStaleSchema = {
|
|
features: featureIds,
|
|
stale,
|
|
};
|
|
|
|
const path = `api/admin/projects/${projectId}/stale`;
|
|
const req = createRequest(path, {
|
|
method: 'POST',
|
|
body: JSON.stringify(payload),
|
|
});
|
|
|
|
return makeRequest(req.caller, req.id);
|
|
};
|
|
|
|
return {
|
|
createProject,
|
|
validateId,
|
|
editProject,
|
|
deleteProject,
|
|
addEnvironmentToProject,
|
|
removeEnvironmentFromProject,
|
|
addAccessToProject,
|
|
removeUserFromRole,
|
|
removeGroupFromRole,
|
|
changeUserRole,
|
|
changeGroupRole,
|
|
archiveFeatures,
|
|
reviveFeatures,
|
|
staleFeatures,
|
|
deleteFeature,
|
|
deleteFeatures,
|
|
searchProjectUser,
|
|
errors,
|
|
loading,
|
|
};
|
|
};
|
|
|
|
export default useProjectApi;
|