# @latelyjs/fetch-event-source

> fetch-event-source

Latest version **0.0.4** (published 2023-12-23) · ISC license · 0 weekly downloads

## Install

```sh
npm install @latelyjs/fetch-event-source
pnpm add @latelyjs/fetch-event-source
yarn add @latelyjs/fetch-event-source
bun add @latelyjs/fetch-event-source
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.4 |
| Published | 2023-12-23 |
| First published | 2023-12-23 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=14.0.0 |
| Dependencies | 0 |
| Unpacked size | 10.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Maintainers | laterly |
| Keywords | fetch-event-source |

## Links

- npm: https://www.npmjs.com/package/@latelyjs/fetch-event-source
- Repository: https://github.com/latelyjs/lately.js.git#main
- Homepage: https://github.com/latelyjs/lately.js/tree/main#readme
- Issues: https://github.com/latelyjs/lately.js/issues
- npm.io page: https://npm.io/package/@latelyjs/fetch-event-source

## Alternatives

- [async-exit-hook](https://npm.io/package/async-exit-hook.md) — 3.7M weekly downloads
- [evnty](https://npm.io/package/evnty.md) — 7.2K weekly downloads
- [eleventy-plugin-asciidoc](https://npm.io/package/eleventy-plugin-asciidoc.md) — 3.5K weekly downloads
- [@jswork/next-get2get](https://npm.io/package/@jswork/next-get2get.md) — 945 weekly downloads
- [@dashersw/axon](https://npm.io/package/@dashersw/axon.md) — 934 weekly downloads

## Recent versions

- 0.0.4 (latest) — 2023-12-23
- 0.0.3 — 2023-12-23
- 0.0.2 — 2023-12-23
- 0.0.1 — 2023-12-23

## README

## Fetch Event Source

基于fetch 获取流式数据

### Install

```sh
npm install @latelyjs/fetch-event-source
```

### Usage

```ts
import { fetchEventSource } from '@latelyjs/fetch-event-source';

await fetchEventSource('/api/sse', {
    onmessage(msg) {
        console.log(msg.data);
    }
});
```

### Example

```ts
import { fetchEventSource, EventStreamContentType} from '@latelyjs/fetch-event-source';

const controller = new AbortController();

const finish = () => {
    console.log('finish') 
};

const getHeaders = () => {
  const headers: Record<string, string> = {
    "Content-Type": "application/json",
    "x-requested-with": "XMLHttpRequest",
  };
  const authHeader = "Authorization";

  const token = ""; //token

  headers[authHeader] = `${token}`;
}

const requestPayload = {};

fetchEventSource('/api/sse', 
    {
        method: "POST",
        body: JSON.stringify(requestPayload),
        signal: controller.signal,
        headers: getHeaders(),
    },
   {
    async onopen(response) {
       if (
            !response.ok ||
            !response.headers
                .get("content-type")
                ?.startsWith(EventStreamContentType) ||
              response.status !== 200
        ) {
              finish();
        }
    },
    onmessage(msg) {
        // msg
        console.log(msg.data);
    },
    onclose() {
        //Abort
    },
    onerror(error) {
        //error
    },
    onfinily(){
        //end
        finish();
    }
});
```

---
_Source: https://npm.io/package/@latelyjs/fetch-event-source · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
