import {z} from 'zod' export type GetApiEventsIdArgs = {id: string}; export type GetApiEventsIdOptions = {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 eventResponse = z.object({success: z.boolean().optional(), request_id: z.string().optional(), data: event.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 }; /** * Get Event * * GET /events/{id} */ export const getApiEventsId = async (args: GetApiEventsIdArgs, options?: GetApiEventsIdOptions) => { 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 (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/events/${encodeURIComponent(String(args.id))}${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(eventResponse, await res.json()) };