-
Notifications
You must be signed in to change notification settings - Fork 47
/
respec-oas.js
384 lines (350 loc) · 12.7 KB
/
respec-oas.js
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
function getEndpoint({apis, path}) {
let endpoint = {
post: {
summary: 'Error: API Endpoint not defined - ' + path
}
};
for(const api of apis) {
if(api.paths[path] !== undefined) {
endpoint = api.paths[path];
}
}
return endpoint;
}
function buildComponentTables({config, document, apis}) {
const apiTables = document.querySelectorAll("table.api-component-table");
// process every table
for(const table of apiTables) {
// set up the API component table headers
const tableHeader = document.createElement('tr');
tableHeader.innerHTML = '<th>Endpoint</th><th>Expected Caller</th>';
table.appendChild(tableHeader);
// summarize each API endpoint
for(const path of table.dataset.apiPath.split(/\s+/)) {
if(path.trim().length > 0) {
const endpoint = getEndpoint({apis, path});
for(const verb in endpoint) {
var expectedCaller = endpoint[verb]['x-expectedCaller'];
const tableRow = document.createElement('tr');
if(expectedCaller === undefined)
{
expectedCaller = "Expected Caller Undefined";;
}
tableRow.innerHTML =
`<td>${verb.toUpperCase()} ${path}</td><td>${expectedCaller}</td>`;
table.appendChild(tableRow);
}
}
}
}
}
function buildApiSummaryTables({config, document, apis}) {
const apiTables = document.querySelectorAll("table.api-summary-table");
// process every table
for(const table of apiTables) {
// set up the API summary table headers
const tableHeader = document.createElement('tr');
tableHeader.innerHTML = '<th>Endpoint</th><th>Description</th>';
table.appendChild(tableHeader);
// summarize each API endpoint
for(const path of table.dataset.apiPath.split(/\s+/)) {
if(path.trim().length > 0) {
const endpoint = getEndpoint({apis, path});
for(const verb in endpoint) {
const {summary} = endpoint[verb];
const tableRow = document.createElement('tr');
tableRow.innerHTML =
`<td>${verb.toUpperCase()} ${path}</td><td>${summary}</td>`;
table.appendChild(tableRow);
}
}
}
}
}
function buildEndpointDetails({config, document, apis}) {
const apiDetailSections = document.querySelectorAll(".api-detail");
// process every detail section
for(const section of apiDetailSections) {
// detail each API endpoint
const [verb, path] = section.dataset.apiEndpoint.split(/\s+/);
const endpoint = getEndpoint({apis, path})[verb];
// summary for endpoint
const summary = document.createElement('p');
summary.innerHTML =
verb.toUpperCase() + ' ' + path + ' - ' + endpoint.summary;
section.appendChild(summary);
// schema for endpoint
if(endpoint.requestBody) {
const requestSchema =
endpoint.requestBody.content['application/json'].schema.properties ||
endpoint.requestBody.content['application/json'].schema;
const schemaSummary = document.createElement('p');
if(requestSchema.anyOf) {
schemaSummary.innerHTML = `The ${path} endpoint uses any of ` +
`the following schemas when receiving a `;
} else {
schemaSummary.innerHTML = `The ${path} endpoint uses ` +
`the following schema when receiving a `;
}
schemaSummary.innerHTML += `${verb.toUpperCase()}:`;
section.appendChild(schemaSummary);
let requestSchemaHtml = document.createElement('p');
if(requestSchema) {
if(requestSchema.anyOf) {
for(const i in requestSchema.anyOf) {
const anySchema = requestSchema.anyOf[i];
requestSchemaHtml = renderJsonSchema(anySchema.properties || anySchema);
section.appendChild(requestSchemaHtml);
if(i + 1 < requestSchema.anyOf.length) {
const nextSchemaSummary = document.createElement('p');
nextSchemaSummary.innerHTML = `Alternatively, the ${path} ` +
`endpoint can also use the following schema:`;
section.appendChild(nextSchemaSummary);
}
}
} else {
requestSchemaHtml = renderJsonSchema(requestSchema);
section.appendChild(requestSchemaHtml);
}
} else {
requestSchemaHtml.innerHTML = 'RENDERING ERROR'; // default
section.appendChild(requestSchemaHtml);
}
}
// responses for endpoint
const responsesSummary = document.createElement('p');
responsesSummary.innerHTML = `The ${path} endpoint can result ` +
`in any of these responses when receiving a ${verb.toUpperCase()}:`;
section.appendChild(responsesSummary);
const responsesTable = buildResponsesTable(endpoint);
section.appendChild(responsesTable);
}
}
/**
* Builds the responses table for an endpoint.
*
* @param {object} endpoint - An endpoint object.
*
* @returns {HTMLElement} A responses a table.
*/
function buildResponsesTable(endpoint) {
// responses for endpoint
const responsesTable = document.createElement('table');
responsesTable.setAttribute('class', 'simple');
responsesTable.innerHTML = '<tr><th>Response</th><th>Description</th><th>Body</th></tr>';
for(const response in endpoint.responses) {
const responseDetail = endpoint.responses[response];
const {description, content} = responseDetail;
const responseSchema = getResponseBodySchema(content);
const row = document.createElement('tr');
row.appendChild(textEl({text: response}));
row.appendChild(textEl({text: description}));
const data3 = document.createElement('td');
data3.appendChild(responseSchema);
row.appendChild(data3);
responsesTable.appendChild(row);
}
return responsesTable;
}
/**
* Takes in a response's content object and renders any schema found.
*
* @param {object} [content] - Response content.
*
* @returns {HTMLElement} An HTML element.
*/
function getResponseBodySchema(content) {
if(!content) {
return document.createElement('span');
}
const section = document.createElement('section');
section.style['font-size'] = '0.75rem';
return Object.entries(content).reduce((combined, [contentType, {schema}]) => {
combined.appendChild(textEl({el: 'i', text: `content-type: ${contentType}`}));
combined.appendChild(document.createElement('br'));
if(schema) {
const _el = document.createElement('td');
if(schema?.type === 'array') {
_el.innerHTML = 'Each item in the array MUST be ';
_el.innerHTML += renderJsonSchemaObject(schema.items);
} else {
_el.innerHTML = renderJsonSchemaObject(schema);
}
combined.appendChild(_el);
}
return combined;
}, section);
}
/**
* Takes in text and create an element with that textContent.
*
* @param {object} options - Options to use.
* @param {string} [options.el='td'] - An element type.
* @param {string} options.text - Text content for an element.
*
* @returns {object} An html element.
*/
function textEl({el = 'td', text}) {
const _el = document.createElement(el);
_el.textContent = text;
return _el;
}
function renderJsonSchema(schema) {
const schemaToRender = schema;
const requestSchemaTable = document.createElement('table');
const tableHeader = document.createElement('thead');
const tableBody = document.createElement('tbody');
requestSchemaTable.classList.add('simple');
tableHeader.innerHTML = '<tr><th>Property</th><th>Description</th></tr>';
// render every property in the complex schema
for(const property in schemaToRender) {
if(property === 'example') {
continue;
}
const subSchema = schema[property];
let propertyRendering = `<code>${property}</code>`;
let valueRendering = 'Error: JSON Schema value rendering failure.'; // default
if(subSchema.type === 'object') {
propertyRendering = `<code>${property}</code> [object]`;
valueRendering = renderJsonSchemaObject(subSchema);
} else if(Array.isArray(subSchema)) {
for(const i in subSchema) {
const schemaItem = subSchema[i];
if(i < 1) {
valueRendering = '';
} else if(property === 'allOf') {
propertyRendering = 'All of';
valueRendering += ' and ';
} else {
propertyRendering = 'Any of';
valueRendering += ' or ';
}
valueRendering += renderJsonSchemaObject(schemaItem);
}
} else {
valueRendering = '<pre>' + JSON.stringify(subSchema, null, 2) + '</pre>';
}
tableBody.innerHTML +=
`<tr><td style='vertical-align: top;'>${propertyRendering}</td>` +
`<td>${valueRendering}</td></tr>`;
}
requestSchemaTable.appendChild(tableHeader);
requestSchemaTable.appendChild(tableBody);
return requestSchemaTable;
}
function renderJsonSchemaObject(schema) {
let objectRendering = '';
if(schema.anyOf) {
let collectedSchemas = '';
for(const index in schema.anyOf) {
const item = schema.anyOf[index];
collectedSchemas += renderJsonSchemaObject(item);
// if there are more items in anyOf add an or
if((index + 1) < schema.anyOf.length) {
collectedSchemas += ' or ';
}
}
return collectedSchemas;
}
if(schema.allOf) {
const mergedSchema = {
type: 'object',
properties: {}
};
for(item of schema.allOf) {
for(property in item.properties) {
mergedSchema.properties[property] = item.properties[property];
}
}
objectRendering = renderJsonSchemaObject(mergedSchema);
} else if(schema.oneOf) {
objectRendering += ' either ';
let itemCount = 0;
for(const item of schema.oneOf) {
if(item.type === 'string') {
objectRendering += 'a string';
} else if(item.type === 'object') {
objectRendering += renderJsonSchemaObject(item);
} else if(item.type === 'array') {
objectRendering += 'an array';
if(item.items) {
objectRendering += ` of ${item.items.type}(s)`;
}
}
itemCount += 1;
if(itemCount < schema.oneOf.length) {
objectRendering += ' or ';
}
}
} else if(schema.type === 'object') {
if(!schema.properties) {
if(schema.description) {
objectRendering = schema.description.replace(/\.$/, "") +
' (an object)';
} else {
objectRendering = 'an object';
}
} else {
objectRendering += 'an object of the following form: <dl>';
for(const property in schema.properties) {
const value = schema.properties[property];
objectRendering += renderJsonSchemaProperty(property, value);
}
objectRendering += '</dl>';
}
} else {
objectRendering = '<pre>' + JSON.stringify(schema, null, 2) + '</pre>';
}
return objectRendering;
}
function renderJsonSchemaProperty(property, value) {
let propertyRendering = `<dt><code>${property}</code> [${value.type}]</dt>`;
propertyRendering += '<dd>' + value.description + ' ' +
renderJsonSchemaValue(property, value) + '</dd>';
return propertyRendering;
}
function renderJsonSchemaValue(property, value) {
let valueRendering = '';
if(value.type === 'array') {
valueRendering = `Each item in the <code>${property}</code> array MUST be `;
if(value.items.type === 'object') {
valueRendering += 'an object of the following form:';
//typeDetails += renderJsonSchemaValue(property, value);
} else if(value.items.type === 'string') {
valueRendering += 'a string.';
} else {
valueRendering += `a ${value.items.type}:`;
}
} else if(value.type === 'object') {
valueRendering =
`The <code>${property}</code> object MUST be `;
valueRendering += renderJsonSchemaObject(value);
} else if(value.type === 'string' || value.type === 'boolean') {
// no-op
} else {
valueRendering = '<pre>' + JSON.stringify(value, null, 2) + '</pre>';
}
return valueRendering;
}
async function injectOas(config, document) {
try {
const issuerApi = await SwaggerParser.validate('issuer.yml');
console.log('API name: %s, Version: %s',
issuerApi.info.title, issuerApi.info.version);
const exchangesApi = await SwaggerParser.validate('exchanges.yml');
console.log('API name: %s, Version: %s',
exchangesApi.info.title, exchangesApi.info.version);
const verifierApi = await SwaggerParser.validate('verifier.yml');
console.log('API name: %s, Version: %s',
verifierApi.info.title, verifierApi.info.version);
const holderApi = await SwaggerParser.validate('holder.yml');
console.log('API name: %s, Version: %s',
holderApi.info.title, holderApi.info.version);
const apis = [issuerApi, verifierApi, holderApi, exchangesApi];
buildApiSummaryTables({config, document, apis});
buildEndpointDetails({config, document, apis});
buildComponentTables({config, document, apis});
} catch(err) {
console.error(err);
}
}