(response: Response, responseType: ResponseType, parseJson: ParseJsonFunction, encoding?: BufferEncoding)
| 156 | }; |
| 157 | |
| 158 | export const parseBody = (response: Response, responseType: ResponseType, parseJson: ParseJsonFunction, encoding?: BufferEncoding): unknown => { |
| 159 | const {rawBody} = response; |
| 160 | const cachedDecodedBody = decodedBodyCache.get(response); |
| 161 | |
| 162 | try { |
| 163 | if (responseType === 'text') { |
| 164 | if (cachedDecodedBody !== undefined) { |
| 165 | return cachedDecodedBody; |
| 166 | } |
| 167 | |
| 168 | return decodeUint8Array(rawBody, encoding); |
| 169 | } |
| 170 | |
| 171 | if (responseType === 'json') { |
| 172 | if (rawBody.length === 0) { |
| 173 | return ''; |
| 174 | } |
| 175 | |
| 176 | const text = cachedDecodedBody ?? decodeUint8Array(rawBody, encoding); |
| 177 | return parseJson(text); |
| 178 | } |
| 179 | |
| 180 | if (responseType === 'buffer') { |
| 181 | return rawBody; |
| 182 | } |
| 183 | } catch (error) { |
| 184 | throw new ParseError(error as Error, response); |
| 185 | } |
| 186 | |
| 187 | throw new ParseError({ |
| 188 | message: `Unknown body type '${responseType as string}'`, |
| 189 | name: 'Error', |
| 190 | }, response); |
| 191 | }; |
no test coverage detected
searching dependent graphs…