import {z} from 'zod' export type GetApiV3ActivitySendMessagesMessageIdOptions = {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 sendMessage = z.object({message: z.object({message_id: z.string().optional(), thread_id: z.string().optional(), grant_id: z.string().optional(), application_id: z.string().optional(), from: z.string().optional(), sending_domain: z.string().optional(), subject: z.string().optional(), sent_at: z.number().int().optional(), last_event_at: z.number().int().optional()}).optional(), recipients: z.array(z.object({recipient: z.string().optional(), recipient_domain: z.string().optional(), current_status: z.enum(["accepted", "delivered", "bounced", "complaint", "rejected"]).optional(), bounce_class: z.enum(["hard", "soft", "undetermined"]).optional(), events: z.array(z.object({id: z.string().optional(), event_type: z.enum(["accepted", "delivered", "bounced", "complaint", "rejected"]).optional(), timestamp: z.number().int().optional(), bounce_class: z.enum(["hard", "soft", "undetermined"]).optional(), reason: z.string().optional(), detail: detail.optional()})).optional()})).optional()}); export const getApiV3ActivitySendMessagesMessageIdResponse = z.object({request_id: z.string().optional(), data: sendMessage.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 }; /** * Return a message's delivery status * * GET /v3/activity/send/messages/{message_id} */ export const getApiV3ActivitySendMessagesMessageId = async (options?: GetApiV3ActivitySendMessagesMessageIdOptions) => { 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") if (NYLASAPIKEY !== undefined) headers.set('authorization', `Bearer ${NYLASAPIKEY}`) const url = `${baseUrl.replace(/\/+$/, '')}/v3/activity/send/messages/${encodeURIComponent(String(args.message_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(getApiV3ActivitySendMessagesMessageIdResponse, await res.json()) };