PromucFlow_constructor/app/client/src/sagas/ActionExecution/DownloadActionSaga.ts
Hetu Nandu 39b0a4e5a6
feat: Native promises support in Appsmith (#8988)
Co-authored-by: Apeksha Bhosale <7846888+ApekshaBhosale@users.noreply.github.com>
2021-12-23 14:17:20 +00:00

46 lines
1.5 KiB
TypeScript

import { getType, isURL, Types } from "utils/TypeHelpers";
import downloadjs from "downloadjs";
import AppsmithConsole from "utils/AppsmithConsole";
import Axios from "axios";
import {
ActionTriggerType,
DownloadActionDescription,
} from "entities/DataTree/actionTriggers";
import { ActionValidationError } from "sagas/ActionExecution/errorUtils";
export default async function downloadSaga(
action: DownloadActionDescription["payload"],
) {
const { data, name, type } = action;
if (!name) {
throw new ActionValidationError(
ActionTriggerType.DOWNLOAD,
"name",
Types.STRING,
getType(name),
);
}
const dataType = getType(data);
if (dataType === Types.ARRAY || dataType === Types.OBJECT) {
const jsonString = JSON.stringify(data, null, 2);
downloadjs(jsonString, name, type);
AppsmithConsole.info({
text: `download('${jsonString}', '${name}', '${type}') was triggered`,
});
} else if (dataType === Types.STRING && isURL(data)) {
// In the event that a url string is supplied, we need to fetch the image with the response type arraybuffer.
// This also covers the case where the file to be downloaded is Binary.
Axios.get(data, { responseType: "arraybuffer" }).then((res) => {
downloadjs(res.data, name, type);
AppsmithConsole.info({
text: `download('${data}', '${name}', '${type}') was triggered`,
});
});
} else {
downloadjs(data, name, type);
AppsmithConsole.info({
text: `download('${data}', '${name}', '${type}') was triggered`,
});
}
}