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

Commit

Permalink
Codeclimate fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Dom Harrington committed Nov 2, 2017
1 parent 70e602c commit 1d7d91e
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,12 @@ describe('examples', () => {
secondTab.simulate('click', { preventDefault() {} });

expect(codeSampleResponseTabs.state('exampleTab')).toBe(1);
expect(codeSampleResponseTabs.find('a').first().hasClass('selected')).toEqual(false);
expect(
codeSampleResponseTabs
.find('a')
.first()
.hasClass('selected'),
).toEqual(false);

expect(firstTab.find('span.httpsuccess').length).toBe(1);
expect(secondTab.find('span.httperror').length).toBe(1);
Expand Down
2 changes: 1 addition & 1 deletion packages/api-explorer-ui/__tests__/Response.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const petstore = require('./fixtures/petstore/oas.json');
const oas = new Oas(petstore);

const props = {
operation: oas.operation('/pet/{petId}', 'get')
operation: oas.operation('/pet/{petId}', 'get'),
};

describe('selectedStatus', () => {
Expand Down
16 changes: 4 additions & 12 deletions packages/api-explorer-ui/src/Doc.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,9 @@ class Doc extends React.Component {
});
}
onSubmit() {
if (
!isAuthReady(
this.oas.operation(this.props.doc.swagger.path, this.props.doc.api.method),
this.state.formData.auth,
)
) {
const operation = this.oas.operation(this.props.doc.swagger.path, this.props.doc.api.method);

if (!isAuthReady(operation, this.state.formData.auth)) {
this.setState({ showAuthBox: true });
setTimeout(() => {
this.authInput.focus();
Expand All @@ -58,12 +55,7 @@ class Doc extends React.Component {

this.setState({ loading: true, showAuthBox: false, needsAuth: false });

const har = oasToHar(
this.oas,
this.oas.operation(this.props.doc.swagger.path, this.props.doc.api.method),
this.state.formData,
{ proxyUrl: true },
);
const har = oasToHar(this.oas, operation, this.state.formData, { proxyUrl: true });

return fetchHar(har).then(async res => {
this.setState({
Expand Down
81 changes: 43 additions & 38 deletions packages/api-explorer-ui/src/Response.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,35 +23,52 @@ class ResponseSchema extends React.Component {
this.selectedStatus(e.target.value);
}

// TODO https://github.com/readmeio/api-explorer/issues/43
// renderSchema() {
// let schema;
//
// try {
// if (operation.responses[this.state.selectedStatus].content) {
// if (
// operation.responses[this.state.selectedStatus].content['application/json'].schema.type ===
// 'object' &&
// operation.responses[this.state.selectedStatus].content['application/json'].schema
// .properties
// ) {
// schema =
// operation.responses[this.state.selectedStatus].content['application/json'].schema
// .properties;
// }
// } else if (
// operation.responses[this.state.selectedStatus].content['application/xml'].schema.type ===
// 'object' &&
// operation.responses[this.state.selectedStatus].content['application/xml'].schema.properties
// ) {
// schema =
// operation.responses[this.state.selectedStatus].content['application/xml'].schema
// .properties;
// }
// } catch (e) {} // eslint-disable-line no-empty

/* {schema && (
<table>
{swaggerUtils.convertToParams([response], 'response').forEach(param => {
<tr>
<th>param.name</th>
<td>
param.type
{param.description && marked(param.description)}
</td>
</tr>;
})}
</table>
)} */
// }

render() {
const { operation } = this.props;
const keys = Object.keys(operation.responses);

// let schema;
//
// try {
// if (operation.responses[this.state.selectedStatus].content) {
// if (
// operation.responses[this.state.selectedStatus].content['application/json'].schema.type ===
// 'object' &&
// operation.responses[this.state.selectedStatus].content['application/json'].schema
// .properties
// ) {
// schema =
// operation.responses[this.state.selectedStatus].content['application/json'].schema
// .properties;
// }
// } else if (
// operation.responses[this.state.selectedStatus].content['application/xml'].schema.type ===
// 'object' &&
// operation.responses[this.state.selectedStatus].content['application/xml'].schema.properties
// ) {
// schema =
// operation.responses[this.state.selectedStatus].content['application/xml'].schema
// .properties;
// }
// } catch (e) {} // eslint-disable-line no-empty

return (
<div className="hub-reference-response-definitions">
<h3>
Expand All @@ -74,19 +91,7 @@ class ResponseSchema extends React.Component {
{operation.responses[this.state.selectedStatus].description && (
<p className="desc">{operation.responses[this.state.selectedStatus].description}</p>
)}
{/* {schema && (
<table>
{swaggerUtils.convertToParams([response], 'response').forEach(param => {
<tr>
<th>param.name</th>
<td>
param.type
{param.description && marked(param.description)}
</td>
</tr>;
})}
</table>
)} */}
{/* this.renderSchema() */}
</div>
</div>
);
Expand Down
55 changes: 26 additions & 29 deletions packages/api-explorer-ui/src/lib/create-code-shower.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,35 @@
function getLanguage(response) {
return response.content ? Object.keys(response.content)[0] : '';
}

function getExample(response, lang) {
return response.content[lang].examples ? response.content[lang].examples.response.value : '';
}

module.exports = type => {
return pathOperation => {
// Only working for results
if (type !== 'results') return [];

pathOperation._cache = pathOperation._cache || {};

if (pathOperation._cache[type]) return pathOperation._cache[type];

const codes = [];
if (type === 'results') {
// Only examples so far...
Object.keys(pathOperation.responses || {}).forEach(status => {
const response = pathOperation.responses[status];
let lang;
if (response.content) {
lang = Object.keys(response.content)[0];
} else if (!response.content) {
return;
}

let example;

if (response.content[lang].examples) {
example = response.content[lang].examples.response.value;
} else if (!response.content[lang].examples) {
return;
}

if (example) {
codes.push({
code: typeof example === 'object' ? JSON.stringify(example, undefined, 2) : example,
language: lang,
status,
});
}
});
}
const codes = Object.keys(pathOperation.responses || {}).map(status => {
const response = pathOperation.responses[status];

const language = getLanguage(response);
if (!language) return false;

const example = getExample(response, language);
if (!example) return false;

return {
code: typeof example === 'object' ? JSON.stringify(example, undefined, 2) : example,
language,
status,
};
}).filter(Boolean);

pathOperation._cache[type] = codes;
return codes;
Expand Down

0 comments on commit 1d7d91e

Please sign in to comment.