import axios, { AxiosInstance } from 'axios'; import { FigmaNode } from './types'; export class FigmaClient { private client: AxiosInstance; constructor(private token: string) { this.client = axios.create({ baseURL: 'https://api.figma.com/v1', headers: { 'X-Figma-Token': token, }, }); } async getFile(fileKey: string): Promise { const response = await this.client.get(`/files/${fileKey}`); return response.data; } async getFileNodes(fileKey: string, nodeIds: string[]): Promise { const response = await this.client.get(`/files/${fileKey}/nodes`, { params: { ids: nodeIds.join(',') }, }); return response.data; } async getImages(fileKey: string, nodeIds: string[]): Promise> { const response = await this.client.get(`/images/${fileKey}`, { params: { ids: nodeIds.join(','), format: 'png', scale: 2, }, }); return response.data.images || {}; } extractAllNodes(node: FigmaNode): FigmaNode[] { const nodes: FigmaNode[] = [node]; if (node.children) { for (const child of node.children) { nodes.push(...this.extractAllNodes(child)); } } return nodes; } rgbToHex(r: number, g: number, b: number): string { const toHex = (n: number) => { const hex = Math.round(n * 255).toString(16); return hex.length === 1 ? '0' + hex : hex; }; return `#${toHex(r)}${toHex(g)}${toHex(b)}`.toUpperCase(); } }