import {z} from 'zod' export type GetApiApiV1WebhooksArgs = {cursor?: string | undefined, limit?: number | undefined}; export type GetApiApiV1WebhooksOptions = {baseUrl?: string | undefined, AccessToken?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiApiV1WebhooksResponse = z.object({object: z.literal("WebhookList"), items: z.array(z.union([z.object({object: z.literal("Webhook"), id: z.string().min(1), account_ids: z.array(z.object({id: z.string().min(1), name: z.string().optional(), type: z.union([z.literal("GOOGLE"), z.literal("GOOGLE_CALENDAR"), z.literal("ICLOUD"), z.literal("LINKEDIN"), z.literal("MAIL"), z.literal("MOBILE"), z.literal("OUTLOOK"), z.literal("TWITTER"), z.literal("WHATSAPP"), z.literal("SLACK"), z.literal("TELEGRAM")])})), enabled: z.boolean(), name: z.string().optional(), request_url: z.string(), format: z.enum(["json", "form"]), headers: z.array(z.object({key: z.string(), value: z.string()})), data: z.union([z.array(z.object({name: z.string(), key: z.enum(["account_id", "account_type", "account_info", "chat_id", "timestamp", "webhook_name", "message_id", "message", "mentions", "reaction", "reaction_sender", "read_by", "sender", "is_sender", "attendees", "attachments", "subject", "provider_chat_id", "provider_message_id", "is_event", "chat_pinned", "quoted", "reply_to", "is_forwarded", "is_view_once", "deleted_for", "chat_content_type", "message_type", "is_group", "folder", "event_type"])})), z.array(z.object({name: z.string(), key: z.enum(["account_id", "account_type", "webhook_name", "timestamp", "user_provider_id", "user_full_name", "user_public_identifier", "user_profile_url", "user_picture_url"])})), z.array(z.object({name: z.string(), key: z.enum(["email_id", "account_id", "webhook_name", "date", "from_attendee", "to_attendees", "cc_attendees", "bcc_attendees", "reply_to_attendees", "subject", "body", "body_plain", "message_id", "provider_id", "tracking_id", "read_date", "is_complete", "in_reply_to", "has_attachments", "attachments", "folders", "role", "origin", "thread_id", "deprecated_id"])})), z.array(z.object({name: z.string(), key: z.enum(["event_id", "tracking_id", "type", "date", "email_id", "account_id", "ip", "user_agent", "url", "label", "custom_domain"])})), z.array(z.object({name: z.string(), key: z.enum(["account_id", "account_type", "id", "ical_uid", "master_event_id", "calendar_id", "created_at", "updated_at", "title", "body", "location", "is_cancelled", "is_all_day", "is_attendees_list_hidden", "attendees", "start", "end", "recurrence", "organizer", "conference_id", "conference", "visibility", "transparency", "event_type", "color"])}))]), events: z.array(z.union([z.enum(["message_received", "message_read", "message_reaction", "message_edited", "message_deleted", "message_delivered"]), z.literal("new_relation"), z.enum(["mail_sent", "mail_received", "mail_moved"]), z.enum(["mail_opened", "mail_link_clicked"]), z.enum(["creation_success", "creation_fail", "deleted", "reconnected", "sync_success", "stopped", "ok", "connecting", "error", "credentials", "permissions"]), z.enum(["calendar_event_created", "calendar_event_updated", "calendar_event_deleted"])])).optional(), type: z.literal("AUTO")}), z.object({object: z.literal("Webhook"), id: z.string().min(1), account_ids: z.array(z.object({id: z.string().min(1), name: z.string().optional(), type: z.union([z.literal("GOOGLE"), z.literal("GOOGLE_CALENDAR"), z.literal("ICLOUD"), z.literal("LINKEDIN"), z.literal("MAIL"), z.literal("MOBILE"), z.literal("OUTLOOK"), z.literal("TWITTER"), z.literal("WHATSAPP"), z.literal("SLACK"), z.literal("TELEGRAM")])})), enabled: z.boolean(), name: z.string().optional(), request_url: z.string(), format: z.enum(["json", "form"]), headers: z.array(z.object({key: z.string(), value: z.string()})), data: z.union([z.array(z.object({name: z.string(), key: z.enum(["account_id", "account_type", "account_info", "chat_id", "timestamp", "webhook_name", "message_id", "message", "mentions", "reaction", "reaction_sender", "read_by", "sender", "is_sender", "attendees", "attachments", "subject", "provider_chat_id", "provider_message_id", "is_event", "chat_pinned", "quoted", "reply_to", "is_forwarded", "is_view_once", "deleted_for", "chat_content_type", "message_type", "is_group", "folder", "event_type"])})), z.array(z.object({name: z.string(), key: z.enum(["account_id", "account_type", "webhook_name", "timestamp", "user_provider_id", "user_full_name", "user_public_identifier", "user_profile_url", "user_picture_url"])})), z.array(z.object({name: z.string(), key: z.enum(["email_id", "account_id", "webhook_name", "date", "from_attendee", "to_attendees", "cc_attendees", "bcc_attendees", "reply_to_attendees", "subject", "body", "body_plain", "message_id", "provider_id", "tracking_id", "read_date", "is_complete", "in_reply_to", "has_attachments", "attachments", "folders", "role", "origin", "thread_id", "deprecated_id"])})), z.array(z.object({name: z.string(), key: z.enum(["event_id", "tracking_id", "type", "date", "email_id", "account_id", "ip", "user_agent", "url", "label", "custom_domain"])})), z.array(z.object({name: z.string(), key: z.enum(["account_id", "account_type", "id", "ical_uid", "master_event_id", "calendar_id", "created_at", "updated_at", "title", "body", "location", "is_cancelled", "is_all_day", "is_attendees_list_hidden", "attendees", "start", "end", "recurrence", "organizer", "conference_id", "conference", "visibility", "transparency", "event_type", "color"])}))]), events: z.array(z.union([z.enum(["message_received", "message_read", "message_reaction", "message_edited", "message_deleted", "message_delivered"]), z.literal("new_relation"), z.enum(["mail_sent", "mail_received", "mail_moved"]), z.enum(["mail_opened", "mail_link_clicked"]), z.enum(["creation_success", "creation_fail", "deleted", "reconnected", "sync_success", "stopped", "ok", "connecting", "error", "credentials", "permissions"]), z.enum(["calendar_event_created", "calendar_event_updated", "calendar_event_deleted"])])).optional(), type: z.literal("TRIGGER"), trigger: z.object({icon: z.union([z.literal("WebhookIcon"), z.literal("SettingsInputIcon"), z.literal("LeakAddIcon"), z.literal("Diversity2Icon"), z.literal("AutoFixHighIcon"), z.literal("SignPostIcon"), z.literal("ShutterSpeedIcon"), z.literal("SyncAltIcon"), z.literal("WhatsAppIcon"), z.literal("LinkedInIcon"), z.literal("ImportExportIcon"), z.literal("PodcastsIcon"), z.literal("CableIcon"), z.literal("CalendarIcon")]), name: z.string(), context: z.union([z.literal("MESSAGE"), z.literal("CHAT")])})})])), cursor: z.union([z.unknown(), z.unknown()])}); 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 all webhooks * * GET /api/v1/webhooks */ export const getApiApiV1Webhooks = async (args: GetApiApiV1WebhooksArgs, options?: GetApiApiV1WebhooksOptions) => { const {baseUrl = "https://api1.unipile.com:13111", fetch: fetchFn = fetch, AccessToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args.limit != null) query.append("limit", String(args.limit)) if (AccessToken !== undefined) headers.set("X-API-KEY", AccessToken) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/webhooks${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(getApiApiV1WebhooksResponse, await res.json()) };