Skip to content
This repository was archived by the owner on Jun 10, 2024. It is now read-only.

wefixers/fetch-event-source

Folders and files

NameName
Last commit message
Last commit date

Latest commit

38cdc87 · May 13, 2023

History

3 Commits
May 13, 2023
May 13, 2023
May 13, 2023
May 13, 2023
May 13, 2023
May 13, 2023
May 13, 2023
May 13, 2023
May 13, 2023
May 13, 2023
May 13, 2023
May 13, 2023

Repository files navigation

Fetch Event Source

A port of @microsoft/fetch-event-source

This package do uses async generator.

Install

pnpm i @fixers/fetch-event-source
npm install @fixers/fetch-event-source

Usage

// do a regular fetch
const response = await fetch("https://...", {
    method: "POST",
    body: JSON.stringify({ .. }),
    headers: {
      Accept: "text/event-stream"
    },
  }
);

// create the reader
const reader = response.body!.getReader();

for await (const message of createEventMessageReader(reader)) {
  // the message will be a EventSourceMessage
  const data = message.data
}

You are in control so make sure to check the response for validity

// do a regular fetch
const response = await fetch(
  "https://...",
  {
    method: "POST",
    headers: {
      Accept: "text/event-stream"
    },
    body: JSON.stringify({ .. }),
  }
);

// you are responsible for doing checks
if (response.ok) {

}

// or as an alternative
if (response.status >= 200 && response.status < 300) {
}

// check the response header
const contentType = response.headers.get("content-type");
if (!contentType?.startsWith('text/event-stream')) {
  throw new Error(
    `Expected content-type to be text/event-stream, Actual: ${contentType}`
  );
}