import {z} from 'zod' export type GetApiApiInboxesChannelIdThreadsThreadIdMessagesMessageIdArgs = {channelId: string, threadId: string, messageId: string}; export type GetApiApiInboxesChannelIdThreadsThreadIdMessagesMessageIdOptions = {baseUrl?: string | undefined, ApiKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const inboxMessageAttachment = z.object({id: z.string().optional(), name: z.string().nullable().optional(), contentType: z.string().nullable().optional(), size: z.number().int().nullable().optional(), isInline: z.boolean().optional(), contentId: z.string().nullable().optional()}); export const getApiApiInboxesChannelIdThreadsThreadIdMessagesMessageIdResponse = z.object({data: z.object({messageId: z.string().optional(), threadId: z.string().optional(), subject: z.string().nullable().optional(), body: z.string().nullable().optional(), type: z.string().nullable().optional(), timestamp: z.string().optional(), direction: z.enum(["in", "out"]).optional(), from: z.object({}).nullable().optional(), to: z.array(z.object({})).optional(), sentByUser: z.string().nullable().optional(), sentVia: z.enum(["aptly", "native"]).nullable().optional(), bodyType: z.enum(["html", "text"]).optional(), cc: z.array(z.object({})).optional(), attachments: z.array(inboxMessageAttachment).optional()}).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 }; /** * Get one email message with its full body and attachments * * GET /api/inboxes/{channelId}/threads/{threadId}/messages/{messageId} * * Also accepts auth: DelegateToken, PartnerBearer */ export const getApiApiInboxesChannelIdThreadsThreadIdMessagesMessageId = async (args: GetApiApiInboxesChannelIdThreadsThreadIdMessagesMessageIdArgs, options?: GetApiApiInboxesChannelIdThreadsThreadIdMessagesMessageIdOptions) => { const {baseUrl = "https://core-api.getaptly.com", fetch: fetchFn = fetch, ApiKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (ApiKeyHeader !== undefined) headers.set("x-token", ApiKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/api/inboxes/${encodeURIComponent(String(args.channelId))}/threads/${encodeURIComponent(String(args.threadId))}/messages/${encodeURIComponent(String(args.messageId))}${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(getApiApiInboxesChannelIdThreadsThreadIdMessagesMessageIdResponse, await res.json()) };