-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.js
153 lines (136 loc) · 4.15 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
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
149
150
151
152
153
const fetch = require('node-fetch');
const { colorString, getElementParams } = require('./lib');
const baseUrl = 'https://api.figma.com';
module.exports = {
loadCanvas,
loadVectorListImages,
loadVectors,
loadNodes,
loadRefImages,
loadListImages,
loadNodeImages,
getHeaders
};
function getHeaders(devToken) {
const headers = new fetch.Headers();
headers.append('X-Figma-Token', devToken);
return headers;
}
async function loadCanvas(fileKey, headers) {
const resp = await fetch(`${baseUrl}/v1/files/${fileKey}?geometry=paths`, { headers });
const data = await resp.json();
const document = data.document;
if (data.err) {
throw new Error(data.err);
}
const canvas = document.children[0];
return canvas;
}
async function loadNodes(ids, fileKey, headers) {
if (ids.length > 0) {
const resp = await fetch(`${baseUrl}/v1/files/${fileKey}/nodes?geometry=paths&ids=${ids.join(',')}`, { headers });
const data = await resp.json();
if (data.err) {
throw new Error(data.err);
}
return data.nodes;
} else {
return {};
}
}
async function loadNodeImages({ imageMap, fileKey, headers, options }) {
const { imageScale, imageFormat } = options;
if (Object.keys(imageMap).length > 0) {
const guids = Object.keys(imageMap).join(',');
const resp = await fetch(
`${baseUrl}/v1/images/${fileKey}?ids=${guids}&use_absolute_bounds=true&format=${imageFormat}&scale=${imageScale}`,
{
headers
}
);
const data = await resp.json();
if (data.err) {
throw new Error(data.err);
}
return data.images || {};
} else {
return {};
}
}
async function loadRefImages({ fileKey, headers }) {
const resp = await fetch(`${baseUrl}/v1/files/${fileKey}/images`, { headers });
const data = await resp.json();
if (data.err) {
throw new Error(data.err);
}
return data.meta.images || {};
}
async function loadListImages({ fileKey, headers, options }, guids, format = 'svg', absolute = false, scale = null) {
if (guids.length > 0) {
const { imageScale } = options;
scale = scale || imageScale;
const resp = await fetch(
`${baseUrl}/v1/images/${fileKey}?ids=${guids}&scale=${format === 'svg' ? 1 : imageScale}&format=${format}${
absolute ? '&use_absolute_bounds=true' : ''
}`,
{
headers
}
);
const data = await resp.json();
if (data.err) {
throw new Error(data.err);
}
return data.images || {};
} else {
return {};
}
}
async function loadVectorListImages(shared, format = 'svg', absolute = false) {
const { vectorMap } = shared;
return loadListImages(shared, Object.keys(vectorMap).join(','), format, absolute);
}
async function loadVectors(shared) {
const { headers } = shared;
const vectors = await loadVectorListImages(shared, 'svg', true);
const vectorsRelative = await loadVectorListImages(shared, 'svg', false);
let promises = [];
const guids = [];
for (const guid in vectors) {
if (vectors[guid] == null) vectors[guid] = vectorsRelative[guid];
if (vectors[guid] == null) continue;
guids.push(guid);
promises.push(fetch(vectors[guid], { headers }));
}
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++) {
vectors[guids[i]] = responses[i].replace('<svg ', '<svg preserveAspectRatio="none" ');
}
// for (const guid in vectorMap) {
// if (!vectors[guid]) {
// const node = vectorMap[guid];
// let svg = '<svg preserveAspectRatio="none">';
// let color = '#ffffff';
// if (node.fills && node.fills.length > 0) {
// for (const fill of node.fills) {
// if (fill.type === 'SOLID') {
// color = colorString(fill.color);
// }
// }
// }
// if (node.fillGeometry && node.fillGeometry.length > 0) {
// for (const fill of node.fillGeometry) {
// svg += `<path d="${fill.path}" fill="${color}"></path>`;
// }
// }
// svg += '</svg>';
// vectors[guid] = svg;
// }
// }
return vectors;
}