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!

migrated to node-fetch & some other general improvements & fixes

Blue Mousee6f53006effd
diff --git a/package.json b/package.json
index 5bf331bfb..2dbb3900e 100644
--- a/package.json
+++ b/package.json
@@ -33,11 +33,13 @@
"homepage": "https://swetrix.com/docs",
"dependencies": {
"@types/node": "^20.5.9",
+ "node-fetch": "^3.3.2",
"tslib": "^2.6.2"
},
"devDependencies": {
"@rollup/plugin-commonjs": "^25.0.4",
"@rollup/plugin-node-resolve": "^15.2.1",
+ "@types/node-fetch": "^2.6.4",
"rimraf": "^4.4.1",
"rollup": "^2.79.1",
"rollup-plugin-node-resolve": "^5.2.0",
diff --git a/rollup.config.js b/rollup.config.js
index 0bd5fd267..1b99a7068 100644
--- a/rollup.config.js
+++ b/rollup.config.js
@@ -13,6 +13,7 @@ export default [
],
plugins: [
typescript(), nodeResolve(), commonjs(), sourceMaps(),
- ]
+ ],
+ inlineDynamicImports: true,
},
]
diff --git a/src/index.ts b/src/index.ts
index 4195121b0..ff81e5cd3 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -1,4 +1,4 @@
-import * as https from 'https'
+import fetch from 'node-fetch'
import { isInBrowser } from './utils'
export interface LibOptions {
@@ -15,10 +15,7 @@ export interface LibOptions {
/**
* Set a custom URL of the API server (for selfhosted variants of Swetrix).
- *
- * **Please do not include https:// prefix into this value.**
- * This library will automatically add it (and it expects the API server to support HTTPS protocol).
- */
+ */
apiURL?: string
/**
@@ -125,7 +122,7 @@ export interface TrackPageViewOptions {
perf?: PerformanceMetrics
}
-const DEFAULT_API_HOST = 'api.swetrix.com/log'
+const DEFAULT_API_HOST = 'https://api.swetrix.com/log'
/**
* Server-side implementation of Swetrix tracking library.
@@ -204,7 +201,7 @@ export class Swetrix {
const data = {
pid: this.projectID,
}
- this.sendRequest('heartbeat', ip, userAgent, data)
+ this.sendRequest('hb', ip, userAgent, data)
}
private checkIgnore(path?: string): boolean {
@@ -240,34 +237,31 @@ export class Swetrix {
return true
}
- private sendRequest(path: string, ip: string, userAgent: string, body: object): void {
- const link = (this.options?.apiURL || DEFAULT_API_HOST).split('/')
+ private async sendRequest(path: string, ip: string, userAgent: string, body: object): Promise<void> {
+ const link = this.options?.apiURL || DEFAULT_API_HOST
const postData = JSON.stringify(body)
- const hostname = link[0]
- const pathPrefix = link.slice(1).join('/')
-
- const options = {
- hostname,
- path: `${pathPrefix}/${path}`,
- method: 'POST',
- headers: {
- 'Content-Type': 'application/json',
- 'X-Client-IP-Address': ip,
- 'User-Agent': userAgent,
- },
+ const headers = {
+ 'Content-Type': 'application/json',
+ 'X-Client-IP-Address': ip,
+ 'User-Agent': userAgent,
}
- const req = https.request(options)
+ try {
+ const response = await fetch(`${link}/${path}`, {
+ method: 'POST',
+ headers,
+ body: postData,
+ })
- req.on('error', (error) => {
- this.debug(`Error while sending request: ${error.message}`, true)
- this.debug(`Request hostname: ${hostname}`, true)
+ if (!response.ok) {
+ throw new Error(`Request failed with status ${response.status}`)
+ }
+ } catch (reason) {
+ this.debug(`Error while sending request: ${reason}`, true)
+ this.debug(`Request link: ${link}`, true)
this.debug(`Request path: /${path}`, true)
this.debug(`Request body: ${JSON.stringify(body, null, 2)}`, true)
- })
-
- req.write(postData)
- req.end()
+ }
}
}