frigate/web-old/src/api/index.jsx

34 lines
789 B
React
Raw Normal View History

2022-02-26 22:11:00 +03:00
import { h } from 'preact';
2021-02-11 18:13:21 +03:00
import { baseUrl } from './baseUrl';
2022-02-26 22:11:00 +03:00
import useSWR, { SWRConfig } from 'swr';
import { WsProvider } from './ws';
2022-02-26 22:11:00 +03:00
import axios from 'axios';
2021-01-26 18:04:03 +03:00
2022-05-19 15:31:02 +03:00
axios.defaults.baseURL = `${baseUrl}api/`;
axios.defaults.headers.common = {
'X-CSRF-TOKEN': 1,
'X-CACHE-BYPASS': 1,
};
2021-01-26 18:04:03 +03:00
2022-03-06 07:16:31 +03:00
export function ApiProvider({ children, options }) {
2021-02-16 07:10:20 +03:00
return (
2022-02-26 22:11:00 +03:00
<SWRConfig
value={{
2022-05-12 20:05:34 +03:00
fetcher: (path, params) => axios.get(path, { params }).then((res) => res.data),
2022-03-06 07:16:31 +03:00
...options,
2022-02-26 22:11:00 +03:00
}}
>
<WsWithConfig>{children}</WsWithConfig>
2022-02-26 22:11:00 +03:00
</SWRConfig>
2021-02-16 07:10:20 +03:00
);
}
function WsWithConfig({ children }) {
2022-02-26 22:11:00 +03:00
const { data } = useSWR('config');
return data ? <WsProvider config={data}>{children}</WsProvider> : children;
}
2021-01-26 18:04:03 +03:00
export function useApiHost() {
2022-02-26 22:11:00 +03:00
return baseUrl;
2021-01-26 18:04:03 +03:00
}