import BridgeFunction from './bridgeFunction'; import { getMiniAppDetail } from '../../factory/api'; import { MiniAppError, MiniAppErrorType } from '../miniAppError'; import { internalHeaders } from '../../factory/request'; import { getRequest, getHost, errorCodes } from '../miniAppRequest'; class RequestInternal extends BridgeFunction { name = 'requestInternal'; protected async executeFunction() { try { const resAppDetail: any = await getMiniAppDetail(); if (!resAppDetail?.data?.unsigned?.data?.isInternal) { throw new MiniAppError(MiniAppErrorType.insufficientScope); } const reqHeader = Object.assign( {}, internalHeaders({}), this.functionContext?.header ); if ( ['bff', 'gff'].indexOf(this.functionContext?.endpointType ?? '') < 0 || ['dev', 'stg', 'prd'].indexOf(this.functionContext?.environment ?? '') < 0 ) { throw new MiniAppError(MiniAppErrorType.invalidUrl); } const response = await getRequest({ method: this.functionContext?.method, url: getHost( this.functionContext?.endpointType!, this.functionContext?.environment! ) + (this.functionContext?.path?.startsWith('/') ? '' : '/') + (this.functionContext?.path ?? ''), headers: reqHeader, 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) : 'STATUS_CODE_ERROR'; return this.cbHandler?.success({ errorCode: errorMsg }); } } } } export default RequestInternal;