working versions of nextjs and web
This commit is contained in:
parent
9c7e01e6d4
commit
e104dd53dc
26 changed files with 267 additions and 425 deletions
0
packages/node/CHANGELOG.md
Normal file
0
packages/node/CHANGELOG.md
Normal file
21
packages/node/LICENSE
Normal file
21
packages/node/LICENSE
Normal file
|
@ -0,0 +1,21 @@
|
|||
MIT License
|
||||
|
||||
Copyright (c) 2023 Sumbit Labs Ltd.
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
0
packages/node/README.md
Normal file
0
packages/node/README.md
Normal file
35
packages/node/package.json
Normal file
35
packages/node/package.json
Normal file
|
@ -0,0 +1,35 @@
|
|||
{
|
||||
"name": "@aptabase/node",
|
||||
"version": "0.1.0",
|
||||
"type": "module",
|
||||
"description": "Node.js SDK for Aptabase: Open Source, Privacy-First and Simple Analytics for Mobile, Desktop and Web Apps",
|
||||
"main": "./dist/index.cjs",
|
||||
"module": "./dist/index.js",
|
||||
"types": "./dist/index.d.ts",
|
||||
"exports": {
|
||||
".": {
|
||||
"require": "./dist/index.cjs",
|
||||
"import": "./dist/index.js",
|
||||
"types": "./dist/index.d.ts"
|
||||
}
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/aptabase/aptabase-js.git",
|
||||
"directory": "packages/node"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://github.com/aptabase/aptabase-js/issues"
|
||||
},
|
||||
"homepage": "https://github.com/aptabase/aptabase-js",
|
||||
"license": "MIT",
|
||||
"scripts": {
|
||||
"build": "tsup"
|
||||
},
|
||||
"files": [
|
||||
"README.md",
|
||||
"LICENSE",
|
||||
"dist",
|
||||
"package.json"
|
||||
]
|
||||
}
|
5
packages/node/src/global.d.ts
vendored
Normal file
5
packages/node/src/global.d.ts
vendored
Normal file
|
@ -0,0 +1,5 @@
|
|||
type AptabaseState = {
|
||||
appKey?: string;
|
||||
};
|
||||
|
||||
declare var __APTABASE__: AptabaseState;
|
52
packages/node/src/index.js
Normal file
52
packages/node/src/index.js
Normal file
|
@ -0,0 +1,52 @@
|
|||
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}`);
|
||||
}
|
||||
});
|
||||
}
|
50
packages/node/src/index.ts
Normal file
50
packages/node/src/index.ts
Normal file
|
@ -0,0 +1,50 @@
|
|||
export function init(appKey: string) {
|
||||
globalThis.__APTABASE__ = { appKey };
|
||||
}
|
||||
|
||||
// We only need the headers from the request object
|
||||
type PartialRequest = Pick<Request, 'headers'>;
|
||||
|
||||
export async function trackEvent(
|
||||
req: PartialRequest,
|
||||
eventName: string,
|
||||
props?: Record<string, string | number | boolean>,
|
||||
): Promise<void> {
|
||||
const appKey = globalThis.__APTABASE__?.appKey;
|
||||
if (!appKey) return Promise.resolve();
|
||||
|
||||
const userAgent = req.headers.get('user-agent') ?? '';
|
||||
|
||||
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 = await 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 = await response.text();
|
||||
console.warn(`Failed to send event "${eventName}": ${response.status} ${responseBody}`);
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn(`Failed to send event "${eventName}": ${e}`);
|
||||
}
|
||||
}
|
14
packages/node/tsconfig.json
Normal file
14
packages/node/tsconfig.json
Normal file
|
@ -0,0 +1,14 @@
|
|||
{
|
||||
"compilerOptions": {
|
||||
"target": "es6",
|
||||
"strict": true,
|
||||
"moduleResolution": "node",
|
||||
"esModuleInterop": true,
|
||||
"baseUrl": ".",
|
||||
"paths": {
|
||||
"types": ["@types"]
|
||||
},
|
||||
"declaration": true,
|
||||
"declarationDir": "./dist"
|
||||
}
|
||||
}
|
11
packages/node/tsup.config.ts
Normal file
11
packages/node/tsup.config.ts
Normal file
|
@ -0,0 +1,11 @@
|
|||
import { defineConfig } from 'tsup';
|
||||
|
||||
export default defineConfig({
|
||||
entry: ['src/index.ts'],
|
||||
format: ['cjs', 'esm'],
|
||||
dts: true,
|
||||
splitting: false,
|
||||
minify: true,
|
||||
sourcemap: true,
|
||||
clean: true,
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue