(config: RequestConfig, reqId?: string)
| 91 | } |
| 92 | |
| 93 | private async sendRequest<T>(config: RequestConfig, reqId?: string) { |
| 94 | try { |
| 95 | // Force request! |
| 96 | if (!reqId) { |
| 97 | const { data: result } = await axios<PacketProtocol<T>>(config); |
| 98 | return result?.data; |
| 99 | } |
| 100 | |
| 101 | // Request cache |
| 102 | const startTime = Date.now(); |
| 103 | if (!config.timeout) config.timeout = 1000 * 30; |
| 104 | const { data: result } = await axios<PacketProtocol<T>>(config); |
| 105 | const endTime = Date.now(); |
| 106 | const reqSpeed = endTime - startTime; |
| 107 | if (reqSpeed < this.REQUEST_CACHE_TIME) await this.wait(this.REQUEST_CACHE_TIME - reqSpeed); |
| 108 | const realData = result.data; |
| 109 | this.responseMap.set(reqId, { |
| 110 | timestamp: Date.now(), |
| 111 | data: realData |
| 112 | }); |
| 113 | this.event.emit(reqId, realData); |
| 114 | } catch (error: AxiosError | Error | any) { |
| 115 | const axiosErr = error as AxiosError; |
| 116 | const otherErr = error as Error | any; |
| 117 | if (axiosErr?.response?.data) { |
| 118 | const protocol = axiosErr?.response?.data as IPanelResponseProtocol; |
| 119 | if (protocol.data && protocol.status !== 200) { |
| 120 | this.throwRequestError(reqId, String(protocol.data)); |
| 121 | return; |
| 122 | } |
| 123 | } |
| 124 | this.throwRequestError(reqId, otherErr); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | private throwRequestError(reqId?: string, error?: any) { |
| 129 | if (!(error instanceof Error)) error = new Error(error); |
no test coverage detected