MCPcopy Create free account
hub / github.com/angular/angular / parseBody

Method parseBody

packages/common/http/src/fetch.ts:337–369  ·  view source on GitHub ↗
(
    request: HttpRequest<any>,
    binContent: Uint8Array<ArrayBuffer>,
    contentType: string,
    status: number,
  )

Source from the content-addressed store, hash-verified

335 }
336
337 private parseBody(
338 request: HttpRequest<any>,
339 binContent: Uint8Array<ArrayBuffer>,
340 contentType: string,
341 status: number,
342 ): string | ArrayBuffer | Blob | object | null {
343 switch (request.responseType) {
344 case 'json':
345 // stripping the XSSI when present
346 const text = new TextDecoder().decode(binContent).replace(XSSI_PREFIX, '');
347 if (text === '') {
348 return null;
349 }
350 try {
351 return JSON.parse(text) as object;
352 } catch (e: unknown) {
353 // Allow handling non-JSON errors (!) as plain text, same as the XHR
354 // backend. Without this special sauce, any non-JSON error would be
355 // completely inaccessible downstream as the `HttpErrorResponse.error`
356 // would be set to the `SyntaxError` from then failing `JSON.parse`.
357 if (status < 200 || status >= 300) {
358 return text;
359 }
360 throw e;
361 }
362 case 'text':
363 return getTextDecoder(contentType).decode(binContent);
364 case 'blob':
365 return new Blob([binContent], {type: contentType});
366 case 'arraybuffer':
367 return binContent.buffer;
368 }
369 }
370
371 private createRequestInit(req: HttpRequest<any>): RequestInit {
372 if (req.reportUploadProgress) {

Callers 1

doRequestMethod · 0.95

Calls 3

getTextDecoderFunction · 0.85
parseMethod · 0.65
replaceMethod · 0.45

Tested by

no test coverage detected