1.2.0 • Published 2 years ago

rx-event-source v1.2.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

RxEventSource

GitHub release (latest SemVer) All Contributors Gitpod Ready-to-Code codecov GitHub Workflow Status Snyk Vulnerabilities for GitHub Repo Semantic release npm bundle size GitHub

API Reference

Open in Gitpod

Background

Helper function to create an RxJS observable from EventSource.

Installation

NPM

npm install rx-event-source rxjs@7

or

yarn add rx-event-source rxjs@7

Usage

Basic usage

See available options.

import { eventSource$ } from 'rx-event-source';

const options = {
  // ...
};

eventSource$('/api/v1/sse', { withCredentials: false }, options).subscribe((data) => {
  console.log(data);
});

Handling streaming errors

The streaming error can be emitted as a message with specific data format and then handled manually by throwing an error.

import { eventSource$ } from 'rx-event-source';

const options = {
  // ...
};

function isError(data) {
  // ...
}

eventSource$('/api/v1/sse', { withCredentials: false }, options).pipe(
  map((data) => {
    if (isError(data)) {
      throw new Error(data.error);
    }
    return data;
  })
).subscribe((data) => {
  console.log(data);
});

Contributors ✨

Thanks goes to these wonderful people (emoji key):

This project follows the all-contributors specification. Contributions of any kind welcome!

1.2.0

2 years ago

1.1.0

2 years ago

1.0.0

2 years ago