({ method = "POST", apiPath, params = {}, data = {}, skipToken = false })
| 163 | } |
| 164 | |
| 165 | async request({ method = "POST", apiPath, params = {}, data = {}, skipToken = false }) { |
| 166 | const upperMethod = method.toUpperCase(); |
| 167 | const options = { |
| 168 | method: upperMethod, |
| 169 | url: `${API_BASE}${apiPath}`, |
| 170 | headers: this.headers(upperMethod === "POST" ? { "Content-Type": "application/json" } : {}), |
| 171 | timeout: 20000, |
| 172 | validateStatus: () => true, |
| 173 | }; |
| 174 | if (upperMethod === "GET") options.params = params; |
| 175 | else options.data = data; |
| 176 | if (skipToken) delete options.headers.Authorization; |
| 177 | |
| 178 | const res = await axios.request(options); |
| 179 | if (res.status !== 200) throw new Error(`HTTP ${res.status}: ${JSON.stringify(res.data)}`); |
| 180 | const result = res.data; |
| 181 | if (result && typeof result === "object" && result.error !== undefined && Number(result.error) !== 0) { |
| 182 | const err = new Error(result.errorMsg || result.message || result.msg || JSON.stringify(result)); |
| 183 | err.code = result.error; |
| 184 | throw err; |
| 185 | } |
| 186 | return result?.data ?? result; |
| 187 | } |
| 188 | |
| 189 | async getLoginCode() { |
| 190 | const { data } = await wechat.getCode(this.account); |
no test coverage detected