chatbot-template/lib/utils.ts
2024-10-11 18:00:22 +05:30

43 lines
978 B
TypeScript

import { clsx, type ClassValue } from "clsx";
import { twMerge } from "tailwind-merge";
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs));
}
interface ApplicationError extends Error {
info: string;
status: number;
}
export const fetcher = async (url: string) => {
const res = await fetch(url);
if (!res.ok) {
const error = new Error(
"An error occurred while fetching the data.",
) as ApplicationError;
error.info = await res.json();
error.status = res.status;
throw error;
}
return res.json();
};
export function getLocalStorage(key: string) {
if (typeof window !== "undefined") {
return JSON.parse(localStorage.getItem(key) || "[]");
}
return [];
}
export function generateUUID(): string {
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (c) {
const r = (Math.random() * 16) | 0;
const v = c === "x" ? r : (r & 0x3) | 0x8;
return v.toString(16);
});
}