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';
|
2022-11-24 05:03:20 +03:00
|
|
|
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/`;
|
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
|
|
|
}}
|
|
|
|
|
>
|
2022-11-24 05:03:20 +03:00
|
|
|
<WsWithConfig>{children}</WsWithConfig>
|
2022-02-26 22:11:00 +03:00
|
|
|
</SWRConfig>
|
2021-02-16 07:10:20 +03:00
|
|
|
);
|
2021-02-18 07:53:57 +03:00
|
|
|
}
|
|
|
|
|
|
2022-11-24 05:03:20 +03:00
|
|
|
function WsWithConfig({ children }) {
|
2022-02-26 22:11:00 +03:00
|
|
|
const { data } = useSWR('config');
|
2022-11-24 05:03:20 +03:00
|
|
|
return data ? <WsProvider config={data}>{children}</WsProvider> : children;
|
2022-02-22 07:03:01 +03:00
|
|
|
}
|
|
|
|
|
|
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
|
|
|
}
|