The weeb for the next gen discord boat - Wamellow wamellow.com
bot discord
at master 37 lines 1.1 kB view raw
1import type { ApiError, ApiV1GuildsGetResponse } from "@/typings"; 2 3export interface ApiRequestOptions { 4 force?: boolean; 5} 6 7export const cacheOptions = { 8 cacheTime: 1_000 * 60 * 60, 9 refetchOnWindowFocus: false, 10 refetchOnMount: false 11}; 12 13export const defaultFetchOptions = { 14 headers: { Authorization: process.env.API_SECRET as string }, 15 next: { revalidate: 60 * 60 } 16}; 17 18export async function getData<T>(path: string) { 19 const response = await fetch(`${process.env.NEXT_PUBLIC_API}${path}`, { 20 credentials: "include" 21 }); 22 23 return response.json() as Promise<T | ApiError>; 24} 25 26export async function getGuild(guildId?: string | null, options?: ApiRequestOptions): Promise<ApiV1GuildsGetResponse | ApiError | undefined> { 27 if (!guildId) return; 28 29 const res = await fetch(`${process.env.NEXT_PUBLIC_API}/guilds/${guildId}`, { 30 headers: { Authorization: process.env.API_SECRET as string }, 31 next: options?.force 32 ? { revalidate: 0 } 33 : { revalidate: 60 * 60 } 34 }); 35 36 return res.json(); 37}