Skip to content

Commit 66b587b

Browse files
fix(form-data): fix ssr error due to window object access (#373)
* fix(form-data): override import from package root * fix(httpsnippet): linting errors
1 parent 8a1bc90 commit 66b587b

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

Diff for: src/httpsnippet.ts

+9-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { map as eventStreamMap } from 'event-stream';
2-
import FormData from 'form-data';
2+
import FormData from 'form-data/lib/form_data';
33
import { Param, PostDataCommon, Request as NpmHarRequest } from 'har-format';
44
import { validateRequest } from 'har-validator-compiled';
55
import { stringify as queryStringify } from 'querystring';
@@ -13,6 +13,13 @@ import { ClientId, TargetId, targets } from './targets/targets';
1313
export { availableTargets, extname } from './helpers/utils';
1414
export { addTarget, addTargetClient } from './targets/targets';
1515

16+
// We're implementing the logic for which FormData object to use, ourselves.
17+
// This allows us to use the native FormData object in the browser and the `form-data` module in Node,
18+
// instead of relying on the package entrypoint to handle that.
19+
const resolveFormData =
20+
// @ts-expect-error — we're only using window.FormData if it exists
21+
typeof window !== 'undefined' && window.FormData ? window.FormData : FormData;
22+
1623
const DEBUG_MODE = false;
1724

1825
const debug = {
@@ -174,7 +181,7 @@ export class HTTPSnippet {
174181
request.postData.mimeType = 'multipart/form-data';
175182

176183
if (request.postData?.params) {
177-
const form = new FormData();
184+
const form = new resolveFormData();
178185

179186
// The `form-data` module returns one of two things: a native FormData object, or its own polyfill
180187
// Since the polyfill does not support the full API of the native FormData object, when this library is running in a browser environment it'll fail on two things:
@@ -186,15 +193,13 @@ export class HTTPSnippet {
186193
// Since the native FormData object is iterable, we easily detect what version of `form-data` we're working with here to allow `multipart/form-data` requests to be compiled under both browser and Node environments.
187194
//
188195
// This hack is pretty awful but it's the only way we can use this library in the browser as if we code this against just the native FormData object, we can't polyfill that back into Node because Blob and File objects, which something like `formdata-polyfill` requires, don't exist there.
189-
// @ts-expect-error TODO
190196
const isNativeFormData = typeof form[Symbol.iterator] === 'function';
191197

192198
// TODO: THIS ABSOLUTELY MUST BE REMOVED.
193199
// IT BREAKS SOME USE-CASES FOR MULTIPART FORMS THAT DEPEND ON BEING ABLE TO SET THE BOUNDARY.
194200
// easter egg
195201
const boundary = '---011000010111000001101001'; // this is binary for "api". yep.
196202
if (!isNativeFormData) {
197-
// @ts-expect-error THIS IS WRONG. VERY WRONG.
198203
form._boundary = boundary;
199204
}
200205

@@ -205,16 +210,13 @@ export class HTTPSnippet {
205210

206211
if (isNativeFormData) {
207212
if (isBlob(value)) {
208-
// @ts-expect-error TODO
209213
form.append(name, value, filename);
210214
} else {
211215
form.append(name, value);
212216
}
213217
} else {
214218
form.append(name, value, {
215-
// @ts-expect-error TODO
216219
filename,
217-
// @ts-expect-error TODO
218220
contentType: param.contentType || null,
219221
});
220222
}

Diff for: src/types/form-data.d.ts

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
declare module 'form-data/lib/form_data' {
2+
import FormData from 'form-data';
3+
export default FormData;
4+
}

0 commit comments

Comments
 (0)