import { nextCursor, slackRequest } from './request.ts'
import { boundedLimit, inputRecord, optionalBoolean, optionalString } from './validation.ts'
const allowedTypes = new Set(['public_channel', 'private_channel', 'im', 'mpim'])
type ConversationsResponse = {
ok: boolean
channels?: unknown[]
response_metadata?: { next_cursor?: string }
[key: string]: unknown
}
/** List conversations available to the authorizing Slack user. */
export default async function listConversations(params: Record<string, unknown> = {}) {
const input = inputRecord(params)
const requestedTypes = input.types ?? ['public_channel', 'private_channel', 'im', 'mpim']
if (!Array.isArray(requestedTypes) || requestedTypes.some((type) => typeof type !== 'string' || !allowedTypes.has(type))) {
throw new Error('types must contain only public_channel, private_channel, im, or mpim.')
}
const result = await slackRequest<ConversationsResponse>('users.conversations', {
types: requestedTypes.join(','),
limit: boundedLimit(input.limit),
cursor: optionalString(input, 'cursor'),
exclude_archived: optionalBoolean(input, 'excludeArchived') ?? true,
})
return {
conversations: result.channels ?? [],
nextCursor: nextCursor(result),
}
}