QLTools.min.js 4.2 KB

1
  1. function QLTools(t,e,s){return new class{constructor(t,e,s){this.isEnableLog=!0,this.logSeparator="\n██",this.url=t,this.clientId=e,this.clientSecret=s,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()}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,e,s)=>{n(t,this.adapterStatus(e),s)}),this.isNode()&&this.node.request(t,(t,e,s)=>{n(t,this.adapterStatus(e),s)}),this.isJSBox()&&((t="string"==typeof t?{url:t}:t).header=t.headers,t.handler=function(t){let e=t.error,s=(e=e&&JSON.stringify(t.error),t.data);"object"==typeof s&&(s=JSON.stringify(t.data)),n(e,this.adapterStatus(t.response),s)},$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,e,s)=>{n(t,this.adapterStatus(e),s)}),this.isNode()&&this.node.request.post(t,(t,e,s)=>{n(t,this.adapterStatus(e),s)}),this.isJSBox()&&((t="string"==typeof t?{url:t}:t).header=t.headers,t.handler=function(t){let e=t.error,s=(e=e&&JSON.stringify(t.error),t.data);"object"==typeof s&&(s=JSON.stringify(t.data)),n(e,this.adapterStatus(t.response),s)},$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,e,s)=>{n(t,this.adapterStatus(e),s)})),this.isNode()&&(t.method="PUT",this.node.request.put(t,(t,e,s)=>{n(t,this.adapterStatus(e),s)}))}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,e,s)=>{n(t,this.adapterStatus(e),s)})),this.isNode()&&(t.method="DELETE",this.node.request.put(t,(t,e,s)=>{n(t,this.adapterStatus(e),s)}))}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,e,s)=>{try{var n=JSON.parse(s);i(n)}catch(t){this.log(t)}finally{i()}})})}async callApi(t,e,s=null,n=null){let i=this.url+"/open/"+e,r=(s&&0<s.length&&(i+="/"+s),""),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.reqType;t?t.call(this,a,async(t,e,s)=>{try{var n=JSON.parse(s);i(n)}catch(t){this.log(t)}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,e,s)}