import {z} from 'zod' export type GetApiV3ActivitySendArgs = {event_type?: (Array<'accepted' | 'delivered' | 'bounced' | 'complaint' | 'rejected'>) | undefined, bounce_class?: ('hard' | 'soft' | 'undetermined') | undefined, recipient?: string | undefined, recipient_domain?: string | undefined, sending_domain?: string | undefined, from?: string | undefined, grant_id?: string | undefined, message_id?: string | undefined, thread_id?: string | undefined, start?: number | undefined, end?: number | undefined, order?: ('desc' | 'asc') | undefined, limit?: number | undefined, page_token?: string | undefined}; export type GetApiV3ActivitySendOptions = {baseUrl?: string | undefined, NYLASAPIKEY?: string | undefined, fetch?: typeof fetch | undefined}; export const detail = z.object({smtp_response: z.string().optional(), processing_time_ms: z.number().int().optional(), reporting_mta: z.string().optional(), remote_mta_ip: z.string().optional(), bounce_type: z.string().optional(), bounce_subtype: z.string().optional(), status: z.string().optional(), action: z.string().optional(), diagnostic_code: z.string().optional(), feedback_type: z.string().optional(), complaint_subtype: z.string().optional()}); export const getApiV3ActivitySendResponse = z.object({request_id: z.string().optional(), data: z.array(z.object({id: z.string().optional(), event_type: z.enum(["accepted", "delivered", "bounced", "complaint", "rejected"]).optional(), bounce_class: z.enum(["hard", "soft", "undetermined"]).optional(), timestamp: z.number().int().optional(), application_id: z.string().optional(), grant_id: z.string().optional(), message_id: z.string().optional(), thread_id: z.string().optional(), from: z.string().optional(), sending_domain: z.string().optional(), recipient: z.string().optional(), recipient_domain: z.string().optional(), subject: z.string().optional(), reason: z.string().optional(), detail: detail.optional()})).optional(), next_cursor: z.string().nullable().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 send events * * GET /v3/activity/send */ export const getApiV3ActivitySend = async (args: GetApiV3ActivitySendArgs, options?: GetApiV3ActivitySendOptions) => { const {baseUrl = "https://api.us.nylas.com", fetch: fetchFn = fetch, NYLASAPIKEY} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") for (const item of args.event_type ?? []) query.append("event_type", String(item)) if (args.bounce_class != null) query.append("bounce_class", String(args.bounce_class)) if (args.recipient != null) query.append("recipient", String(args.recipient)) if (args.recipient_domain != null) query.append("recipient_domain", String(args.recipient_domain)) if (args.sending_domain != null) query.append("sending_domain", String(args.sending_domain)) if (args.from != null) query.append("from", String(args.from)) if (args.grant_id != null) query.append("grant_id", String(args.grant_id)) if (args.message_id != null) query.append("message_id", String(args.message_id)) if (args.thread_id != null) query.append("thread_id", String(args.thread_id)) if (args.start != null) query.append("start", String(args.start)) if (args.end != null) query.append("end", String(args.end)) if (args.order != null) query.append("order", String(args.order)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.page_token != null) query.append("page_token", String(args.page_token)) if (NYLASAPIKEY !== undefined) headers.set('authorization', `Bearer ${NYLASAPIKEY}`) const url = `${baseUrl.replace(/\/+$/, '')}/v3/activity/send${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(getApiV3ActivitySendResponse, await res.json()) };