({ method = "GET", apiPath, query = {}, data = {}, skipToken = false })
| 152 | } |
| 153 | |
| 154 | async request({ method = "GET", apiPath, query = {}, data = {}, skipToken = false }) { |
| 155 | const oldToken = this.token; |
| 156 | if (skipToken) this.token = {}; |
| 157 | const options = { |
| 158 | method, |
| 159 | url: this.buildUrl(apiPath, query), |
| 160 | headers: this.getHeaders(method === "POST" ? { "Content-Type": "application/json" } : {}), |
| 161 | timeout: 15000, |
| 162 | validateStatus: () => true, |
| 163 | }; |
| 164 | if (method !== "GET") options.data = data; |
| 165 | if (skipToken) this.token = oldToken; |
| 166 | |
| 167 | const { status, data: result } = await axios.request(options); |
| 168 | if (status !== 200) throw new Error(`HTTP ${status}: ${typeof result === "string" ? result.slice(0, 200) : JSON.stringify(result)}`); |
| 169 | if (!result || result.code !== 0) { |
| 170 | const err = new Error(result?.msg || result?.message || JSON.stringify(result)); |
| 171 | err.code = result?.code; |
| 172 | throw err; |
| 173 | } |
| 174 | return result.data; |
| 175 | } |
| 176 | |
| 177 | async getWxCode() { |
| 178 | const wxServerUrl = process.env.wx_server_url; |
no test coverage detected