ZL
About Articles Contact
Published on May 27, 2025
Filed under:
#javascript,
#zlfetch

Streaming capabilities comes to zlFetch

Just last weekend, I sat down and gave zlFetch another brand new update — streaming capabilities. With v6.2.0, zlFetch is now able to:

  • Receive and convert streams automatically (without you having to write a stream decoder manually)
  • Have Typescript support (thanks to juji).

Now zlFetch is now prime for handling AI-related streams😉.

What is zlFetch?

It’s a library I’ve created that helps you with the Fetch API. It provides a ton of quality-of-life improvements like:

  • Automatic response solution (so no need to use response.json)
  • Promise-like error handling
  • Usage with await easily
  • Simple Basic and Token based authentication headers
  • And a ton of other things

Just my personal opinion:

  • It’s better than axios cos it’s less complicated
  • It’s better than ky cos it has better defaults

Three Types of Streams

As far as I know, there are three types of streams:

  1. Server Sent Events (SSE)
  2. Chunked Encoding
  3. A normal stream

Server Sent Events lets you create a connection between a browser and the server. If the connection is dropped, the browser can automatically connect (with the help of Event Source). This happens if the server sets Content-Type: text/event-stream.

Chunked Encoding lets one server tell another that they’re going to send data in bits. The receiving server is supposed to receive everything, combine them, before sending letting the clients use them. This happens if the sending server sets the Transfer-Encoding: Chunked header.

Normal Streams happens when the sending server doesn’t send over a Content-Length property.

zlFetch detects all three kinds of streams and decodes them for you automatically, so you can just loop through the response body to get your data — no need for manual decoding!

Oh, by the way, if the chunk contains JSON data, zlFetch converts that into a JavaScript object for you automatically. Again, quality of life improvements — so you can build stuff without having to deal with boilerplate!

// Basic Stream Handling Example with Fetch
const response = await fetch('some-url')
const reader = response.body.getReader()
const decoder = new TextDecoder()
while (true) {
const { done, value } = await reader.read()
buffer += decoder.decode(value, { stream: true })
// Decode the chunk
let chunk =
typeof value === 'string' ? value : decoder.decode(value, { stream: true })
chunk = chunk.trim()
try {
chunk = JSON.parse(chunk)
} catch (error) {}
// FINALLY, you do something with your chunk 🫠
}
// With zlFetch
const response = await zlFetch('some-url')
for await (const chunk of response.body) {
// Do something with your chunk 😄
}

Even for chunked encoding, you can actually receive the data in your browser as chunks! To make that happen, we can’t parse the stream in advance inside zlFetch. But we’ve given you a readStream helper to, you know, deal with the stream decoding stuff.

import zlFetch, { readStream } from 'zl-fetch'
// Decoding chunked encoding as streams in the browser
const response = await zlFetch('some-url')
// Just add this helper
const stream = readStream(response)
for await (const chunk of stream) {
// Then do something with your chunk 😄
}

Text Event Stream and Server Sent Events

Server Sent Events is a pretty cool technology. It’s similar to Fetch, but made for streams — because your browser can reconnect to the server if you get disconnected!

The best way to consume Server Sent Events is through the Event Source API. We’ve made that simpler too with zlEventSource.

I’d leave further explanation for a future article. If you’re interested, you can check out the docs right now 😉.

Previous We Might Need Something Between Root and Relative CSS Units for “Base Elements”

Join My Newsletter

I share what I’m learning on this newsletter: code, building businesses, and living well.

Sometimes I write about technical deep-dives, product updates, musings on how to live, and sometimes my struggles and how I’m breaking through.

Regardless of the type of content, I do my best to send you an update every week.

If you’re into making things and growing as a person, you’ll probably feel at home here.

“

Zell’s writing is very accessible to newcomers because he shares his learning experience. He covers current topics and helps all readers level up their web development skills. Must subscribe.

Chen Hui Jing
Chen Hui Jing — Web Developer
The Footer

General

Home About Contact Testimonials Tools I Use

Projects

Magical Dev School Splendid Labz

Socials

Youtube Instagram Tiktok Github Bluesky X

Follow Along

Email RSS
© 2013 - 2025 Zell Liew / All rights reserved / Terms