forked from danhollick/FigmaAPIThing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
58 lines (44 loc) · 1.52 KB
/
server.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
var express = require('express')
var app = express()
var fetch = require('isomorphic-fetch')
require('dotenv').config();
const FigmaAPIKey = process.env.API_KEY
const FigmaFileID = 'VAyAHaZn1tHmjOFK79pbnMTj'
async function figmaFileFetch(fileId){
let result = await fetch('https://api.figma.com/v1/files/' + fileId , {
method: 'GET',
headers: {
'X-Figma-Token': FigmaAPIKey
}
})
let figmaFileStruct = await result.json()
let figmaFrames = figmaFileStruct.document.children
.filter(child => child.type === 'CANVAS')[0].children
.filter(child => child.type === 'FRAME')
.map(frame => {
return {
name: frame.name,
id: frame.id
}
})
let ids = figmaFrames.map(comp => comp.id).join(',')
let imageResult = await fetch('https://api.figma.com/v1/images/' + fileId + '?scale=2&ids=' + ids, {
method: 'GET',
headers: {
'X-Figma-Token': FigmaAPIKey
}
}).catch(error => console.log(error))
let figmaImages = await imageResult.json()
figmaImages = figmaImages.images
return figmaFrames.map(frame => {
return {
name: frame.name,
url: figmaImages[frame.id]
}
})
}
app.use('/frames', async function (req, res, next) {
let result = await figmaFileFetch(FigmaFileID).catch(error => console.log(error))
res.send(result)
})
app.listen(3001, console.log("Holy shit, I'm a server and I am listening on port 3001"))