import {z} from 'zod' export type GetApiV1AppIdConversationsArgs = {appId: string, startingAfter?: string | undefined, limit?: number | undefined, orderBy?: ('lastActivity' | 'createdAt') | undefined, orderDirection?: ('DESC' | 'ASC') | undefined, offsetTs?: number | undefined, lastMessageBefore?: number | undefined, lastMessageAfter?: number | undefined}; export type GetApiV1AppIdConversationsOptions = {baseUrl?: string | undefined, JWTAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const message = z.object({id: z.string().optional(), type: z.enum(["UserMessage", "SystemMessage"]).optional(), origin: z.enum(["web", "rest", "email", "import"]).optional(), location: z.array(z.number()).optional(), text: z.string().nullable().optional(), custom: z.record(z.string(), z.string()).optional(), attachment: z.object({dimensions: z.object({height: z.number().int().optional(), width: z.number().int().optional()}).optional(), size: z.number().int().optional(), url: z.string().optional()}).nullable().optional(), conversationId: z.string().optional(), createdAt: z.number().nullable().optional(), senderId: z.string().nullable().optional(), editedAt: z.number().int().optional(), referencedMessageId: z.string().nullable().optional(), readBy: z.array(z.unknown()).nullable().optional()}); export const conversation = z.object({id: z.string().optional(), participants: z.object({access: z.enum(["Read", "ReadWrite", "None"]).optional(), notify: z.union([z.boolean(), z.string()]).nullable().optional()}).optional(), subject: z.string().max(2048).nullable().optional(), custom: z.record(z.string(), z.string().nullable()).optional(), createdAt: z.number().int().optional(), topicId: z.string().min(1).max(2048).optional(), photoUrl: z.string().min(1).max(2048).nullable().optional(), welcomeMessages: z.array(z.string().nullable()).optional(), lastMessage: message.optional()}); export const conversationsInApp = z.object({data: z.array(conversation).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 conversations * * GET /v1/{appId}/conversations * * Also accepts auth: ApiKeyAuth */ export const getApiV1AppIdConversations = async (args: GetApiV1AppIdConversationsArgs, options?: GetApiV1AppIdConversationsOptions) => { const {baseUrl = "https://api.talkjs.com", fetch: fetchFn = fetch, JWTAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.startingAfter != null) query.append("startingAfter", String(args.startingAfter)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.orderBy != null) query.append("orderBy", String(args.orderBy)) if (args.orderDirection != null) query.append("orderDirection", String(args.orderDirection)) if (args.offsetTs != null) query.append("offsetTs", String(args.offsetTs)) if (args.lastMessageBefore != null) query.append("lastMessageBefore", String(args.lastMessageBefore)) if (args.lastMessageAfter != null) query.append("lastMessageAfter", String(args.lastMessageAfter)) if (JWTAuth !== undefined) headers.set('authorization', `Bearer ${JWTAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/${encodeURIComponent(String(args.appId))}/conversations${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(conversationsInApp, await res.json()) };