Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

fetch -> https

Blue Mouse6e9887f7e71d
diff --git a/src/utils.ts b/src/utils.ts
index 4d88ea713..eb6a9de33 100644
--- a/src/utils.ts
+++ b/src/utils.ts
@@ -1,3 +1,5 @@
+import https from 'https'
+
const DEFAULT_API_HOST = 'https://api.swetrix.com/captcha'
export const ENDPOINTS = {
@@ -5,19 +7,50 @@ export const ENDPOINTS = {
}
export const makeAPIRequest = async (path: string, method: string, body: object, apiURL?: string): Promise<any> => {
- let res
-
- try {
- res = await fetch(`${apiURL || DEFAULT_API_HOST}${path}`, {
- method,
- headers: {
- 'Content-Type': 'application/json',
- },
- body: JSON.stringify(body),
- })
- } catch (e) {
- throw `Unable to make API request, error: ${e}`
+ const options = {
+ method,
+ headers: {
+ 'Content-Type': 'application/json',
+ },
+ }
+
+ if (body) {
+ // @ts-ignore
+ options.body = JSON.stringify(body)
}
- return await res.json()
+ return new Promise((resolve, reject) => {
+ const url = `${apiURL || DEFAULT_API_HOST}${path}`
+
+ const req = https.request(url, options, (res: any) => {
+ let data = ''
+
+ res.on('data', (chunk: any) => {
+ data += chunk
+ })
+
+ res.on('end', () => {
+ if (res.statusCode >= 400) {
+ reject(`Request failed with status code ${res.statusCode}: ${data}`)
+ } else {
+ try {
+ resolve(JSON.parse(data))
+ } catch (e) {
+ reject(`Unable to parse JSON response: ${data}`)
+ }
+ }
+ })
+ })
+
+ req.on('error', (e) => {
+ reject(`Unable to make API request, error: ${e}`)
+ })
+
+ if (body) {
+ // @ts-ignore
+ req.write(options.body)
+ }
+
+ req.end()
+ })
}