From 8e7ad692ac3026f881be9ee6739ee1db0986e862 Mon Sep 17 00:00:00 2001 From: Sanjeet Uppal Date: Tue, 3 Oct 2017 10:12:02 -0700 Subject: [PATCH] Begin refactor of endpoint functions --- packages/api-explorer-ui/src/Doc.jsx | 4 +- .../src/lib/code-sample-response.js | 37 +++++++++++++++++++ 2 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 packages/api-explorer-ui/src/lib/code-sample-response.js diff --git a/packages/api-explorer-ui/src/Doc.jsx b/packages/api-explorer-ui/src/Doc.jsx index f727afb34..a67e9958b 100644 --- a/packages/api-explorer-ui/src/Doc.jsx +++ b/packages/api-explorer-ui/src/Doc.jsx @@ -21,6 +21,7 @@ class Doc extends React.Component { loading: false, showAuthBox: false, needsAuth: false, + responseTabClass: 'hub-reference-right hub-reference-results tabber-parent', }; this.onChange = this.onChange.bind(this); this.oas = new Oas(this.props.oas); @@ -75,8 +76,7 @@ class Doc extends React.Component { formData={this.state.formData} /> - {/* TODO add ClassNames*/} -
+
diff --git a/packages/api-explorer-ui/src/lib/code-sample-response.js b/packages/api-explorer-ui/src/lib/code-sample-response.js new file mode 100644 index 000000000..e22983d74 --- /dev/null +++ b/packages/api-explorer-ui/src/lib/code-sample-response.js @@ -0,0 +1,37 @@ +const { constructRequest } = require('fetch-har'); +const { statusCodes } = require('./statuscodes'); + +const req = constructRequest; + +function result(res) { + const data = res.responseJSON ? JSON.stringify(res.responseJSON, undefined, 2) : res.responseText; + + const isBinary = !!res.getAllResponseHeaders().match(/Content-Disposition: attachment;/); + + const headersFormatted = []; + + req.headers.forEach((ele, i) => { + headersFormatted.push(`${i} : ${ele}`); + }); + + let responseHeaders = res.getAllResponseHeaders(); + responseHeaders = responseHeaders + .filter(responseHeaders.split('\n'), v => !v.match(/x-final-url:/i)) + .join('\n'); + + const results = { + init: true, + method: req.method, + requestHeaders: headersFormatted.join('\n'), + responseHeaders, + isBinary, + url: req.url, + data: res.responseText, + statusCode: statusCodes(res.status || 404), + dataString: data, + }; + + this.setState({ responseTabClass: 'hub-reference-right hub-reference-results tabber-parent on' }); + + setTab('[data-tab=result]'); +}