feature/tauri-js-rs
ts 43 lines 1013 Bytes
Raw
1 import { clsx, type ClassValue } from 'clsx'
2 import { customAlphabet } from 'nanoid'
3 import { twMerge } from 'tailwind-merge'
4
5 export function cn(...inputs: ClassValue[]) {
6 return twMerge(clsx(inputs))
7 }
8
9 export const nanoid = customAlphabet(
10 '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz',
11 7
12 ) // 7-character random string
13
14 export async function fetcher<JSON = any>(
15 input: RequestInfo,
16 init?: RequestInit
17 ): Promise<JSON> {
18 const res = await fetch(input, init)
19
20 if (!res.ok) {
21 const json = await res.json()
22 if (json.error) {
23 const error = new Error(json.error) as Error & {
24 status: number
25 }
26 error.status = res.status
27 throw error
28 } else {
29 throw new Error('An unexpected error occurred')
30 }
31 }
32
33 return res.json()
34 }
35
36 export function formatDate(input: string | number | Date): string {
37 const date = new Date(input)
38 return date.toLocaleDateString('en-US', {
39 month: 'long',
40 day: 'numeric',
41 year: 'numeric'
42 })
43 }