Skip to content
This repository has been archived by the owner on Nov 28, 2022. It is now read-only.

Add button to open up viewed spec file #646

Merged
merged 4 commits into from
Apr 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 37 additions & 15 deletions example/src/ApiList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ class ApiList extends React.Component {
constructor(props) {
super(props);

const qs = parse(document.location.search.replace('?', ''));

this.state = {
apis: localDirectory,
selected: parse(document.location.search.replace('?', '')).selected || 'swagger-files/petstore.json',
selected: qs.selected || 'swagger-files/petstore.json',
customUrl: qs.customUrl,
};

this.changeApi = this.changeApi.bind(this);
Expand All @@ -33,33 +36,52 @@ class ApiList extends React.Component {
componentDidUpdate(prevProps, prevState) {
const { selected } = this.state;

if (prevState.selected !== selected) {
if (prevState.selected !== selected && selected !== 'enter-a-url') {
this.props.fetchSwagger(selected);
window.history.pushState('', '', `?${stringify({ selected })}`);
}
}

changeApi(e) {
this.setState({ selected: e.currentTarget.value });
this.setState({ selected: e.currentTarget.value, customUrl: false });
}

render() {
const { apis, selected } = this.state;
const { apis, selected, customUrl } = this.state;

return (
<h3>
Select an API:&nbsp;
<select onChange={this.changeApi} value={selected}>
{Object.keys(apis).map(name => {
const api = apis[name];
const preferred = api.preferred || Object.keys(api.versions)[0];
return (
<option key={name} value={api.versions[preferred].swaggerUrl}>
{name.substring(0, 30)}
</option>
);
})}
</select>
{selected === 'enter-a-url' ? (
<form style={{ display: 'inline' }}>
<input name="selected" type="url" />
<input name="customUrl" type="hidden" value="true" />
<button type="submit">Go</button>
</form>
) : (
<>
<select onChange={this.changeApi} value={selected}>
<option value="enter-a-url">Enter a URL</option>
{selected && customUrl && (
<option disabled value={selected}>
{selected}
</option>
)}

{Object.keys(apis).map(name => {
const api = apis[name];
const preferred = api.preferred || Object.keys(api.versions)[0];
return (
<option key={name} value={api.versions[preferred].swaggerUrl}>
{name.substring(0, 30)}
</option>
);
})}
</select>
&nbsp;
<a href={selected}>View document</a>
</>
)}
</h3>
);
}
Expand Down
11 changes: 8 additions & 3 deletions example/src/SpecFetcher.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const React = require('react');
const swagger2openapi = require('swagger2openapi');
const yaml = require('yaml');

const createDocs = require('../../packages/api-explorer/lib/create-docs');

Expand Down Expand Up @@ -31,9 +32,13 @@ function withSpecFetching(Component) {
.then(res => {
if (!res.ok) {
throw new Error('Failed to fetch.');
} else if (url.match(/\.(yaml|yml)/)) {
// @todo Should just try to automaticaly convert the spec if it isn't JSON.
throw new Error('Please convert your specification to JSON first.');
}

if (res.headers.get('content-type') === 'application/yaml' || url.match(/\.(yaml|yml)/)) {
this.updateStatus('Converting YAML to JSON');
return res.text().then(text => {
return yaml.parse(text);
});
}

return res.json();
Expand Down