reuse a bunch of stuff
This commit is contained in:
parent
c0a2387f76
commit
e256d13c92
14 changed files with 1593 additions and 138 deletions
21
packages/nextjs/LICENSE
Normal file
21
packages/nextjs/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.
|
|
@ -33,6 +33,9 @@
|
|||
"dist",
|
||||
"package.json"
|
||||
],
|
||||
"dependencies": {
|
||||
"@aptabase/react": "*"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@rollup/plugin-replace": "5.0.2",
|
||||
"@rollup/plugin-typescript": "11.1.3",
|
||||
|
|
|
@ -1,82 +0,0 @@
|
|||
'use client';
|
||||
|
||||
import { createContext, useContext } from 'react';
|
||||
import { AptabaseOptions } from './types';
|
||||
|
||||
type TrackEventFn = (eventName: string, props?: Record<string, string | number | boolean>) => Promise<void>;
|
||||
|
||||
type ContextProps = {
|
||||
appKey?: string;
|
||||
client?: AptabaseClient;
|
||||
} & AptabaseOptions;
|
||||
|
||||
export type AptabaseClient = {
|
||||
trackEvent: TrackEventFn;
|
||||
};
|
||||
|
||||
const AptabaseContext = createContext<ContextProps>({});
|
||||
|
||||
type Props = {
|
||||
appKey: string;
|
||||
options?: AptabaseOptions;
|
||||
children: React.ReactNode;
|
||||
};
|
||||
|
||||
export function AptabaseProvider({ appKey, options, children }: Props) {
|
||||
const client: AptabaseClient = {
|
||||
trackEvent: (eventName, props) => sendEvent(appKey, eventName, props),
|
||||
};
|
||||
|
||||
return <AptabaseContext.Provider value={{ appKey, ...options, client }}>{children}</AptabaseContext.Provider>;
|
||||
}
|
||||
|
||||
export function useAptabase(): AptabaseClient {
|
||||
const ctx = useContext(AptabaseContext);
|
||||
if (!ctx.client) {
|
||||
throw new Error(
|
||||
'useAptabase must be used within AptabaseProvider. Did you forget to wrap your app in <AptabaseProvider>?',
|
||||
);
|
||||
}
|
||||
|
||||
return ctx.client;
|
||||
}
|
||||
|
||||
async function sendEvent(
|
||||
appKey: string | undefined,
|
||||
eventName: string,
|
||||
props?: Record<string, string | number | boolean>,
|
||||
): Promise<void> {
|
||||
if (!appKey) 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: {
|
||||
'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}`);
|
||||
}
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
export * from './client';
|
||||
export * from '@aptabase/react';
|
||||
|
||||
export function init(appKey: string) {
|
||||
globalThis.__APTABASE__ = { appKey };
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue