import {z} from 'zod' export type GetApiV1EmailThreadsThreadIdArgs = {thread_id: string}; export type GetApiV1EmailThreadsThreadIdOptions = {baseUrl: string, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const emailAddress = z.object({address: z.string().max(320), name: z.string().max(255).nullable().optional()}); export const srcCommunicationEmailThreadsSchemasMessageResponse = z.object({id: z.string(), direction: z.enum(["inbound", "outbound"]), provider_message_id: z.string().nullable(), provider_thread_id: z.string().nullable(), rfc822_message_id: z.string().nullable(), in_reply_to_header: z.string().nullable(), from_address: z.string(), to_addresses: z.array(emailAddress).optional(), cc_addresses: z.array(emailAddress).optional(), bcc_addresses: z.array(emailAddress).optional(), subject: z.string().nullable(), content: z.string(), triage_decision: z.string().nullable(), triage_reason: z.string().nullable(), delivery_status: z.string().nullable(), delivery_status_at: z.string().nullable(), sent_at: z.string().nullable(), received_at: z.string().nullable(), created_at: z.string()}); export const threadResponse = z.object({provider_thread_id: z.string(), session_id: z.string(), subject: z.string().nullable(), participants: z.array(z.string()).optional(), message_count: z.number().int(), last_message_at: z.string().nullable(), messages: z.array(srcCommunicationEmailThreadsSchemasMessageResponse)}); 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 }; /** * Read an email thread by Nylas provider_thread_id * * GET /v1/email/threads/{thread_id} */ export const getApiV1EmailThreadsThreadId = async (args: GetApiV1EmailThreadsThreadIdArgs, options: GetApiV1EmailThreadsThreadIdOptions) => { const {baseUrl, fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (APIKeyHeader !== undefined) headers.set("x-api-key", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/v1/email/threads/${encodeURIComponent(String(args.thread_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(threadResponse, await res.json()) };