(apiPath, { method = "GET", data, params, auth = true } = {})
| 205 | } |
| 206 | |
| 207 | async request(apiPath, { method = "GET", data, params, auth = true } = {}) { |
| 208 | const options = { |
| 209 | method, |
| 210 | url: new URL(apiPath, API_BASE).toString(), |
| 211 | params, |
| 212 | headers: this.signedHeaders({}, auth), |
| 213 | timeout: 15000, |
| 214 | validateStatus: () => true, |
| 215 | }; |
| 216 | if (data !== undefined) options.data = data; |
| 217 | const { data: result, status, headers } = await axios.request(options); |
| 218 | if (headers?.["access-token"]) this.token = headers["access-token"]; |
| 219 | if (headers?.["x-access-token"]) this.refreshToken = headers["x-access-token"]; |
| 220 | if (status === 401 || status === 403) throw new Error(`HTTP ${status}: ${result?.Message || JSON.stringify(result)}`); |
| 221 | if (status !== 200) throw new Error(`HTTP ${status}: ${JSON.stringify(result)}`); |
| 222 | if (!isSuccess(result)) throw new Error(`${result?.Code ?? ""} ${result?.Message || result?.msg || JSON.stringify(result)}`.trim()); |
| 223 | if (this.token) this.saveCachedToken(); |
| 224 | return result; |
| 225 | } |
| 226 | |
| 227 | async getLoginCode() { |
| 228 | if (!process.env.wx_auth) throw new Error("缺少 wx_auth,无法从 wx_server 获取 code"); |
no test coverage detected