MCPcopy
hub / github.com/pingdotgg/uploadthing / fetchData

Function fetchData

packages/react/src/utils/useFetch.ts:57–86  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

55 cancelRequest.current = false;
56
57 const fetchData = async () => {
58 dispatch({ type: "loading" });
59
60 // If a cache exists for this url, return it
61 if (cache.current[url]) {
62 dispatch({ type: "fetched", payload: cache.current[url] });
63 return;
64 }
65
66 try {
67 const response = await fetch(url, options);
68 if (!response.ok) {
69 throw new Error(response.statusText);
70 }
71
72 const dataOrError = await safeParseJSON<T>(response);
73 if (dataOrError instanceof Error) {
74 throw dataOrError;
75 }
76
77 cache.current[url] = dataOrError;
78 if (cancelRequest.current) return;
79
80 dispatch({ type: "fetched", payload: dataOrError });
81 } catch (error) {
82 if (cancelRequest.current) return;
83
84 dispatch({ type: "error", payload: error as Error });
85 }
86 };
87
88 void fetchData();
89

Callers 1

useFetchFunction · 0.70

Calls 2

dispatchFunction · 0.85
fetchFunction · 0.85

Tested by

no test coverage detected