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