From 0f28be3d2b0778d5af2cbfa8967fee456c4f9c8e Mon Sep 17 00:00:00 2001 From: goenning Date: Fri, 1 Sep 2023 16:16:35 +0100 Subject: [PATCH] remove hardcoded data --- packages/node/src/global.d.ts | 3 +- packages/node/src/index.ts | 59 +++++++++++++++++++--------- packages/web/src/index.ts | 72 +++++++++++++++++------------------ 3 files changed, 80 insertions(+), 54 deletions(-) diff --git a/packages/node/src/global.d.ts b/packages/node/src/global.d.ts index 8754882..22dde7c 100644 --- a/packages/node/src/global.d.ts +++ b/packages/node/src/global.d.ts @@ -1,5 +1,6 @@ type AptabaseState = { - appKey?: string; + appKey: string; + apiUrl: string; options?: import('./types').AptabaseOptions; }; diff --git a/packages/node/src/index.ts b/packages/node/src/index.ts index ddd8b8e..f9ea9d0 100644 --- a/packages/node/src/index.ts +++ b/packages/node/src/index.ts @@ -4,8 +4,23 @@ export { AptabaseOptions }; const isDebug = process.env.NODE_ENV === 'development'; +const _hosts: { [region: string]: string } = { + US: 'https://us.aptabase.com', + EU: 'https://eu.aptabase.com', + DEV: 'http://localhost:3000', + SH: '', +}; + export function init(appKey: string, options?: AptabaseOptions) { - globalThis.__APTABASE__ = { appKey, options }; + const parts = appKey.split('-'); + if (parts.length !== 3 || _hosts[parts[1]] === undefined) { + console.warn(`Aptabase: The App Key "${appKey}" is invalid. Tracking will be disabled.`); + return; + } + + const baseUrl = getBaseUrl(parts[1], options); + const apiUrl = `${baseUrl}/api/v0/event`; + globalThis.__APTABASE__ = { appKey, apiUrl, options }; } // We only need the headers from the request object @@ -16,24 +31,11 @@ export async function trackEvent( eventName: string, props?: Record, ): Promise { - const { appKey } = globalThis.__APTABASE__ || {}; + const { appKey, apiUrl, options } = globalThis.__APTABASE__ || {}; if (!appKey) return Promise.resolve(); - const body = JSON.stringify({ - timestamp: new Date().toISOString(), - sessionId: 'CHANGE-THIS', - eventName: eventName, - systemProps: { - isDebug, - locale: extractLocale(req.headers.get('accept-language')), - appVersion: '', - sdkVersion: globalThis.__APTABASE_SDK_VERSION__ ?? `aptabase-node@${process.env.PKG_VERSION}`, - }, - props: props, - }); - try { - const response = await fetch('http://localhost:3000/api/v0/event', { + const response = await fetch(apiUrl, { method: 'POST', headers: { 'User-Agent': req.headers.get('user-agent') ?? '', @@ -41,7 +43,18 @@ export async function trackEvent( 'App-Key': appKey, }, credentials: 'omit', - body, + body: JSON.stringify({ + timestamp: new Date().toISOString(), + sessionId: 'CHANGE-THIS', + eventName: eventName, + systemProps: { + isDebug, + locale: extractLocale(req.headers.get('accept-language')), + appVersion: options, + sdkVersion: globalThis.__APTABASE_SDK_VERSION__ ?? `aptabase-node@${process.env.PKG_VERSION}`, + }, + props: props, + }), }); if (response.status >= 300) { @@ -53,6 +66,18 @@ export async function trackEvent( } } +function getBaseUrl(region: string, options?: AptabaseOptions): string | undefined { + if (region === 'SH') { + if (!options?.host) { + console.warn(`Host parameter must be defined when using Self-Hosted App Key. Tracking will be disabled.`); + return; + } + return options.host; + } + + return _hosts[region]; +} + function extractLocale(locale: string | null): string | null { if (!locale) { return null; diff --git a/packages/web/src/index.ts b/packages/web/src/index.ts index 6e67a96..6773c1b 100644 --- a/packages/web/src/index.ts +++ b/packages/web/src/index.ts @@ -17,18 +17,6 @@ const _hosts: { [region: string]: string } = { SH: '', }; -function getBaseUrl(region: string, options?: AptabaseOptions): string | undefined { - if (region === 'SH') { - if (!options?.host) { - console.warn(`Host parameter must be defined when using Self-Hosted App Key. Tracking will be disabled.`); - return; - } - return options.host; - } - - return _hosts[region]; -} - export function init(appKey: string, options?: AptabaseOptions) { _appKey = appKey; _options = options; @@ -43,7 +31,7 @@ export function init(appKey: string, options?: AptabaseOptions) { _apiUrl = `${baseUrl}/api/v0/event`; } -export function trackEvent(eventName: string, props?: Record) { +export async function trackEvent(eventName: string, props?: Record): Promise { if (!_appKey) return; if (typeof window === 'undefined' || !window.fetch) { @@ -53,35 +41,47 @@ export function trackEvent(eventName: string, props?: Record { - if (response.status >= 300) { - console.warn(`Failed to send event "${eventName}": ${response.status} ${response.statusText}`); - } - }) - .catch(console.error); + body: JSON.stringify({ + timestamp: new Date().toISOString(), + sessionId: 'CHANGE-THIS', + eventName: eventName, + systemProps: { + isDebug, + locale, + appVersion: _options?.appVersion ?? '', + sdkVersion: globalThis.__APTABASE_SDK_VERSION__ ?? `aptabase-web@${process.env.PKG_VERSION}`, + }, + props: props, + }), + }); + + if (response.status >= 300) { + const responseBody = await response.text(); + console.warn(`Failed to send event "${eventName}": ${response.status} ${responseBody}`); + } + } catch (e) { + console.warn(`Failed to send event "${eventName}": ${e}`); + } +} + +function getBaseUrl(region: string, options?: AptabaseOptions): string | undefined { + if (region === 'SH') { + if (!options?.host) { + console.warn(`Host parameter must be defined when using Self-Hosted App Key. Tracking will be disabled.`); + return; + } + return options.host; + } + + return _hosts[region]; } function getBrowserLocale(): string | null {