/
generate-code-modal.js
187 lines (164 loc) · 5.68 KB
/
generate-code-modal.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import React, { PureComponent } from 'react';
import PropTypes from 'prop-types';
import autobind from 'autobind-decorator';
import HTTPSnippet, { availableTargets } from 'httpsnippet';
import CopyButton from '../base/copy-button';
import { Dropdown, DropdownButton, DropdownItem } from '../base/dropdown';
import CodeEditor from '../codemirror/code-editor';
import Modal from '../base/modal';
import ModalBody from '../base/modal-body';
import ModalHeader from '../base/modal-header';
import ModalFooter from '../base/modal-footer';
import { exportHarRequest } from '../../../common/har';
import Link from '../base/link';
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',
};
const TO_ADD_CONTENT_LENGTH = {
node: ['native'],
};
@autobind
class GenerateCodeModal extends PureComponent {
constructor(props) {
super(props);
let client;
let target;
// Load preferences from localStorage
try {
target = JSON.parse(window.localStorage.getItem('insomnia::generateCode::target'));
} catch (e) {}
try {
client = JSON.parse(window.localStorage.getItem('insomnia::generateCode::client'));
} catch (e) {}
this.state = {
cmd: '',
request: null,
target: target || DEFAULT_TARGET,
client: client || DEFAULT_CLIENT,
};
}
_setModalRef(n) {
this.modal = n;
}
_setEditorRef(n) {
this._editor = n;
}
hide() {
this.modal.hide();
}
_handleClientChange(client) {
const { target, request } = this.state;
this._generateCode(request, target, client);
}
_handleTargetChange(target) {
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);
}
async _generateCode(request, target, client) {
// Some clients need a content-length for the request to succeed
const addContentLength = (TO_ADD_CONTENT_LENGTH[target.key] || []).find(c => c === client.key);
const { environmentId } = this.props;
const har = await exportHarRequest(request._id, environmentId, addContentLength);
const snippet = new HTTPSnippet(har);
const cmd = snippet.convert(target.key, client.key);
this.setState({ request, cmd, client, target });
// Save client/target for next time
window.localStorage.setItem('insomnia::generateCode::client', JSON.stringify(client));
window.localStorage.setItem('insomnia::generateCode::target', JSON.stringify(target));
}
show(request) {
const { client, target } = this.state;
this._generateCode(request, target, client);
this.modal.show();
}
render() {
const { cmd, target, client } = this.state;
const { editorFontSize, editorIndentSize, editorKeyMap } = this.props;
const targets = availableTargets();
// NOTE: Just some extra precautions in case the target is messed up
let clients = [];
if (target && Array.isArray(target.clients)) {
clients = target.clients;
}
return (
<Modal ref={this._setModalRef} tall {...this.props}>
<ModalHeader>Generate Client Code</ModalHeader>
<ModalBody
noScroll
style={{
display: 'grid',
gridTemplateColumns: 'minmax(0, 1fr)',
gridTemplateRows: 'auto minmax(0, 1fr)',
}}>
<div className="pad">
<Dropdown outline>
<DropdownButton className="btn btn--clicky">
{target ? target.title : 'n/a'}
<i className="fa fa-caret-down" />
</DropdownButton>
{targets.map(target => (
<DropdownItem key={target.key} onClick={this._handleTargetChange} value={target}>
{target.title}
</DropdownItem>
))}
</Dropdown>
<Dropdown outline>
<DropdownButton className="btn btn--clicky">
{client ? client.title : 'n/a'}
<i className="fa fa-caret-down" />
</DropdownButton>
{clients.map(client => (
<DropdownItem key={client.key} onClick={this._handleClientChange} value={client}>
{client.title}
</DropdownItem>
))}
</Dropdown>
<CopyButton content={cmd} className="pull-right btn btn--clicky" />
</div>
<CodeEditor
lineWrapping
placeholder="Generating code snippet..."
className="border-top"
key={Date.now()}
mode={MODE_MAP[target.key] || target.key}
ref={this._setEditorRef}
fontSize={editorFontSize}
indentSize={editorIndentSize}
keyMap={editorKeyMap}
defaultValue={cmd}
/>
</ModalBody>
<ModalFooter>
<div className="margin-left italic txt-sm tall">
* Code snippets generated by
<Link href="https://github.com/Mashape/httpsnippet">httpsnippet</Link>
</div>
<button className="btn" onClick={this.hide}>
Done
</button>
</ModalFooter>
</Modal>
);
}
}
GenerateCodeModal.propTypes = {
environmentId: PropTypes.string.isRequired,
editorFontSize: PropTypes.number.isRequired,
editorIndentSize: PropTypes.number.isRequired,
editorKeyMap: PropTypes.string.isRequired,
};
export default GenerateCodeModal;