mirror of
https://github.com/formbricks/formbricks.git
synced 2026-05-05 05:11:32 -05:00
40 lines
1.1 KiB
TypeScript
40 lines
1.1 KiB
TypeScript
export const fetchRessource = async (url: string) => {
|
|
const res = await fetch(url);
|
|
|
|
// If the status code is not in the range 200-299,
|
|
// we still try to parse and throw it.
|
|
if (!res.ok) {
|
|
const error: any = new Error("An error occurred while fetching the data.");
|
|
// Attach extra info to the error object.
|
|
error.info = await res.json();
|
|
error.status = res.status;
|
|
throw error;
|
|
}
|
|
|
|
return res.json();
|
|
};
|
|
|
|
export const fetcher = async (url: string) => {
|
|
const res = await fetch(url);
|
|
|
|
// If the status code is not in the range 200-299,
|
|
// we still try to parse and throw it.
|
|
if (!res.ok) {
|
|
const error: any = new Error("An error occurred while fetching the data.");
|
|
// Attach extra info to the error object.
|
|
error.info = await res.json();
|
|
error.status = res.status;
|
|
throw error;
|
|
}
|
|
|
|
return res.json();
|
|
};
|
|
|
|
export const updateRessource = async (url: string, { arg }: { arg: any }) => {
|
|
return fetch(url, {
|
|
method: "PUT",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify(arg),
|
|
});
|
|
};
|