-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathuseEndpoints.ts
More file actions
44 lines (34 loc) · 1.8 KB
/
useEndpoints.ts
File metadata and controls
44 lines (34 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import useSWR, {type Fetcher, type SWRConfiguration} from 'swr';
import {authenticatedFetcher, fetcher} from '../utils/swr.js';
import {useAuthStorage} from './useAuthStorage.ts';
import type {FaunaPresentUser, FaunaUser} from "../fauna-types";
import type {MqttSensorHistory, PortierActionLogEntry, PortierDevice, RawMqttReading} from "../portier-types";
function useAuthSWR<TResult>(key: URL | string, config?: SWRConfiguration) {
const {accessToken} = useAuthStorage();
const hasAccessToken = !!accessToken;
const path = hasAccessToken ? key : null;
return useSWR(path, authenticatedFetcher as Fetcher<TResult>, config);
}
function useCheckSWR<TResult>(key: URL | string, config?: SWRConfiguration) {
return useSWR(key, fetcher as Fetcher<TResult>, config);
}
export function useDevices(config?: SWRConfiguration) {
return useAuthSWR<PortierDevice[]>(import.meta.env.PORTIER_URL.concat('api/devices'), config);
}
export function useCurrentUser(config?: SWRConfiguration) {
return useAuthSWR<FaunaUser>(import.meta.env.OIDC_AUTHORITY_URL.concat('api/current_user'), config);
}
export function usePresentUsers(config?: SWRConfiguration) {
return useCheckSWR<FaunaPresentUser[]>(import.meta.env.PRESENCE_URL.concat('api/users/present'), config);
}
export function useMqttStatus(config?: SWRConfiguration) {
return useCheckSWR<{ [topic: string]: RawMqttReading }>(import.meta.env.MQTT_PROXY_URL.concat('status'), config);
}
export function useMqttHistory(config?: SWRConfiguration) {
return useCheckSWR<{
[sensor: string]: MqttSensorHistory
}>(import.meta.env.MQTT_PROXY_URL.concat('sensors/'), config);
}
export function useActionLog(config?: SWRConfiguration) {
return useAuthSWR<PortierActionLogEntry[]>(import.meta.env.PORTIER_URL.concat('api/actionLog/0/0'), config);
}