easy-api.ts
Version:
A powerful library to create your own API with ease.
1 lines • 1.96 kB
JavaScript
Object.defineProperty(exports,"__esModule",{value:!0});let tslib_1=require("tslib"),APIFunction_1=require("../../classes/structures/APIFunction"),axios_1=tslib_1.__importDefault(require("axios")),Error_1=require("../../classes/structures/Error");class HTTPRequest extends APIFunction_1.APIFunction{name="$httpRequest";description="Makes an HTTP request.";parameters=[{name:"URL",description:"Target URL to make the request to.",type:APIFunction_1.ParamType.String,required:!0,rest:!1,defaultValue:null},{name:"Configuration",description:"Request typedConfig body.",type:APIFunction_1.ParamType.JSON,required:!1,rest:!1,defaultValue:JSON.stringify({})}];usage="$httpRequest[URL;config?]";returns=APIFunction_1.ParamType.Unknown;compile=!0;aliases=[];async run(e,[t,r="{}"]){if(!r.startsWith("{")&&!r.endsWith("}"))throw new Error_1.EATS_Error("Invalid request typedConfig object in: "+e.function?.name);let s=JSON.parse(r);(r={}).url=t,r.method=s.method??"get",s.headers&&(r.headers=s.headers),s.data&&(r.data=s.data),s.params&&(r.params=s.params),s.timeout&&(r.timeout=s.timeout),s.withCredentials&&(r.withCredentials=s.withCredentials),s.auth&&(r.auth=s.auth),s.responseType&&(r.responseType=s.responseType),s.responseEncoding&&(r.responseEncoding=s.responseEncoding),s.xsrfCookieName&&(r.xsrfCookieName=s.xsrfCookieName),s.xsrfHeaderName&&(r.xsrfHeaderName=s.xsrfHeaderName),s.maxContentLength&&(r.maxContentLength=s.maxContentLength),s.maxBodyLength&&(r.maxBodyLength=s.maxBodyLength),s.maxRedirects&&(r.maxRedirects=s.maxRedirects),s.proxy&&(r.proxy=s.proxy),s.decompress&&(r.decompress=s.decompress),null!=(t=await(0,axios_1.default)(r).catch(e=>{if(!s.logError)return null;console.log(e)}))&&(e.internalVarExists("EATS_REQUEST")||e.setInternalVar("EATS_REQUEST",{status:null,body:null}),r=e.getInternalVar("EATS_REQUEST"),Object.assign(r,{status:t.status}),Object.assign(r,{body:t.data}),e.setInternalVar("EATS_REQUEST",r))}}exports.default=HTTPRequest;