Skip to content

Commit 1b076e2

Browse files
linter fixes
1 parent 047fe08 commit 1b076e2

File tree

3 files changed

+51
-16
lines changed

3 files changed

+51
-16
lines changed

demo/package.json

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"dependencies": {
1313
"@elastic/request-converter": "file:..",
1414
"bootstrap": "^5.3.3",
15+
"compare-versions": "^6.1.1",
1516
"react": "^19.0.0",
1617
"react-bootstrap": "^2.10.9",
1718
"react-dom": "^19.0.0",
@@ -21,6 +22,7 @@
2122
"@eslint/js": "^9.19.0",
2223
"@types/react": "^19.0.8",
2324
"@types/react-dom": "^19.0.3",
25+
"@types/react-syntax-highlighter": "^15.5.13",
2426
"@vitejs/plugin-react": "^4.3.4",
2527
"eslint": "^9.19.0",
2628
"eslint-plugin-react": "^7.37.4",

demo/src/App.tsx

+48-15
Original file line numberDiff line numberDiff line change
@@ -6,46 +6,72 @@ import Stack from 'react-bootstrap/Stack';
66
import Form from 'react-bootstrap/Form';
77
import SyntaxHighlighter from 'react-syntax-highlighter';
88
import { atomOneLight } from 'react-syntax-highlighter/dist/esm/styles/hljs';
9+
import { compareVersions } from 'compare-versions';
910
import { convertRequests, loadSchema, listFormats } from "@elastic/request-converter";
1011

1112
function App() {
1213
const formats = listFormats().sort();
13-
const [hasSchema, setHasSchema] = useState(false);
14-
const [source, setSource] = useState('GET /_search');
15-
const [code, setCode] = useState('Loading...');
16-
const [language, setLanguage] = useState(formats[0]);
14+
const [versions, setVersions] = useState<string[]>(['main']);
15+
const [schemaVersion, setSchemaVersion] = useState<string>('main');
16+
const [hasSchema, setHasSchema] = useState<boolean>(false);
17+
const [source, setSource] = useState<string>('GET /_search');
18+
const [error, setError] = useState<string | null>(null);
19+
const [code, setCode] = useState<string>('Loading...');
20+
const [language, setLanguage] = useState<string>(formats[0]);
1721

1822
useEffect(() => {
19-
document.getElementById("source").focus();
23+
const sourceElem = document.getElementById("source");
24+
if (sourceElem) {
25+
sourceElem.focus();
26+
}
2027
}, []);
2128

2229
useEffect(() => {
23-
(async () => {
24-
const response = await fetch('https://raw.githubusercontent.com/elastic/elasticsearch-specification/refs/heads/main/output/schema/schema.json');
30+
void (async () => {
31+
const r = await fetch('https://api.github.com/repos/elastic/elasticsearch-specification/branches');
32+
if (r.status === 200) {
33+
const s: {name: string}[] = (await r.json()) as {name: string}[];
34+
const versions = s.map(v => v.name).filter(v => v.match(/^[0-9]+\.[0-9x]+$/));
35+
setVersions(['main'].concat(versions.sort(compareVersions).reverse()));
36+
}
37+
})();
38+
}, []);
39+
40+
useEffect(() => {
41+
void (async () => {
42+
const response = await fetch(`https://raw.githubusercontent.com/elastic/elasticsearch-specification/refs/heads/${schemaVersion}/output/schema/schema.json`);
2543
if (response.ok) {
26-
loadSchema(await response.json());
44+
setHasSchema(false);
45+
await loadSchema((await response.json()) as object);
2746
setHasSchema(true);
2847
}
2948
})();
30-
}, []);
49+
}, [schemaVersion]);
3150

3251
useEffect(() => {
33-
(async () => {
52+
void (async () => {
3453
if (hasSchema) {
3554
let c: string = "";
3655
try {
3756
c = await convertRequests(source, language, {complete: true, printResponse: true}) as string;
57+
setError(null);
3858
}
3959
catch (err) {
60+
if (err instanceof Error) {
61+
setError(err.toString());
62+
}
63+
else {
64+
setError('Uknown error');
65+
}
4066
}
4167
if (c) {
42-
setCode(c as string);
68+
setCode(c);
4369
}
4470
}
4571
})();
4672
}, [hasSchema, source, language]);
4773

48-
const onRequestChanged = (ev: React.ChangeEventHandler<HTMLSelectElement>) => {
74+
const onRequestChanged = (ev: React.ChangeEvent<HTMLSelectElement>) => {
4975
setSource(ev.target.value);
5076
ev.target.style.height = ev.target.scrollHeight + "px";
5177
};
@@ -55,13 +81,19 @@ function App() {
5581
<h1>Elasticsearch Request Converter Demo</h1>
5682
<Row>
5783
<Col className="col-6">
58-
Source Request
84+
<Stack direction="horizontal" className="heading">
85+
<p>Source&nbsp;Request</p>
86+
<p className="spacer"></p>
87+
<Form.Select id="version-choice" size="sm" defaultValue="main" onChange={ev => setSchemaVersion(ev.target.value)}>
88+
{versions.map(v => <option key={v} value={v}>{v}</option>)}
89+
</Form.Select>
90+
</Stack>
5991
</Col>
6092
<Col className="col-6">
6193
<Stack direction="horizontal" className="heading">
6294
<p>Converted&nbsp;Code</p>
6395
<p className="spacer"></p>
64-
<Form.Select id="language-choice" size="sm" defaultValue={language} onChange={ev => setLanguage(ev.currentTarget.value)}>
96+
<Form.Select id="language-choice" size="sm" defaultValue={language} onChange={ev => setLanguage(ev.target.value)}>
6597
{formats.map(fmt => <option key={fmt} value={fmt}>{fmt}</option>)}
6698
</Form.Select>
6799
</Stack>
@@ -70,7 +102,8 @@ function App() {
70102
<Form id="main-form">
71103
<Row id="main-row">
72104
<Col className="col-6">
73-
<Form.Control as="textarea" id="source" value={source} onChange={onRequestChanged} />
105+
<Form.Control className={error ? "is-invalid" : ""} as="textarea" id="source" value={source} onChange={(ev: any) => onRequestChanged(ev)} />
106+
{error && <Form.Control.Feedback type="invalid">{error}</Form.Control.Feedback>}
74107
</Col>
75108
<Col className="col-6">
76109
<SyntaxHighlighter wrapLongLines={true} language={language} style={atomOneLight}>{code}</SyntaxHighlighter>

demo/src/index.css

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ textarea.form-control {
3030
width: 100%;
3131
}
3232

33-
#language-choice {
33+
#version-choice, #language-choice {
3434
width: 150px;
3535
}
3636

0 commit comments

Comments
 (0)