({ method = "GET", apiPath, params = {}, data = {}, skipToken = false })
| 294 | } |
| 295 | |
| 296 | async request({ method = "GET", apiPath, params = {}, data = {}, skipToken = false }) { |
| 297 | const options = { |
| 298 | method, |
| 299 | url: `${this.app.apiBase}${apiPath.startsWith("/") ? apiPath : `/${apiPath}`}`, |
| 300 | headers: this.headers(), |
| 301 | timeout: 20000, |
| 302 | validateStatus: () => true, |
| 303 | }; |
| 304 | if (skipToken) delete options.headers.Authorization; |
| 305 | if (method === "GET") options.params = params; |
| 306 | else options.data = data; |
| 307 | |
| 308 | const { data: result, status } = await axios.request(options); |
| 309 | if (status !== 200) throw new Error(`HTTP ${status}: ${JSON.stringify(result)}`); |
| 310 | |
| 311 | const responseStatus = result && result.ResponseStatus; |
| 312 | if (responseStatus) { |
| 313 | const code = Number(responseStatus.ErrorCode); |
| 314 | if (code !== 0) { |
| 315 | const err = new Error(`${responseStatus.Message || JSON.stringify(result)}(${responseStatus.ErrorCode})`); |
| 316 | err.raw = result; |
| 317 | throw err; |
| 318 | } |
| 319 | return result.Data; |
| 320 | } |
| 321 | |
| 322 | if (typeof result?.code !== "undefined" && Number(result.code) !== 0 && Number(result.code) !== 200) { |
| 323 | throw new Error(result.msg || result.message || JSON.stringify(result)); |
| 324 | } |
| 325 | return result?.Data ?? result?.data ?? result; |
| 326 | } |
| 327 | |
| 328 | async run() { |
| 329 | const cached = this.getCachedToken(); |
no test coverage detected