Skip to content

Commit 1b8e588

Browse files
committed
Fix code export of bodies in the context menu
This was broken, because if you haven't opened the request yet (common for context menu items) then the body decoding hasn't started, so the export always includes a "BODY NOT AVAILABLE" warning. If we just wait for decoding like this first, that goes away.
1 parent 8501d52 commit 1b8e588

File tree

2 files changed

+42
-9
lines changed

2 files changed

+42
-9
lines changed

src/components/view/view-context-menu-builder.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { action } from 'mobx';
1+
import { action, runInAction } from 'mobx';
22

33
import { CollectedEvent } from '../../types';
44

@@ -77,10 +77,13 @@ export class ViewEventContextMenuBuilder {
7777
...(preferredExportFormat && isPaidUser ? [{
7878
type: 'option',
7979
label: `Copy as ${getCodeSnippetFormatName(preferredExportFormat)} Snippet`,
80-
callback: (data: HttpExchange) =>
80+
callback: async (data: HttpExchange) => {
8181
copyToClipboard(
82-
generateCodeSnippet(data, preferredExportFormat)
83-
)
82+
await generateCodeSnippet(data, preferredExportFormat, {
83+
waitForBodyDecoding: true
84+
})
85+
);
86+
}
8487
}] as const : []),
8588
{
8689
type: 'submenu',
@@ -92,13 +95,18 @@ export class ViewEventContextMenuBuilder {
9295
items: snippetExportOptions[snippetGroupName].map((snippetOption) => ({
9396
type: 'option',
9497
label: getCodeSnippetFormatName(snippetOption),
95-
callback: action((data: HttpExchange) => {
98+
callback: async (data: HttpExchange) => {
9699
// When you pick an option here, it updates your preferred default option
97-
this.uiStore.exportSnippetFormat = getCodeSnippetFormatKey(snippetOption);
100+
runInAction(() => {
101+
this.uiStore.exportSnippetFormat = getCodeSnippetFormatKey(snippetOption);
102+
});
103+
98104
copyToClipboard(
99-
generateCodeSnippet(data, snippetOption)
105+
await generateCodeSnippet(data, snippetOption, {
106+
waitForBodyDecoding: true
107+
})
100108
);
101-
})
109+
}
102110
}))
103111
}))
104112
},

src/model/ui/export.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as _ from 'lodash';
22
import * as HTTPSnippet from "@httptoolkit/httpsnippet";
33

44
import { saveFile } from "../../util/ui";
5+
import { ObservablePromise } from '../../util/observable';
56

67
import { HttpExchange } from "../http/exchange";
78
import { generateHarRequest, generateHar, ExtendedHarRequest } from '../http/har';
@@ -21,7 +22,31 @@ export const exportHar = async (exchange: HttpExchange) => {
2122
saveFile(filename, 'application/har+json;charset=utf-8', harContent);
2223
};
2324

24-
export const generateCodeSnippet = (exchange: HttpExchange, snippetFormat: SnippetOption) => {
25+
export function generateCodeSnippet(
26+
exchange: HttpExchange,
27+
snippetFormat: SnippetOption,
28+
options: { waitForBodyDecoding: true }
29+
): ObservablePromise<string>;
30+
export function generateCodeSnippet(
31+
exchange: HttpExchange,
32+
snippetFormat: SnippetOption,
33+
options?: { waitForBodyDecoding?: boolean }
34+
): string;
35+
export function generateCodeSnippet(
36+
exchange: HttpExchange,
37+
snippetFormat: SnippetOption,
38+
options: { waitForBodyDecoding?: boolean } = {}
39+
): string | ObservablePromise<string> {
40+
// If the body isn't decoded yet, and it should be, wait for that decoding first.
41+
if (options.waitForBodyDecoding && (
42+
exchange.request.body.decodedPromise.state === 'pending' ||
43+
exchange.request.body.decodedPromise.state === undefined
44+
)) {
45+
// Doesn't matter if this errors - we'll make that explicit in the export later.
46+
return exchange.request.body.decodedPromise.catch(() => {})
47+
.then(() => generateCodeSnippet(exchange, snippetFormat, options));
48+
}
49+
2550
// First, we need to get a HAR that appropriately represents this request as we
2651
// want to export it:
2752
const harRequest = generateHarRequest(exchange.request, false, {

0 commit comments

Comments
 (0)