@mp3wizard/figma-console-mcp 1.14.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (201) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +816 -0
  3. package/dist/apps/design-system-dashboard/scoring/accessibility.d.ts +14 -0
  4. package/dist/apps/design-system-dashboard/scoring/accessibility.d.ts.map +1 -0
  5. package/dist/apps/design-system-dashboard/scoring/accessibility.js +278 -0
  6. package/dist/apps/design-system-dashboard/scoring/accessibility.js.map +1 -0
  7. package/dist/apps/design-system-dashboard/scoring/component-metadata.d.ts +29 -0
  8. package/dist/apps/design-system-dashboard/scoring/component-metadata.d.ts.map +1 -0
  9. package/dist/apps/design-system-dashboard/scoring/component-metadata.js +358 -0
  10. package/dist/apps/design-system-dashboard/scoring/component-metadata.js.map +1 -0
  11. package/dist/apps/design-system-dashboard/scoring/consistency.d.ts +14 -0
  12. package/dist/apps/design-system-dashboard/scoring/consistency.d.ts.map +1 -0
  13. package/dist/apps/design-system-dashboard/scoring/consistency.js +342 -0
  14. package/dist/apps/design-system-dashboard/scoring/consistency.js.map +1 -0
  15. package/dist/apps/design-system-dashboard/scoring/coverage.d.ts +14 -0
  16. package/dist/apps/design-system-dashboard/scoring/coverage.d.ts.map +1 -0
  17. package/dist/apps/design-system-dashboard/scoring/coverage.js +231 -0
  18. package/dist/apps/design-system-dashboard/scoring/coverage.js.map +1 -0
  19. package/dist/apps/design-system-dashboard/scoring/engine.d.ts +27 -0
  20. package/dist/apps/design-system-dashboard/scoring/engine.d.ts.map +1 -0
  21. package/dist/apps/design-system-dashboard/scoring/engine.js +93 -0
  22. package/dist/apps/design-system-dashboard/scoring/engine.js.map +1 -0
  23. package/dist/apps/design-system-dashboard/scoring/naming-semantics.d.ts +14 -0
  24. package/dist/apps/design-system-dashboard/scoring/naming-semantics.d.ts.map +1 -0
  25. package/dist/apps/design-system-dashboard/scoring/naming-semantics.js +309 -0
  26. package/dist/apps/design-system-dashboard/scoring/naming-semantics.js.map +1 -0
  27. package/dist/apps/design-system-dashboard/scoring/token-architecture.d.ts +14 -0
  28. package/dist/apps/design-system-dashboard/scoring/token-architecture.d.ts.map +1 -0
  29. package/dist/apps/design-system-dashboard/scoring/token-architecture.js +350 -0
  30. package/dist/apps/design-system-dashboard/scoring/token-architecture.js.map +1 -0
  31. package/dist/apps/design-system-dashboard/scoring/types.d.ts +89 -0
  32. package/dist/apps/design-system-dashboard/scoring/types.d.ts.map +1 -0
  33. package/dist/apps/design-system-dashboard/scoring/types.js +41 -0
  34. package/dist/apps/design-system-dashboard/scoring/types.js.map +1 -0
  35. package/dist/apps/design-system-dashboard/server.d.ts +24 -0
  36. package/dist/apps/design-system-dashboard/server.d.ts.map +1 -0
  37. package/dist/apps/design-system-dashboard/server.js +160 -0
  38. package/dist/apps/design-system-dashboard/server.js.map +1 -0
  39. package/dist/apps/token-browser/server.d.ts +26 -0
  40. package/dist/apps/token-browser/server.d.ts.map +1 -0
  41. package/dist/apps/token-browser/server.js +137 -0
  42. package/dist/apps/token-browser/server.js.map +1 -0
  43. package/dist/browser/base.d.ts +58 -0
  44. package/dist/browser/base.d.ts.map +1 -0
  45. package/dist/browser/base.js +6 -0
  46. package/dist/browser/base.js.map +1 -0
  47. package/dist/browser/local.d.ts +87 -0
  48. package/dist/browser/local.d.ts.map +1 -0
  49. package/dist/browser/local.js +318 -0
  50. package/dist/browser/local.js.map +1 -0
  51. package/dist/cloudflare/apps/design-system-dashboard/scoring/accessibility.js +277 -0
  52. package/dist/cloudflare/apps/design-system-dashboard/scoring/component-metadata.js +357 -0
  53. package/dist/cloudflare/apps/design-system-dashboard/scoring/consistency.js +341 -0
  54. package/dist/cloudflare/apps/design-system-dashboard/scoring/coverage.js +230 -0
  55. package/dist/cloudflare/apps/design-system-dashboard/scoring/engine.js +92 -0
  56. package/dist/cloudflare/apps/design-system-dashboard/scoring/naming-semantics.js +308 -0
  57. package/dist/cloudflare/apps/design-system-dashboard/scoring/token-architecture.js +349 -0
  58. package/dist/cloudflare/apps/design-system-dashboard/scoring/types.js +40 -0
  59. package/dist/cloudflare/apps/design-system-dashboard/server.js +159 -0
  60. package/dist/cloudflare/apps/token-browser/server.js +136 -0
  61. package/dist/cloudflare/browser/base.js +5 -0
  62. package/dist/cloudflare/browser/cloudflare.js +156 -0
  63. package/dist/cloudflare/browser-manager.js +157 -0
  64. package/dist/cloudflare/core/cloud-websocket-connector.js +267 -0
  65. package/dist/cloudflare/core/cloud-websocket-relay.js +199 -0
  66. package/dist/cloudflare/core/comment-tools.js +292 -0
  67. package/dist/cloudflare/core/config.js +161 -0
  68. package/dist/cloudflare/core/console-monitor.js +427 -0
  69. package/dist/cloudflare/core/design-code-tools.js +2504 -0
  70. package/dist/cloudflare/core/design-system-manifest.js +260 -0
  71. package/dist/cloudflare/core/design-system-tools.js +863 -0
  72. package/dist/cloudflare/core/enrichment/enrichment-service.js +272 -0
  73. package/dist/cloudflare/core/enrichment/index.js +7 -0
  74. package/dist/cloudflare/core/enrichment/relationship-mapper.js +351 -0
  75. package/dist/cloudflare/core/enrichment/style-resolver.js +326 -0
  76. package/dist/cloudflare/core/figma-api.js +409 -0
  77. package/dist/cloudflare/core/figma-connector.js +7 -0
  78. package/dist/cloudflare/core/figma-desktop-connector.js +1184 -0
  79. package/dist/cloudflare/core/figma-reconstruction-spec.js +402 -0
  80. package/dist/cloudflare/core/figma-style-extractor.js +311 -0
  81. package/dist/cloudflare/core/figma-tools.js +2947 -0
  82. package/dist/cloudflare/core/logger.js +53 -0
  83. package/dist/cloudflare/core/port-discovery.js +282 -0
  84. package/dist/cloudflare/core/snippet-injector.js +96 -0
  85. package/dist/cloudflare/core/types/design-code.js +4 -0
  86. package/dist/cloudflare/core/types/enriched.js +5 -0
  87. package/dist/cloudflare/core/types/index.js +4 -0
  88. package/dist/cloudflare/core/websocket-connector.js +256 -0
  89. package/dist/cloudflare/core/websocket-server.js +646 -0
  90. package/dist/cloudflare/core/write-tools.js +2091 -0
  91. package/dist/cloudflare/index.js +2899 -0
  92. package/dist/cloudflare/test-browser.js +88 -0
  93. package/dist/core/comment-tools.d.ts +11 -0
  94. package/dist/core/comment-tools.d.ts.map +1 -0
  95. package/dist/core/comment-tools.js +293 -0
  96. package/dist/core/comment-tools.js.map +1 -0
  97. package/dist/core/config.d.ts +17 -0
  98. package/dist/core/config.d.ts.map +1 -0
  99. package/dist/core/config.js +162 -0
  100. package/dist/core/config.js.map +1 -0
  101. package/dist/core/console-monitor.d.ts +82 -0
  102. package/dist/core/console-monitor.d.ts.map +1 -0
  103. package/dist/core/console-monitor.js +428 -0
  104. package/dist/core/console-monitor.js.map +1 -0
  105. package/dist/core/design-code-tools.d.ts +127 -0
  106. package/dist/core/design-code-tools.d.ts.map +1 -0
  107. package/dist/core/design-code-tools.js +2505 -0
  108. package/dist/core/design-code-tools.js.map +1 -0
  109. package/dist/core/design-system-manifest.d.ts +272 -0
  110. package/dist/core/design-system-manifest.d.ts.map +1 -0
  111. package/dist/core/design-system-manifest.js +261 -0
  112. package/dist/core/design-system-manifest.js.map +1 -0
  113. package/dist/core/design-system-tools.d.ts +17 -0
  114. package/dist/core/design-system-tools.d.ts.map +1 -0
  115. package/dist/core/design-system-tools.js +864 -0
  116. package/dist/core/design-system-tools.js.map +1 -0
  117. package/dist/core/enrichment/enrichment-service.d.ts +52 -0
  118. package/dist/core/enrichment/enrichment-service.d.ts.map +1 -0
  119. package/dist/core/enrichment/enrichment-service.js +273 -0
  120. package/dist/core/enrichment/enrichment-service.js.map +1 -0
  121. package/dist/core/enrichment/index.d.ts +8 -0
  122. package/dist/core/enrichment/index.d.ts.map +1 -0
  123. package/dist/core/enrichment/index.js +8 -0
  124. package/dist/core/enrichment/index.js.map +1 -0
  125. package/dist/core/enrichment/relationship-mapper.d.ts +106 -0
  126. package/dist/core/enrichment/relationship-mapper.d.ts.map +1 -0
  127. package/dist/core/enrichment/relationship-mapper.js +352 -0
  128. package/dist/core/enrichment/relationship-mapper.js.map +1 -0
  129. package/dist/core/enrichment/style-resolver.d.ts +80 -0
  130. package/dist/core/enrichment/style-resolver.d.ts.map +1 -0
  131. package/dist/core/enrichment/style-resolver.js +327 -0
  132. package/dist/core/enrichment/style-resolver.js.map +1 -0
  133. package/dist/core/figma-api.d.ts +201 -0
  134. package/dist/core/figma-api.d.ts.map +1 -0
  135. package/dist/core/figma-api.js +410 -0
  136. package/dist/core/figma-api.js.map +1 -0
  137. package/dist/core/figma-connector.d.ts +48 -0
  138. package/dist/core/figma-connector.d.ts.map +1 -0
  139. package/dist/core/figma-connector.js +8 -0
  140. package/dist/core/figma-connector.js.map +1 -0
  141. package/dist/core/figma-desktop-connector.d.ts +265 -0
  142. package/dist/core/figma-desktop-connector.d.ts.map +1 -0
  143. package/dist/core/figma-desktop-connector.js +1184 -0
  144. package/dist/core/figma-desktop-connector.js.map +1 -0
  145. package/dist/core/figma-reconstruction-spec.d.ts +166 -0
  146. package/dist/core/figma-reconstruction-spec.d.ts.map +1 -0
  147. package/dist/core/figma-reconstruction-spec.js +403 -0
  148. package/dist/core/figma-reconstruction-spec.js.map +1 -0
  149. package/dist/core/figma-style-extractor.d.ts +76 -0
  150. package/dist/core/figma-style-extractor.d.ts.map +1 -0
  151. package/dist/core/figma-style-extractor.js +312 -0
  152. package/dist/core/figma-style-extractor.js.map +1 -0
  153. package/dist/core/figma-tools.d.ts +23 -0
  154. package/dist/core/figma-tools.d.ts.map +1 -0
  155. package/dist/core/figma-tools.js +2948 -0
  156. package/dist/core/figma-tools.js.map +1 -0
  157. package/dist/core/logger.d.ts +22 -0
  158. package/dist/core/logger.d.ts.map +1 -0
  159. package/dist/core/logger.js +54 -0
  160. package/dist/core/logger.js.map +1 -0
  161. package/dist/core/port-discovery.d.ts +110 -0
  162. package/dist/core/port-discovery.d.ts.map +1 -0
  163. package/dist/core/port-discovery.js +283 -0
  164. package/dist/core/port-discovery.js.map +1 -0
  165. package/dist/core/snippet-injector.d.ts +24 -0
  166. package/dist/core/snippet-injector.d.ts.map +1 -0
  167. package/dist/core/snippet-injector.js +97 -0
  168. package/dist/core/snippet-injector.js.map +1 -0
  169. package/dist/core/types/design-code.d.ts +262 -0
  170. package/dist/core/types/design-code.d.ts.map +1 -0
  171. package/dist/core/types/design-code.js +5 -0
  172. package/dist/core/types/design-code.js.map +1 -0
  173. package/dist/core/types/enriched.d.ts +213 -0
  174. package/dist/core/types/enriched.d.ts.map +1 -0
  175. package/dist/core/types/enriched.js +6 -0
  176. package/dist/core/types/enriched.js.map +1 -0
  177. package/dist/core/types/index.d.ts +112 -0
  178. package/dist/core/types/index.d.ts.map +1 -0
  179. package/dist/core/types/index.js +5 -0
  180. package/dist/core/types/index.js.map +1 -0
  181. package/dist/core/websocket-connector.d.ts +55 -0
  182. package/dist/core/websocket-connector.d.ts.map +1 -0
  183. package/dist/core/websocket-connector.js +257 -0
  184. package/dist/core/websocket-connector.js.map +1 -0
  185. package/dist/core/websocket-server.d.ts +191 -0
  186. package/dist/core/websocket-server.d.ts.map +1 -0
  187. package/dist/core/websocket-server.js +647 -0
  188. package/dist/core/websocket-server.js.map +1 -0
  189. package/dist/core/write-tools.d.ts +7 -0
  190. package/dist/core/write-tools.d.ts.map +1 -0
  191. package/dist/core/write-tools.js +2092 -0
  192. package/dist/core/write-tools.js.map +1 -0
  193. package/dist/local.d.ts +84 -0
  194. package/dist/local.d.ts.map +1 -0
  195. package/dist/local.js +5039 -0
  196. package/dist/local.js.map +1 -0
  197. package/figma-desktop-bridge/README.md +313 -0
  198. package/figma-desktop-bridge/code.js +2818 -0
  199. package/figma-desktop-bridge/manifest.json +67 -0
  200. package/figma-desktop-bridge/ui.html +1236 -0
  201. package/package.json +87 -0
