Skip to content

Commit 2c81336

Browse files
committed
Explain various non-exportable body cases explicitly in snippets
1 parent a400c40 commit 2c81336

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

src/components/view/http/http-export-card.tsx

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { reportError } from '../../../errors';
1414

1515
import { AccountStore } from '../../../model/account/account-store';
1616
import { UiStore } from '../../../model/ui-store';
17-
import { generateHarRequest, generateHar } from '../../../model/http/har';
17+
import { generateHarRequest, generateHar, ExtendedHarRequest } from '../../../model/http/har';
1818

1919
import { ProHeaderPill, CardSalesPitch } from '../../account/pro-placeholders';
2020
import {
@@ -84,13 +84,33 @@ const snippetEditorOptions = {
8484
hover: { enabled: false }
8585
};
8686

87-
const simplifyHarForSnippetExport = (harRequest: HarFormat.Request) => {
87+
const simplifyHarForSnippetExport = (harRequest: ExtendedHarRequest) => {
88+
const postData = !!harRequest.postData
89+
? harRequest.postData
90+
: harRequest._requestBodyStatus === 'discarded:not-representable'
91+
? {
92+
mimeType: 'text/plain',
93+
text: "!!! UNREPRESENTABLE BINARY REQUEST BODY - BODY MUST BE EXPORTED SEPARATELY !!!"
94+
}
95+
: harRequest._requestBodyStatus === 'discarded:too-large'
96+
? {
97+
mimeType: 'text/plain',
98+
text: "!!! VERY LARGE REQUEST BODY - BODY MUST BE EXPORTED & INCLUDED SEPARATELY !!!"
99+
}
100+
: harRequest._requestBodyStatus === 'discarded:not-decodable'
101+
? {
102+
mimeType: 'text/plain',
103+
text: "!!! REQUEST BODY COULD NOT BE DECODED !!!"
104+
}
105+
: undefined;
106+
88107
// When exporting code snippets the primary goal is to generate convenient code to send the
89108
// request that's *sematantically* equivalent to the original request, not to force every
90109
// tool to produce byte-for-byte identical requests (that's effectively impossible). To do
91110
// this, we drop headers that tools can produce automatically for themselves:
92111
return {
93112
...harRequest,
113+
postData,
94114
headers: _.filter(harRequest.headers, (header) => {
95115
// All clients should be able to automatically generate the correct content-length
96116
// headers as required for a request where it's unspecified. If we override this,

src/model/http/har.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,11 @@ export type RequestContentData = {
4141
comment?: string;
4242
};
4343

44-
interface ExtendedHarRequest extends HarFormat.Request {
45-
_postDataDiscarded?: boolean;
44+
export interface ExtendedHarRequest extends HarFormat.Request {
45+
_requestBodyStatus?:
46+
| 'discarded:too-large'
47+
| 'discarded:not-representable'
48+
| 'discarded:not-decodable';
4649
_content?: RequestContentData;
4750
}
4851

@@ -136,7 +139,7 @@ export function generateHarRequest(
136139

137140
if (request.body.decoded) {
138141
if (request.body.decoded.byteLength > HAR_BODY_SIZE_LIMIT) {
139-
requestEntry._postDataDiscarded = true;
142+
requestEntry._requestBodyStatus = 'discarded:too-large';
140143
requestEntry.comment = `Body discarded during HAR generation: longer than limit of ${HAR_BODY_SIZE_LIMIT} bytes`;
141144
} else {
142145
try {
@@ -146,6 +149,7 @@ export function generateHarRequest(
146149
);
147150
} catch (e) {
148151
if (e instanceof TypeError) {
152+
requestEntry._requestBodyStatus = 'discarded:not-representable';
149153
requestEntry._content = {
150154
text: request.body.decoded.toString('base64'),
151155
size: request.body.decoded.byteLength,
@@ -156,6 +160,8 @@ export function generateHarRequest(
156160
}
157161
}
158162
}
163+
} else {
164+
requestEntry._requestBodyStatus = 'discarded:not-decodable';
159165
}
160166

161167
return requestEntry;

0 commit comments

Comments
 (0)