Skip to content

Commit

Permalink
make code readable
Browse files Browse the repository at this point in the history
  • Loading branch information
rithviknishad committed Oct 9, 2023
1 parent 905780e commit 29fec55
Showing 1 changed file with 24 additions and 23 deletions.
47 changes: 24 additions & 23 deletions src/Utils/request/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,30 +38,13 @@ export default async function request<TData, TBody>(

try {
const res = await fetch(url, options);
const data = await getResponseBody<TData>(res);

if (!res.ok) {
const data: Record<string, unknown> = await res.json();
result = {
res,
data: undefined,
error: data,
};
} else if (
res.headers.get("content-type")?.includes("application/json")
) {
const data: TData = await res.json();
result = {
res,
data,
error: undefined,
};
} else {
result = {
res,
data: undefined,
error: undefined,
};
}
result = {
res,
data: res.ok ? data : undefined,
error: res.ok ? undefined : (data as Record<string, unknown>),
};

onResponse?.(result);
handleResponse(result, silent);
Expand All @@ -78,3 +61,21 @@ export default async function request<TData, TBody>(
);
return result;
}

const getResponseBody = async <TData>(res: Response): Promise<TData> => {
if (!(res.headers.get("content-length") !== "0")) {
return null as TData;
}

const isJson = res.headers.get("content-type")?.includes("application/json");

if (!isJson) {
return (await res.text()) as TData;
}

try {
return await res.json();
} catch {
return (await res.text()) as TData;
}
};

0 comments on commit 29fec55

Please sign in to comment.