1 import axios from "axios"
2 import Client, { ClientArgs } from "./client"
3 import {
4 FrameOffset,
5 GetCommentsResult,
6 GetComponentResult,
7 GetComponentSetResult,
8 GetFileComponentSetsResult,
9 GetFileComponentsResult,
10 GetFileNodesResult,
11 GetFileResult,
12 GetFileStylesResult,
13 GetImageFillsResult,
14 GetImageResult,
15 GetProjectFilesResult,
16 GetStyleResult,
17 GetTeamComponentSetsResult,
18 GetTeamComponentsResult,
19 GetTeamProjectsResult,
20 GetTeamStylesResult,
21 GetVersionsResult,
22 NodeType,
23 PostCommentResult,
24 Vector,
25 } from "./types"
26 import { encodeQuery } from "./utils"
27
28 type FigmaArgs = Omit<ClientArgs, "baseURL">
29
30 const FIGMA_BASE_URL = "https://api.figma.com/v1/"
31
32 class Figma {
33 private _api: Client
34
35 constructor({ accessToken, maxRetries = 3 }: FigmaArgs) {
36 this._api = new Client({
37 accessToken,
38 baseURL: FIGMA_BASE_URL,
39 maxRetries,
40 })
41 }
42
43 /**
44 * Get a resource by its URL.
45 */
46 async getResource(url: string) {
47 const response = await axios.get(url)
48
49 if (Math.floor(response.status / 100) !== 2) {
50 throw response.statusText
51 }
52
53 return response.data
54 }
55
56 async getFile(
57 file_key: string,
58 options?: {
59 /** A specific version ID to get. Omitting this will get the current version of the file */
60 version?: string
61 /** If specified, only a subset of the document will be returned corresponding to the nodes listed, their children, and everything between the root node and the listed nodes */
62 ids?: string[]
63 /** Positive integer representing how deep into the document tree to traverse. For example, setting this to 1 returns only Pages, setting it to 2 returns Pages and all top level objects on each page. Not setting this parameter returns all nodes */
64 depth?: number
65 /** Set to "paths" to export vector data */
66 geometry?: "paths"
67 /** A comma separated list of plugin IDs and/or the string "shared". Any data present in the document written by those plugins will be included in the result in the `pluginData` and `sharedPluginData` properties. */
68 plugin_data?: string
69 /** Set to returns branch metadata for the requested file */
70 branch_data?: boolean
71 }
72 ): Promise<GetFileResult> {
73 const queryString = options
74 ? `?${encodeQuery({
75 ...options,
76 ids: options.ids && options.ids.join(","),
77 })}`
78 : ""
79
80 return this._api.request("GET", `files/${file_key}${queryString}`)
81 }
82
83 async getFileNodes<TNode extends NodeType = "DOCUMENT">(
84 file_key: string,
85 options: {
86 /** A comma separated list of node IDs to retrieve and convert */
87 ids: string[]
88 /** A specific version ID to get. Omitting this will get the current version of the file */
89 version?: string
90 /** Positive integer representing how deep into the document tree to traverse. For example, setting this to 1 returns only Pages, setting it to 2 returns Pages and all top level objects on each page. Not setting this parameter returns all nodes */
91 depth?: number
92 /** Set to "paths" to export vector data */
93 geometry?: "paths"
94 /** A comma separated list of plugin IDs and/or the string "shared". Any data present in the document written by those plugins will be included in the result in the `pluginData` and `sharedPluginData` properties. */
95 plugin_data?: string
96 }
97 ): Promise<GetFileNodesResult<TNode>> {
98 const queryString = `?${encodeQuery({
99 ...options,
100 ids: options.ids.join(","),
101 })}`
102
103 return this._api.request("GET", `files/${file_key}/nodes${queryString}`)
104 }
105
106 async getImage(
107 file_key: string,
108 options: {
109 /** A comma separated list of node IDs to render */
110 ids: string[]
111 /** A number between 0.01 and 4, the image scaling factor */
112 scale: number
113 /** A string enum for the image output format */
114 format: "jpg" | "png" | "svg" | "pdf"
115 /** Whether to include id attributes for all SVG elements. `Default: false` */
116 svg_include_id?: boolean
117 /** Whether to simplify inside/outside strokes and use stroke attribute if possible instead of <mask>. `Default: true` */
118 svg_simplify_stroke?: boolean
119 /** Use the full dimensions of the node regardless of whether or not it is cropped or the space around it is empty. Use this to export text nodes without cropping. `Default: false` */
120 use_absolute_bounds?: boolean
121 /** A specific version ID to get. Omitting this will get the current version of the file */
122 version?: string
123 }
124 ): Promise<GetImageResult> {
125 const queryString = options
126 ? `?${encodeQuery({
127 ...options,
128 ids: options.ids && options.ids.join(","),
129 })}`
130 : ""
131
132 return this._api.request("GET", `images/${file_key}${queryString}`)
133 }
134
135 async getImageFills(file_key: string): Promise<GetImageFillsResult> {
136 return this._api.request("GET", `files/${file_key}/images`)
137 }
138
139 async getComments(file_key: string): Promise<GetCommentsResult> {
140 return this._api.request("GET", `files/${file_key}/comments`)
141 }
142
143 async postComment(
144 file_key: string,
145 /** The text contents of the comment to post */
146 message: string,
147 /** The position of where to place the comment. This can either be an absolute canvas position or the relative position within a frame. */
148 client_meta: Vector | FrameOffset,
149 /** (Optional) The comment to reply to, if any. This must be a root comment, that is, you cannot reply to a comment that is a reply itself (a reply has a parent_id). */
150 comment_id?: string
151 ): Promise<PostCommentResult> {
152 return this._api.request("POST", `files/${file_key}/comments`, {
153 message,
154 client_meta,
155 comment_id,
156 })
157 }
158
159 async deleteComment(file_key: string, comment_id: string): Promise<void> {
160 return this._api.request(
161 "DELETE",
162 `files/${file_key}/comments/${comment_id}`
163 )
164 }
165
166 async getVersions(file_key: string): Promise<GetVersionsResult> {
167 return this._api.request("GET", `files/${file_key}/versions`)
168 }
169
170 async getTeamProjects(teamId: string): Promise<GetTeamProjectsResult> {
171 return this._api.request("GET", `teams/${teamId}/projects`)
172 }
173
174 async getProjectFiles(
175 project_id: string,
176 options: {
177 /** Set to returns branch metadata for the requested file */
178 branch_data?: boolean
179 }
180 ): Promise<GetProjectFilesResult> {
181 const queryString = options
182 ? `?${encodeQuery({
183 ...options,
184 })}`
185 : ""
186
187 return this._api.request(
188 "GET",
189 `projects/${project_id}/files${queryString}`
190 )
191 }
192
193 /**
194 * Get a paginated list of published components within a team library
195 */
196 async getTeamComponents(
197 team_id: string,
198 options: {
199 /** Number of items in a paged list of results. Defaults to 30. */
200 page_size?: number
201 /** Cursor indicating which id after which to start retrieving components for. Exclusive with before. The cursor value is an internally tracked integer that doesn't correspond to any Ids */
202 after?: number
203 /** Cursor indicating which id before which to start retrieving components for. Exclusive with after. The cursor value is an internally tracked integer that doesn't correspond to any Ids */
204 before?: number
205 }
206 ): Promise<GetTeamComponentsResult> {
207 const queryString = options ? `?${encodeQuery(options)}` : ""
208
209 return this._api.request("GET", `teams/${team_id}/components${queryString}`)
210 }
211
212 /**
213 * Get a list of published components within a file library
214 */
215 async getFileComponents(file_key: string): Promise<GetFileComponentsResult> {
216 return this._api.request("GET", `files/${file_key}/components`)
217 }
218
219 /**
220 * Get metadata on a component by key.
221 */
222 async getComponent(component_key: string): Promise<GetComponentResult> {
223 return this._api.request("GET", `components/${component_key}`)
224 }
225
226 /**
227 * Get a paginated list of published component_sets within a team library
228 */
229 async getTeamComponentSets(
230 team_id: string,
231 options: {
232 /** Number of items in a paged list of results. Defaults to 30. */
233 page_size?: number
234 /** Cursor indicating which id after which to start retrieving component sets for. Exclusive with before. The cursor value is an internally tracked integer that doesn't correspond to any Ids */
235 after?: number
236 /** Cursor indicating which id before which to start retrieving component sets for. Exclusive with after. The cursor value is an internally tracked integer that doesn't correspond to any Ids */
237 before?: number
238 }
239 ): Promise<GetTeamComponentSetsResult> {
240 const queryString = options ? `?${encodeQuery(options)}` : ""
241
242 return this._api.request(
243 "GET",
244 `teams/${team_id}/component_sets${queryString}`
245 )
246 }
247
248 async getFileComponentSets(
249 file_key: string
250 ): Promise<GetFileComponentSetsResult> {
251 return this._api.request("GET", `files/${file_key}/component_sets`)
252 }
253
254 /**
255 * Get metadata on a component_set by key.
256 */
257 async getComponentSet(key: string): Promise<GetComponentSetResult> {
258 return this._api.request("GET", `component_sets/${key}`)
259 }
260
261 /**
262 * Get a paginated list of published styles within a team library
263 */
264 async getTeamStyles(
265 team_id: string,
266 options: {
267 /** Number of items in a paged list of results. Defaults to 30. */
268 page_size?: number
269 /** Cursor indicating which id after which to start retrieving styles for. Exclusive with before. The cursor value is an internally tracked integer that doesn't correspond to any Ids */
270 after?: number
271 /** Cursor indicating which id before which to start retrieving styles for. Exclusive with after. The cursor value is an internally tracked integer that doesn't correspond to any Ids */
272 before?: number
273 }
274 ): Promise<GetTeamStylesResult> {
275 const queryString = options ? `?${encodeQuery(options)}` : ""
276
277 return this._api.request("GET", `teams/${team_id}/styles${queryString}`)
278 }
279
280 /**
281 * Get a list of published styles within a file library
282 */
283 async getFileStyles(file_key: string): Promise<GetFileStylesResult> {
284 return this._api.request("GET", `files/${file_key}/styles`)
285 }
286
287 /**
288 * Get metadata on a style by key.
289 */
290 async getStyle(key: string): Promise<GetStyleResult> {
291 return this._api.request("GET", `styles/${key}`)
292 }
293
294 // Variables - Beta API (TODO)
295
296 // Webhooks - Beta API (TODO)
297
298 // Activity Logs - Beta API (TODO)
299
300 // Payments - Beta API (TODO)
301
302 // Dev Resources - Beta API (TODO)
303 }
304
305 export * from "./assertions"
306 export * from "./types"
307
308 export default Figma