Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/cold-ravens-drum.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@byteslice/events": minor
---

Added info and debug logger.
4 changes: 2 additions & 2 deletions bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 41 additions & 0 deletions packages/events/src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,23 @@ import { createClient } from '@supabase/supabase-js'
import { Resend } from 'resend'
import type { EventProperties, PageParams } from './types'

type LogLevel = 'info' | 'debug' | 'none'

const LOG_LEVEL = (process.env.BYTESLICE_LOG_LEVEL || 'none') as LogLevel

const logger = {
info: (...args: unknown[]) => {
if (LOG_LEVEL === 'info' || LOG_LEVEL === 'debug') {
console.log('[ByteSlice Events]', ...args)
}
},
debug: (...args: unknown[]) => {
if (LOG_LEVEL === 'debug') {
console.debug('[ByteSlice Events]', ...args)
}
},
}

if (
!process.env.NEXT_PUBLIC_SUPABASE_URL ||
!process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY
Expand All @@ -27,6 +44,9 @@ export function track(
): Promise<Result<{ id: string }, Error>> {
return withResult(
async () => {
logger.info('Tracking event:', event)
logger.debug('Event properties:', properties)

const { data, error } = await supabase
.from('events')
.insert({
Expand All @@ -40,7 +60,11 @@ export function track(
.select('id')
.single()

logger.info('Event tracked:', data?.id)
logger.debug('Event data:', data)

if (error) {
logger.debug('Error tracking event:', error)
throw error
}

Expand All @@ -61,6 +85,9 @@ export function page(
): Promise<Result<{ id: string }, Error>> {
return withResult(
async () => {
logger.info('Tracking page view')
logger.debug('Page params:', params)

const { data, error } = await supabase
.from('events')
.insert({
Expand All @@ -72,7 +99,11 @@ export function page(
.select('id')
.single()

logger.info('Page view tracked:', data?.id)
logger.debug('Page view data:', data)

if (error) {
logger.debug('Error tracking page view:', error)
throw error
}

Expand All @@ -98,14 +129,24 @@ export async function sendEmail(
): Promise<Result<{ id: string }, Error>> {
return withResult(
async () => {
logger.info('Sending email to:', params.to)
logger.debug('Email details:', {
subject: params.subject,
from: params.from,
})

const { data, error } = await resend.emails.send({
from: params.from ?? 'transactions@byteslice.co',
to: params.to,
subject: params.subject,
html: params.html,
})

logger.info('Email sent:', data?.id)
logger.debug('Email data:', data)

if (error) {
logger.debug('Error sending email:', error)
throw error
}

Expand Down