-
Notifications
You must be signed in to change notification settings - Fork 4
/
redoc-swagger.html
241 lines (211 loc) · 8.69 KB
/
redoc-swagger.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>CB-Tumblebug API Dashboard</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
display: flex;
flex-direction: column;
height: 100vh;
width: 100vw;
overflow: hidden;
}
#title-bar {
height: 50px; /* Height for the title bar */
background-color: #262626;
color: #fcfcfc;
display: flex;
align-items: center;
justify-content: center;
font-size: 20px;
font-weight: bold;
}
#content-container {
display: flex;
height: calc(100vh - 50px); /* Remaining height after title bar */
width: 100%;
}
#redoc-container {
width: 65%; /* Redoc is now on the left side */
height: 100%;
overflow-y: scroll;
border-right: 1px solid #ddd;
}
#swagger-container {
width: 35%; /* Swagger UI is now on the right side */
height: 100%;
display: flex;
flex-direction: column;
}
#swagger-header {
height: 40px;
background-color: #ffffff;
color: rgb(0, 0, 0);
display: flex;
align-items: center;
justify-content: center;
z-index: 1000;
position: sticky; /* Make header stick to the top */
top: 0;
}
#swagger-ui-container {
height: calc(100% - 40px); /* Adjust height based on header */
overflow-y: scroll;
}
</style>
<!-- Load Swagger UI styles -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/5.17.14/swagger-ui.css">
</head>
<body>
<div id="title-bar"> CB-Tumblebug API Dashboard</div>
<div id="content-container">
<div id="redoc-container"></div>
<div id="swagger-container">
<div id="swagger-header">Try out API with Swagger UI</div>
<div id="swagger-ui-container"></div>
</div>
</div>
<!-- Load the latest Redoc and Swagger UI scripts -->
<script src="https://cdn.jsdelivr.net/npm/redoc@next/bundles/redoc.standalone.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/5.17.14/swagger-ui-bundle.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/5.17.14/swagger-ui-standalone-preset.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js-yaml.min.js"></script>
<script>
// Basic authentication details
const username = 'default';
const password = 'default';
const credentials = btoa(username + ':' + password);
const authHeader = 'Basic ' + credentials;
console.log('Starting to fetch YAML');
const protocol = window.location.protocol;
const hostname = window.location.hostname;
const apiPort = '1323';
const apiDocPath = '/tumblebug/api/doc.yaml';
const apiDocUrl = `${protocol}//${hostname}:${apiPort}${apiDocPath}`;
console.log('API Document URL:', apiDocUrl);
// Fetch the YAML file with authentication
fetch(apiDocUrl, {
headers: {
'Authorization': authHeader
}
})
.then(response => {
console.log('Fetch response:', response);
if (!response.ok) {
throw new Error('Authentication failed: ' + response.statusText);
}
return response.text();
})
.then(yamlText => {
console.log('YAML fetched successfully');
const spec = jsyaml.load(yamlText);
console.log('YAML parsed successfully');
// Initialize Swagger UI (placed on the right side)
const ui = SwaggerUIBundle({
url: apiDocUrl,
dom_id: '#swagger-ui-container',
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset
],
docExpansion: "list",
layout: "BaseLayout",
deepLinking: true, // Enable deep linking
//displayOperationId: true, // Display operationId in Swagger UI
tagsSorter: "alpha",
operationsSorter: "alpha",
requestInterceptor: (request) => {
request.headers['Authorization'] = authHeader;
return request;
},
validatorUrl: null,
onComplete: () => {
console.log('Swagger UI loaded');
}
});
console.log('Swagger UI initialized');
// Initialize Redoc (placed on the left side)
Redoc.init(spec, {
scrollYOffset: 50,
hideLoading: true,
suppressWarnings: true,
requiredPropsFirst: true,
sortPropsAlphabetically: true,
expandResponses: "200",
// pathInMiddlePanel: true,
theme: {
typography: {
fontFamily: '"Arial", Helvetica, Open Sans, sans-serif',
headings: {
fontFamily: '"Arial", "Roboto Condensed", sans-serif'
},
code: {
fontFamily: '"Arial", "Courier", monospace'
}
},
rightPanel: {
width: '40%',
backgroundColor: '#6c757d',
textColor: '#f8f9fa'
},
codeSample: {
backgroundColor: '#343a40'
}
}
}, document.getElementById('redoc-container'));
console.log('Redoc initialized');
// Function to remove special characters from a string
function removeSpecialCharacters(str) {
return str.replace(/[^a-zA-Z0-9]/g, '');
}
// Use MutationObserver to detect URL hash changes
const observer = new MutationObserver(() => {
const hash = window.location.hash;
if (hash.startsWith('#tag')) { // Only proceed if the hash starts with #tag
const operationId = hash.split('/').pop();
// Find the corresponding operation in Swagger UI based on the operationId
const allOperations = document.querySelectorAll('#swagger-ui-container [id]');
let targetOperation = null;
allOperations.forEach(op => {
const idAttribute = op.getAttribute('id');
const idParts = idAttribute.split('-');
const lastIdPart = idParts.pop();
// Check if the last part of the id matches the operationId
if (lastIdPart === operationId) {
targetOperation = op;
}
if (idAttribute.includes('operations-tag-')) {
const idWithoutPrefix = idAttribute.replace('operations-tag-', '');
const idWithoutSpecialChars = removeSpecialCharacters(idWithoutPrefix);
const operationIdWithoutSpecialChars = removeSpecialCharacters(operationId);
if (!targetOperation && idWithoutSpecialChars === operationIdWithoutSpecialChars) {
targetOperation = op;
}
}
});
if (targetOperation) {
console.log('Target found for operationId:', operationId);
targetOperation.scrollIntoView({ behavior: 'smooth' });
} else {
console.log('Target not found for operationId:', operationId);
}
}
});
// Observe changes to the URL hash
observer.observe(document.querySelector('body'), {
attributes: true,
childList: true,
subtree: true
});
})
.catch(error => {
console.error('Failed to load API specification:', error);
alert('Failed to load API specification: ' + error.message);
});
</script>
</body>
</html>