import {z} from 'zod' export type GetApiApiV2EmailNotificationsArgs = {'page[before]'?: string | undefined, 'page[after]'?: string | undefined, 'page[size]'?: number | undefined}; export type GetApiApiV2EmailNotificationsOptions = {baseUrl?: string | undefined, apiTokenAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const recipientObject = z.object({delivery_status: z.object({code: z.string().optional(), id: z.number().int().optional(), message: z.string().optional(), name: z.string().optional()}).optional(), email_address: z.string().optional(), user_id: z.number().int().optional()}); export const emailNotificationObject = z.object({comment_id: z.number().int().optional(), created_at: z.string().optional(), email_id: z.string().optional(), message_id: z.string().optional(), notification_id: z.number().int().optional(), recipients: z.array(recipientObject).optional(), ticket_id: z.number().int().optional(), updated_at: z.string().optional(), url: z.string().optional()}); export const emailNotificationsResponse = z.object({email_notifications: z.array(emailNotificationObject).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 Email Notifications * * GET /api/v2/email_notifications */ export const getApiApiV2EmailNotifications = async (args: GetApiApiV2EmailNotificationsArgs, options?: GetApiApiV2EmailNotificationsOptions) => { const {baseUrl = "https://example.zendesk.com", fetch: fetchFn = fetch, apiTokenAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['page[before]'] != null) query.append("page[before]", String(args['page[before]'])) if (args['page[after]'] != null) query.append("page[after]", String(args['page[after]'])) if (args['page[size]'] != null) query.append("page[size]", String(args['page[size]'])) if (apiTokenAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${apiTokenAuth.username}:${apiTokenAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/email_notifications${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(emailNotificationsResponse, await res.json()) };