@@ -0,0 +1,409 @@
1
+ /**
2
+ * Figma REST API Client
3
+ * Handles HTTP calls to Figma's REST API for file data, variables, components, and styles
4
+ */
5
+ import { createChildLogger } from './logger.js';
6
+ const logger = createChildLogger({ component: 'figma-api' });
7
+ const FIGMA_API_BASE = 'https://api.figma.com/v1';
8
+ /**
9
+ * Extract file key from Figma URL
10
+ * @example https://www.figma.com/design/abc123/My-File -> abc123
11
+ */
12
+ export function extractFileKey(url) {
13
+ try {
14
+ const urlObj = new URL(url);
15
+ // Match patterns like /design/FILE_KEY or /file/FILE_KEY
16
+ const match = urlObj.pathname.match(/\/(design|file)\/([a-zA-Z0-9]+)/);
17
+ return match ? match[2] : null;
18
+ }
19
+ catch (error) {
20
+ logger.error({ error, url }, 'Failed to extract file key from URL');
21
+ return null;
22
+ }
23
+ }
24
+ /**
25
+ * Extract comprehensive URL info including branch and node IDs
26
+ * Supports both URL formats:
27
+ * - Path-based: /design/{fileKey}/branch/{branchKey}/{fileName}
28
+ * - Query-based: /design/{fileKey}/{fileName}?branch-id={branchId}
29
+ *
30
+ * @example https://www.figma.com/design/abc123/branch/xyz789/My-File?node-id=1-2
31
+ * -> { fileKey: 'abc123', branchId: 'xyz789', nodeId: '1:2' }
32
+ * @example https://www.figma.com/design/abc123/My-File?branch-id=xyz789&node-id=1-2
33
+ * -> { fileKey: 'abc123', branchId: 'xyz789', nodeId: '1:2' }
34
+ */
35
+ export function extractFigmaUrlInfo(url) {
36
+ try {
37
+ const urlObj = new URL(url);
38
+ // First try: Path-based branch format /design/{fileKey}/branch/{branchKey}/{fileName}
39
+ const branchPathMatch = urlObj.pathname.match(/\/(design|file)\/([a-zA-Z0-9]+)\/branch\/([a-zA-Z0-9]+)/);
40
+ if (branchPathMatch) {
41
+ const fileKey = branchPathMatch[2];
42
+ const branchId = branchPathMatch[3];
43
+ const nodeIdParam = urlObj.searchParams.get('node-id');
44
+ const nodeId = nodeIdParam ? nodeIdParam.replace(/-/g, ':') : undefined;
45
+ return { fileKey, branchId, nodeId };
46
+ }
47
+ // Second try: Standard format /design/{fileKey}/{fileName} with optional ?branch-id=
48
+ const standardMatch = urlObj.pathname.match(/\/(design|file)\/([a-zA-Z0-9]+)/);
49
+ if (!standardMatch)
50
+ return null;
51
+ const fileKey = standardMatch[2];
52
+ const branchId = urlObj.searchParams.get('branch-id') || undefined;
53
+ const nodeIdParam = urlObj.searchParams.get('node-id');
54
+ // Convert node-id from URL format (1-2) to Figma format (1:2)
55
+ const nodeId = nodeIdParam ? nodeIdParam.replace(/-/g, ':') : undefined;
56
+ return { fileKey, branchId, nodeId };
57
+ }
58
+ catch (error) {
59
+ logger.error({ error, url }, 'Failed to extract Figma URL info');
60
+ return null;
61
+ }
62
+ }
63
+ /**
64
+ * Wrap a promise with a timeout
65
+ * @param promise The promise to wrap
66
+ * @param ms Timeout in milliseconds
67
+ * @param label Label for error message
68
+ * @returns Promise that rejects if timeout exceeded
69
+ */
70
+ export function withTimeout(promise, ms, label) {
71
+ const timeoutPromise = new Promise((_, reject) => {
72
+ const timeoutId = setTimeout(() => {
73
+ reject(new Error(`${label} timed out after ${ms}ms`));
74
+ }, ms);
75
+ // Ensure timeout is cleared if promise resolves first
76
+ promise.finally(() => clearTimeout(timeoutId));
77
+ });
78
+ return Promise.race([promise, timeoutPromise]);
79
+ }
80
+ /**
81
+ * Figma API Client
82
+ * Makes authenticated requests to Figma REST API
83
+ */
84
+ export class FigmaAPI {
85
+ constructor(config) {
86
+ this.accessToken = config.accessToken;
87
+ }
88
+ /**
89
+ * Make authenticated request to Figma API
90
+ */
91
+ async request(endpoint, options = {}) {
92
+ const url = `${FIGMA_API_BASE}${endpoint}`;
93
+ // Detect token type and use appropriate authentication header
94
+ // OAuth tokens start with 'figu_' and require Authorization: Bearer header
95
+ // Personal Access Tokens use X-Figma-Token header
96
+ const isOAuthToken = this.accessToken.startsWith('figu_');
97
+ logger.debug({
98
+ url,
99
+ hasToken: !!this.accessToken,
100
+ isOAuthToken,
101
+ authMethod: isOAuthToken ? 'Bearer' : 'X-Figma-Token'
102
+ }, 'Making Figma API request');
103
+ const headers = {
104
+ 'Content-Type': 'application/json',
105
+ ...(options.headers || {}),
106
+ };
107
+ // Add authentication header based on token type
108
+ if (isOAuthToken) {
109
+ headers['Authorization'] = `Bearer ${this.accessToken}`;
110
+ }
111
+ else {
112
+ headers['X-Figma-Token'] = this.accessToken;
113
+ }
114
+ const response = await fetch(url, {
115
+ ...options,
116
+ headers,
117
+ });
118
+ if (!response.ok) {
119
+ const errorText = await response.text();
120
+ logger.error({ status: response.status, statusText: response.statusText, body: errorText }, 'Figma API request failed');
121
+ throw new Error(`Figma API error (${response.status}): ${errorText}`);
122
+ }
123
+ const data = await response.json();
124
+ return data;
125
+ }
126
+ /**
127
+ * GET /v1/files/:file_key
128
+ * Get full file data including document tree, components, and styles
129
+ */
130
+ async getFile(fileKey, options) {
131
+ let endpoint = `/files/${fileKey}`;
132
+ const params = new URLSearchParams();
133
+ if (options?.version)
134
+ params.append('version', options.version);
135
+ if (options?.ids)
136
+ params.append('ids', options.ids.join(','));
137
+ if (options?.depth !== undefined)
138
+ params.append('depth', options.depth.toString());
139
+ if (options?.geometry)
140
+ params.append('geometry', options.geometry);
141
+ if (options?.plugin_data)
142
+ params.append('plugin_data', options.plugin_data);
143
+ if (options?.branch_data)
144
+ params.append('branch_data', 'true');
145
+ if (params.toString()) {
146
+ endpoint += `?${params.toString()}`;
147
+ }
148
+ return this.request(endpoint);
149
+ }
150
+ /**
151
+ * Resolve a branch key from a branch ID
152
+ * If branchId is provided, fetches branch data and returns the branch's unique key
153
+ * Otherwise returns the main file key unchanged
154
+ * @param fileKey The main file key from the URL
155
+ * @param branchId Optional branch ID from URL query param (branch-id)
156
+ * @returns The effective file key to use for API calls (branch key if on branch, otherwise fileKey)
157
+ */
158
+ async getBranchKey(fileKey, branchId) {
159
+ if (!branchId) {
160
+ return fileKey;
161
+ }
162
+ try {
163
+ logger.info({ fileKey, branchId }, 'Resolving branch key');
164
+ const fileData = await this.getFile(fileKey, { branch_data: true });
165
+ const branches = fileData.branches || [];
166
+ // Try to find branch by key (branchId might already be the key)
167
+ // or by matching other identifiers
168
+ const branch = branches.find((b) => b.key === branchId || b.name === branchId);
169
+ if (branch?.key) {
170
+ logger.info({ fileKey, branchId, branchKey: branch.key, branchName: branch.name }, 'Resolved branch key');
171
+ return branch.key;
172
+ }
173
+ // If branchId looks like a file key (alphanumeric), it might already be the branch key
174
+ // In this case, return it directly as it may be usable
175
+ if (/^[a-zA-Z0-9]+$/.test(branchId)) {
176
+ logger.info({ fileKey, branchId }, 'Branch ID appears to be a key, using directly');
177
+ return branchId;
178
+ }
179
+ logger.warn({ fileKey, branchId, availableBranches: branches.map((b) => ({ key: b.key, name: b.name })) }, 'Branch not found in file, using main file key');
180
+ return fileKey;
181
+ }
182
+ catch (error) {
183
+ logger.error({ error, fileKey, branchId }, 'Failed to resolve branch key, using main file key');
184
+ return fileKey;
185
+ }
186
+ }
187
+ /**
188
+ * GET /v1/files/:file_key/variables/local
189
+ * Get local variables (design tokens) from a file
190
+ */
191
+ async getLocalVariables(fileKey) {
192
+ const response = await this.request(`/files/${fileKey}/variables/local`);
193
+ // Figma API returns {status, error, meta: {variableCollections, variables}}
194
+ // Extract meta to match expected format
195
+ return response.meta || response;
196
+ }
197
+ /**
198
+ * GET /v1/files/:file_key/variables/published
199
+ * Get published variables from a file
200
+ */
201
+ async getPublishedVariables(fileKey) {
202
+ const response = await this.request(`/files/${fileKey}/variables/published`);
203
+ // Figma API returns {status, error, meta: {variableCollections, variables}}
204
+ // Extract meta to match expected format
205
+ return response.meta || response;
206
+ }
207
+ /**
208
+ * GET /v1/files/:file_key/nodes
209
+ * Get specific nodes by ID
210
+ */
211
+ async getNodes(fileKey, nodeIds, options) {
212
+ let endpoint = `/files/${fileKey}/nodes`;
213
+ const params = new URLSearchParams();
214
+ params.append('ids', nodeIds.join(','));
215
+ if (options?.version)
216
+ params.append('version', options.version);
217
+ if (options?.depth !== undefined)
218
+ params.append('depth', options.depth.toString());
219
+ if (options?.geometry)
220
+ params.append('geometry', options.geometry);
221
+ if (options?.plugin_data)
222
+ params.append('plugin_data', options.plugin_data);
223
+ endpoint += `?${params.toString()}`;
224
+ return this.request(endpoint);
225
+ }
226
+ /**
227
+ * GET /v1/files/:file_key/styles
228
+ * Get styles from a file
229
+ */
230
+ async getStyles(fileKey) {
231
+ return this.request(`/files/${fileKey}/styles`);
232
+ }
233
+ /**
234
+ * GET /v1/files/:file_key/components
235
+ * Get components from a file
236
+ */
237
+ async getComponents(fileKey) {
238
+ return this.request(`/files/${fileKey}/components`);
239
+ }
240
+ /**
241
+ * GET /v1/files/:file_key/component_sets
242
+ * Get component sets (variants) from a file
243
+ */
244
+ async getComponentSets(fileKey) {
245
+ return this.request(`/files/${fileKey}/component_sets`);
246
+ }
247
+ /**
248
+ * GET /v1/images/:file_key
249
+ * Renders images for specified nodes
250
+ * @param fileKey - The file key
251
+ * @param nodeIds - Node IDs to render (single string or array)
252
+ * @param options - Rendering options
253
+ * @returns Map of node IDs to image URLs (URLs expire after 30 days)
254
+ */
255
+ async getImages(fileKey, nodeIds, options) {
256
+ const params = new URLSearchParams();
257
+ // Handle single or multiple node IDs
258
+ const ids = Array.isArray(nodeIds) ? nodeIds.join(',') : nodeIds;
259
+ params.append('ids', ids);
260
+ // Add optional parameters
261
+ if (options?.scale !== undefined)
262
+ params.append('scale', options.scale.toString());
263
+ if (options?.format)
264
+ params.append('format', options.format);
265
+ if (options?.svg_outline_text !== undefined)
266
+ params.append('svg_outline_text', options.svg_outline_text.toString());
267
+ if (options?.svg_include_id !== undefined)
268
+ params.append('svg_include_id', options.svg_include_id.toString());
269
+ if (options?.svg_include_node_id !== undefined)
270
+ params.append('svg_include_node_id', options.svg_include_node_id.toString());
271
+ if (options?.svg_simplify_stroke !== undefined)
272
+ params.append('svg_simplify_stroke', options.svg_simplify_stroke.toString());
273
+ if (options?.contents_only !== undefined)
274
+ params.append('contents_only', options.contents_only.toString());
275
+ const endpoint = `/images/${fileKey}?${params.toString()}`;
276
+ logger.info({ fileKey, ids, options }, 'Rendering images');
277
+ return this.request(endpoint);
278
+ }
279
+ /**
280
+ * GET /v1/files/:file_key/comments
281
+ * Get comments on a file
282
+ */
283
+ async getComments(fileKey, options) {
284
+ const params = new URLSearchParams();
285
+ if (options?.as_md)
286
+ params.set('as_md', 'true');
287
+ const query = params.toString() ? `?${params.toString()}` : '';
288
+ return this.request(`/files/${fileKey}/comments${query}`);
289
+ }
290
+ /**
291
+ * POST /v1/files/:file_key/comments
292
+ * Post a comment on a file
293
+ */
294
+ async postComment(fileKey, message, clientMeta, commentId) {
295
+ return this.request(`/files/${fileKey}/comments`, {
296
+ method: 'POST',
297
+ body: JSON.stringify({
298
+ message,
299
+ ...(clientMeta && { client_meta: clientMeta }),
300
+ ...(commentId && { comment_id: commentId }),
301
+ }),
302
+ });
303
+ }
304
+ /**
305
+ * DELETE /v1/files/:file_key/comments/:comment_id
306
+ * Delete a comment on a file
307
+ */
308
+ async deleteComment(fileKey, commentId) {
309
+ return this.request(`/files/${fileKey}/comments/${commentId}`, {
310
+ method: 'DELETE',
311
+ });
312
+ }
313
+ /**
314
+ * Helper: Get all design tokens (variables) with formatted output
315
+ * Both local and published can fail gracefully (e.g., 403 without Enterprise plan)
316
+ */
317
+ async getAllVariables(fileKey) {
318
+ // Wrap both in catch handlers to prevent unhandled promise rejections
319
+ // which can crash the server when REST API returns 403
320
+ const [localResult, publishedResult] = await Promise.all([
321
+ this.getLocalVariables(fileKey).catch((err) => {
322
+ const errorMsg = err instanceof Error ? err.message : String(err);
323
+ return { error: errorMsg, variables: {}, variableCollections: {} };
324
+ }),
325
+ this.getPublishedVariables(fileKey).catch((err) => {
326
+ const errorMsg = err instanceof Error ? err.message : String(err);
327
+ return { error: errorMsg, variables: {} };
328
+ }),
329
+ ]);
330
+ return {
331
+ local: 'error' in localResult ? { meta: { variables: {}, variableCollections: {} } } : localResult,
332
+ published: 'error' in publishedResult ? { variables: {} } : publishedResult,
333
+ ...(('error' in localResult) && { localError: localResult.error }),
334
+ ...(('error' in publishedResult) && { publishedError: publishedResult.error }),
335
+ };
336
+ }
337
+ /**
338
+ * Helper: Get component metadata with properties
339
+ */
340
+ async getComponentData(fileKey, nodeId) {
341
+ const response = await this.getNodes(fileKey, [nodeId], { depth: 2 });
342
+ return response.nodes?.[nodeId];
343
+ }
344
+ /**
345
+ * Helper: Search for components by name
346
+ */
347
+ async searchComponents(fileKey, searchTerm) {
348
+ const { meta } = await this.getComponents(fileKey);
349
+ const components = meta?.components || [];
350
+ return components.filter((comp) => comp.name?.toLowerCase().includes(searchTerm.toLowerCase()));
351
+ }
352
+ }
353
+ /**
354
+ * Helper function to format variables for display
355
+ */
356
+ export function formatVariables(variablesData) {
357
+ const collections = Object.entries(variablesData.variableCollections || {}).map(([id, collection]) => ({
358
+ id,
359
+ name: collection.name,
360
+ key: collection.key,
361
+ modes: collection.modes,
362
+ variableIds: collection.variableIds,
363
+ }));
364
+ const variables = Object.entries(variablesData.variables || {}).map(([id, variable]) => ({
365
+ id,
366
+ name: variable.name,
367
+ key: variable.key,
368
+ resolvedType: variable.resolvedType,
369
+ valuesByMode: variable.valuesByMode,
370
+ variableCollectionId: variable.variableCollectionId,
371
+ scopes: variable.scopes,
372
+ description: variable.description,
373
+ }));
374
+ const variablesByType = variables.reduce((acc, v) => {
375
+ acc[v.resolvedType] = (acc[v.resolvedType] || 0) + 1;
376
+ return acc;
377
+ }, {});
378
+ return {
379
+ collections,
380
+ variables,
381
+ summary: {
382
+ totalCollections: collections.length,
383
+ totalVariables: variables.length,
384
+ variablesByType,
385
+ },
386
+ };
387
+ }
388
+ /**
389
+ * Helper function to format component data for display
390
+ */
391
+ export function formatComponentData(componentNode) {
392
+ return {
393
+ id: componentNode.id,
394
+ name: componentNode.name,
395
+ type: componentNode.type,
396
+ description: componentNode.description,
397
+ descriptionMarkdown: componentNode.descriptionMarkdown,
398
+ properties: componentNode.componentPropertyDefinitions,
399
+ children: componentNode.children?.map((child) => ({
400
+ id: child.id,
401
+ name: child.name,
402
+ type: child.type,
403
+ })),
404
+ bounds: componentNode.absoluteBoundingBox,
405
+ fills: componentNode.fills,
406
+ strokes: componentNode.strokes,
407
+ effects: componentNode.effects,
408
+ };
409
+ }
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Figma Connector Interface
3
+ *
4
+ * Transport abstraction for the WebSocket Desktop Bridge plugin.
5
+ * Allows getDesktopConnector() to return the active WebSocket transport.
6
+ */
7
+ export {};