working versions of nextjs and web
This commit is contained in:
parent
9c7e01e6d4
commit
e104dd53dc
26 changed files with 267 additions and 425 deletions
|
@ -3,15 +3,30 @@
|
|||
"version": "0.1.0",
|
||||
"type": "module",
|
||||
"description": "Next.js SDK for Aptabase: Open Source, Privacy-First and Simple Analytics for Mobile, Desktop and Web Apps",
|
||||
"types": "./dist/index.d.ts",
|
||||
"sideEffects": false,
|
||||
"exports": {
|
||||
"./package.json": "./package.json",
|
||||
".": {
|
||||
"types": "./dist/index.d.ts",
|
||||
"default": "./dist/index.js"
|
||||
"import": "./dist/index.js",
|
||||
"require": "./dist/index.cjs",
|
||||
"types": "./dist/index.d.ts"
|
||||
},
|
||||
"./client": {
|
||||
"import": "./dist/client.js",
|
||||
"require": "./dist/client.cjs",
|
||||
"types": "./dist/client.d.ts"
|
||||
},
|
||||
"./server": {
|
||||
"types": "./dist/server.d.ts",
|
||||
"default": "./dist/server.js"
|
||||
"import": "./dist/server.js",
|
||||
"require": "./dist/server.cjs",
|
||||
"types": "./dist/server.d.ts"
|
||||
}
|
||||
},
|
||||
"typesVersions": {
|
||||
"*": {
|
||||
"*": [
|
||||
"dist/*"
|
||||
]
|
||||
}
|
||||
},
|
||||
"repository": {
|
||||
|
@ -25,7 +40,7 @@
|
|||
"homepage": "https://github.com/aptabase/aptabase-js",
|
||||
"license": "MIT",
|
||||
"scripts": {
|
||||
"build": "tsc"
|
||||
"build": "tsup"
|
||||
},
|
||||
"files": [
|
||||
"README.md",
|
||||
|
@ -34,16 +49,9 @@
|
|||
"package.json"
|
||||
],
|
||||
"dependencies": {
|
||||
"@aptabase/node": "*",
|
||||
"@aptabase/react": "*"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@rollup/plugin-replace": "5.0.2",
|
||||
"@rollup/plugin-typescript": "11.1.3",
|
||||
"rollup": "3.28.1",
|
||||
"@rollup/plugin-terser": "0.4.3",
|
||||
"tslib": "2.6.2",
|
||||
"typescript": "5.2.2"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"react": "^18.0.0",
|
||||
"next": "^13.0.0"
|
||||
|
|
1
packages/nextjs/src/client.tsx
Normal file
1
packages/nextjs/src/client.tsx
Normal file
|
@ -0,0 +1 @@
|
|||
export * from '@aptabase/react';
|
|
@ -1,5 +1 @@
|
|||
export * from '@aptabase/react';
|
||||
|
||||
export function init(appKey: string) {
|
||||
globalThis.__APTABASE__ = { appKey };
|
||||
}
|
||||
export type README = 'Use @aptabase/next/server or @aptabase/next/client instead.';
|
||||
|
|
|
@ -1,62 +1,34 @@
|
|||
import { trackEvent as nodeTrackEvent } from '@aptabase/node';
|
||||
import { type NextIncomingMessage } from 'next/dist/server/request-meta';
|
||||
import { headers } from 'next/headers';
|
||||
|
||||
export { init } from '@aptabase/node';
|
||||
|
||||
export async function trackEvent(
|
||||
eventName: string,
|
||||
props?: Record<string, string | number | boolean>,
|
||||
req?: NextIncomingMessage,
|
||||
): Promise<void> {
|
||||
const appKey = globalThis.__APTABASE__.appKey;
|
||||
if (!appKey) return Promise.resolve();
|
||||
const headers = getHeaders(req);
|
||||
if (!headers) return Promise.resolve();
|
||||
|
||||
const userAgent = getUserAgent(req);
|
||||
if (!userAgent) return Promise.resolve();
|
||||
|
||||
const body = JSON.stringify({
|
||||
timestamp: new Date().toISOString(),
|
||||
sessionId: 'CHANGE-THIS',
|
||||
eventName: eventName,
|
||||
systemProps: {
|
||||
isDebug: true,
|
||||
locale: 'en',
|
||||
appVersion: '',
|
||||
sdkVersion: 'aptabase-nextjs@0.0.1',
|
||||
},
|
||||
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}`);
|
||||
}
|
||||
return nodeTrackEvent({ headers }, eventName, props);
|
||||
}
|
||||
|
||||
function getUserAgent(req?: NextIncomingMessage): string | undefined {
|
||||
function getHeaders(req?: NextIncomingMessage): Headers | undefined {
|
||||
if (req) {
|
||||
return req.headers['user-agent'] ?? 'Unknown';
|
||||
// we only need the user-agent header
|
||||
return new Headers({
|
||||
'user-agent': req.headers['user-agent'] ?? '',
|
||||
});
|
||||
}
|
||||
|
||||
// headers() might throw an error if called outside of a request context.
|
||||
try {
|
||||
return headers().get('User-Agent') ?? 'Unknown';
|
||||
return headers();
|
||||
} catch {}
|
||||
|
||||
// If we're here, we're probably using the Pages Router and the user forgot to pass the req parameter.
|
||||
// If we're here, the app is probably using the Pages Router and the developer forgot to pass the req parameter.
|
||||
if (!req) {
|
||||
console.warn("Aptabase: The 'req' parameter of trackEvent is required when using Pages Router.");
|
||||
}
|
||||
|
|
|
@ -1,22 +1,14 @@
|
|||
{
|
||||
"compilerOptions": {
|
||||
"declaration": true,
|
||||
"esModuleInterop": true,
|
||||
"lib": ["esnext", "dom"],
|
||||
"module": "esnext",
|
||||
"jsx": "react-jsx",
|
||||
"moduleResolution": "node",
|
||||
"noEmitOnError": true,
|
||||
"noFallthroughCasesInSwitch": true,
|
||||
"noImplicitReturns": true,
|
||||
"noUnusedLocals": true,
|
||||
"noUnusedParameters": true,
|
||||
"outDir": "dist",
|
||||
"sourceMap": true,
|
||||
"target": "es6",
|
||||
"strict": true,
|
||||
"target": "es2020",
|
||||
"types": ["node"],
|
||||
},
|
||||
"include": ["src/*", "src/global.d.ts"],
|
||||
"exclude": ["node_modules"]
|
||||
}
|
||||
"moduleResolution": "node",
|
||||
"esModuleInterop": true,
|
||||
"baseUrl": ".",
|
||||
"paths": {
|
||||
"types": ["@types"]
|
||||
},
|
||||
"declaration": true,
|
||||
"declarationDir": "./dist"
|
||||
}
|
||||
}
|
||||
|
|
11
packages/nextjs/tsup.config.ts
Normal file
11
packages/nextjs/tsup.config.ts
Normal file
|
@ -0,0 +1,11 @@
|
|||
import { defineConfig } from 'tsup';
|
||||
|
||||
export default defineConfig({
|
||||
entry: ['src/index.ts', 'src/server.ts', 'src/client.tsx'],
|
||||
format: ['cjs', 'esm'],
|
||||
dts: true,
|
||||
splitting: false,
|
||||
minify: true,
|
||||
sourcemap: true,
|
||||
clean: true,
|
||||
});
|
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"
|
||||
]
|
||||
}
|
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,
|
||||
});
|
|
@ -35,7 +35,7 @@
|
|||
"dependencies": {
|
||||
"@aptabase/web": "*"
|
||||
},
|
||||
"devDependencies": {
|
||||
"tsup": "7.2.0"
|
||||
"peerDependencies": {
|
||||
"react": "^18.0.0"
|
||||
}
|
||||
}
|
||||
|
|
|
@ -31,8 +31,5 @@
|
|||
"LICENSE",
|
||||
"dist",
|
||||
"package.json"
|
||||
],
|
||||
"devDependencies": {
|
||||
"tsup": "7.2.0"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue