-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
204 lines (178 loc) · 7.96 KB
/
index.html
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<!DOCTYPE html>
<head>
<title>GraphQL请求体转换</title>
<meta charset="utf-8">
<link rel="stylesheet" href="./static/css/bootstrap.min.css">
<script src="./static/js/fetch.min.js"></script>
<script src="./static/js/jquery.min.js"></script>
<script src="./static/js/bootstrap.min.js"></script>
<style>
.main-board {
margin: 60px 20% 0px 20%;
}
.space-line {
height: 20px;
}
</style>
</head>
<body>
<div class="main-board">
<textarea id="console-body" class="form-control" rows="8" placeholder="填入http的请求体"></textarea>
<div class="space-line"></div>
<button id="magic-button" type="button" class="btn btn-success" onclick="magic_show()">转换</button>
<div class="space-line"></div>
<textarea id="result-body" class="form-control" rows="15" placeholder="获取GraphQL可用的json格式"></textarea>
<div class="space-line"></div>
<button id="copy-button" type="button" class="btn btn-info" onclick="copy_result()">复制转换结果</button>
</div>
<script type="text/javascript">
function remove_two_quota(little_str) {
return little_str.replace('"', '').replace('"', '')
}
function firstToSmall(little_str) {
first_letter = little_str.charAt(0)
left_letter = little_str.substring(1)
return first_letter.toLowerCase() + left_letter
}
function find_index(str, cha, num) {
var x = str.indexOf(cha);
for (var i = 0; i < num; i++) {
x = str.indexOf(cha, x + 1);
}
return x;
}
function new_line_space(num) {
var rr = ""
for (var i = 0; i < num * 2; i++) {
rr += " "
}
return rr;
}
function magic_show() {
console.log("show")
consoleText = document.getElementById('console-body').value
resultBody = ""
if (consoleText.includes('mutation')) {
console.log('this is mutation')
resultBody += "mutation{\n"
consoleTextJson = JSON.parse(consoleText)
if (!consoleText.includes('operationName')) {
// 不包含operationName 是因为直接用的GraphQL的请求页面,不是React发出的请求
queryStr = JSON.stringify(consoleTextJson['query'])
queryStr = queryStr.replace(/"/g, '')
queryStr = queryStr.replace(/\\n/g, '\n')
queryStr = queryStr.replace(/\\/g, '"')
document.getElementById("result-body").value = queryStr
return ""
}
console.log(consoleTextJson['variables'])
// 获取query的方法名
operationName = consoleTextJson['operationName']
resultBody += new_line_space(1) + firstToSmall(remove_two_quota(JSON.stringify(operationName)))
console.log(resultBody)
// 获取query的参数
variables = consoleTextJson['variables']
resultBody += "(\n" + new_line_space(2)
var_str = JSON.stringify(variables)
var_str = var_str.substring(1, var_str.length - 1)
// 先用冒号分隔
var_strs = var_str.split(":")
var space_num = 2
for (var index in var_strs) {
small_str = var_strs[index]
// 去掉key值上多余的冒号
if (index < var_strs.length - 1) {
small_str = small_str.replace(/(.*)\"/, '$1');
small_str = small_str.replace(/(.*)\"/, '$1');
}
// 换行操作,格式更好看一点
if (small_str.includes('{')) {
space_num = space_num + 1
small_str = small_str.replace("{", "{\n" + new_line_space(space_num))
}
if (small_str.includes(",")) {
small_str = small_str.replace(",", ",\n" + new_line_space(space_num))
}
// 拼接起来加冒号
if (index == 0) {
resultBody += small_str
} else {
resultBody += ": " + small_str
}
}
resultBody += "\n ){\n"
// 返回体结构
queryStr = JSON.stringify(consoleTextJson['query'])
start_index = find_index(queryStr, "{", 1)
queryStr = queryStr.substring(start_index + 3, queryStr.length - 11)
queryStr = queryStr.replace(/\\n/g, '\n')
resultBody += queryStr
resultBody += "\n }"
resultBody += "\n}"
document.getElementById("result-body").value = resultBody
return ""
}
if (consoleText.includes('query')) {
console.log('this is query')
consoleTextJson = JSON.parse(consoleText)
resultBody = ""
console.log(consoleTextJson)
console.log(consoleTextJson['variables'])
if (!consoleText.includes('operationName')) {
// 不包含operationName 是因为直接用的GraphQL的请求页面,不是React发出的请求
queryStr = JSON.stringify(consoleTextJson['query'])
queryStr = queryStr.replace(/"/g, '')
queryStr = queryStr.replace(/\\n/g, '\n')
document.getElementById("result-body").value = queryStr
return ""
}
resultBody += "query{\n"
// 获取query的方法名
operationName = consoleTextJson['operationName']
resultBody += " " + firstToSmall(remove_two_quota(JSON.stringify(operationName)))
console.log(resultBody)
// 获取query的参数
variables = consoleTextJson['variables']
if (Object.keys(variables).length == 0) {
resultBody += "{\n"
} else {
resultBody += "("
for (var key in variables) {
console.log(key)
value = variables[key]
if (isNaN(value)) {
resultBody += key + ": \"" + variables[key] + "\", "
} else {
resultBody += key + ": " + variables[key] + ", "
}
}
resultBody = resultBody.substring(0, resultBody.length - 2)
resultBody += "){\n"
}
// 返回体结构
queryStr = JSON.stringify(consoleTextJson['query'])
start_index = find_index(queryStr, "{", 1)
queryStr = queryStr.substring(start_index + 3, queryStr.length - 11)
queryStr = queryStr.replace(/\\n/g, '\n')
resultBody += queryStr
resultBody += "\n }"
resultBody += "\n}"
document.getElementById("result-body").value = resultBody
return ""
}
if (resultBody == "") {
alert("格式不正确吧?没有query或者mutation")
} else {
document.getElementById("result-body").value = resultBody
}
}
function copy_result() {
const textArea = document.querySelector('#result-body');
textArea.select();
if (document.execCommand('copy')) {
document.execCommand('copy');
console.log('复制成功');
}
}
</script>
</body>