import BridgeFunction from './bridgeFunction'; import { MiniAppError, MiniAppErrorType } from '../miniAppError'; import axios from 'axios'; class DownloadFile extends BridgeFunction { name = 'downloadFile'; protected async executeFunction(args: any[]) { const whiteListDomains = args[0]; if ( whiteListDomains.indexOf(new URL(this.functionContext?.url ?? '').host) > -1 ) { try { if (!this.functionContext.url) { this.cbHandler?.fail( new MiniAppError(MiniAppErrorType.badRequestInsufficientParameter) ); } else { const res = await axios.get('/downloadfile', { params: { url: this.functionContext?.url, header: this.functionContext?.header, filePath: this.functionContext?.filePath, }, }); if (res?.data) { return this.cbHandler?.success(res?.data); } else { this.cbHandler?.fail(new MiniAppError(MiniAppErrorType.unknown)); } } } catch (e) { this.cbHandler?.fail(new MiniAppError(e?.response?.data?.error)); } } else { this.cbHandler?.fail(new MiniAppError(MiniAppErrorType.whiteListError)); } } } export default DownloadFile;