(apiPath, { method = "GET", data = {}, auth = true } = {})
| 146 | } |
| 147 | |
| 148 | async request(apiPath, { method = "GET", data = {}, auth = true } = {}) { |
| 149 | const options = { |
| 150 | method, |
| 151 | url: new URL(apiPath, API_BASE).toString(), |
| 152 | headers: this.headers(), |
| 153 | timeout: 15000, |
| 154 | validateStatus: () => true, |
| 155 | }; |
| 156 | if (!auth) delete options.headers["X-Dts-Token"]; |
| 157 | if (method.toUpperCase() === "GET") options.params = data; |
| 158 | else options.data = data; |
| 159 | |
| 160 | const { data: result, status } = await axios.request(options); |
| 161 | if (status !== 200) throw new Error(`HTTP ${status}: ${JSON.stringify(result)}`); |
| 162 | if (Number(result?.errno) !== 0) throw new Error(`${result?.errno ?? ""} ${result?.errmsg || result?.message || JSON.stringify(result)}`.trim()); |
| 163 | return result; |
| 164 | } |
| 165 | |
| 166 | async getLoginCode() { |
| 167 | if (!process.env.wx_auth) throw new Error("缺少 wx_auth,无法从 wx_server 获取 code"); |
no test coverage detected