misc_pterodactyl-panel/resources/scripts/api/account/api-keys.ts

34 lines
1.3 KiB
TypeScript
Raw Normal View History

import useSWR, { SWRConfiguration, SWRResponse } from 'swr';
import http, { FractalResponseList } from '@/api/http';
2022-02-27 16:04:50 +00:00
import Transformers from '@definitions/user/transformers';
import { PersonalAccessToken } from '@definitions/user/models';
import { AxiosError } from 'axios';
import useUserSWRContextKey from '@/plugins/useUserSWRContextKey';
const useAPIKeys = (
config?: SWRConfiguration<PersonalAccessToken[], AxiosError>,
): SWRResponse<PersonalAccessToken[], AxiosError> => {
const key = useUserSWRContextKey([ 'account', 'api-keys' ]);
return useSWR(key, async () => {
const { data } = await http.get('/api/client/account/api-keys');
return (data as FractalResponseList).data.map((datum: any) => {
return Transformers.toPersonalAccessToken(datum.attributes);
});
}, config || { revalidateOnMount: false });
};
const createAPIKey = async (description: string): Promise<[ PersonalAccessToken, string ]> => {
const { data } = await http.post('/api/client/account/api-keys', { description });
const token = Transformers.toPersonalAccessToken(data.attributes);
return [ token, data.meta?.secret_token || '' ];
};
const deleteAPIKey = async (identifier: string) =>
await http.delete(`/api/client/account/api-keys/${identifier}`);
export { useAPIKeys, createAPIKey, deleteAPIKey };