Skip to content

Commit 8a2396d

Browse files
committed
feat: add options to show request and response headers in network log
1 parent 4985f18 commit 8a2396d

File tree

1 file changed

+34
-21
lines changed

1 file changed

+34
-21
lines changed

src/NetworkLogItem.tsx

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,15 @@ import Icon from './Icon';
1212

1313
interface NetworkLogItemProps {
1414
log: NetworkLog;
15+
showRequestHeader?: boolean;
16+
showResponseHeader?: boolean;
1517
}
1618

17-
const NetworkLogItem: React.FC<NetworkLogItemProps> = ({ log }) => {
19+
const NetworkLogItem: React.FC<NetworkLogItemProps> = ({
20+
log,
21+
showResponseHeader = false,
22+
showRequestHeader = false,
23+
}) => {
1824
const [expanded, setExpanded] = useState<boolean>(false);
1925

2026
const getStatusColor = (status?: number): string => {
@@ -42,19 +48,21 @@ const NetworkLogItem: React.FC<NetworkLogItemProps> = ({ log }) => {
4248
}
4349
};
4450

45-
const generateCurl = (log: NetworkLog): string => {
46-
let curl: string = `curl -X ${log.method}`;
51+
const generateCurl = (apiLog: NetworkLog): string => {
52+
let curl: string = `curl -X ${apiLog.method}`;
4753

48-
if (log.headers && typeof log.headers === 'object') {
49-
Object.entries(log.headers).forEach(([key, value]: [string, string]) => {
50-
curl += ` \\\n -H "${key}: ${value}"`;
51-
});
54+
if (apiLog.headers && typeof apiLog.headers === 'object') {
55+
Object.entries(apiLog.headers).forEach(
56+
([key, value]: [string, string]) => {
57+
curl += ` \\\n -H "${key}: ${value}"`;
58+
}
59+
);
5260
}
5361

54-
if (log.body) {
55-
curl += ` \\\n -d '${log.body}'`;
62+
if (apiLog.body) {
63+
curl += ` \\\n -d '${apiLog.body}'`;
5664
}
57-
curl += ` \\\n "${log.url}"`;
65+
curl += ` \\\n "${apiLog.url}"`;
5866
return curl;
5967
};
6068

@@ -119,11 +127,14 @@ const NetworkLogItem: React.FC<NetworkLogItemProps> = ({ log }) => {
119127
textToCopy={generateCurl(log)}
120128
/>
121129
</View>
122-
<View style={styles.clipboardContainer}>
123-
<Text style={styles.sectionTitle}>Request Headers:</Text>
124-
{/* <Text style={styles.codeText}>{formatHeaders(log.headers)}</Text> */}
125-
</View>
126-
130+
{showRequestHeader && (
131+
<View style={styles.clipboardContainer}>
132+
<Text style={styles.sectionTitle}>Request Headers:</Text>
133+
<Text style={styles.codeText}>
134+
{formatHeaders(log.headers)}
135+
</Text>
136+
</View>
137+
)}
127138
<View style={styles.clipboardContainer}>
128139
{log.body && (
129140
<>
@@ -135,12 +146,14 @@ const NetworkLogItem: React.FC<NetworkLogItemProps> = ({ log }) => {
135146

136147
{log.response && (
137148
<>
138-
<View style={styles.clipboardContainer}>
139-
<Text style={styles.sectionTitle}>Response Headers:</Text>
140-
<Text style={styles.codeText}>
141-
{formatHeaders(log.response.headers)}
142-
</Text>
143-
</View>
149+
{showResponseHeader && (
150+
<View style={styles.clipboardContainer}>
151+
<Text style={styles.sectionTitle}>Response Headers:</Text>
152+
<Text style={styles.codeText}>
153+
{formatHeaders(log.response.headers)}
154+
</Text>
155+
</View>
156+
)}
144157

145158
<View style={styles.clipboardContainer}>
146159
<Text style={styles.sectionTitle}>Response Body:</Text>

0 commit comments

Comments
 (0)