import React, { PropTypes } from "react" import { fromJS } from "immutable" import { getSampleSchema } from "core/utils" const getExampleComponent = ( sampleResponse, examples, HighlightCode ) => { if ( examples && examples.size ) { return examples.entrySeq().map( ([ key, example ]) => { let exampleValue try { exampleValue = example && example.toJS ? example.toJS() : example exampleValue = JSON.stringify(exampleValue) } catch(e) { exampleValue = String(example) } return (
{ key }
) }).toArray() } if ( sampleResponse ) { return
} return null } export default class Response extends React.Component { static propTypes = { code: PropTypes.string.isRequired, response: PropTypes.object, className: PropTypes.string, getComponent: PropTypes.func.isRequired, specSelectors: PropTypes.object.isRequired, fn: PropTypes.object.isRequired, contentType: PropTypes.string } static defaultProps = { response: fromJS({}), }; render() { let { code, response, className, fn, getComponent, specSelectors, contentType } = this.props let { inferSchema } = fn let schema = inferSchema(response.toJS()) let headers = response.get("headers") let examples = response.get("examples") const Headers = getComponent("headers") const HighlightCode = getComponent("highlightCode") const ModelExample = getComponent("modelExample") const Markdown = getComponent( "Markdown" ) let sampleResponse = schema ? getSampleSchema(schema, contentType, { includeReadOnly: true }) : null let example = getExampleComponent( sampleResponse, examples, HighlightCode ) return ( { code }
{ example ? ( ) : null} { headers ? ( ) : null} ) } }