( data: any, filename: string = 'DataKit_Export', extension: string = 'json' )
| 113 | * Download JSON data as a file |
| 114 | */ |
| 115 | export const downloadJSON = ( |
| 116 | data: any, |
| 117 | filename: string = 'DataKit_Export', |
| 118 | extension: string = 'json' |
| 119 | ): void => { |
| 120 | try { |
| 121 | // Use specific MIME type for Jupyter notebooks |
| 122 | const mimeType = extension === 'ipynb' |
| 123 | ? 'application/x-ipynb+json' |
| 124 | : 'application/json'; |
| 125 | |
| 126 | const blob = new Blob([JSON.stringify(data, null, 2)], { |
| 127 | type: mimeType |
| 128 | }); |
| 129 | const url = URL.createObjectURL(blob); |
| 130 | const a = document.createElement('a'); |
| 131 | a.href = url; |
| 132 | a.download = `${filename}.${extension}`; |
| 133 | document.body.appendChild(a); |
| 134 | a.click(); |
| 135 | document.body.removeChild(a); |
| 136 | URL.revokeObjectURL(url); |
| 137 | } catch (error) { |
| 138 | console.error('Failed to download JSON:', error); |
| 139 | throw new Error('JSON download failed. Please try again.'); |
| 140 | } |
| 141 | }; |
| 142 | |
| 143 | /** |
| 144 | * Download text content as a file |
no outgoing calls
no test coverage detected