export interface OpenpanelEventOptions { profileId?: string; } export interface PostEventPayload { name: string; timestamp: string; profileId?: string; properties?: Record & OpenpanelEventOptions; } export interface UpdateProfilePayload { profileId: string; firstName?: string; lastName?: string; email?: string; avatar?: string; properties?: Record; } export interface IncrementProfilePayload { profileId: string; property: string; value: number; } export interface DecrementProfilePayload { profileId?: string; property: string; value: number; } export interface OpenpanelSdkOptions { url?: string; clientId: string; clientSecret?: string; verbose?: boolean; } export interface OpenpanelState { profileId?: string; properties: Record; } function awaitProperties( properties: Record> ): Promise> { return Promise.all( Object.entries(properties).map(async ([key, value]) => { return [key, (await value) ?? '']; }) ).then((entries) => Object.fromEntries(entries)); } function createApi(_url: string) { const headers: Record> = { 'Content-Type': 'application/json', }; return { headers, async fetch( path: string, data: ReqBody, options?: RequestInit ): Promise { const url = `${_url}${path}`; let timer: ReturnType; const h = await awaitProperties(headers); return new Promise((resolve) => { const wrappedFetch = (attempt: number) => { clearTimeout(timer); fetch(url, { headers: h, method: 'POST', body: JSON.stringify(data ?? {}), keepalive: true, ...(options ?? {}), }) .then(async (res) => { if (res.status === 401) { return null; } if (res.status !== 200 && res.status !== 202) { return retry(attempt, resolve); } const response = await res.text(); if (!response) { return resolve(null); } resolve(response as ResBody); }) .catch(() => { return retry(attempt, resolve); }); }; function retry( attempt: number, resolve: (value: ResBody | null) => void ) { if (attempt > 1) { return resolve(null); } timer = setTimeout( () => { wrappedFetch(attempt + 1); }, Math.pow(2, attempt) * 500 ); } wrappedFetch(0); }); }, }; } export class OpenpanelSdk< Options extends OpenpanelSdkOptions = OpenpanelSdkOptions, > { public options: Options; public api: ReturnType; private state: OpenpanelState = { properties: {}, }; constructor(options: Options) { this.options = options; this.api = createApi(options.url ?? 'https://api.openpanel.dev'); this.api.headers['openpanel-client-id'] = options.clientId; if (this.options.clientSecret) { this.api.headers['openpanel-client-secret'] = this.options.clientSecret; } } // Public public setProfileId(profileId: string) { this.state.profileId = profileId; } public async setProfile(payload: UpdateProfilePayload) { this.setProfileId(payload.profileId); return this.api.fetch('/profile', { ...payload, properties: { ...this.state.properties, ...payload.properties, }, }); } public async increment( property: string, value: number, options?: OpenpanelEventOptions ) { const profileId = options?.profileId ?? this.state.profileId; if (!profileId) { return console.log('No profile id'); } return this.api.fetch( '/profile/increment', { profileId, property, value, } ); } public async decrement( property: string, value: number, options?: OpenpanelEventOptions ) { const profileId = options?.profileId ?? this.state.profileId; if (!profileId) { return console.log('No profile id'); } return this.api.fetch( '/profile/decrement', { profileId, property, value, } ); } public async event( name: string, properties?: PostEventPayload['properties'] ) { const profileId = properties?.profileId ?? this.state.profileId; delete properties?.profileId; return this.api.fetch('/event', { name, properties: { ...this.state.properties, ...(properties ?? {}), }, timestamp: this.timestamp(), profileId, }); } public setGlobalProperties(properties: Record) { this.state.properties = { ...this.state.properties, ...properties, }; } public clear() { this.state.profileId = undefined; } // Private private timestamp() { return new Date().toISOString(); } }