import {z} from 'zod' export type GetApiApiInboxesChannelIdMessagesArgs = {channelId: string, dateFrom?: string | undefined, dateTo?: string | undefined, direction?: ('in' | 'out') | undefined, unreadOnly?: boolean | undefined, includeJunk?: boolean | undefined, page?: number | undefined, pageSize?: number | undefined}; export type GetApiApiInboxesChannelIdMessagesOptions = {baseUrl?: string | undefined, ApiKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const inboxThreadMetrics = z.object({firstResponseHrs: z.number().nullable().optional(), avgResponseHrs: z.number().nullable().optional(), responseCount: z.number().int().optional(), reopened: z.boolean().optional(), touchCount: z.number().int().optional(), inboundCount: z.number().int().optional(), outboundCount: z.number().int().optional(), lastMessageAt: z.string().nullable().optional(), lastMessageDirection: z.enum(["in", "out"]).nullable().optional()}); export const inboxMessage = 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()}); export const inboxThread = z.object({threadId: z.string().optional(), subject: z.string().nullable().optional(), status: z.enum(["closed", "new", "open", "sent"]).nullable().optional(), isJunk: z.enum(["archived", "not_junk", "junk"]).nullable().optional(), unread: z.boolean().optional(), assigneeId: z.string().nullable().optional(), type: z.string().nullable().optional(), participants: z.array(z.string()).optional(), labels: z.array(z.object({id: z.string().optional(), name: z.string().nullable().optional()})).optional(), metrics: inboxThreadMetrics.optional(), messages: z.array(inboxMessage).optional()}); export const getApiApiInboxesChannelIdMessagesResponse = z.object({data: z.array(inboxThread).optional(), count: z.number().int().optional(), page: z.number().int().optional(), pageSize: z.number().int().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 threads and messages on an inbox (deprecated) * * GET /api/inboxes/{channelId}/messages * * Also accepts auth: DelegateToken, PartnerBearer */ export const getApiApiInboxesChannelIdMessages = async (args: GetApiApiInboxesChannelIdMessagesArgs, options?: GetApiApiInboxesChannelIdMessagesOptions) => { 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 (args.dateFrom != null) query.append("dateFrom", String(args.dateFrom)) if (args.dateTo != null) query.append("dateTo", String(args.dateTo)) if (args.direction != null) query.append("direction", String(args.direction)) if (args.unreadOnly != null) query.append("unreadOnly", String(args.unreadOnly)) if (args.includeJunk != null) query.append("includeJunk", String(args.includeJunk)) if (args.page != null) query.append("page", String(args.page)) if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) if (ApiKeyHeader !== undefined) headers.set("x-token", ApiKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/api/inboxes/${encodeURIComponent(String(args.channelId))}/messages${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(getApiApiInboxesChannelIdMessagesResponse, await res.json()) };