import axios from "axios"; import config from "@/app.config"; import { useCounterMixin } from "@/stores/mixin"; import { downloadBlobFile } from "@/modules/10_registry/utils/downloadFile"; const mixin = useCounterMixin(); const { showLoader, hideLoader } = mixin; async function genReport(data: any, fileName: string, type: string = "docx") { showLoader(); await axios .post(config.API.reportTemplate + `/docx`, data, { headers: { "content-Type": "application/json", accept: type === "docx" ? "application/vnd.openxmlformats-officedocument.wordprocessingml.document" : "application/pdf", }, responseType: "arraybuffer", }) .then(async (res) => { const responseData = res.data; if (responseData) { const mimeType = type === "docx" ? "application/vnd.openxmlformats-officedocument.wordprocessingml.document" : "application/pdf"; const blob = new Blob([responseData], { type: mimeType }); const url = URL.createObjectURL(blob); const baseName = fileName.trim(); // const extension = type === "docx" ? "docx" : "pdf"; await downloadBlobFile({ downloadUrl: url, fileName: baseName, }); } }) .catch((err) => { console.log(err); }) .finally(() => { hideLoader(); }); } export default genReport;