-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfigma_api.ts
148 lines (136 loc) · 3.83 KB
/
figma_api.ts
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
function getFigmaApiKey() {
const apiKey = process.env.FIGMA_API_KEY;
if (!apiKey) {
throw new Error("FIGMA_API_KEY is not set");
}
return apiKey;
}
export function parseKeyFromUrl(url: string) {
// Extract key from URLs like:
// https://www.figma.com/board/vJzJ1oVCzowAKAayQJx6Ug/...
// https://www.figma.com/design/8SvxepW26v4d0AyyTAw23c/...
// https://www.figma.com/file/8SvxepW26v4d0AyyTAw23c/...
const matches = url.match(/figma\.com\/(board|design|file)\/([^/?]+)/);
if (matches) {
return matches[2]; // Return the second capture group which contains the key
}
throw new Error("Could not parse Figma key from URL");
}
type FigNode = {
id: string;
name: string;
type: string;
children?: FigNode[];
};
type FigFile = {
name: string;
version: string;
document: FigNode;
thumbnailUrl: string;
thumbnailB64: string;
};
export function getCanvasIds(figFileJson: FigNode) {
const canvasIds: string[] = [];
const queue: FigNode[] = [figFileJson];
while (queue.length > 0) {
const node = queue.shift()!;
if (node.type === "CANVAS") {
canvasIds.push(node.id);
continue; // Skip children of canvases
}
if (node.children) {
queue.push(...node.children);
}
}
return canvasIds;
}
export async function downloadFigmaFile(key: string): Promise<FigFile> {
const response = await fetch(`https://api.figma.com/v1/files/${key}`, {
headers: {
"X-FIGMA-TOKEN": getFigmaApiKey(),
},
});
const data = await response.json();
return {
...data,
thumbnailB64: await imageUrlToBase64(data.thumbnailUrl),
};
}
export async function getThumbnails(key: string, ids: string[]): Promise<{ [id: string]: string }> {
const thumbnails = await fetch(
`https://api.figma.com/v1/images/${key}?ids=${ids.join(",")}&format=png&page_size=1`,
{
headers: {
"X-FIGMA-TOKEN": getFigmaApiKey(),
},
}
);
const data = (await thumbnails.json()) as { images: { [id: string]: string }; err?: string };
if (data.err) {
throw new Error(`Error getting thumbnails: ${data.err}`);
}
return data.images;
}
export async function getThumbnailsOfCanvases(
key: string,
document: FigNode
): Promise<{ id: string; url: string; b64: string }[]> {
const canvasIds = getCanvasIds(document);
const thumbnails = await getThumbnails(key, canvasIds);
const results = [];
for (const [id, url] of Object.entries(thumbnails)) {
results.push({
id,
url,
b64: await imageUrlToBase64(url),
});
}
return results;
}
export async function readComments(fileKey: string) {
const response = await fetch(`https://api.figma.com/v1/files/${fileKey}/comments`, {
headers: {
"X-FIGMA-TOKEN": getFigmaApiKey(),
},
});
return await response.json();
}
export async function postComment(
fileKey: string,
message: string,
x: number,
y: number,
nodeId?: string
) {
const response = await fetch(`https://api.figma.com/v1/files/${fileKey}/comments`, {
method: "POST",
headers: {
"X-FIGMA-TOKEN": getFigmaApiKey(),
"Content-Type": "application/json",
},
body: JSON.stringify({
message,
client_meta: { node_offset: { x, y }, node_id: nodeId },
}),
});
return await response.json();
}
export async function replyToComment(fileKey: string, commentId: string, message: string) {
const response = await fetch(`https://api.figma.com/v1/files/${fileKey}/comments`, {
method: "POST",
headers: {
"X-FIGMA-TOKEN": getFigmaApiKey(),
"Content-Type": "application/json",
},
body: JSON.stringify({
message,
comment_id: commentId,
}),
});
return await response.json();
}
async function imageUrlToBase64(url: string) {
const response = await fetch(url);
const arrayBuffer = await response.arrayBuffer();
return Buffer.from(arrayBuffer).toString("base64");
}