figma.ts
3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import { FigmaFrameInfo, FigmaImageFormat, FigmaColorObject } from '../../types/figma-types';
import { get } from '../../utils/axios';
import { logger } from '../../utils/logger';
/**
* Fetch Figma nodes by fileId and nodeId
* @param fileId - Figma file ID
* @param nodeId - Figma node ID
* @param token - Figma API token
* @returns Figma frame information
*/
export const fetchFigmaNode = async (fileId: string, nodeId: string, token: string): Promise<FigmaFrameInfo | undefined> => {
const url = `https://api.figma.com/v1/files/${fileId}/nodes?ids=${nodeId}`;
try {
const data = await get<{ nodes: Record<string, { document: FigmaFrameInfo }> }>(url, {
headers: {
'X-Figma-Token': token,
},
});
// format node id to match the format in the response
const resData = data.nodes?.[nodeId];
return resData?.document;
} catch (error) {
logger.printErrorLog(`Failed to fetch Figma node: ${error instanceof Error ? error.message : 'Unknown error'}`);
return undefined;
}
};
/**
* Fetch Figma image by fileId and nodeId
* @param fileId - Figma file ID
* @param nodeIds - Figma node ID, multiple node ids can be passed separated by commas
* @param token - Figma API token
* @param format - Figma image format
* @returns Figma image
*/
export const fetchFigmaImages = async (
fileId: string,
nodeIds: string,
token: string,
format?: FigmaImageFormat
): Promise<Record<string, string>> => {
const url = `https://api.figma.com/v1/images/${fileId}`;
try {
const data = await get<{ images: Record<string, string> }>(url, {
headers: {
'X-Figma-Token': token,
},
params: {
ids: nodeIds,
format: format || 'png',
},
});
const images = data.images || {};
// format node id to match the format from response to request
return Object.fromEntries(Object.entries(images));
} catch (error) {
logger.printErrorLog(`Failed to fetch Figma images: ${error instanceof Error ? error.message : 'Unknown error'}`);
return {};
}
};
/**
* Clean Figma node and children. Remove invisible nodes and children.
* @param node - Figma node or children
* @returns Cleaned Figma node or children. If the node is invisible, return null.
*/
export const cleanFigma = (node: FigmaFrameInfo): FigmaFrameInfo | undefined => {
// if node is invisible, return undefined
if (node.visible === false) {
return undefined;
}
// if node has children, recursively clean each child
if (node.children && Array.isArray(node.children)) {
node.children = node.children
.map(child => cleanFigma(child)) // recursively clean each child
.filter(child => child !== undefined); // filter out invisible nodes
}
return node;
};
/**
* Check if node has border
* @param node - Figma node
* @returns True if node has border, false otherwise
*/
export const checkBorder = (node: FigmaFrameInfo): boolean => {
const strokes = node.strokes;
const strokeWeight = node.strokeWeight;
if (!strokes || !strokes.length || !strokeWeight) return false;
const visibleStrokes = strokes.filter((s: FigmaColorObject) => s.visible !== false);
if (visibleStrokes.length === 0) return false;
return true;
};