From a821d22d3493974727d15b78c7de4c6223b8d101 Mon Sep 17 00:00:00 2001 From: "bo.yang" Date: Tue, 18 May 2021 16:08:30 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E9=94=99=E8=AF=AF?= =?UTF-8?q?=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/holdcloud.js | 48 ++++++++++++++++++++---------------------------- lib/request.js | 27 +++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 28 deletions(-) create mode 100644 lib/request.js diff --git a/lib/holdcloud.js b/lib/holdcloud.js index 54825c1..07f91fb 100644 --- a/lib/holdcloud.js +++ b/lib/holdcloud.js @@ -2,7 +2,8 @@ const _ = require('lodash'); const crypto = require('crypto'); -const got = require('got'); +const {request} = require('./request'); + class HoldCloud { /** * [constructor description] @@ -21,18 +22,7 @@ class HoldCloud { } } -const getRp = (baseUrl, noStrictSSL, options = {}) => { - return got.extend({ - prefixUrl: baseUrl, - responseType: 'json', - resolveBodyOnly: true, - https: { - rejectUnauthorized: noStrictSSL ? false : true, - }, - }); -} - -HoldCloud.prototype.requestWithToken = async function (options) { +HoldCloud.prototype.requestWithToken = async function (options = {}) { options = _.cloneDeep(options); if (!this.token) { await this.updateToken(); @@ -40,24 +30,20 @@ HoldCloud.prototype.requestWithToken = async function (options) { _.set(options, 'headers.jweToken', this.token); let tokenRefreshed = false; const self = this; - const rp = getRp(this.BASE_URI, this.noStrictSSL); - return (() => { - try { - return rp(options); - } catch (error) { - if (error.statusCode === 401 && !tokenRefreshed) { - self.updateToken(); - tokenRefreshed = true; - self.requestWithToken(options); - } else { - throw error; - } + const rp = request(this.BASE_URI, this.noStrictSSL); + return await rp(options).catch((error) => { + if (error.statusCode === 401 && !tokenRefreshed) { + self.updateToken(); + tokenRefreshed = true; + self.requestWithToken(options); + } else { + throw error; } - })(); + }); }; HoldCloud.prototype.updateToken = async function () { - const rp = getRp(this.BASE_URI, this.noStrictSSL); + const rp = request(this.BASE_URI, this.noStrictSSL); const result = await rp({ method: 'POST', url: 'login', @@ -111,7 +97,13 @@ HoldCloud.prototype.createContainerApp = async function (projectId, options) { HoldCloud.prototype.checkContainerappsName = async function (projectId, name) { const data = await this.requestWithToken({ method: 'GET', - url: `project/${projectId}/unionservices?itemsPerPage=10&page=1&filterBy=name,^${name}$,&sortBy=a,name`, + url: `project/${projectId}/unionservices`, + searchParams: { + itemsPerPage: 10, + page: 1, + filterBy: `name,^${name}$,`, + sortBy: 'a,name,' + } }); if (data.totalItems !== 0 && data.items[0].name === name) { return {code: 400, id: data.items[0].id}; diff --git a/lib/request.js b/lib/request.js new file mode 100644 index 0000000..e7f22c3 --- /dev/null +++ b/lib/request.js @@ -0,0 +1,27 @@ +const got = require('got'); + +exports.request = (baseUrl, noStrictSSL, options = {}) => { + return got.extend({ + prefixUrl: baseUrl, + responseType: 'json', + resolveBodyOnly: true, + https: { + rejectUnauthorized: noStrictSSL ? false : true, + }, + hooks: { + beforeError: [ + error => { + const {response} = error; + error.statusCode = 500; + error.message = '服务器发生错误'; + if (response?.body) { + error.statusCode = response.statusCode; + error.message = response.body.message; + } + return error; + }, + ], + }, + }); +}; +