homepage/src/utils/proxy/use-widget-api.js

18 lines
515 B
JavaScript
Raw Normal View History

2022-09-27 22:59:14 +03:00
import useSWR from "swr";
import { formatProxyUrl } from "./api-helpers";
export default function useWidgetAPI(widget, ...options) {
2022-11-07 09:24:15 -08:00
const config = {};
2023-01-09 01:33:58 -08:00
if (options && options[1]?.refreshInterval) {
config.refreshInterval = options[1].refreshInterval;
2022-11-07 09:24:15 -08:00
}
let url = formatProxyUrl(widget, ...options)
if (options[0] === "") {
url = null
}
const { data, error, mutate } = useSWR(url, config);
2022-11-07 09:24:15 -08:00
// make the data error the top-level error
2023-04-05 22:45:27 -07:00
return { data, error: data?.error ?? error, mutate }
2022-09-27 22:59:14 +03:00
}