microsoft/vscode-react-native

Public

mirrored from https://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
improve-configuration

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

src/extension/networkInspector/requestBodyFormatters/jsonFormatter.ts

55lines · modecode

1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT license. See LICENSE file in the project root for details.
3
4/* eslint-disable */
5/* eslint-enable prettier/prettier*/
6
7import { OutputChannelLogger } from "../../log/OutputChannelLogger";
8import { Request, Response } from "../networkMessageData";
9import { IFormatter, decodeBody, FormattedBody } from "./requestBodyFormatter";
10
11/**
12 * @preserve
13 * Start region: the code is borrowed from https://github.com/facebook/flipper/blob/v0.79.1/desktop/plugins/network/RequestDetails.tsx#L609-L653
14 *
15 * Copyright (c) Facebook, Inc. and its affiliates.
16 *
17 * This source code is licensed under the MIT license found in the
18 * LICENSE file in the root directory of this source tree.
19 *
20 * @format
21 */
22
23export class JSONFormatter implements IFormatter {
24 constructor(private logger: OutputChannelLogger) {}
25
26 public formatRequest(request: Request, contentType: string): FormattedBody | null {
27 return this.format(decodeBody(request, this.logger), contentType);
28 }
29
30 public formatResponse(response: Response, contentType: string): FormattedBody | null {
31 return this.format(decodeBody(response, this.logger), contentType);
32 }
33
34 private format(body: string, contentType: string): FormattedBody | null {
35 if (
36 contentType.startsWith("application/json") ||
37 contentType.startsWith("application/hal+json") ||
38 contentType.startsWith("text/javascript") ||
39 contentType.startsWith("application/x-fb-flatbuffer")
40 ) {
41 try {
42 return JSON.parse(body);
43 } catch (SyntaxError) {
44 // Multiple top level JSON roots, map them one by one
45 return body.split("\n").map(json => JSON.parse(json));
46 }
47 }
48 return null;
49 }
50}
51
52/**
53 * @preserve
54 * End region: https://github.com/facebook/flipper/blob/v0.79.1/desktop/plugins/network/RequestDetails.tsx#L609-L653
55 */
56