(nodes: ImageNode[], fileId: string, token: string)
| 16 | * @returns Image nodes |
| 17 | */ |
| 18 | export const fetchImages = async (nodes: ImageNode[], fileId: string, token: string): Promise<ImageNode[]> => { |
| 19 | if (!fileId || !nodes?.length) { |
| 20 | return []; |
| 21 | } |
| 22 | |
| 23 | const svgs = nodes.filter(node => node.format === FigmaImageFormat.SVG); |
| 24 | const pngs = nodes.filter(node => node.format === FigmaImageFormat.PNG); |
| 25 | const getImagePromises: Promise<{ [key: string]: string } | undefined>[] = []; |
| 26 | |
| 27 | if (svgs.length > 0) { |
| 28 | getImagePromises.push(fetchFigmaImages(fileId, svgs.map(node => node.id).join(','), token, FigmaImageFormat.SVG)); |
| 29 | } |
| 30 | if (pngs.length > 0) { |
| 31 | getImagePromises.push(fetchFigmaImages(fileId, pngs.map(node => node.id).join(','), token, FigmaImageFormat.PNG)); |
| 32 | } |
| 33 | |
| 34 | const images: ImageNode[] = []; |
| 35 | const results = await Promise.all(getImagePromises); |
| 36 | results.forEach((res: { [key: string]: string } | undefined) => { |
| 37 | if (res) { |
| 38 | for (const [key, value] of Object.entries(res)) { |
| 39 | images.push({ |
| 40 | id: key, |
| 41 | name: '', |
| 42 | format: FigmaImageFormat.PNG, |
| 43 | ...(nodes.find(n => n.id === key) || {}), |
| 44 | url: value || '', |
| 45 | }); |
| 46 | } |
| 47 | } |
| 48 | }); |
| 49 | |
| 50 | return images; |
| 51 | }; |
| 52 | |
| 53 | /** |
| 54 | * Download images from figma document |
no test coverage detected