stock-bot/apps/stock/web-app/src/utils/downloadJsonText.ts

15 lines
538 B
TypeScript

/** Save inert JSON text and release the browser URL after it consumes the click. */
export function downloadJsonText(text: string, filename: string): void {
const url = URL.createObjectURL(new Blob([text], { type: 'application/json' }));
let anchor: HTMLAnchorElement | undefined;
try {
anchor = document.createElement('a');
anchor.href = url;
anchor.download = filename;
document.body.append(anchor);
anchor.click();
} finally {
setTimeout(() => URL.revokeObjectURL(url), 0);
anchor?.remove();
}
}