-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
figma.ts
132 lines (116 loc) Β· 3.48 KB
/
figma.ts
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
import { Document } from "@langchain/core/documents";
import { getEnvironmentVariable } from "@langchain/core/utils/env";
import { BaseDocumentLoader } from "../base.js";
/**
* Interface representing a Figma file. It includes properties for the
* file name, role, last modified date, editor type, thumbnail URL,
* version, document node, schema version, main file key, and an array of
* branches.
*/
export interface FigmaFile {
name: string;
role: string;
lastModified: string;
editorType: string;
thumbnailUrl: string;
version: string;
document: Node;
schemaVersion: number;
mainFileKey: string;
branches: Array<{
key: string;
name: string;
thumbnail_url: string;
last_modified: string;
link_access: string;
}>;
}
/**
* Interface representing the parameters for configuring the FigmaLoader.
* It includes optional properties for the access token, an array of node
* IDs, and the file key.
*/
export interface FigmaLoaderParams {
accessToken?: string;
nodeIds: string[];
fileKey: string;
}
/**
* Class representing a document loader for loading Figma files. It
* extends the BaseDocumentLoader and implements the FigmaLoaderParams
* interface. The constructor takes a config object as a parameter, which
* contains the access token, an array of node IDs, and the file key.
* @example
* ```typescript
* const loader = new FigmaFileLoader({
* accessToken: "FIGMA_ACCESS_TOKEN",
* nodeIds: ["id1", "id2", "id3"],
* fileKey: "key",
* });
* const docs = await loader.load();
* ```
*/
export class FigmaFileLoader
extends BaseDocumentLoader
implements FigmaLoaderParams
{
public accessToken?: string;
public nodeIds: string[];
public fileKey: string;
private headers: Record<string, string> = {};
constructor({
accessToken = getEnvironmentVariable("FIGMA_ACCESS_TOKEN"),
nodeIds,
fileKey,
}: FigmaLoaderParams) {
super();
this.accessToken = accessToken;
this.nodeIds = nodeIds;
this.fileKey = fileKey;
if (this.accessToken) {
this.headers = {
"x-figma-token": this.accessToken,
};
}
}
/**
* Constructs the URL for the Figma API call.
* @returns The constructed URL as a string.
*/
private constructFigmaApiURL(): string {
return `https://api.figma.com/v1/files/${
this.fileKey
}/nodes?ids=${this.nodeIds.join(",")}`;
}
/**
* Fetches the Figma file using the Figma API and returns it as a
* FigmaFile object.
* @returns A Promise that resolves to a FigmaFile object.
*/
private async getFigmaFile(): Promise<FigmaFile> {
const url = this.constructFigmaApiURL();
const response = await fetch(url, { headers: this.headers });
const data = await response.json();
if (!response.ok) {
throw new Error(
`Unable to get figma file: ${response.status} ${JSON.stringify(data)}`
);
}
if (!data) {
throw new Error("Unable to get file");
}
return data as FigmaFile;
}
/**
* Fetches the Figma file using the Figma API, creates a Document instance
* with the JSON representation of the file as the page content and the
* API URL as the metadata, and returns it.
* @returns A Promise that resolves to an array of Document instances.
*/
public async load(): Promise<Document[]> {
const data = await this.getFigmaFile();
const text = JSON.stringify(data);
const metadata = { source: this.constructFigmaApiURL() };
return [new Document({ pageContent: text, metadata })];
}
}