rsnext/packages/next/telemetry/post-payload.ts
Joe Haddad 1f4e34ca00
Anonymous Telemetry (#8529)
* Add anonymous telemetry

* Fix types

* Remove semver package

* Rename build time variable

* Track CSS-in-JS solutions

* Single retry and 5s timeout
2019-08-29 12:43:06 -04:00

28 lines
738 B
TypeScript

import retry from 'async-retry'
import fetch from 'node-fetch'
export function _postPayload(endpoint: string, body: object) {
return (
retry(
() =>
fetch(endpoint, {
method: 'POST',
body: JSON.stringify(body),
headers: { 'content-type': 'application/json' },
timeout: 5000,
}).then(res => {
if (!res.ok) {
const err = new Error(res.statusText)
;(err as any).response = res
throw err
}
}),
{ minTimeout: 500, retries: 1, factor: 1 }
)
.catch(() => {
// We swallow errors when telemetry cannot be sent
})
// Ensure promise is voided
.then(() => {}, () => {})
)
}