You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi, i have an API that support Streams and it responds chunks of data. It returns a big list as chunks, following JSONL format. fetchJSONLines below is a ts implementation to read from the API. It works as expected. I'd like to use ky library, but i cannot see how to integrate it into my code.
Does anyone faced such scenario? Any hint on how to integrate ky here?
import{Observer}from'rxjs';import{LinesTransformer}from'./lines-transformer';functionparseJSON<T>(input: string): T{returnJSON.parse(input)asT;}exportasyncfunctionfetchJSONLines<T=unknown,TError=Error>(input: RequestInfo|URL,observer: Observer<T>,init?: RequestInit,){try{constresponse=awaitfetch(input,init);if(!response.ok){thrownewError(`HTTP error! Status: ${response.status}`);}if(!response.body){thrownewError('Response body is null');}constts=newTransformStream<Uint8Array,string>(newLinesTransformer());constrs=response.body.pipeThrough(ts);// Note: The reading and processing are handled by the transformer// Just ensure the stream is fully readconstreader=rs.getReader();// Initial read before entering the loopletchunk=awaitreader.read();while(!chunk.done){try{constjson=parseJSON<T>(chunk.value);// Parse each line of JSON hereobserver.next(json);}catch(e){observer.error(easTError);// Handle JSON parsing errors here}chunk=awaitreader.read();}observer.complete();}catch(e){observer.error(easTError);}}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hi, i have an API that support Streams and it responds chunks of data. It returns a big list as chunks, following JSONL format.
fetchJSONLines
below is a ts implementation to read from the API. It works as expected. I'd like to use ky library, but i cannot see how to integrate it into my code.Does anyone faced such scenario? Any hint on how to integrate ky here?
Beta Was this translation helpful? Give feedback.
All reactions