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.

Restore text fetcher

Tim Wilsonceeff353391f
diff --git a/packages/utils/src/functions/index.ts b/packages/utils/src/functions/index.ts
index a0a17a2b42..5ea4ca5f59 100644
--- a/packages/utils/src/functions/index.ts
+++ b/packages/utils/src/functions/index.ts
@@ -30,6 +30,7 @@ export * from "./regex-escape";
export * from "./resize-image";
export * from "./smart-truncate";
export * from "./stable-sort";
+export * from "./text-fetcher";
export * from "./time-ago";
export * from "./trim";
export * from "./truncate";
diff --git a/packages/utils/src/functions/text-fetcher.ts b/packages/utils/src/functions/text-fetcher.ts
new file mode 100644
index 0000000000..dc17e629fb
--- /dev/null
+++ b/packages/utils/src/functions/text-fetcher.ts
@@ -0,0 +1,28 @@
+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;
+ } catch (e) {}
+ const error = new Error(message) as SWRError;
+ error.info = message;
+ error.status = res.status;
+
+ throw error;
+ }
+
+ return res.text();
+}