From d39c8580f40e26a768d9919ae2af156871ed8360 Mon Sep 17 00:00:00 2001 From: Vivian Cheung <45827928+viv-cheung@users.noreply.github.com> Date: Wed, 22 Jan 2025 09:27:01 -0500 Subject: [PATCH] Make sure that if no reader is available, we try simple parsing first (#58) * Make sure that if no reader is available, we try simple parsing first * Delete .DS_Store --------- Co-authored-by: Sayo --- .DS_Store | Bin 6148 -> 0 bytes src/api.ts | 23 +++++++++++++++++------ 2 files changed, 17 insertions(+), 6 deletions(-) delete mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store deleted file mode 100644 index 8cdf217d8a730f8aef041debd078acd6560b0ddb..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6148 zcmeHKO>5LZ7=C7(vP)aBClL>lYe9we4>Z;l@g^)DMcv)9EoL{R+tq78#2+9C9u)lp z3i=N`crJKX4<0>=CqbWgWrCeh(_i}$KC!go9xsggCQso6!;$%kasu21&pzcJ^lWgFXujJ-z4kx25B~+ zt@G{1#rDT1?=MyTqeJ~WTjkndzJ6vZ+C^P#VUB^W4lG`-M`#ZF>rZb!bR2%0XOfjn znxixyAQnDL7$U=jzdM*>M0R_aT}!i}ziwG1U$Z~|X`7@_yAhR}Rkg#~qX*ijdB5Dg zAAKsc^SPSwLH-KYwSB3(LWUV@Z{imD%F!Gt&t_W1m8&P@el^BwUT0sm3SNiAm&*Q4 zyykuK)OF##K&^H$)GV?1_*v=EoZhW>i|5SeX{G93T@USC>MipgQWcKpc~@urZOUEM z^;>Vg%ahy?srmf4`=WT}8ly{v{v56Ow%*F))x_dne|MT@(=j8s(;iXtGj*1aXudE_ zP#`F9d=!xNA!Zj$JmwDV(!s%g0uUpbZR4?B$B3~Qh>6GCAuVjir4n7L@t+vRrPCh! zxWr@b(51uJ%AChnHvS96*y^;$HXJ5#7{;JLP@t;7f;(N6{r^hy_kT4hoCO7f0>?@L z6ZQJN4PKJpTWc35du_~qn_Zg3 diff --git a/src/api.ts b/src/api.ts index 8eb496e..08d9f4c 100644 --- a/src/api.ts +++ b/src/api.ts @@ -107,13 +107,24 @@ export async function requestApi( // 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 = '';