-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Expand file tree
/
Copy pathindex.js
More file actions
268 lines (237 loc) · 9.62 KB
/
index.js
File metadata and controls
268 lines (237 loc) · 9.62 KB
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
import { debounce } from 'lodash';
import { useTheme } from 'providers/Theme/index';
import React, { useEffect, useMemo, useState } from 'react';
import { formatResponse, getContentType } from 'utils/common';
import { runJqFilter } from 'utils/common/jq-service';
import { getDefaultResponseFormat, detectContentTypeFromBase64 } from 'utils/response';
import LargeResponseWarning from '../LargeResponseWarning';
import QueryResultFilter from './QueryResultFilter';
import QueryResultPreview from './QueryResultPreview';
import StyledWrapper from './StyledWrapper';
// Raw format options (for byte format types)
const RAW_FORMAT_OPTIONS = [
{ id: 'raw', label: 'Raw', type: 'item', codeMirrorMode: 'text/plain' },
{ id: 'hex', label: 'Hex', type: 'item', codeMirrorMode: 'text/plain' },
{ id: 'base64', label: 'Base64', type: 'item', codeMirrorMode: 'text/plain' }
];
// Preview format options
const PREVIEW_FORMAT_OPTIONS = [
// Structured formats
{ id: 'json', label: 'JSON', type: 'item', codeMirrorMode: 'application/ld+json' },
{ id: 'html', label: 'HTML', type: 'item', codeMirrorMode: 'xml' },
{ id: 'xml', label: 'XML', type: 'item', codeMirrorMode: 'xml' },
{ id: 'javascript', label: 'JavaScript', type: 'item', codeMirrorMode: 'javascript' },
// Divider
{ type: 'divider', id: 'divider-structured-raw' },
// Raw formats
...RAW_FORMAT_OPTIONS
];
const formatErrorMessage = (error) => {
if (!error) return 'Something went wrong';
const remoteMethodError = 'Error invoking remote method \'send-http-request\':';
if (error?.includes(remoteMethodError)) {
const parts = error.split(remoteMethodError);
return parts[1]?.trim() || error;
}
return error;
};
// Custom hook to determine the initial format and tab based on the data buffer and headers
export const useInitialResponseFormat = (dataBuffer, headers) => {
return useMemo(() => {
const detectedContentType = detectContentTypeFromBase64(dataBuffer);
const contentType = getContentType(headers);
// Wait until both content types are available
if (detectedContentType === null || contentType === undefined) {
return { initialFormat: null, initialTab: null, contentType: contentType };
}
const initial = getDefaultResponseFormat(contentType);
return { initialFormat: initial.format, initialTab: initial.tab, contentType: contentType };
}, [dataBuffer, headers]);
};
// Custom hook to determine preview format options based on content type
export const useResponsePreviewFormatOptions = (dataBuffer, headers) => {
return useMemo(() => {
const detectedContentType = detectContentTypeFromBase64(dataBuffer);
const contentType = getContentType(headers);
const byteFormatTypes = ['image', 'video', 'audio', 'pdf', 'zip'];
const isByteFormatType = (contentType) => {
if (contentType.toLowerCase().includes('svg')) return false; // SVG is text-based
return byteFormatTypes.some((type) => contentType.includes(type));
};
const getContentTypeToCheck = () => {
if (detectedContentType) {
return detectedContentType;
}
return contentType;
};
const contentTypeToCheck = getContentTypeToCheck();
if (contentTypeToCheck && isByteFormatType(contentTypeToCheck)) {
// Return only raw format options (no structured formats)
return RAW_FORMAT_OPTIONS;
}
// Return all format options
return PREVIEW_FORMAT_OPTIONS;
}, [dataBuffer, headers]);
};
const QueryResult = ({
item,
collection,
data,
dataBuffer,
disableRunEventListener,
headers,
error,
selectedFormat, // one of the options in PREVIEW_FORMAT_OPTIONS
selectedTab // 'editor' or 'preview'
}) => {
const contentType = getContentType(headers);
const [filter, setFilter] = useState(null);
const [filterType, setFilterType] = useState('jsonpath');
const [jqResult, setJqResult] = useState(null);
const [jqError, setJqError] = useState(null);
const [showLargeResponse, setShowLargeResponse] = useState(false);
const { displayedTheme } = useTheme();
const responseSize = useMemo(() => {
const response = item.response || {};
if (typeof response.size === 'number') {
return response.size;
}
// Fallback: estimate from base64 length (base64 is ~4/3 of original size)
if (dataBuffer && typeof dataBuffer === 'string') {
return Math.floor(dataBuffer.length * 0.75);
}
return 0;
}, [dataBuffer, item.response]);
const isLargeResponse = responseSize > 10 * 1024 * 1024; // 10 MB
const detectedContentType = useMemo(() => {
return detectContentTypeFromBase64(dataBuffer);
}, [dataBuffer, isLargeResponse]);
// Run jq filter asynchronously when filterType is 'jq'
useEffect(() => {
if (filterType !== 'jq' || !filter) {
setJqResult(null);
setJqError(null);
return;
}
let cancelled = false;
setJqResult(null);
setJqError(null);
runJqFilter(data, filter)
.then((result) => {
if (!cancelled) {
setJqResult(result);
setJqError(null);
}
})
.catch((err) => {
if (!cancelled) {
setJqResult(null);
setJqError(err.message);
}
});
return () => { cancelled = true; };
}, [data, filter, filterType]);
const formattedData = useMemo(
() => {
if (isLargeResponse && !showLargeResponse) {
return '';
}
// For jq mode with an active filter, use the async result
if (filterType === 'jq' && filter) {
return jqResult ?? formatResponse(data, dataBuffer, selectedFormat, null);
}
// For JSONPath mode, pass filter to formatResponse as before
const jsonPathFilter = filterType === 'jsonpath' ? filter : null;
return formatResponse(data, dataBuffer, selectedFormat, jsonPathFilter);
},
[data, dataBuffer, selectedFormat, filter, filterType, jqResult, isLargeResponse, showLargeResponse]
);
const debouncedResultFilterOnChange = debounce((e) => {
setFilter(e.target.value);
}, 250);
const previewMode = useMemo(() => {
// Derive preview mode based on selected format
if (selectedFormat === 'html') return 'preview-web';
if (selectedFormat === 'json') return 'preview-json';
if (selectedFormat === 'xml') return 'preview-xml';
if (selectedFormat === 'raw') return 'preview-text';
if (selectedFormat === 'javascript') return 'preview-web';
// For base64/hex, check content type to determine binary preview type
if (selectedFormat === 'base64' || selectedFormat === 'hex') {
if (detectedContentType) {
if (detectedContentType.includes('image')) return 'preview-image';
if (detectedContentType.includes('pdf')) return 'preview-pdf';
if (detectedContentType.includes('audio')) return 'preview-audio';
if (detectedContentType.includes('video')) return 'preview-video';
}
// for all other content types, return preview-text
return 'preview-text';
}
return 'preview-text';
}, [selectedFormat, detectedContentType]);
const codeMirrorMode = useMemo(() => {
// Find the codeMirrorMode from PREVIEW_FORMAT_OPTIONS (contains all format options)
return PREVIEW_FORMAT_OPTIONS
.filter((option) => option.type === 'item' || !option.type)
.find((option) => option.id === selectedFormat)?.codeMirrorMode || 'text/plain';
}, [selectedFormat]);
const queryFilterEnabled = useMemo(() => codeMirrorMode.includes('json') && selectedFormat === 'json' && selectedTab === 'editor', [codeMirrorMode, selectedFormat, selectedTab]);
const hasScriptError = item.preRequestScriptErrorMessage || item.postResponseScriptErrorMessage;
return (
<StyledWrapper
className="w-full h-full relative flex"
queryFilterEnabled={queryFilterEnabled}
>
{error ? (
<div>
{hasScriptError ? null : (
<div className="error" style={{ whiteSpace: 'pre-line' }}>{formatErrorMessage(error)}</div>
)}
{error && typeof error === 'string' && error.toLowerCase().includes('self signed certificate') ? (
<div className="mt-6 muted text-xs">
You can disable SSL verification in the Preferences. <br />
To open the Preferences, click on the gear icon in the bottom left corner.
</div>
) : null}
</div>
) : isLargeResponse && !showLargeResponse ? (
<LargeResponseWarning
item={item}
responseSize={responseSize}
onRevealResponse={() => setShowLargeResponse(true)}
/>
) : (
<div className="h-full flex flex-col">
<div className="flex-1 relative">
<div className="absolute top-0 left-0 h-full w-full" data-testid="response-preview-container">
<QueryResultPreview
selectedTab={selectedTab}
data={data}
dataBuffer={dataBuffer}
formattedData={formattedData}
item={item}
contentType={detectedContentType ?? contentType}
previewMode={previewMode}
codeMirrorMode={codeMirrorMode}
collection={collection}
disableRunEventListener={disableRunEventListener}
displayedTheme={displayedTheme}
/>
</div>
{queryFilterEnabled && (
<QueryResultFilter
filter={filter}
onChange={debouncedResultFilterOnChange}
mode={codeMirrorMode}
filterType={filterType}
onFilterTypeChange={setFilterType}
jqError={jqError}
/>
)}
</div>
</div>
)}
</StyledWrapper>
);
};
export default QueryResult;