@daveyplate/supabase-swr-entities
Version:
An entity management library for Supabase and SWR
43 lines (42 loc) • 1.41 kB
JavaScript
import { useCallback } from "react";
import { useSWRConfig } from "swr";
/**
* Get the locale value from the internationalized data.
*/
export function getLocaleValue(obj, locale, defaultLocale) {
return (obj === null || obj === void 0 ? void 0 : obj[locale]) || (defaultLocale ? obj === null || obj === void 0 ? void 0 : obj[defaultLocale] : obj === null || obj === void 0 ? void 0 : obj[Object.keys(obj)[0]]);
}
export function useClearCache() {
const { cache } = useSWRConfig();
const clearCache = useCallback(() => {
for (let key of cache.keys())
cache.delete(key);
}, [cache]);
return { clearCache };
}
/**
* Generate API path from table, id & params.
*/
export function apiPath(table, id, params) {
if (!table)
return null;
const route = table.replaceAll('_', '-');
let path = `/api/${route}`;
if (id) {
path += `/${id}`;
}
if (params) {
const query = new URLSearchParams(params);
path += `?${query.toString()}`;
}
return path;
}
/**
* process.env.NEXT_PUBLIC_IS_EXPORT == '1' or 'true' || process.env.NEXT_PUBLIC_IS_MOBILE == '1' or 'true'
*/
export const isExport = () => {
return process.env.NEXT_PUBLIC_IS_EXPORT == '1'
|| process.env.NEXT_PUBLIC_IS_EXPORT == 'true'
|| process.env.NEXT_PUBLIC_IS_MOBILE == '1'
|| process.env.NEXT_PUBLIC_IS_MOBILE == 'true';
};