homepage/src/utils/proxy/use-widget-api.js
shamoon 19c25713c4 Run pre-commit hooks over existing codebase
Co-Authored-By: Ben Phelps <ben@phelps.io>
2023-10-18 09:49:33 -07:00

18 lines
518 B
JavaScript

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