Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/resources/scripts/api/account/ssh-keys.ts
7461 views
1
import useSWR, { ConfigInterface } from 'swr';
2
import http, { FractalResponseList } from '@/api/http';
3
import { SSHKey, Transformers } from '@definitions/user';
4
import { AxiosError } from 'axios';
5
import { useUserSWRKey } from '@/plugins/useSWRKey';
6
7
const useSSHKeys = (config?: ConfigInterface<SSHKey[], AxiosError>) => {
8
const key = useUserSWRKey(['account', 'ssh-keys']);
9
10
return useSWR(
11
key,
12
async () => {
13
const { data } = await http.get('/api/client/account/ssh-keys');
14
15
return (data as FractalResponseList).data.map((datum: any) => {
16
return Transformers.toSSHKey(datum.attributes);
17
});
18
},
19
{ revalidateOnMount: false, ...(config || {}) }
20
);
21
};
22
23
const createSSHKey = async (name: string, publicKey: string): Promise<SSHKey> => {
24
const { data } = await http.post('/api/client/account/ssh-keys', { name, public_key: publicKey });
25
26
return Transformers.toSSHKey(data.attributes);
27
};
28
29
const deleteSSHKey = async (fingerprint: string): Promise<void> =>
30
await http.post('/api/client/account/ssh-keys/remove', { fingerprint });
31
32
export { useSSHKeys, createSSHKey, deleteSSHKey };
33
34