Support for custom API path

This commit is contained in:
goenning 2023-12-29 09:22:45 -03:00
parent 348898fc82
commit c980afd786
7 changed files with 34 additions and 17 deletions

View file

@ -1,3 +1,7 @@
## 0.3.1
- Support for custom API path
## 0.3.0 ## 0.3.0
- Internal refactor - Internal refactor

View file

@ -1,6 +1,6 @@
{ {
"name": "@aptabase/react", "name": "@aptabase/react",
"version": "0.3.0", "version": "0.3.1",
"type": "module", "type": "module",
"description": "React SDK for Aptabase: Open Source, Privacy-First and Simple Analytics for Mobile, Desktop and Web Apps", "description": "React SDK for Aptabase: Open Source, Privacy-First and Simple Analytics for Mobile, Desktop and Web Apps",
"main": "./dist/index.cjs", "main": "./dist/index.cjs",

View file

@ -1,13 +1,14 @@
'use client'; 'use client';
import { createContext, useContext, useEffect } from 'react'; import { createContext, useContext, useEffect } from 'react';
import { inMemorySessionId, sendEvent, validateAppKey, type AptabaseOptions } from '../../shared'; import { getApiUrl, inMemorySessionId, sendEvent, validateAppKey, type AptabaseOptions } from '../../shared';
// Session expires after 1 hour of inactivity // Session expires after 1 hour of inactivity
const SESSION_TIMEOUT = 1 * 60 * 60; const SESSION_TIMEOUT = 1 * 60 * 60;
const sdkVersion = `aptabase-react@${process.env.PKG_VERSION}`; const sdkVersion = `aptabase-react@${process.env.PKG_VERSION}`;
let _appKey = ''; let _appKey = '';
let _apiUrl: string | undefined;
let _options: AptabaseOptions | undefined; let _options: AptabaseOptions | undefined;
type ContextProps = { type ContextProps = {
@ -18,14 +19,18 @@ type ContextProps = {
function init(appKey: string, options?: AptabaseOptions) { function init(appKey: string, options?: AptabaseOptions) {
if (!validateAppKey(appKey)) return; if (!validateAppKey(appKey)) return;
_apiUrl = getApiUrl(appKey, options);
_appKey = appKey; _appKey = appKey;
_options = options; _options = options;
} }
async function trackEvent(eventName: string, props?: Record<string, string | number | boolean>): Promise<void> { async function trackEvent(eventName: string, props?: Record<string, string | number | boolean>): Promise<void> {
if (!_apiUrl) return;
const sessionId = inMemorySessionId(SESSION_TIMEOUT); const sessionId = inMemorySessionId(SESSION_TIMEOUT);
await sendEvent({ await sendEvent({
apiUrl: _apiUrl,
sessionId, sessionId,
appKey: _appKey, appKey: _appKey,
isDebug: _options?.isDebug, isDebug: _options?.isDebug,

View file

@ -6,8 +6,6 @@ const isInBrowserExtension = typeof chrome !== 'undefined' && !!chrome.runtime.i
let _sessionId = newSessionId(); let _sessionId = newSessionId();
let _lastTouched = new Date(); let _lastTouched = new Date();
const apiUrl: Record<string, string> = {};
const _hosts: { [region: string]: string } = { const _hosts: { [region: string]: string } = {
US: 'https://us.aptabase.com', US: 'https://us.aptabase.com',
EU: 'https://eu.aptabase.com', EU: 'https://eu.aptabase.com',
@ -16,8 +14,13 @@ const _hosts: { [region: string]: string } = {
}; };
export type AptabaseOptions = { export type AptabaseOptions = {
// Custom host for self-hosted Aptabase.
host?: string; host?: string;
// Custom path for API endpoint. Useful when using reverse proxy.
apiPath?: string;
// Defines the app version.
appVersion?: string; appVersion?: string;
// Defines whether the app is running on debug mode.
isDebug?: boolean; isDebug?: boolean;
}; };
@ -51,26 +54,25 @@ export function validateAppKey(appKey: string): boolean {
return true; return true;
} }
function getApiUrl(appKey: string, options?: AptabaseOptions): string | undefined { export function getApiUrl(appKey: string, options?: AptabaseOptions): string | undefined {
const url = apiUrl[appKey]; const apiPath = options?.apiPath ?? '/api/v0/event';
if (url) url;
const region = appKey.split('-')[1]; const region = appKey.split('-')[1];
let host = _hosts[region];
if (region === 'SH') { if (region === 'SH') {
if (!options?.host) { if (!options?.host) {
console.warn(`Host parameter must be defined when using Self-Hosted App Key. Tracking will be disabled.`); console.warn(`Host parameter must be defined when using Self-Hosted App Key. Tracking will be disabled.`);
return; return;
} }
host = options.host; return `${options.host}${apiPath}`;
} }
apiUrl[appKey] = `${host}/api/v0/event`; const host = options?.host ?? _hosts[region];
return apiUrl[appKey]; return `${host}${apiPath}`;
} }
export async function sendEvent(opts: { export async function sendEvent(opts: {
apiUrl: string;
appKey?: string; appKey?: string;
sessionId: string; sessionId: string;
locale?: string; locale?: string;
@ -90,11 +92,8 @@ export async function sendEvent(opts: {
return; return;
} }
const apiUrl = getApiUrl(opts.appKey);
if (!apiUrl) return;
try { try {
const response = await fetch(apiUrl, { const response = await fetch(opts.apiUrl, {
method: 'POST', method: 'POST',
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',

View file

@ -1,3 +1,7 @@
## 0.4.1
- Support for custom API path
## 0.4.0 ## 0.4.0
- Internal refactor - Internal refactor

View file

@ -1,6 +1,6 @@
{ {
"name": "@aptabase/web", "name": "@aptabase/web",
"version": "0.4.0", "version": "0.4.1",
"type": "module", "type": "module",
"description": "JavaScript SDK for Aptabase: Open Source, Privacy-First and Simple Analytics for Mobile, Desktop and Web Apps", "description": "JavaScript SDK for Aptabase: Open Source, Privacy-First and Simple Analytics for Mobile, Desktop and Web Apps",
"main": "./dist/index.cjs", "main": "./dist/index.cjs",

View file

@ -1,10 +1,11 @@
import { inMemorySessionId, sendEvent, validateAppKey, type AptabaseOptions } from '../../shared'; import { getApiUrl, inMemorySessionId, sendEvent, validateAppKey, type AptabaseOptions } from '../../shared';
// Session expires after 1 hour of inactivity // Session expires after 1 hour of inactivity
const SESSION_TIMEOUT = 1 * 60 * 60; const SESSION_TIMEOUT = 1 * 60 * 60;
const sdkVersion = `aptabase-web@${process.env.PKG_VERSION}`; const sdkVersion = `aptabase-web@${process.env.PKG_VERSION}`;
let _appKey = ''; let _appKey = '';
let _apiUrl: string | undefined;
let _options: AptabaseOptions | undefined; let _options: AptabaseOptions | undefined;
export { type AptabaseOptions }; export { type AptabaseOptions };
@ -12,14 +13,18 @@ export { type AptabaseOptions };
export function init(appKey: string, options?: AptabaseOptions) { export function init(appKey: string, options?: AptabaseOptions) {
if (!validateAppKey(appKey)) return; if (!validateAppKey(appKey)) return;
_apiUrl = getApiUrl(appKey, options);
_appKey = appKey; _appKey = appKey;
_options = options; _options = options;
} }
export async function trackEvent(eventName: string, props?: Record<string, string | number | boolean>): Promise<void> { export async function trackEvent(eventName: string, props?: Record<string, string | number | boolean>): Promise<void> {
if (!_apiUrl) return;
const sessionId = inMemorySessionId(SESSION_TIMEOUT); const sessionId = inMemorySessionId(SESSION_TIMEOUT);
await sendEvent({ await sendEvent({
apiUrl: _apiUrl,
sessionId, sessionId,
appKey: _appKey, appKey: _appKey,
isDebug: _options?.isDebug, isDebug: _options?.isDebug,