Repository navigation
Expand file tree
/
Copy pathresponseFormatUtility.ts
More file actions
128 lines (115 loc) · 4.94 KB
/
Copy pathresponseFormatUtility.ts
File metadata and controls
128 lines (115 loc) · 4.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import { createScanner, SyntaxKind } from 'jsonc-parser';
import * as os from 'os';
import { window } from 'vscode';
import { MimeUtility } from './mimeUtility';
import { isJSONString } from './misc';
const pd = require('pretty-data').pd;
export class ResponseFormatUtility {
private static readonly jsonSpecialTokenMapping = {
[SyntaxKind.OpenBraceToken]: '{',
[SyntaxKind.CloseBraceToken]: '}',
[SyntaxKind.OpenBracketToken]: '[',
[SyntaxKind.CloseBracketToken]: ']',
[SyntaxKind.ColonToken]: ':',
[SyntaxKind.CommaToken]: ',',
[SyntaxKind.NullKeyword]: 'null',
[SyntaxKind.TrueKeyword]: 'true',
[SyntaxKind.FalseKeyword]: 'false'
};
public static formatBody(body: string, contentType: string | undefined, suppressValidation: boolean): string {
if (contentType) {
if (MimeUtility.isJSON(contentType)) {
if (isJSONString(body)) {
body = this.jsonPrettify(body);
} else if (body && !suppressValidation) {
window.showWarningMessage('The content type of response is application/json, while response body is not a valid json string');
}
} else if (MimeUtility.isXml(contentType)) {
body = pd.xml(body);
} else if (MimeUtility.isCSS(contentType)) {
body = pd.css(body);
} else {
// Add this for the case that the content type of response body is not very accurate #239
if (isJSONString(body)) {
body = this.jsonPrettify(body);
}
}
}
return body;
}
private static jsonPrettify(text: string, indentSize = 2) {
const scanner = createScanner(text, true);
let indentLevel = 0;
function newLineAndIndent() {
return os.EOL + ' '.repeat(indentLevel * indentSize);
}
function scanNext(): [SyntaxKind, string] {
const token = scanner.scan();
const offset = scanner.getTokenOffset();
const length = scanner.getTokenLength();
const value = text.substr(offset, length);
return [ token, value ];
}
let [firstToken, firstTokenValue] = scanNext();
let secondToken: SyntaxKind;
let secondTokenValue: string;
let result = '';
while (firstToken !== SyntaxKind.EOF) {
[secondToken, secondTokenValue] = scanNext();
switch (firstToken) {
case SyntaxKind.OpenBraceToken:
result += this.jsonSpecialTokenMapping[firstToken];
if (secondToken !== SyntaxKind.CloseBraceToken) {
indentLevel++;
result += newLineAndIndent();
}
break;
case SyntaxKind.OpenBracketToken:
result += this.jsonSpecialTokenMapping[firstToken];
if (secondToken !== SyntaxKind.CloseBracketToken) {
indentLevel++;
result += newLineAndIndent();
}
break;
case SyntaxKind.CloseBraceToken:
case SyntaxKind.CloseBracketToken:
case SyntaxKind.NullKeyword:
case SyntaxKind.TrueKeyword:
case SyntaxKind.FalseKeyword:
result += this.jsonSpecialTokenMapping[firstToken];
if (secondToken === SyntaxKind.CloseBraceToken
|| secondToken === SyntaxKind.CloseBracketToken) {
indentLevel--;
result += newLineAndIndent();
}
break;
case SyntaxKind.CommaToken:
result += this.jsonSpecialTokenMapping[firstToken];
if (secondToken === SyntaxKind.CloseBraceToken
|| secondToken === SyntaxKind.CloseBracketToken) {
indentLevel--;
}
result += newLineAndIndent();
break;
case SyntaxKind.ColonToken:
result += this.jsonSpecialTokenMapping[firstToken] + ' ';
break;
case SyntaxKind.StringLiteral:
case SyntaxKind.NumericLiteral:
case SyntaxKind.Unknown:
result += firstTokenValue;
if (secondToken === SyntaxKind.CloseBraceToken
|| secondToken === SyntaxKind.CloseBracketToken) {
indentLevel--;
result += newLineAndIndent();
}
break;
default:
result += firstTokenValue;
}
firstToken = secondToken;
firstTokenValue = secondTokenValue;
}
return result;
}
}