(baseURL, apiPath, { method = "GET", data, params, auth = true, form = false } = {})
| 151 | } |
| 152 | |
| 153 | async request(baseURL, apiPath, { method = "GET", data, params, auth = true, form = false } = {}) { |
| 154 | const options = { |
| 155 | method, |
| 156 | url: new URL(apiPath, baseURL).toString(), |
| 157 | params, |
| 158 | headers: this.headers(form ? { |
| 159 | "Content-Type": "application/x-www-form-urlencoded;", |
| 160 | } : {}), |
| 161 | timeout: 15000, |
| 162 | validateStatus: () => true, |
| 163 | }; |
| 164 | if (!auth) delete options.headers.token; |
| 165 | if (data !== undefined) options.data = form ? formBody(data) : data; |
| 166 | |
| 167 | const { data: result, status } = await axios.request(options); |
| 168 | if (status !== 200) throw new Error(`HTTP ${status}: ${JSON.stringify(result)}`); |
| 169 | if (!isSuccess(result)) throw new Error(`${result?.code ?? ""} ${result?.msg || result?.error || JSON.stringify(result)}`.trim()); |
| 170 | return result; |
| 171 | } |
| 172 | |
| 173 | async getLoginCode() { |
| 174 | if (!process.env.wx_auth) throw new Error("缺少 wx_auth,无法从 wx_server 获取 code"); |
no test coverage detected