import { ResponseHandler } from "./RepsonseHandler"; export interface DownloadMetadata { name?: string; type?: string; } const DownloadFileHandler: ResponseHandler = (res, next, extra) => { if (res.status === 404) { console.log("Nothing to download"); return res.json().then(next); } return res.blob() .then(data => { let a = document.createElement("a"); a.href = window.URL.createObjectURL(data); a.download = `${extra?.name || "file"}.${extra?.type || "txt"}`; a.click(); next() }) .catch(next); } export default DownloadFileHandler;