import {z} from 'zod' export type GetApiApiV1ChatsArgs = {unread?: boolean | undefined, cursor?: string | undefined, before?: string | undefined, after?: string | undefined, limit?: number | undefined, account_type?: ('WHATSAPP' | 'LINKEDIN' | 'SLACK' | 'TWITTER' | 'MESSENGER' | 'INSTAGRAM' | 'TELEGRAM') | undefined, account_id?: string | undefined}; export type GetApiApiV1ChatsOptions = {baseUrl?: string | undefined, AccessToken?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiApiV1ChatsResponse = z.object({object: z.literal("ChatList"), items: z.array(z.object({object: z.literal("Chat"), id: z.string().min(1), account_id: z.string().min(1), account_type: z.union([z.literal("WHATSAPP"), z.literal("LINKEDIN"), z.literal("SLACK"), z.literal("TWITTER"), z.literal("MESSENGER"), z.literal("INSTAGRAM"), z.literal("TELEGRAM")]), provider_id: z.string(), attendee_provider_id: z.string().optional(), name: z.union([z.string(), z.unknown()]), type: z.union([z.literal(0), z.literal(1), z.literal(2)]), timestamp: z.union([z.string(), z.unknown()]), unread_count: z.number(), archived: z.union([z.literal(0), z.literal(1)]), muted_until: z.union([z.literal(-1), z.string(), z.unknown()]), read_only: z.union([z.literal(0), z.literal(1), z.literal(2)]), disabledFeatures: z.array(z.union([z.literal("reactions"), z.literal("reply")])).optional(), subject: z.string().optional(), organization_id: z.string().optional(), mailbox_id: z.string().optional(), content_type: z.union([z.literal("inmail"), z.literal("sponsored"), z.literal("linkedin_offer")]).optional(), folder: z.array(z.union([z.literal("INBOX"), z.literal("INBOX_LINKEDIN_CLASSIC"), z.literal("INBOX_LINKEDIN_RECRUITER"), z.literal("INBOX_LINKEDIN_SALES_NAVIGATOR"), z.literal("INBOX_LINKEDIN_ORGANIZATION"), z.literal("INBOX_INSTAGRAM_GENERAL"), z.literal("PENDING"), z.literal("OTHER"), z.literal("SPAM"), z.literal("ARCHIVED"), z.literal("HIDDEN"), z.literal("UNKNOWN"), z.string()])).optional(), pinned: z.union([z.literal(0), z.literal(1)])})), cursor: z.union([z.unknown(), z.unknown()])}); 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 all chats * * GET /api/v1/chats */ export const getApiApiV1Chats = async (args: GetApiApiV1ChatsArgs, options?: GetApiApiV1ChatsOptions) => { const {baseUrl = "https://api1.unipile.com:13111", fetch: fetchFn = fetch, AccessToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.unread != null) query.append("unread", String(args.unread)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args.before != null) query.append("before", String(args.before)) if (args.after != null) query.append("after", String(args.after)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.account_type != null) query.append("account_type", String(args.account_type)) if (args.account_id != null) query.append("account_id", String(args.account_id)) if (AccessToken !== undefined) headers.set("X-API-KEY", AccessToken) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/chats${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(getApiApiV1ChatsResponse, await res.json()) };