-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.js
63 lines (53 loc) · 1.62 KB
/
api.js
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
const fetch = require('node-fetch');
const baseUrl = 'https://api.figma.com';
module.exports = {
loadCanvas,
loadURLImages,
loadImages,
loadNodes,
getHeaders
};
function getHeaders(devToken) {
const headers = new fetch.Headers();
headers.append('X-Figma-Token', devToken);
return headers;
}
async function loadCanvas(fileKey, headers) {
let resp = await fetch(`${baseUrl}/v1/files/${fileKey}?geometry=paths`, { headers });
let data = await resp.json();
const document = data.document;
const canvas = document.children[0];
return canvas;
}
async function loadNodes(ids, fileKey, headers) {
let resp = await fetch(`${baseUrl}/v1/files/${fileKey}/nodes?geometry=paths&ids=${ids.join(',')}`, { headers });
let data = await resp.json();
return data.nodes;
}
async function loadURLImages(vectorList, fileKey, headers) {
const guids = vectorList.join(',');
data = await fetch(`${baseUrl}/v1/images/${fileKey}?ids=${guids}&format=svg`, { headers });
return await data.json();
}
async function loadImages(imageJSON) {
const images = imageJSON.images || {};
if (images) {
let promises = [];
let guids = [];
for (const guid in images) {
if (images[guid] == null) continue;
guids.push(guid);
promises.push(fetch(images[guid]));
}
let responses = await Promise.all(promises);
promises = [];
for (const resp of responses) {
promises.push(resp.text());
}
responses = await Promise.all(promises);
for (let i = 0; i < responses.length; i++) {
images[guids[i]] = responses[i].replace('<svg ', '<svg preserveAspectRatio="none" ');
}
}
return images;
}