diff --git a/packages/nextjs/src/server.ts b/packages/nextjs/src/server.ts index c88da7c..3610311 100644 --- a/packages/nextjs/src/server.ts +++ b/packages/nextjs/src/server.ts @@ -1,8 +1,11 @@ -import { trackEvent as nodeTrackEvent } from '@aptabase/node'; +import * as node from '@aptabase/node'; +import { type AptabaseOptions } from '@aptabase/node'; import { type NextIncomingMessage } from 'next/dist/server/request-meta'; import { headers } from 'next/headers'; -export { init } from '@aptabase/node'; +export function init(appKey: string, options?: AptabaseOptions): void { + node.init(appKey, options); +} export async function trackEvent( eventName: string, @@ -12,7 +15,7 @@ export async function trackEvent( const headers = getHeaders(req); if (!headers) return Promise.resolve(); - return nodeTrackEvent({ headers }, eventName, props); + return node.trackEvent({ headers }, eventName, props); } function getHeaders(req?: NextIncomingMessage): Headers | undefined { diff --git a/packages/nextjs/src/types.ts b/packages/nextjs/src/types.ts deleted file mode 100644 index e8835a2..0000000 --- a/packages/nextjs/src/types.ts +++ /dev/null @@ -1,4 +0,0 @@ -export type AptabaseOptions = { - host?: string; - appVersion?: string; -}; diff --git a/packages/node/src/global.d.ts b/packages/node/src/global.d.ts index ecaf0f1..c15685e 100644 --- a/packages/node/src/global.d.ts +++ b/packages/node/src/global.d.ts @@ -1,5 +1,6 @@ type AptabaseState = { appKey?: string; + options?: import('./types').AptabaseOptions; }; declare var __APTABASE__: AptabaseState; diff --git a/packages/node/src/index.js b/packages/node/src/index.js deleted file mode 100644 index 2908e07..0000000 --- a/packages/node/src/index.js +++ /dev/null @@ -1,52 +0,0 @@ -var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { - function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } - return new (P || (P = Promise))(function (resolve, reject) { - function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } - function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } - function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } - step((generator = generator.apply(thisArg, _arguments || [])).next()); - }); -}; -export function init(appKey) { - globalThis.__APTABASE__ = { appKey }; -} -export function trackEvent(req, eventName, props) { - var _a; - return __awaiter(this, void 0, void 0, function* () { - const appKey = globalThis.__APTABASE__.appKey; - if (!appKey) - return Promise.resolve(); - const userAgent = (_a = req.headers['user-agent']) !== null && _a !== void 0 ? _a : ''; - const body = JSON.stringify({ - timestamp: new Date().toISOString(), - sessionId: 'CHANGE-THIS', - eventName: eventName, - systemProps: { - isDebug: true, - locale: 'en', - appVersion: '', - sdkVersion: 'aptabase-node@0.1.0', - }, - props: props, - }); - try { - const response = yield fetch('http://localhost:3000/api/v0/event', { - method: 'POST', - headers: { - 'User-Agent': userAgent, - 'Content-Type': 'application/json', - 'App-Key': appKey, - }, - credentials: 'omit', - body, - }); - if (response.status >= 300) { - const responseBody = yield response.text(); - console.warn(`Failed to send event "${eventName}": ${response.status} ${responseBody}`); - } - } - catch (e) { - console.warn(`Failed to send event "${eventName}": ${e}`); - } - }); -} diff --git a/packages/node/src/index.ts b/packages/node/src/index.ts index 1717a91..c24f993 100644 --- a/packages/node/src/index.ts +++ b/packages/node/src/index.ts @@ -1,5 +1,9 @@ -export function init(appKey: string) { - globalThis.__APTABASE__ = { appKey }; +import { AptabaseOptions } from './types'; + +export { AptabaseOptions }; + +export function init(appKey: string, options?: AptabaseOptions) { + globalThis.__APTABASE__ = { appKey, options }; } // We only need the headers from the request object diff --git a/packages/node/src/types.ts b/packages/node/src/types.ts new file mode 100644 index 0000000..08198c7 --- /dev/null +++ b/packages/node/src/types.ts @@ -0,0 +1,11 @@ +export type AptabaseOptions = { + host?: string; + appVersion?: string; +}; + +type AptabaseState = { + appKey?: string; + options?: AptabaseOptions; +}; + +declare var __APTABASE__: AptabaseState;