UNPKG

sendrly

Version:

Official Sendrly TypeScript/JavaScript SDK - Simple, type-safe email automation for developers

131 lines (130 loc) 3.6 kB
import type { z } from 'zod'; import type { eventPayloadSchema } from './validation'; /** * Configuration options for the Sendrly SDK */ export interface SendrlyConfig { /** * Your Sendrly API key */ apiKey: string; /** * Enable debug logging * @default false */ debug?: boolean; /** * Base URL for API requests * @default 'https://stebgknkomdhjikpcytk.supabase.co/functions/v1' */ baseUrl?: string; /** * Request timeout in milliseconds * @default 10000 */ timeout?: number; /** * Maximum number of retry attempts * @default 3 */ maxRetries?: number; /** * Custom fetch implementation * @default globalThis.fetch */ fetch?: typeof fetch; /** * Custom logger implementation * @default console */ logger?: { debug: (message: string, ...args: unknown[]) => void; error: (message: string, ...args: unknown[]) => void; }; } /** * Base response type for all API responses */ export interface BaseApiResponse { success: boolean; error?: string; } /** * Response type for contact-related operations */ export interface ContactResponse extends BaseApiResponse { contact?: { email: string; marketing_opt_out: boolean; properties: Record<string, unknown>; }; } /** * Response type for event-related operations */ export interface EventResponse extends BaseApiResponse { event?: { name: string; properties: Record<string, unknown>; }; } /** * Edge Function payload type that matches the schema */ export type EdgeFunctionPayload = { contact: { email: string; marketing_opt_out?: boolean; properties?: Record<string, unknown>; }; event: { name: string; properties?: Record<string, unknown>; }; }; /** * Validated event payload type from zod schema */ export type ValidatedEventPayload = z.infer<typeof eventPayloadSchema>; /** * Error types for SDK errors */ export type SendrlyErrorType = 'INVALID_CONFIG' | 'VALIDATION_ERROR' | 'API_ERROR' | 'NETWORK_ERROR' | 'TIMEOUT_ERROR' | 'UNKNOWN_ERROR' | 'NOT_FOUND' | 'INVALID_API_KEY'; /** * Custom error class for SDK errors */ export declare class SendrlyError extends Error { #private; readonly type: SendrlyErrorType; readonly statusCode?: number; readonly retryable: boolean; constructor(message: string, type: SendrlyErrorType, statusCode?: number); } /** * Contact information for event tracking * This matches the schema expected by the /event edge function */ export interface Contact<T extends Record<string, unknown> = Record<string, unknown>> { /** Contact's email address */ email: string; /** Whether the contact has opted out of marketing communications */ marketing_opt_out?: boolean; /** Additional properties for the contact */ properties?: T; } /** * Event information for tracking with improved type inference */ export interface Event<T extends Record<string, unknown> = Record<string, unknown>> { /** Name of the event (e.g., "user.signup", "order.completed") */ name: string; /** Additional properties for the event */ properties?: T; } /** * User-facing payload for triggering events with generic type support */ export interface EventPayload<TEventProps extends Record<string, unknown> = Record<string, unknown>, TContactProps extends Record<string, unknown> = Record<string, unknown>> { contact: Contact<TContactProps>; event: Event<TEventProps>; }