-
Notifications
You must be signed in to change notification settings - Fork 33
/
fetch.ts
43 lines (36 loc) · 1.53 KB
/
fetch.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/**
* Fetch wrapper
*
* We want to polyfill fetch when not available with axios but use it when it is.
* NOTE: The current version of Axios has an issue when in non-node environments like Clouflare Workers.
* This is currently solved by using the global fetch if available instead.
* See https://github.com/PostHog/posthog-js-lite/issues/127 for more info
*/
import { PostHogFetchOptions, PostHogFetchResponse } from 'posthog-core/src'
type FetchLike = (url: string, options: PostHogFetchOptions) => Promise<PostHogFetchResponse>
let _fetch: FetchLike | undefined =
// eslint-disable-next-line @typescript-eslint/prefer-ts-expect-error
// @ts-ignore
typeof fetch !== 'undefined' ? fetch : typeof global.fetch !== 'undefined' ? global.fetch : undefined
if (!_fetch) {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const axios = require('axios')
_fetch = async (url: string, options: PostHogFetchOptions): Promise<PostHogFetchResponse> => {
const res = await axios.request({
url,
headers: options.headers,
method: options.method.toLowerCase(),
data: options.body,
signal: options.signal,
// fetch only throws on network errors, not on HTTP errors
validateStatus: () => true,
})
return {
status: res.status,
text: async () => res.data,
json: async () => res.data,
}
}
}
// NOTE: We have to export this as default, even though we prefer named exports as we are relying on detecting "fetch" in the global scope
export default _fetch as FetchLike