import {z} from 'zod' export type GetApiEventsArgs = {TeamId?: string | undefined, CampaignId?: string | undefined, ContactId?: string | undefined, SenderId?: string | undefined, type?: ('scheduled' | 'sent' | 'open' | 'reply' | 'click' | 'bounce' | 'unsubscribe') | undefined, channel?: ('email' | 'linkedin' | 'text' | 'call') | undefined, limit?: number | undefined, offset?: number | undefined, start_date?: string | undefined, end_date?: string | undefined}; export type GetApiEventsOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const event = z.object({id: z.string().optional(), type: z.enum(["scheduled", "sent", "open", "reply", "unsubscribe", "visit", "message", "follow", "task", "like", "connection_request", "move_contact"]).optional(), channel: z.enum(["email", "linkedin", "text", "call", "instagram"]).optional(), date: z.string().optional(), sequence_step: z.number().int().optional(), opened: z.boolean().optional(), clicked: z.boolean().optional(), replied: z.boolean().optional(), bounced: z.boolean().optional(), autoresponse: z.boolean().optional(), opens: z.number().int().optional(), clicks: z.number().int().optional(), replies: z.number().int().optional(), unsubscribes: z.number().int().optional(), subject: z.string().optional(), contact: z.object({id: z.string().optional(), email: z.string().optional(), first_name: z.string().optional(), last_name: z.string().optional()}).optional(), campaign: z.object({id: z.string().optional(), name: z.string().optional()}).optional(), sender: z.object({id: z.string().optional(), email: z.string().optional(), send_as: z.string().optional()}).optional(), campaign_id: z.string().optional(), contact_id: z.string().optional(), sender_id: z.string().optional(), created_at: z.string().optional(), updated_at: z.string().optional()}); export const pagination = z.object({limit: z.number().int().optional(), offset: z.number().int().optional(), total: z.number().int().optional(), has_more: z.boolean().optional()}); export const eventListResponse = z.object({success: z.boolean().optional(), request_id: z.string().optional(), data: z.array(event).optional(), pagination: pagination.optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * List Events * * GET /events */ export const getApiEvents = async (args: GetApiEventsArgs, options?: GetApiEventsOptions) => { const {baseUrl = "https://api.supersend.io/v2", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.TeamId != null) query.append("TeamId", String(args.TeamId)) if (args.CampaignId != null) query.append("CampaignId", String(args.CampaignId)) if (args.ContactId != null) query.append("ContactId", String(args.ContactId)) if (args.SenderId != null) query.append("SenderId", String(args.SenderId)) if (args.type != null) query.append("type", String(args.type)) if (args.channel != null) query.append("channel", String(args.channel)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.start_date != null) query.append("start_date", String(args.start_date)) if (args.end_date != null) query.append("end_date", String(args.end_date)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/events${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(eventListResponse, await res.json()) };