Skip to content

Commit 07d7ad2

Browse files
committed
1/7 update
1 parent 5bc1798 commit 07d7ad2

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed
53.7 KB
Loading
52.9 KB
Loading
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
---
2+
categories: [Portfolio]
3+
layout: post
4+
title: Figma API를 사용한 디자인 엘리먼트 가져오기
5+
subtitle: Figma API 사용법
6+
tags: [POST]
7+
author: Young
8+
comments: True
9+
---
10+
11+
## Figma API 사용하여 디자인 엘리먼트 가져오기
12+
13+
지난 시간 디자인 시스템에 관련된 사항을 정리하였는데
14+
15+
이 때 이야기 하였던 Figma API를 사용하여 디자인 산출물을 HTML 코드로 변환해주는 작업을 해보려 합니다.
16+
17+
## 만들어보자!
18+
19+
Figma API를 사용하기 위해서는 Figma 계정의 토큰이 필요합니다.
20+
21+
![세팅](2024-01-07-23-50-19.png)
22+
23+
Fimga의 계정에서 Settings 에 들어갑니다.
24+
25+
![](2024-01-07-23-51-19.png)
26+
27+
아래 처럼 Personal Access token을 발행할 수 있습니다.
28+
29+
```javascript
30+
const fileKey = 'file key';
31+
const personalAccessToken = 'personal access token';
32+
33+
// Figma API 엔드포인트
34+
const url = `https://api.figma.com/v1/files/${fileKey}`;
35+
36+
// Define your API endpoint
37+
router.get('/design', async (req, res) => {
38+
try {
39+
console.log('call!');
40+
// Make a request to the Figma API to fetch design elements
41+
const response = await axios.get(url, {
42+
headers: {
43+
'X-Figma-Token': personalAccessToken
44+
}
45+
});
46+
47+
const data = response.data;
48+
// targetFrame 찾기
49+
const targetChildren = data.document.children;
50+
const targetPage = targetChildren.find((page) => page.name === '원하는 페이지');
51+
const targetFrame = targetPage.children.find((frame) => frame.name === '원하는 프레임');
52+
const instanceGroup = targetFrame.children.filter(ele => ele.type === 'INSTANCE').map(ele => ele.name);
53+
54+
// instanceGroup 의 각각을 html 로 변환해주기
55+
const html = convertHTML(instanceGroup.reverse());
56+
fs.writeFile('./src/html/myNewFile.html', html, (err) => {
57+
if (err) throw err;
58+
console.log('The file has been saved!');
59+
});
60+
res.send('html make success!');
61+
62+
} catch (error) {
63+
// Handle any errors that occur during the request
64+
console.error(error);
65+
res.status(500).json({ error: 'An error occurred' });
66+
}
67+
});
68+
```
69+
70+
위와 같이 node로 REST API를 만들어서 사용하였습니다.
71+
72+
/design이라는 경로로 들어오면 내가 원하는 페이지의 원하는 프레임의 엘리먼트 정보를 name으로 받아서
73+
74+
해당하는 name 값들을 디자인 시스템에 정한 html로 변환하여
75+
뭉쳐주는 작업을 진행합니다.
76+
77+
뭉쳐준 html 결과물을 .html 파일로 저장 한 후 확인하면
78+
79+
동일한 결과물이 나오는 것을 확인할 수 있습니다.
80+
81+
다만, 여기서는 name값만 가져오도록 만들었는데
82+
83+
점차, 추가적으로 property 값 등을 가져와서 수정할 수 있도록 해야겠습니다.
84+

0 commit comments

Comments
 (0)