import { type FilteredNode, filterFigmaNode } from "../utils";
/**
* Get information about a single node
*/
export async function getNodeInfo(nodeId: string): Promise<FilteredNode> {
const node = await figma.getNodeByIdAsync(nodeId);
if (!node) {
throw new Error(`Node not found with ID: ${nodeId}`);
}
const response = await (node as SceneNode).exportAsync({
format: "JSON_REST_V1",
});
const filtered = filterFigmaNode(
(response as { document: unknown }).document,
);
if (!filtered) {
throw new Error(`Could not process node: ${nodeId}`);
}
return filtered;
}
/**
* Get information about multiple nodes
*/
export async function getNodesInfo(
nodeIds: string[],
): Promise<Array<{ nodeId: string; document: FilteredNode }>> {
// Load all nodes in parallel
const nodes = await Promise.all(
nodeIds.map((id) => figma.getNodeByIdAsync(id)),
);
// Filter out any null values (nodes that weren't found)
const validNodes = nodes.filter((node): node is SceneNode => node !== null);
// Export all valid nodes in parallel
const responses = await Promise.all(
validNodes.map(async (node) => {
const response = await node.exportAsync({
format: "JSON_REST_V1",
});
const filtered = filterFigmaNode(
(response as { document: unknown }).document,
);
return {
nodeId: node.id,
document: filtered ?? null,
};
}),
);
return responses.filter((r) => r.document !== null);
}
/**
* Read selected design nodes
*/
export async function readMyDesign(): Promise<{
selection: FilteredNode[];
count: number;
}> {
const selection = figma.currentPage.selection;
if (selection.length === 0) {
return { selection: [], count: 0 };
}
const filteredNodes = await Promise.all(
selection.map(async (node) => {
const response = await node.exportAsync({
format: "JSON_REST_V1",
});
return filterFigmaNode((response as { document: unknown }).document);
}),
);
return {
selection: filteredNodes.filter((n): n is FilteredNode => n !== null),
count: selection.length,
};
}