Skip to content

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

Package listing

@kody/figma

src/delete-comment.ts

70 lines · 2.5 KB · TypeScript
import { figmaRequest } from './client.ts'
import { parseFileKey } from './setup.ts'
import type { DryRunResult, FigmaAuthInput } from './types.ts'
import { optionalBoolean, optionalString, requireRecord, requireString } from './types.ts'
import { requireAuthMode } from './auth.ts'

export type DeleteCommentInput = FigmaAuthInput & {
	/** File key, or a figma.com file/design/board URL. */
	fileKey: string
	commentId: string
	confirm?: boolean
	dryRun?: boolean
}

/**
 * Delete a Figma file comment. Requires `confirm: true`, or use `dryRun: true`.
 * Needs `file_comments:write`.
 * @example
 * import deleteComment from 'kody:@kody/figma/delete-comment'
 * const preview = await deleteComment({
 *   fileKey: 'abc123',
 *   commentId: '456',
 *   dryRun: true,
 * })
 */
export async function deleteComment(
	input: DeleteCommentInput,
): Promise<{ deleted: true; commentId: string } | DryRunResult<{ method: string; path: string }>> {
	const fileKey = parseFileKey(input.fileKey)
	const commentId = requireString(input.commentId, 'commentId')
	const path = `/v1/files/${encodeURIComponent(fileKey)}/comments/${encodeURIComponent(commentId)}`
	if (input.dryRun) {
		return { dryRun: true, wouldCall: { method: 'DELETE', path } }
	}
	if (input.confirm !== true) {
		throw new Error(
			'Deleting a Figma comment requires confirm: true after explicit user approval, or dryRun: true.',
		)
	}
	await figmaRequest({
		...input,
		operation: 'comments.write',
		method: 'DELETE',
		path,
	})
	return { deleted: true, commentId }
}

/**
 * Delete a Figma file comment. Requires `confirm: true`, or use `dryRun: true`.
 * @example
 * import deleteComment from 'kody:@kody/figma/delete-comment'
 * const preview = await deleteComment({ fileKey: 'abc123', commentId: '456', dryRun: true })
 */
export default async function deleteCommentEntrypoint(
	params: Partial<DeleteCommentInput> & Record<string, unknown> = {},
) {
	const input = requireRecord(params, 'delete-comment')
	return deleteComment({
		fileKey: String(input.fileKey ?? input.fileUrl ?? ''),
		commentId: requireString(input.commentId, 'commentId'),
		integrationName: optionalString(input.integrationName, 'integrationName'),
		integration: optionalString(input.integration, 'integration'),
		account: optionalString(input.account, 'account'),
		secretName: optionalString(input.secretName, 'secretName'),
		auth: requireAuthMode(input.auth),
		confirm: optionalBoolean(input.confirm, 'confirm'),
		dryRun: optionalBoolean(input.dryRun, 'dryRun'),
	})
}