import {z} from 'zod' export type GetApiApiInboxesChannelIdThreadsThreadIdArgs = {channelId: string, threadId: string}; export type GetApiApiInboxesChannelIdThreadsThreadIdOptions = {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 getApiApiInboxesChannelIdThreadsThreadIdResponse = z.object({data: inboxThread.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 thread on an inbox * * GET /api/inboxes/{channelId}/threads/{threadId} * * Also accepts auth: DelegateToken, PartnerBearer */ export const getApiApiInboxesChannelIdThreadsThreadId = async (args: GetApiApiInboxesChannelIdThreadsThreadIdArgs, options?: GetApiApiInboxesChannelIdThreadsThreadIdOptions) => { 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))}${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(getApiApiInboxesChannelIdThreadsThreadIdResponse, await res.json()) };