{"version":3,"file":"figma.cjs","names":["BaseDocumentLoader","Document"],"sources":["../../../src/document_loaders/web/figma.ts"],"sourcesContent":["import { Document } from \"@langchain/core/documents\";\nimport { getEnvironmentVariable } from \"@langchain/core/utils/env\";\nimport { BaseDocumentLoader } from \"@langchain/core/document_loaders/base\";\n\n/**\n * Interface representing a Figma file. It includes properties for the\n * file name, role, last modified date, editor type, thumbnail URL,\n * version, document node, schema version, main file key, and an array of\n * branches.\n */\nexport interface FigmaFile {\n  name: string;\n  role: string;\n  lastModified: string;\n  editorType: string;\n  thumbnailUrl: string;\n  version: string;\n  document: Node;\n  schemaVersion: number;\n  mainFileKey: string;\n  branches: Array<{\n    key: string;\n    name: string;\n    thumbnail_url: string;\n    last_modified: string;\n    link_access: string;\n  }>;\n}\n\n/**\n * Interface representing the parameters for configuring the FigmaLoader.\n * It includes optional properties for the access token, an array of node\n * IDs, and the file key.\n */\nexport interface FigmaLoaderParams {\n  accessToken?: string;\n  nodeIds: string[];\n  fileKey: string;\n}\n\n/**\n * Class representing a document loader for loading Figma files. It\n * extends the BaseDocumentLoader and implements the FigmaLoaderParams\n * interface. The constructor takes a config object as a parameter, which\n * contains the access token, an array of node IDs, and the file key.\n * @example\n * ```typescript\n * const loader = new FigmaFileLoader({\n *   accessToken: \"FIGMA_ACCESS_TOKEN\",\n *   nodeIds: [\"id1\", \"id2\", \"id3\"],\n *   fileKey: \"key\",\n * });\n * const docs = await loader.load();\n * ```\n */\nexport class FigmaFileLoader\n  extends BaseDocumentLoader\n  implements FigmaLoaderParams\n{\n  public accessToken?: string;\n\n  public nodeIds: string[];\n\n  public fileKey: string;\n\n  private headers: Record<string, string> = {};\n\n  constructor({\n    accessToken = getEnvironmentVariable(\"FIGMA_ACCESS_TOKEN\"),\n    nodeIds,\n    fileKey,\n  }: FigmaLoaderParams) {\n    super();\n\n    this.accessToken = accessToken;\n    this.nodeIds = nodeIds;\n    this.fileKey = fileKey;\n\n    if (this.accessToken) {\n      this.headers = {\n        \"x-figma-token\": this.accessToken,\n      };\n    }\n  }\n\n  /**\n   * Constructs the URL for the Figma API call.\n   * @returns The constructed URL as a string.\n   */\n  private constructFigmaApiURL(): string {\n    return `https://api.figma.com/v1/files/${\n      this.fileKey\n    }/nodes?ids=${this.nodeIds.join(\",\")}`;\n  }\n\n  /**\n   * Fetches the Figma file using the Figma API and returns it as a\n   * FigmaFile object.\n   * @returns A Promise that resolves to a FigmaFile object.\n   */\n  private async getFigmaFile(): Promise<FigmaFile> {\n    const url = this.constructFigmaApiURL();\n    const response = await fetch(url, { headers: this.headers });\n    const data = await response.json();\n\n    if (!response.ok) {\n      throw new Error(\n        `Unable to get figma file: ${response.status} ${JSON.stringify(data)}`\n      );\n    }\n\n    if (!data) {\n      throw new Error(\"Unable to get file\");\n    }\n\n    return data as FigmaFile;\n  }\n\n  /**\n   * Fetches the Figma file using the Figma API, creates a Document instance\n   * with the JSON representation of the file as the page content and the\n   * API URL as the metadata, and returns it.\n   * @returns A Promise that resolves to an array of Document instances.\n   */\n  public async load(): Promise<Document[]> {\n    const data = await this.getFigmaFile();\n    const text = JSON.stringify(data);\n    const metadata = { source: this.constructFigmaApiURL() };\n\n    return [new Document({ pageContent: text, metadata })];\n  }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAuDA,IAAa,kBAAb,cACUA,sCAAAA,mBAEV;CACE;CAEA;CAEA;CAEA,UAA0C,EAAE;CAE5C,YAAY,EACV,eAAA,GAAA,0BAAA,wBAAqC,qBAAqB,EAC1D,SACA,WACoB;AACpB,SAAO;AAEP,OAAK,cAAc;AACnB,OAAK,UAAU;AACf,OAAK,UAAU;AAEf,MAAI,KAAK,YACP,MAAK,UAAU,EACb,iBAAiB,KAAK,aACvB;;;;;;CAQL,uBAAuC;AACrC,SAAO,kCACL,KAAK,QACN,aAAa,KAAK,QAAQ,KAAK,IAAI;;;;;;;CAQtC,MAAc,eAAmC;EAC/C,MAAM,MAAM,KAAK,sBAAsB;EACvC,MAAM,WAAW,MAAM,MAAM,KAAK,EAAE,SAAS,KAAK,SAAS,CAAC;EAC5D,MAAM,OAAO,MAAM,SAAS,MAAM;AAElC,MAAI,CAAC,SAAS,GACZ,OAAM,IAAI,MACR,6BAA6B,SAAS,OAAO,GAAG,KAAK,UAAU,KAAK,GACrE;AAGH,MAAI,CAAC,KACH,OAAM,IAAI,MAAM,qBAAqB;AAGvC,SAAO;;;;;;;;CAST,MAAa,OAA4B;EACvC,MAAM,OAAO,MAAM,KAAK,cAAc;AAItC,SAAO,CAAC,IAAIC,0BAAAA,SAAS;GAAE,aAHV,KAAK,UAAU,KAAK;GAGS,UAFzB,EAAE,QAAQ,KAAK,sBAAsB,EAAE;GAEJ,CAAC,CAAC"}