2022-11-25 20:25:03 +00:00
|
|
|
import type { AxiosError } from 'axios';
|
|
|
|
import type { SWRConfiguration } from 'swr';
|
|
|
|
import useSWR from 'swr';
|
|
|
|
|
2022-05-14 21:31:53 +00:00
|
|
|
import http, { FractalResponseList } from '@/api/http';
|
|
|
|
import { SSHKey, Transformers } from '@definitions/user';
|
2022-07-04 22:22:58 +00:00
|
|
|
import { useUserSWRKey } from '@/plugins/useSWRKey';
|
2022-05-14 21:31:53 +00:00
|
|
|
|
2022-11-25 20:25:03 +00:00
|
|
|
const useSSHKeys = (config?: SWRConfiguration<SSHKey[], AxiosError>) => {
|
2022-07-04 22:22:58 +00:00
|
|
|
const key = useUserSWRKey(['account', 'ssh-keys']);
|
2022-05-14 21:31:53 +00:00
|
|
|
|
2022-06-26 19:13:52 +00:00
|
|
|
return useSWR(
|
|
|
|
key,
|
|
|
|
async () => {
|
|
|
|
const { data } = await http.get('/api/client/account/ssh-keys');
|
2022-05-14 21:31:53 +00:00
|
|
|
|
2022-06-26 19:13:52 +00:00
|
|
|
return (data as FractalResponseList).data.map((datum: any) => {
|
|
|
|
return Transformers.toSSHKey(datum.attributes);
|
|
|
|
});
|
|
|
|
},
|
2022-11-25 20:25:03 +00:00
|
|
|
{ revalidateOnMount: false, ...(config || {}) },
|
2022-06-26 19:13:52 +00:00
|
|
|
);
|
2022-05-14 21:31:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const createSSHKey = async (name: string, publicKey: string): Promise<SSHKey> => {
|
|
|
|
const { data } = await http.post('/api/client/account/ssh-keys', { name, public_key: publicKey });
|
|
|
|
|
|
|
|
return Transformers.toSSHKey(data.attributes);
|
|
|
|
};
|
|
|
|
|
|
|
|
const deleteSSHKey = async (fingerprint: string): Promise<void> =>
|
2022-05-30 21:28:42 +00:00
|
|
|
await http.post('/api/client/account/ssh-keys/remove', { fingerprint });
|
2022-05-14 21:31:53 +00:00
|
|
|
|
|
|
|
export { useSSHKeys, createSSHKey, deleteSSHKey };
|