# fetch-beacon

> tiny HTTP client that implements sendBeacon API

Latest version **0.0.3** (published 2019-03-11) · ISC license · 0 weekly downloads

## Install

```sh
npm install fetch-beacon
pnpm add fetch-beacon
yarn add fetch-beacon
bun add fetch-beacon
```

## Health

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

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.3 |
| Published | 2019-03-11 |
| First published | 2019-02-15 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 14.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | youruichao@gmail.com |
| Maintainers | luvsic |
| Keywords | http, fetch, sendBeacon |

## Links

- npm: https://www.npmjs.com/package/fetch-beacon
- Repository: https://github.com/luv-sic/fetch-beacon
- Homepage: https://github.com/luv-sic/fetch-beacon#readme
- Issues: https://github.com/luv-sic/fetch-beacon/issues
- npm.io page: https://npm.io/package/fetch-beacon

## Alternatives

- [launchdarkly-js-client-sdk](https://npm.io/package/launchdarkly-js-client-sdk.md) — 2.5M weekly downloads
- [@elastic/elasticsearch](https://npm.io/package/@elastic/elasticsearch.md) — 2.1M weekly downloads
- [@c8y/client](https://npm.io/package/@c8y/client.md) — 15.3K weekly downloads
- [@signaldb/maverickjs](https://npm.io/package/@signaldb/maverickjs.md) — 1.7K weekly downloads
- [@bbc/http-transport-cache](https://npm.io/package/@bbc/http-transport-cache.md) — 1.2K weekly downloads

## Recent versions

- 0.0.3 (latest) — 2019-03-11
- 0.0.2 — 2019-02-21
- 0.0.1 — 2019-02-15

## README

# fetch-beacon

> A tiny HTTP client that implements sendBeacon API

## Why fetch-beacon

* implements [`navigator.sendBeacon`](https://developer.mozilla.org/en-US/docs/Web/API/Navigator/sendBeacon), which is useful for analytics and diagnostics needs.
* simple API as fetch
* timeout and ~~retry~~ support
* custom instance

> WARN: You should not use it besides analytics and diagnostics needs

## Install

```
$ npm install fetch-beacon
```

## Usage

```JavaScript
import beacon from 'fetch-beacon';

(async () => {
	const resp = await beacon('https://example.com', {json: {foo: true}});
	console.log(resp.json());
	//=> `{data: '🦄'}`
})();
```

## API

### beacon(input, [options])

Same as [fetch](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch), except folloing option.

`fetch-beacon` will cache the failed request when get below status code or `TimeoutError` and retry it when document load again.

Status codes: 408 413 429 500 502 503 504

#### options.timeout
Type: `number`,
Default: 10000

#### options.keepalive
Type: `boolean`,
Default: true

~~#### options.retry~~
Type: `number`,
Default: 2

Retry failed request if get one of following code.

### beacon.TimeoutError

The error thrown when the request times out.

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