Optinally download prettified response body

This commit is contained in:
Gregory Schier 2019-12-12 15:02:53 -05:00
parent c96067322c
commit a1f99d47b1
2 changed files with 48 additions and 27 deletions

View File

@ -1,16 +1,35 @@
import React, { PureComponent } from 'react'; // @flow
import PropTypes from 'prop-types'; import * as React from 'react';
import autobind from 'autobind-decorator'; import autobind from 'autobind-decorator';
import { Dropdown, DropdownButton, DropdownDivider, DropdownItem } from '../base/dropdown'; import { Dropdown, DropdownButton, DropdownDivider, DropdownItem } from '../base/dropdown';
import { getPreviewModeName, PREVIEW_MODES } from '../../../common/constants'; import { getPreviewModeName, PREVIEW_MODES } from '../../../common/constants';
type Props = {|
download: (pretty: boolean) => any,
fullDownload: (pretty: boolean) => any,
updatePreviewMode: string => any,
previewMode: string,
showPrettifyOption?: boolean,
|};
@autobind @autobind
class PreviewModeDropdown extends PureComponent { class PreviewModeDropdown extends React.PureComponent<Props> {
_handleClick(previewMode) { async _handleClick(previewMode: string) {
this.props.updatePreviewMode(previewMode); const { updatePreviewMode } = this.props;
await updatePreviewMode(previewMode);
} }
renderPreviewMode(mode) { async _handleDownloadPrettify() {
const { download } = this.props;
download(true);
}
async _handleDownloadNormal() {
const { download } = this.props;
download(false);
}
renderPreviewMode(mode: string) {
const { previewMode } = this.props; const { previewMode } = this.props;
return ( return (
<DropdownItem key={mode} onClick={this._handleClick} value={mode}> <DropdownItem key={mode} onClick={this._handleClick} value={mode}>
@ -21,7 +40,8 @@ class PreviewModeDropdown extends PureComponent {
} }
render() { render() {
const { download, fullDownload, previewMode } = this.props; const { fullDownload, previewMode, showPrettifyOption } = this.props;
return ( return (
<Dropdown beside> <Dropdown beside>
<DropdownButton className="tall"> <DropdownButton className="tall">
@ -31,27 +51,23 @@ class PreviewModeDropdown extends PureComponent {
<DropdownDivider>Preview Mode</DropdownDivider> <DropdownDivider>Preview Mode</DropdownDivider>
{PREVIEW_MODES.map(this.renderPreviewMode)} {PREVIEW_MODES.map(this.renderPreviewMode)}
<DropdownDivider>Actions</DropdownDivider> <DropdownDivider>Actions</DropdownDivider>
<DropdownItem onClick={download}> <DropdownItem onClick={this._handleDownloadNormal}>
<i className="fa fa-save" /> <i className="fa fa-save" />
Save Response Body Save Raw Response
</DropdownItem> </DropdownItem>
<DropdownItem onClick={fullDownload}> {showPrettifyOption && (
<DropdownItem onClick={this._handleDownloadPrettify}>
<i className="fa fa-save" /> <i className="fa fa-save" />
Save Full Response Save Prettified Response
</DropdownItem>
)}
<DropdownItem onClick={fullDownload}>
<i className="fa fa-bug" />
Save HTTP Debug
</DropdownItem> </DropdownItem>
</Dropdown> </Dropdown>
); );
} }
} }
PreviewModeDropdown.propTypes = {
// Functions
updatePreviewMode: PropTypes.func.isRequired,
download: PropTypes.func.isRequired,
fullDownload: PropTypes.func.isRequired,
// Required
previewMode: PropTypes.string.isRequired,
};
export default PreviewModeDropdown; export default PreviewModeDropdown;

View File

@ -29,7 +29,7 @@ import type { HotKeyRegistry } from '../../common/hotkeys';
import { hotKeyRefs } from '../../common/hotkeys'; import { hotKeyRefs } from '../../common/hotkeys';
import type { RequestVersion } from '../../models/request-version'; import type { RequestVersion } from '../../models/request-version';
import { showError } from '../components/modals/index'; import { showError } from '../components/modals/index';
import prettify from 'insomnia-prettify'; import { json as jsonPrettify } from 'insomnia-prettify';
type Props = { type Props = {
// Functions // Functions
@ -75,7 +75,7 @@ class ResponsePane extends React.PureComponent<Props> {
return models.response.getBodyBuffer(this.props.response); return models.response.getBodyBuffer(this.props.response);
} }
async _handleDownloadResponseBody() { async _handleDownloadResponseBody(prettify: boolean) {
const { response, request } = this.props; const { response, request } = this.props;
if (!response || !request) { if (!response || !request) {
// Should never happen // Should never happen
@ -105,10 +105,8 @@ class ResponsePane extends React.PureComponent<Props> {
}); });
readStream.on('end', () => { readStream.on('end', () => {
const to = fs.createWriteStream(outputPath); const to = fs.createWriteStream(outputPath);
if (extension === 'json') { const finalBuffer = Buffer.concat(dataBuffers);
dataBuffers = prettify.json(dataBuffers);
}
to.write(dataBuffers);
to.on('error', err => { to.on('error', err => {
showError({ showError({
title: 'Save Failed', title: 'Save Failed',
@ -116,6 +114,12 @@ class ResponsePane extends React.PureComponent<Props> {
error: err, error: err,
}); });
}); });
if (prettify && contentType.includes('json')) {
to.write(jsonPrettify(finalBuffer.toString('utf8')));
} else {
to.write(finalBuffer);
}
}); });
} }
}); });
@ -304,6 +308,7 @@ class ResponsePane extends React.PureComponent<Props> {
fullDownload={this._handleDownloadFullResponseBody} fullDownload={this._handleDownloadFullResponseBody}
previewMode={previewMode} previewMode={previewMode}
updatePreviewMode={handleSetPreviewMode} updatePreviewMode={handleSetPreviewMode}
showPrettifyOption={response.contentType.includes('json')}
/> />
</Tab> </Tab>
<Tab tabIndex="-1"> <Tab tabIndex="-1">