2017-02-28 21:32:23 +00:00
|
|
|
import React, {PureComponent, PropTypes} from 'react';
|
2017-03-03 01:44:07 +00:00
|
|
|
import autobind from 'autobind-decorator';
|
2016-08-15 17:04:36 +00:00
|
|
|
import HTTPSnippet, {availableTargets} from 'httpsnippet';
|
2017-03-08 05:52:17 +00:00
|
|
|
import CopyButton from '../base/copy-button';
|
2016-11-11 22:01:21 +00:00
|
|
|
import {Dropdown, DropdownButton, DropdownItem} from '../base/dropdown';
|
2017-03-09 06:23:23 +00:00
|
|
|
import Editor from '../codemirror/code-editor';
|
2017-03-08 05:52:17 +00:00
|
|
|
import Modal from '../base/modal';
|
|
|
|
import ModalBody from '../base/modal-body';
|
|
|
|
import ModalHeader from '../base/modal-header';
|
|
|
|
import ModalFooter from '../base/modal-footer';
|
2016-11-19 07:11:10 +00:00
|
|
|
import {exportHar} from '../../../common/har';
|
2016-11-23 20:44:46 +00:00
|
|
|
import {trackEvent} from '../../../analytics/index';
|
2017-04-11 21:20:01 +00:00
|
|
|
import Link from '../base/link';
|
2016-08-15 17:04:36 +00:00
|
|
|
|
|
|
|
const DEFAULT_TARGET = availableTargets().find(t => t.key === 'shell');
|
|
|
|
const DEFAULT_CLIENT = DEFAULT_TARGET.clients.find(t => t.key === 'curl');
|
|
|
|
const MODE_MAP = {
|
|
|
|
c: 'clike',
|
|
|
|
java: 'clike',
|
|
|
|
csharp: 'clike',
|
|
|
|
node: 'javascript',
|
|
|
|
objc: 'clike',
|
|
|
|
ocaml: 'mllike'
|
|
|
|
};
|
|
|
|
|
2016-08-15 22:31:30 +00:00
|
|
|
const TO_ADD_CONTENT_LENGTH = {
|
|
|
|
node: ['native']
|
|
|
|
};
|
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
@autobind
|
2017-02-28 21:32:23 +00:00
|
|
|
class GenerateCodeModal extends PureComponent {
|
2016-08-15 17:04:36 +00:00
|
|
|
constructor (props) {
|
|
|
|
super(props);
|
2016-11-25 19:01:41 +00:00
|
|
|
|
|
|
|
let client;
|
|
|
|
let target;
|
|
|
|
|
|
|
|
// Load preferences from localStorage
|
|
|
|
|
|
|
|
try {
|
2017-03-03 20:09:08 +00:00
|
|
|
target = JSON.parse(window.localStorage.getItem('insomnia::generateCode::target'));
|
2016-11-25 19:01:41 +00:00
|
|
|
} catch (e) {
|
|
|
|
}
|
|
|
|
try {
|
2017-03-03 20:09:08 +00:00
|
|
|
client = JSON.parse(window.localStorage.getItem('insomnia::generateCode::client'));
|
2016-11-25 19:01:41 +00:00
|
|
|
} catch (e) {
|
|
|
|
}
|
|
|
|
|
2016-08-15 17:04:36 +00:00
|
|
|
this.state = {
|
|
|
|
cmd: '',
|
|
|
|
request: null,
|
2016-11-25 23:09:17 +00:00
|
|
|
target: target || DEFAULT_TARGET,
|
2017-03-03 20:09:08 +00:00
|
|
|
client: client || DEFAULT_CLIENT
|
2016-08-15 17:04:36 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
_setModalRef (n) {
|
|
|
|
this.modal = n;
|
|
|
|
}
|
|
|
|
_setEditorRef (n) {
|
|
|
|
this._editor = n;
|
|
|
|
}
|
2017-02-28 21:32:23 +00:00
|
|
|
|
2017-03-29 02:21:49 +00:00
|
|
|
hide () {
|
2017-03-03 01:44:07 +00:00
|
|
|
this.modal.hide();
|
|
|
|
}
|
2017-02-28 21:32:23 +00:00
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
_handleClientChange (client) {
|
2016-09-14 23:25:19 +00:00
|
|
|
const {target, request} = this.state;
|
|
|
|
this._generateCode(request, target, client);
|
2016-11-23 20:44:46 +00:00
|
|
|
trackEvent('Generate Code', 'Client Change', `${target.title}/${client.title}`);
|
2017-03-03 01:44:07 +00:00
|
|
|
}
|
2016-08-15 17:04:36 +00:00
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
_handleTargetChange (target) {
|
2016-08-15 17:04:36 +00:00
|
|
|
const {target: currentTarget} = this.state;
|
|
|
|
if (currentTarget.key === target.key) {
|
|
|
|
// No change
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const client = target.clients.find(c => c.key === target.default);
|
|
|
|
this._generateCode(this.state.request, target, client);
|
2016-11-23 20:44:46 +00:00
|
|
|
trackEvent('Generate Code', 'Target Change', target.title);
|
2017-03-03 01:44:07 +00:00
|
|
|
}
|
2016-08-15 17:04:36 +00:00
|
|
|
|
2016-10-02 20:57:00 +00:00
|
|
|
async _generateCode (request, target, client) {
|
2016-08-15 22:31:30 +00:00
|
|
|
// Some clients need a content-length for the request to succeed
|
|
|
|
const addContentLength = (TO_ADD_CONTENT_LENGTH[target.key] || []).find(c => c === client.key);
|
|
|
|
|
2016-11-17 18:45:54 +00:00
|
|
|
const {environmentId} = this.props;
|
|
|
|
const har = await exportHar(request._id, environmentId, addContentLength);
|
2016-10-02 20:57:00 +00:00
|
|
|
const snippet = new HTTPSnippet(har);
|
|
|
|
const cmd = snippet.convert(target.key, client.key);
|
2016-08-15 17:04:36 +00:00
|
|
|
|
2016-10-02 20:57:00 +00:00
|
|
|
this.setState({request, cmd, client, target});
|
2016-11-25 19:01:41 +00:00
|
|
|
|
|
|
|
// Save client/target for next time
|
2017-03-03 20:09:08 +00:00
|
|
|
window.localStorage.setItem('insomnia::generateCode::client', JSON.stringify(client));
|
|
|
|
window.localStorage.setItem('insomnia::generateCode::target', JSON.stringify(target));
|
2016-08-15 17:04:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
show (request) {
|
2016-11-25 19:01:41 +00:00
|
|
|
const {client, target} = this.state;
|
|
|
|
this._generateCode(request, target, client);
|
2016-11-16 17:18:39 +00:00
|
|
|
this.modal.show();
|
2016-08-15 17:04:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render () {
|
|
|
|
const {cmd, target, client} = this.state;
|
2017-01-24 22:18:11 +00:00
|
|
|
const {editorFontSize, editorKeyMap} = this.props;
|
2016-11-25 19:01:41 +00:00
|
|
|
|
2016-08-15 17:04:36 +00:00
|
|
|
const targets = availableTargets();
|
|
|
|
|
2016-11-25 19:01:41 +00:00
|
|
|
// NOTE: Just some extra precautions in case the target is messed up
|
|
|
|
let clients = [];
|
|
|
|
if (target && Array.isArray(target.clients)) {
|
|
|
|
clients = target.clients;
|
|
|
|
}
|
|
|
|
|
2016-08-15 17:04:36 +00:00
|
|
|
return (
|
2017-03-01 21:15:56 +00:00
|
|
|
<Modal ref={this._setModalRef} tall {...this.props}>
|
2016-08-15 17:04:36 +00:00
|
|
|
<ModalHeader>Generate Client Code</ModalHeader>
|
2017-03-01 21:15:56 +00:00
|
|
|
<ModalBody noScroll style={{
|
2016-08-15 17:04:36 +00:00
|
|
|
display: 'grid',
|
2016-09-20 20:53:34 +00:00
|
|
|
gridTemplateColumns: 'minmax(0, 1fr)',
|
|
|
|
gridTemplateRows: 'auto minmax(0, 1fr)'
|
2016-08-15 17:04:36 +00:00
|
|
|
}}>
|
|
|
|
<div className="pad">
|
2017-03-01 21:15:56 +00:00
|
|
|
<Dropdown outline>
|
2016-11-26 00:49:38 +00:00
|
|
|
<DropdownButton className="btn btn--clicky">
|
2016-11-25 23:09:17 +00:00
|
|
|
{target ? target.title : 'n/a'}
|
2017-03-06 17:51:58 +00:00
|
|
|
<i className="fa fa-caret-down"/>
|
2016-11-11 22:01:21 +00:00
|
|
|
</DropdownButton>
|
|
|
|
{targets.map(target => (
|
2017-02-28 21:32:23 +00:00
|
|
|
<DropdownItem key={target.key} onClick={this._handleTargetChange} value={target}>
|
2016-11-11 22:01:21 +00:00
|
|
|
{target.title}
|
|
|
|
</DropdownItem>
|
|
|
|
))}
|
2016-08-15 17:04:36 +00:00
|
|
|
</Dropdown>
|
|
|
|
|
2017-03-01 21:15:56 +00:00
|
|
|
<Dropdown outline>
|
2016-11-26 00:49:38 +00:00
|
|
|
<DropdownButton className="btn btn--clicky">
|
2016-11-25 23:09:17 +00:00
|
|
|
{client ? client.title : 'n/a'}
|
2017-03-06 17:51:58 +00:00
|
|
|
<i className="fa fa-caret-down"/>
|
2016-11-11 23:06:24 +00:00
|
|
|
</DropdownButton>
|
2016-11-25 19:01:41 +00:00
|
|
|
{clients.map(client => (
|
2017-02-28 21:32:23 +00:00
|
|
|
<DropdownItem key={client.key} onClick={this._handleClientChange} value={client}>
|
2016-11-11 23:06:24 +00:00
|
|
|
{client.title}
|
|
|
|
</DropdownItem>
|
|
|
|
))}
|
2016-08-15 17:04:36 +00:00
|
|
|
</Dropdown>
|
|
|
|
|
2017-02-28 21:32:23 +00:00
|
|
|
<CopyButton content={cmd} className="pull-right btn btn--clicky"/>
|
2016-08-15 17:04:36 +00:00
|
|
|
</div>
|
|
|
|
<Editor
|
2017-03-01 21:15:56 +00:00
|
|
|
lineWrapping
|
2017-03-23 22:10:42 +00:00
|
|
|
placeholder="Generating code snippet..."
|
2016-08-15 17:04:36 +00:00
|
|
|
className="border-top"
|
|
|
|
key={Date.now()}
|
|
|
|
mode={MODE_MAP[target.key] || target.key}
|
2017-02-28 21:32:23 +00:00
|
|
|
ref={this._setEditorRef}
|
2017-01-23 22:41:31 +00:00
|
|
|
fontSize={editorFontSize}
|
2017-01-24 22:18:11 +00:00
|
|
|
keyMap={editorKeyMap}
|
2017-03-01 21:15:56 +00:00
|
|
|
defaultValue={cmd}
|
2016-08-15 17:04:36 +00:00
|
|
|
/>
|
|
|
|
</ModalBody>
|
|
|
|
<ModalFooter>
|
2017-04-11 21:20:01 +00:00
|
|
|
<div className="margin-left italic txt-sm tall">
|
|
|
|
* Code snippets generated by
|
|
|
|
<Link href="https://github.com/Mashape/httpsnippet">
|
|
|
|
httpsnippet
|
|
|
|
</Link>
|
|
|
|
</div>
|
2017-03-29 02:21:49 +00:00
|
|
|
<button className="btn" onClick={this.hide}>
|
2016-10-21 17:20:36 +00:00
|
|
|
Done
|
|
|
|
</button>
|
2016-08-15 17:04:36 +00:00
|
|
|
</ModalFooter>
|
|
|
|
</Modal>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-17 18:45:54 +00:00
|
|
|
GenerateCodeModal.propTypes = {
|
|
|
|
environmentId: PropTypes.string.isRequired,
|
2017-01-23 22:41:31 +00:00
|
|
|
editorFontSize: PropTypes.number.isRequired,
|
2017-03-03 20:09:08 +00:00
|
|
|
editorKeyMap: PropTypes.string.isRequired
|
2016-11-17 18:45:54 +00:00
|
|
|
};
|
2016-08-15 17:04:36 +00:00
|
|
|
|
|
|
|
export default GenerateCodeModal;
|