Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

642 B
Raw Blame
interface SWRError extends Error {
  info: any;
  status: number;
}

export async function textFetcher(
  input: RequestInfo,
  init?: RequestInit & { headers?: Record<string, string> },
): Promise<string> {
  const res = await fetch(input, {
    ...init,
    ...(init?.headers && { headers: init.headers }),
  });

  if (!res.ok) {
    let message = "An error occurred while fetching the data.";
    try {
      message = (await res.json())?.error?.message || message;
    } catch (e) {}
    const error = new Error(message) as SWRError;
    error.info = message;
    error.status = res.status;

    throw error;
  }

  return res.text();
}