Skip to content

Commit

Permalink
Make sure that if no reader is available, we try simple parsing first (
Browse files Browse the repository at this point in the history
…#58)

* Make sure that if no reader is available, we try simple parsing first

* Delete .DS_Store

---------

Co-authored-by: Sayo <[email protected]>
  • Loading branch information
viv-cheung and wtfsayo authored Jan 22, 2025
1 parent 57fd998 commit d39c858
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
Binary file removed .DS_Store
Binary file not shown.
23 changes: 17 additions & 6 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,24 @@ export async function requestApi<T>(
// Check if response is chunked
const transferEncoding = res.headers.get('transfer-encoding');
if (transferEncoding === 'chunked') {
// Handle streaming response
const reader = res.body?.getReader();
// Handle streaming response, if a reader is present
const reader = typeof res.body?.getReader === 'function' ? res.body.getReader() : null;
if (!reader) {
return {
success: false,
err: new Error('No readable stream available'),
};
try {
const text = await res.text();
try {
const value = JSON.parse(text);
return { success: true, value };
} catch (e) {
// Return if just a normal string
return { success: true, value: { text } as any };
}
} catch (e) {
return {
success: false,
err: new Error('No readable stream available and cant parse'),
};
}
}

let chunks: any = '';
Expand Down

0 comments on commit d39c858

Please sign in to comment.