export aptabaseoptions
This commit is contained in:
parent
e104dd53dc
commit
47042f043b
6 changed files with 24 additions and 61 deletions
|
@ -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 { type NextIncomingMessage } from 'next/dist/server/request-meta';
|
||||||
import { headers } from 'next/headers';
|
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(
|
export async function trackEvent(
|
||||||
eventName: string,
|
eventName: string,
|
||||||
|
@ -12,7 +15,7 @@ export async function trackEvent(
|
||||||
const headers = getHeaders(req);
|
const headers = getHeaders(req);
|
||||||
if (!headers) return Promise.resolve();
|
if (!headers) return Promise.resolve();
|
||||||
|
|
||||||
return nodeTrackEvent({ headers }, eventName, props);
|
return node.trackEvent({ headers }, eventName, props);
|
||||||
}
|
}
|
||||||
|
|
||||||
function getHeaders(req?: NextIncomingMessage): Headers | undefined {
|
function getHeaders(req?: NextIncomingMessage): Headers | undefined {
|
||||||
|
|
|
@ -1,4 +0,0 @@
|
||||||
export type AptabaseOptions = {
|
|
||||||
host?: string;
|
|
||||||
appVersion?: string;
|
|
||||||
};
|
|
1
packages/node/src/global.d.ts
vendored
1
packages/node/src/global.d.ts
vendored
|
@ -1,5 +1,6 @@
|
||||||
type AptabaseState = {
|
type AptabaseState = {
|
||||||
appKey?: string;
|
appKey?: string;
|
||||||
|
options?: import('./types').AptabaseOptions;
|
||||||
};
|
};
|
||||||
|
|
||||||
declare var __APTABASE__: AptabaseState;
|
declare var __APTABASE__: AptabaseState;
|
||||||
|
|
|
@ -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}`);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
|
@ -1,5 +1,9 @@
|
||||||
export function init(appKey: string) {
|
import { AptabaseOptions } from './types';
|
||||||
globalThis.__APTABASE__ = { appKey };
|
|
||||||
|
export { AptabaseOptions };
|
||||||
|
|
||||||
|
export function init(appKey: string, options?: AptabaseOptions) {
|
||||||
|
globalThis.__APTABASE__ = { appKey, options };
|
||||||
}
|
}
|
||||||
|
|
||||||
// We only need the headers from the request object
|
// We only need the headers from the request object
|
||||||
|
|
11
packages/node/src/types.ts
Normal file
11
packages/node/src/types.ts
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
export type AptabaseOptions = {
|
||||||
|
host?: string;
|
||||||
|
appVersion?: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
type AptabaseState = {
|
||||||
|
appKey?: string;
|
||||||
|
options?: AptabaseOptions;
|
||||||
|
};
|
||||||
|
|
||||||
|
declare var __APTABASE__: AptabaseState;
|
Loading…
Add table
Add a link
Reference in a new issue