Skip to content

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

Package listing

@kody/figma

src/list-file-components.ts

57 lines · 1.9 KB · TypeScript
import { figmaRequest } from './client.ts'
import { mapComponentList } from './models.ts'
import { parseFileKey } from './setup.ts'
import type { FigmaAuthInput, FigmaComponent } from './types.ts'
import { optionalString, requireRecord } from './types.ts'
import { requireAuthMode } from './auth.ts'

export type ListFileComponentsInput = FigmaAuthInput & {
	/** File key, or a figma.com file/design/board URL. */
	fileKey: string
}

export type ListFileComponentsResult = {
	fileKey: string
	items: Array<FigmaComponent>
}

/**
 * List published components in a Figma file. Needs `library_content:read`.
 * @example
 * import listFileComponents from 'kody:@kody/figma/list-file-components'
 * const { items } = await listFileComponents({ fileKey: 'abc123' })
 */
export async function listFileComponents(
	input: ListFileComponentsInput,
): Promise<ListFileComponentsResult> {
	const fileKey = parseFileKey(input.fileKey)
	const result = await figmaRequest<unknown>({
		...input,
		operation: 'library.read',
		path: `/v1/files/${encodeURIComponent(fileKey)}/components`,
	})
	return {
		fileKey,
		items: mapComponentList(result.body),
	}
}

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