microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
1.11.2

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/networkInspector/requestBodyFormatters/graphQLFormatter.ts

72lines · 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 { IFormatter, decodeBody, FormattedBody } from "./requestBodyFormatter";
8import { OutputChannelLogger } from "../../log/OutputChannelLogger";
9import { Request, Response } from "../networkMessageData";
10import * as querystring from "querystring";
11
12/**
13 * @preserve
14 * Start region: the code is borrowed from https://github.com/facebook/flipper/blob/v0.79.1/desktop/plugins/network/RequestDetails.tsx#L704-L767
15 *
16 * Copyright (c) Facebook, Inc. and its affiliates.
17 *
18 * This source code is licensed under the MIT license found in the
19 * LICENSE file in the root directory of this source tree.
20 *
21 * @format
22 */
23
24export class GraphQLFormatter implements IFormatter {
25 constructor(private logger: OutputChannelLogger) {}
26
27 public formatRequest(request: Request, contentType: string): FormattedBody | null {
28 if (request.url.indexOf("graphql") > 0) {
29 const decoded = decodeBody(request, this.logger);
30 if (!decoded) {
31 return null;
32 }
33 const data = querystring.parse(decoded);
34 if (typeof data.variables === "string") {
35 data.variables = JSON.parse(data.variables);
36 }
37 if (typeof data.query_params === "string") {
38 data.query_params = JSON.parse(data.query_params);
39 }
40 return data;
41 }
42 return null;
43 }
44
45 public formatResponse(response: Response, contentType: string): FormattedBody | null {
46 if (
47 contentType.startsWith("application/json") ||
48 contentType.startsWith("application/hal+json") ||
49 contentType.startsWith("text/javascript") ||
50 contentType.startsWith("text/html") ||
51 contentType.startsWith("application/x-fb-flatbuffer")
52 ) {
53 let decoded = decodeBody(response, this.logger);
54 try {
55 const data = JSON.parse(decoded);
56 return data;
57 } catch (SyntaxError) {
58 // Multiple top level JSON roots, map them one by one
59 return decoded
60 .replace(/}{/g, "}\r\n{")
61 .split("\n")
62 .map(json => JSON.parse(json));
63 }
64 }
65 return null;
66 }
67}
68
69/**
70 * @preserve
71 * End region: https://github.com/facebook/flipper/blob/v0.79.1/desktop/plugins/network/RequestDetails.tsx#L704-L767
72 */