Skip to content
Discussion options

You must be logged in to vote

ky returns standard Response objects, so your streaming code works almost identically. just replace fetch() with ky():

const response = await ky.get('https://api.example.com/stream', {
	headers: {
		accept: 'application/x-ndjson'
	}
});

// access the body stream just like with fetch
const stream = response.body
	?.pipeThrough(new TextDecoderStream())
	.pipeThrough(new LinesTransformer());

for await (const line of stream) {
	if (line.trim()) {
		const data = JSON.parse(line);
		console.log(data);
	}
}

or with your RxJS setup:

const response = await ky.get(url, {headers: {accept: 'application/x-ndjson'}});

const body$ = new Observable<string>(subscriber => {
	const stream = response.body
		

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@marcog83
Comment options

Answer selected by sindresorhus
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants