2024-10-09 06:46:46 +00:00
|
|
|
import ky, { KyInstance } from 'ky';
|
2022-08-09 15:24:43 +00:00
|
|
|
|
2024-10-09 06:46:46 +00:00
|
|
|
import { useAppSelector } from './useAppSelector';
|
|
|
|
import { useOwnAccount } from './useOwnAccount';
|
2022-08-09 15:24:43 +00:00
|
|
|
|
2024-10-09 06:46:46 +00:00
|
|
|
export function useApi(): KyInstance {
|
|
|
|
const { account } = useOwnAccount();
|
|
|
|
const accessToken = useAppSelector((state) => account ? state.auth.users.get(account.url)?.access_token : undefined);
|
|
|
|
|
|
|
|
const headers: Record<string, string> = {};
|
|
|
|
|
|
|
|
if (accessToken) {
|
|
|
|
headers.Authorization = `Bearer ${accessToken}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ky.create({
|
|
|
|
prefixUrl: account ? new URL(account.url).origin : undefined,
|
|
|
|
headers,
|
|
|
|
});
|
|
|
|
}
|