import BridgeFunction from './bridgeFunction'; import { MiniAppError, MiniAppErrorType } from '../miniAppError'; import { getRequest, errorCodes } from '../miniAppRequest'; class Request extends BridgeFunction { name = 'request'; protected async executeFunction(args: any[]) { const whiteListDomains = args[0]; if ( whiteListDomains.indexOf(new URL(this.functionContext?.url ?? '').host) > -1 ) { try { const response = await getRequest({ method: this.functionContext?.method, url: this.functionContext?.url, headers: this.functionContext?.header, params: this.functionContext?.data, }); return this.cbHandler?.success({ statusCode: response?.status, data: JSON.stringify(response?.data), }); } catch (error) { if (!error.response) { throw new MiniAppError(MiniAppErrorType.invalidResponse); } else { const errorMsg = errorCodes.has(error.response?.status) ? errorCodes.get(error.response?.status) : MiniAppErrorType.statusCodeError; return this.cbHandler?.success({ errorCode: errorMsg }); } } } else { throw new MiniAppError(MiniAppErrorType.whiteListError); } } } export default Request;