Path: blob/1.0-develop/resources/scripts/api/account/ssh-keys.ts
7461 views
import useSWR, { ConfigInterface } from 'swr';1import http, { FractalResponseList } from '@/api/http';2import { SSHKey, Transformers } from '@definitions/user';3import { AxiosError } from 'axios';4import { useUserSWRKey } from '@/plugins/useSWRKey';56const useSSHKeys = (config?: ConfigInterface<SSHKey[], AxiosError>) => {7const key = useUserSWRKey(['account', 'ssh-keys']);89return useSWR(10key,11async () => {12const { data } = await http.get('/api/client/account/ssh-keys');1314return (data as FractalResponseList).data.map((datum: any) => {15return Transformers.toSSHKey(datum.attributes);16});17},18{ revalidateOnMount: false, ...(config || {}) }19);20};2122const createSSHKey = async (name: string, publicKey: string): Promise<SSHKey> => {23const { data } = await http.post('/api/client/account/ssh-keys', { name, public_key: publicKey });2425return Transformers.toSSHKey(data.attributes);26};2728const deleteSSHKey = async (fingerprint: string): Promise<void> =>29await http.post('/api/client/account/ssh-keys/remove', { fingerprint });3031export { useSSHKeys, createSSHKey, deleteSSHKey };323334