-
Notifications
You must be signed in to change notification settings - Fork 0
/
openapiCleaner.js
61 lines (53 loc) · 1.79 KB
/
openapiCleaner.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
import { fromEvent, map, tap } from "https://esm.sh/[email protected]";
import { cleanOpenAPI } from "./cleanOpenAPI.js";
import { JSON_SCHEMA, dump, load } from "https://esm.sh/[email protected]";
function setValidation(badge, state, message) {
badge.className = `badge text-bg-${state}`;
badge.textContent = message;
}
const cleanOpenAPIAndPrint = (() => {
const root = document.forms.cleaned;
root.addEventListener("submit", event => event.preventDefault());
const area = root.elements.area;
const badge = root.querySelector("[el=badge]");
const copy = root.querySelector("[el=copyAction]");
fromEvent(copy, 'click')
.pipe(
tap(event => event.preventDefault())
)
.subscribe(() => {
if (navigator && navigator.clipboard && navigator.clipboard.writeText) {
navigator.clipboard.writeText(area.value);
} else {
area.select();
document.execCommand("copy");
}
});
/**
* @param {string} source
*/
function cleanOpenAPIAndPrint$(source) {
const cleaned = cleanOpenAPI(load(source, {schema: JSON_SCHEMA}));
if (cleaned === null) {
setValidation(badge, "danger", "Cannot clean OpenAPI");
} else {
try {
area.value = dump(cleaned, {indent: 2, schema: JSON_SCHEMA, noRefs: true});
setValidation(badge, "success", "JWT decoded");
} catch (e) {
setValidation(badge, "danger", `Error caught: ${e.message}`);
}
}
}
return cleanOpenAPIAndPrint$;
})();
{
const root = document.forms.source;
root.addEventListener("submit", event => event.preventDefault());
const area = root.elements.area;
const badge = root.querySelector("[el=badge]");
setValidation(badge, "success", "Ready");
fromEvent(area, 'blur')
.pipe(map(() => area.value))
.subscribe(cleanOpenAPIAndPrint);
}