Skip to content

Built for people who want to own their automations. Join the waitlist for an invite.

Package listing

@kody/figma

src/list-comments.ts

59 lines · 2.0 KB · TypeScript
import { figmaRequest } from './client.ts'
import { asArray, mapComment } from './models.ts'
import { parseFileKey } from './setup.ts'
import type { FigmaAuthInput, FigmaComment } from './types.ts'
import { optionalBoolean, optionalString, requireRecord } from './types.ts'
import { requireAuthMode } from './auth.ts'

export type ListCommentsInput = FigmaAuthInput & {
	/** File key, or a figma.com file/design/board URL. */
	fileKey: string
	/** Return comment markdown as-is when the API supports it. */
	asMd?: boolean
}

export type ListCommentsResult = {
	fileKey: string
	items: Array<FigmaComment>
}

/**
 * List comments on a Figma file. Needs `file_comments:read`.
 * @example
 * import listComments from 'kody:@kody/figma/list-comments'
 * const { items } = await listComments({ fileKey: 'abc123' })
 */
export async function listComments(input: ListCommentsInput): Promise<ListCommentsResult> {
	const fileKey = parseFileKey(input.fileKey)
	const result = await figmaRequest<{ comments?: unknown }>({
		...input,
		operation: 'comments.read',
		path: `/v1/files/${encodeURIComponent(fileKey)}/comments`,
		query: input.asMd === undefined ? undefined : { as_md: input.asMd },
	})
	return {
		fileKey,
		items: asArray(result.body.comments).map((item) => mapComment(fileKey, item)),
	}
}

/**
 * List comments on a Figma file.
 * @example
 * import listComments from 'kody:@kody/figma/list-comments'
 * const result = await listComments({ fileKey: 'abc123' })
 */
export default async function listCommentsEntrypoint(
	params: Partial<ListCommentsInput> & Record<string, unknown> = {},
) {
	const input = requireRecord(params, 'list-comments')
	return listComments({
		fileKey: String(input.fileKey ?? input.fileUrl ?? ''),
		asMd: optionalBoolean(input.asMd, 'asMd'),
		integrationName: optionalString(input.integrationName, 'integrationName'),
		integration: optionalString(input.integration, 'integration'),
		account: optionalString(input.account, 'account'),
		secretName: optionalString(input.secretName, 'secretName'),
		auth: requireAuthMode(input.auth),
	})
}