@fixers/fetch-event-source
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

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}`
  );
}

Readme

Keywords

none

Package Sidebar

Install

npm i @fixers/fetch-event-source

Weekly Downloads

81

Version

1.0.1

License

MIT

Unpacked Size

24.8 kB

Total Files

8

Last publish

Collaborators

  • lucacicada