| 314 | } |
| 315 | |
| 316 | private parseBody( |
| 317 | request: HttpRequest<any>, |
| 318 | binContent: Uint8Array<ArrayBuffer>, |
| 319 | contentType: string, |
| 320 | status: number, |
| 321 | ): string | ArrayBuffer | Blob | object | null { |
| 322 | switch (request.responseType) { |
| 323 | case 'json': |
| 324 | // stripping the XSSI when present |
| 325 | const text = new TextDecoder().decode(binContent).replace(XSSI_PREFIX, ''); |
| 326 | if (text === '') { |
| 327 | return null; |
| 328 | } |
| 329 | try { |
| 330 | return JSON.parse(text) as object; |
| 331 | } catch (e: unknown) { |
| 332 | // Allow handling non-JSON errors (!) as plain text, same as the XHR |
| 333 | // backend. Without this special sauce, any non-JSON error would be |
| 334 | // completely inaccessible downstream as the `HttpErrorResponse.error` |
| 335 | // would be set to the `SyntaxError` from then failing `JSON.parse`. |
| 336 | if (status < 200 || status >= 300) { |
| 337 | return text; |
| 338 | } |
| 339 | throw e; |
| 340 | } |
| 341 | case 'text': |
| 342 | return new TextDecoder().decode(binContent); |
| 343 | case 'blob': |
| 344 | return new Blob([binContent], {type: contentType}); |
| 345 | case 'arraybuffer': |
| 346 | return binContent.buffer; |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | private createRequestInit(req: HttpRequest<any>): RequestInit { |
| 351 | if (req.reportUploadProgress) { |