import {z} from 'zod' export type GetApiConversationsIdMessagesArgs = {id: string, limit?: number | undefined, offset?: number | undefined}; export type GetApiConversationsIdMessagesOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const conversationMessage = z.object({object: z.literal("message").optional(), id: z.string().optional(), text: z.string().nullable().optional(), html: z.string().nullable().optional(), subject: z.string().nullable().optional(), timestamp: z.string().optional(), is_from_self: z.boolean().optional(), is_read: z.boolean().optional(), status: z.enum(["delivered", "pending", "failed"]).optional(), job_id: z.string().nullable().optional(), conversation_id: z.string().optional(), platform_type: z.enum(["email", "linkedin"]).optional(), sender: z.object({id: z.string().optional(), email: z.string().optional()}).nullable().optional()}); export const pagination = z.object({limit: z.number().int().optional(), offset: z.number().int().optional(), total: z.number().int().optional(), has_more: z.boolean().optional()}); export const messageListResponse = z.object({success: z.boolean().optional(), request_id: z.string().optional(), data: z.array(conversationMessage).optional(), pagination: pagination.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 Messages * * GET /conversations/{id}/messages */ export const getApiConversationsIdMessages = async (args: GetApiConversationsIdMessagesArgs, options?: GetApiConversationsIdMessagesOptions) => { const {baseUrl = "https://api.supersend.io/v2", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/conversations/${encodeURIComponent(String(args.id))}/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(messageListResponse, await res.json()) };