QLTools.min.js 4.3 KB

1
  1. function QLTools(t,s,e){return new class{constructor(t,s,e){this.isEnableLog=!0,this.logSeparator="\n██",this.url=t,this.clientId=s,this.clientSecret=e,this.auth=null}log(t){this.isEnableLog&&console.log(""+this.logSeparator+t)}isSurge(){return"undefined"!=typeof $httpClient}isQuanX(){return"undefined"!=typeof $task}isLoon(){return"undefined"!=typeof $loon}isJSBox(){return"undefined"!=typeof $app&&"undefined"!=typeof $http}isStash(){return"undefined"!=typeof $environment&&$environment["stash-version"]}isNode(){return"function"==typeof require&&!this.isJSBox()}adapterStatus(t){return t&&(t.status?t.statusCode=t.status:t.statusCode&&(t.status=t.statusCode)),t}get(t,n=()=>{}){this.isQuanX()&&((t="string"==typeof t?{url:t}:t).method="GET",$task.fetch(t).then(t=>{n(null,this.adapterStatus(t),t.body)},t=>n(t.error,null,null))),(this.isSurge()||this.isLoon()||this.isStash())&&$httpClient.get(t,(t,s,e)=>{n(t,this.adapterStatus(s),e)}),this.isNode()&&this.node.request(t,(t,s,e)=>{n(t,this.adapterStatus(s),e)}),this.isJSBox()&&((t="string"==typeof t?{url:t}:t).header=t.headers,t.handler=function(t){let s=t.error,e=(s=s&&JSON.stringify(t.error),t.data);"object"==typeof e&&(e=JSON.stringify(t.data)),n(s,this.adapterStatus(t.response),e)},$http.get(t))}post(t,n=()=>{}){this.isQuanX()&&((t="string"==typeof t?{url:t}:t).method="POST",$task.fetch(t).then(t=>{n(null,this.adapterStatus(t),t.body)},t=>n(t.error,null,null))),(this.isSurge()||this.isLoon()||this.isStash())&&$httpClient.post(t,(t,s,e)=>{n(t,this.adapterStatus(s),e)}),this.isNode()&&this.node.request.post(t,(t,s,e)=>{n(t,this.adapterStatus(s),e)}),this.isJSBox()&&((t="string"==typeof t?{url:t}:t).header=t.headers,t.handler=function(t){let s=t.error,e=(s=s&&JSON.stringify(t.error),t.data);"object"==typeof e&&(e=JSON.stringify(t.data)),n(s,this.adapterStatus(t.response),e)},$http.post(t))}put(t,n=()=>{}){this.isQuanX()&&((t="string"==typeof t?{url:t}:t).method="PUT",$task.fetch(t).then(t=>{n(null,this.adapterStatus(t),t.body)},t=>n(t.error,null,null))),(this.isSurge()||this.isLoon()||this.isStash())&&(t.method="PUT",$httpClient.put(t,(t,s,e)=>{n(t,this.adapterStatus(s),e)})),this.isNode()&&(t.method="PUT",this.node.request.put(t,(t,s,e)=>{n(t,this.adapterStatus(s),e)}))}delete(t,n=()=>{}){this.isQuanX()&&((t="string"==typeof t?{url:t}:t).method="DELETE",$task.fetch(t).then(t=>{n(null,this.adapterStatus(t),t.body)},t=>n(t.error,null,null))),(this.isSurge()||this.isLoon()||this.isStash())&&(t.method="DELETE",$httpClient.put(t,(t,s,e)=>{n(t,this.adapterStatus(s),e)})),this.isNode()&&(t.method="DELETE",this.node.request.put(t,(t,s,e)=>{n(t,this.adapterStatus(s),e)}))}setAuth(t){this.auth=t}async login(){var t=await this.getToken();return!!t&&200==t.code&&(t=t.data,this.auth=t.token_type+" "+t.token,!0)}async getToken(){let t={url:`${this.url}/open/auth/token?client_id=${this.clientId}&client_secret=`+this.clientSecret};return new Promise(i=>{this.get(t,async(t,s,e)=>{try{t&&this.log(t);var n=JSON.parse(e);i(n)}catch(t){this.log(t)}finally{i()}})})}async callApi(s,t,e=null,n=null){let i=this.url+"/open/"+t,r=(e&&0<e.length&&(i+="/"+e),""),a=(n&&(r=JSON.stringify(n)),{url:i,headers:{Authorization:this.auth,"Content-Type":"application/json",Accept:"application/json"},body:r});return new Promise(i=>{var t=this[s];t?t.call(this,a,async(t,s,e)=>{try{var n=JSON.parse(e);i(n)}catch(t){i(e)}finally{i()}}):i()})}async getEnvs(){return this.callApi("get","envs")}async getEnvById(t){return this.callApi("get","envs",t)}async deleteEnvs(t){return this.callApi("delete","envs","",t)}async addEnvs(t){return this.callApi("post","envs","",t)}async updateEnv(t){return this.callApi("put","envs","",t)}async getCrons(){return this.callApi("get","crons")}async getCronById(t){return this.callApi("get","crons",t)}async deleteCrons(t){return this.callApi("delete","crons","",t)}async addCron(t){return this.callApi("post","crons","",t)}async updateCron(t){return this.callApi("put","crons","",t)}async runCrons(t){return this.callApi("put","crons","run",t)}async stopCrons(t){return this.callApi("put","crons","stop",t)}async enableCrons(t){return this.callApi("put","crons","enable",t)}async disableCrons(t){return this.callApi("put","crons","disable",t)}async getCronDetail(t){return this.callApi("get","crons",t)}async getCronLog(t){return this.callApi("get","crons",t+"/log")}}(t,s,e)}