1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-01-20 00:08:02 +01:00
unleash.unleash/frontend/src/hooks/api/actions/useServiceAccountsApi/useServiceAccountsApi.ts
Nuno Góis 280e21f073
refactor: bubble promise instead of return await (#4906)
Tiny refactor that bubbles promises instead of using `return await`.
Should be more consistent with the rest of the changes in
https://github.com/Unleash/unleash/pull/4903
2023-10-02 13:59:53 +01:00

67 lines
1.7 KiB
TypeScript

import useAPI from '../useApi/useApi';
export interface IServiceAccountPayload {
name: string;
username: string;
rootRole: number;
}
export const useServiceAccountsApi = () => {
const { loading, makeRequest, createRequest, errors } = useAPI({
propagateErrors: true,
});
const addServiceAccount = async (
serviceAccount: IServiceAccountPayload,
) => {
const requestId = 'addServiceAccount';
const req = createRequest(
'api/admin/service-account',
{
method: 'POST',
body: JSON.stringify(serviceAccount),
},
requestId,
);
const response = await makeRequest(req.caller, req.id);
return response.json();
};
const removeServiceAccount = async (serviceAccountId: number) => {
const requestId = 'removeServiceAccount';
const req = createRequest(
`api/admin/service-account/${serviceAccountId}`,
{ method: 'DELETE' },
requestId,
);
await makeRequest(req.caller, req.id);
};
const updateServiceAccount = async (
serviceAccountId: number,
serviceAccount: IServiceAccountPayload,
) => {
const requestId = 'updateServiceAccount';
const req = createRequest(
`api/admin/service-account/${serviceAccountId}`,
{
method: 'PUT',
body: JSON.stringify(serviceAccount),
},
requestId,
);
await makeRequest(req.caller, req.id);
};
return {
addServiceAccount,
updateServiceAccount,
removeServiceAccount,
errors,
loading,
};
};