mirror of
https://github.com/Unleash/unleash.git
synced 2025-03-27 00:19:39 +01:00
feat: service accounts (hooks) (#2732)
https://linear.app/unleash/issue/2-539/hooks-for-service-accounts Creates and adapts hooks for the Service Accounts feature, to be used by the FE.
This commit is contained in:
parent
3a8107ce6e
commit
11f4435a9e
@ -37,9 +37,26 @@ export const usePersonalAPITokensApi = () => {
|
||||
}
|
||||
};
|
||||
|
||||
const createUserPersonalAPIToken = async (
|
||||
userId: number,
|
||||
payload: ICreatePersonalApiTokenPayload
|
||||
): Promise<INewPersonalAPIToken> => {
|
||||
const req = createRequest(`api/admin/user-admin/${userId}/pat`, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(payload),
|
||||
});
|
||||
try {
|
||||
const response = await makeRequest(req.caller, req.id);
|
||||
return await response.json();
|
||||
} catch (e) {
|
||||
throw e;
|
||||
}
|
||||
};
|
||||
|
||||
return {
|
||||
createPersonalAPIToken,
|
||||
deletePersonalAPIToken,
|
||||
createUserPersonalAPIToken,
|
||||
errors,
|
||||
loading,
|
||||
};
|
||||
|
@ -0,0 +1,66 @@
|
||||
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 await 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,
|
||||
};
|
||||
};
|
@ -0,0 +1,35 @@
|
||||
import IRole from 'interfaces/role';
|
||||
import { IUser } from 'interfaces/user';
|
||||
import { useMemo } from 'react';
|
||||
import { formatApiPath } from 'utils/formatPath';
|
||||
import handleErrorResponses from '../httpErrorResponseHandler';
|
||||
import { useConditionalSWR } from '../useConditionalSWR/useConditionalSWR';
|
||||
import useUiConfig from '../useUiConfig/useUiConfig';
|
||||
|
||||
export const useServiceAccounts = () => {
|
||||
const { uiConfig, isEnterprise } = useUiConfig();
|
||||
|
||||
const { data, error, mutate } = useConditionalSWR(
|
||||
Boolean(uiConfig.flags.serviceAccounts) && isEnterprise(),
|
||||
{ users: [], rootRoles: [] },
|
||||
formatApiPath(`api/admin/service-account`),
|
||||
fetcher
|
||||
);
|
||||
|
||||
return useMemo(
|
||||
() => ({
|
||||
serviceAccounts: (data?.users ?? []) as IUser[],
|
||||
roles: (data?.rootRoles ?? []) as IRole[],
|
||||
loading: !error && !data,
|
||||
refetch: () => mutate(),
|
||||
error,
|
||||
}),
|
||||
[data, error, mutate]
|
||||
);
|
||||
};
|
||||
|
||||
const fetcher = (path: string) => {
|
||||
return fetch(path)
|
||||
.then(handleErrorResponses('Service Accounts'))
|
||||
.then(res => res.json());
|
||||
};
|
@ -43,6 +43,7 @@ export interface IFlags {
|
||||
networkView?: boolean;
|
||||
maintenance?: boolean;
|
||||
messageBanner?: boolean;
|
||||
serviceAccounts?: boolean;
|
||||
}
|
||||
|
||||
export interface IVersionInfo {
|
||||
|
Loading…
Reference in New Issue
Block a user