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

18 lines
559 B
JavaScript

import useSWR from "swr";
import { formatProxyUrl } from "./api-helpers";
export default function useWidgetAPI(widget, endpoint, queryParams, swrConfig = {}) {
const config = { ...swrConfig };
if (queryParams?.refreshInterval) {
config.refreshInterval = queryParams.refreshInterval;
}
let url = formatProxyUrl(widget, endpoint, queryParams);
if (endpoint === "") {
url = null;
}
const { data, error, mutate } = useSWR(url, config);
// make the data error the top-level error
return { data, error: data?.error ?? error, mutate };
}