rsnext/packages/next/telemetry/post-payload.ts

32 lines
783 B
TypeScript
Raw Normal View History

2020-03-29 00:27:09 +01:00
import retry from 'next/dist/compiled/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,
2020-05-18 21:24:37 +02:00
}).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(
() => {},
() => {}
)
)
}