From 1d7d91ecf7fc209de76de33e1753fef86fb1b4f8 Mon Sep 17 00:00:00 2001 From: Dom Harrington Date: Thu, 2 Nov 2017 13:52:01 -0700 Subject: [PATCH] Codeclimate fixes --- .../__tests__/CodeSampleResponse.test.jsx | 7 +- .../__tests__/Response.test.jsx | 2 +- packages/api-explorer-ui/src/Doc.jsx | 16 +--- packages/api-explorer-ui/src/Response.jsx | 81 ++++++++++--------- .../src/lib/create-code-shower.js | 55 ++++++------- 5 files changed, 80 insertions(+), 81 deletions(-) diff --git a/packages/api-explorer-ui/__tests__/CodeSampleResponse.test.jsx b/packages/api-explorer-ui/__tests__/CodeSampleResponse.test.jsx index 5786697d0..5e75d137b 100644 --- a/packages/api-explorer-ui/__tests__/CodeSampleResponse.test.jsx +++ b/packages/api-explorer-ui/__tests__/CodeSampleResponse.test.jsx @@ -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); diff --git a/packages/api-explorer-ui/__tests__/Response.test.jsx b/packages/api-explorer-ui/__tests__/Response.test.jsx index 4187a4e46..34369f5e4 100644 --- a/packages/api-explorer-ui/__tests__/Response.test.jsx +++ b/packages/api-explorer-ui/__tests__/Response.test.jsx @@ -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', () => { diff --git a/packages/api-explorer-ui/src/Doc.jsx b/packages/api-explorer-ui/src/Doc.jsx index 17872eeae..201738262 100644 --- a/packages/api-explorer-ui/src/Doc.jsx +++ b/packages/api-explorer-ui/src/Doc.jsx @@ -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(); @@ -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({ diff --git a/packages/api-explorer-ui/src/Response.jsx b/packages/api-explorer-ui/src/Response.jsx index a7f9d824a..324cd6c63 100644 --- a/packages/api-explorer-ui/src/Response.jsx +++ b/packages/api-explorer-ui/src/Response.jsx @@ -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 && ( + + {swaggerUtils.convertToParams([response], 'response').forEach(param => { + + + + ; + })} +
param.name + param.type + {param.description && marked(param.description)} +
+ )} */ + // } + 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 (

@@ -74,19 +91,7 @@ class ResponseSchema extends React.Component { {operation.responses[this.state.selectedStatus].description && (

{operation.responses[this.state.selectedStatus].description}

)} - {/* {schema && ( - - {swaggerUtils.convertToParams([response], 'response').forEach(param => { - - - - ; - })} -
param.name - param.type - {param.description && marked(param.description)} -
- )} */} + {/* this.renderSchema() */}

); diff --git a/packages/api-explorer-ui/src/lib/create-code-shower.js b/packages/api-explorer-ui/src/lib/create-code-shower.js index 4ee87680c..fe90998a7 100644 --- a/packages/api-explorer-ui/src/lib/create-code-shower.js +++ b/packages/api-explorer-ui/src/lib/create-code-shower.js @@ -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;