fetch-har 10.0.0 → 11.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +16 -7
- package/dist/index.d.mts +6 -0
- package/dist/index.d.ts +6 -21
- package/dist/index.js +216 -267
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +239 -0
- package/dist/index.mjs.map +1 -0
- package/dist/types.d.mts +19 -0
- package/dist/types.d.ts +19 -0
- package/dist/types.js +1 -0
- package/dist/types.js.map +1 -0
- package/dist/types.mjs +1 -0
- package/dist/types.mjs.map +1 -0
- package/package.json +34 -16
- package/.nyc_output/62744757-2956-4005-b134-7af8ad5f74b3.json +0 -1
- package/.nyc_output/processinfo/62744757-2956-4005-b134-7af8ad5f74b3.json +0 -1
- package/.nyc_output/processinfo/index.json +0 -1
- package/.vscode/settings.json +0 -5
- package/example.js +0 -36
- package/src/index.ts +0 -343
- package/tsconfig.json +0 -12
package/README.md
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
# fetch-har
|
|
2
|
+
|
|
2
3
|
Make a [fetch](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) request from a HAR definition.
|
|
3
4
|
|
|
4
5
|
[](https://github.com/readmeio/fetch-har/)
|
|
@@ -12,7 +13,7 @@ Make a [fetch](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) reque
|
|
|
12
13
|
- Supports Node 18+
|
|
13
14
|
- Natively works in all browsers that support [fetch](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) without having to use any polyfils.
|
|
14
15
|
- [Tested](https://github.com/readmeio/fetch-har/actions) across Chrome, Safari, Firefox on Mac, Windows, and Linux.
|
|
15
|
-
- Requests can be mocked with [
|
|
16
|
+
- Requests can be mocked with [`msw`](https://npm.im/msw) or [`fetch-mock`](https://npm.im/fetch-mock) (though the latter does not appear to be maintained).
|
|
16
17
|
|
|
17
18
|
## Installation
|
|
18
19
|
|
|
@@ -21,9 +22,10 @@ npm install --save fetch-har
|
|
|
21
22
|
```
|
|
22
23
|
|
|
23
24
|
## Usage
|
|
25
|
+
|
|
24
26
|
```js
|
|
25
27
|
import fetchHAR from 'fetch-har';
|
|
26
|
-
// const fetchHAR = require('fetch-har')
|
|
28
|
+
// const fetchHAR = require('fetch-har');
|
|
27
29
|
|
|
28
30
|
const har = {
|
|
29
31
|
log: {
|
|
@@ -62,8 +64,11 @@ fetchHAR(har)
|
|
|
62
64
|
```
|
|
63
65
|
|
|
64
66
|
### API
|
|
67
|
+
|
|
65
68
|
#### Options
|
|
69
|
+
|
|
66
70
|
##### userAgent
|
|
71
|
+
|
|
67
72
|
A custom `User-Agent` header to apply to your request. Please note that browsers have their own handling for these headers in `fetch()` calls so it may not work everywhere; it will always be sent in Node however.
|
|
68
73
|
|
|
69
74
|
```js
|
|
@@ -71,18 +76,22 @@ await fetchHAR(har, { userAgent: 'my-client/1.0' });
|
|
|
71
76
|
```
|
|
72
77
|
|
|
73
78
|
##### files
|
|
79
|
+
|
|
74
80
|
An optional object map you can supply to use for `multipart/form-data` file uploads in leu of relying on if the HAR you have has [data URLs](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs). It supports Node file buffers and the [File](https://developer.mozilla.org/en-US/docs/Web/API/File) API.
|
|
75
81
|
|
|
76
82
|
```js
|
|
77
|
-
await fetchHAR(har, {
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
83
|
+
await fetchHAR(har, {
|
|
84
|
+
files: {
|
|
85
|
+
'owlbert.png': await fs.readFile('./owlbert.png'),
|
|
86
|
+
'file.txt': document.querySelector('#some-file-input').files[0],
|
|
87
|
+
},
|
|
88
|
+
});
|
|
81
89
|
```
|
|
82
90
|
|
|
83
91
|
If you don't supply this option `fetch-har` will fallback to the data URL present within the supplied HAR. If no `files` option is present, and no data URL (via `param.value`) is present in the HAR, a fatal exception will be thrown.
|
|
84
92
|
|
|
85
93
|
##### init
|
|
94
|
+
|
|
86
95
|
This optional argument lets you supply any option that's available to supply to the [Request constructor](https://developer.mozilla.org/en-US/docs/Web/API/Request/Request).
|
|
87
96
|
|
|
88
97
|
```js
|
|
@@ -92,7 +101,7 @@ await fetchHAR(har, {
|
|
|
92
101
|
'x-custom-header': 'buster',
|
|
93
102
|
}),
|
|
94
103
|
},
|
|
95
|
-
})
|
|
104
|
+
});
|
|
96
105
|
```
|
|
97
106
|
|
|
98
107
|
> ❗ Note that if you supply `body` or `credentials` to this option they may be overridden by what your HAR requires.
|
package/dist/index.d.mts
ADDED
package/dist/index.d.ts
CHANGED
|
@@ -1,21 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
import
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
*
|
|
8
|
-
* @see {@link https://github.com/microsoft/TypeScript-DOM-lib-generator/issues/1483}
|
|
9
|
-
* @see {@link https://github.com/nodejs/node/issues/46221}
|
|
10
|
-
* @see {@link https://fetch.spec.whatwg.org/#request-class}
|
|
11
|
-
* @see {@link https://github.com/microsoft/TypeScript/blob/main/lib/lib.dom.d.ts}
|
|
12
|
-
*/
|
|
13
|
-
duplex?: 'half';
|
|
14
|
-
}
|
|
15
|
-
export interface FetchHAROptions {
|
|
16
|
-
files?: Record<string, Blob | Buffer>;
|
|
17
|
-
init?: RequestInitWithDuplex;
|
|
18
|
-
userAgent?: string;
|
|
19
|
-
}
|
|
20
|
-
export default function fetchHAR(har: Har, opts?: FetchHAROptions): Promise<Response>;
|
|
21
|
-
export {};
|
|
1
|
+
import { FetchHAROptions } from './types.js';
|
|
2
|
+
import { Har } from 'har-format';
|
|
3
|
+
|
|
4
|
+
declare function fetchHAR(har: Har, opts?: FetchHAROptions): Promise<Response>;
|
|
5
|
+
|
|
6
|
+
export = fetchHAR;
|
package/dist/index.js
CHANGED
|
@@ -1,291 +1,240 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true});var __defProp = Object.defineProperty;
|
|
2
|
+
var __defProps = Object.defineProperties;
|
|
3
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
4
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
7
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
8
|
+
var __spreadValues = (a, b) => {
|
|
9
|
+
for (var prop in b || (b = {}))
|
|
10
|
+
if (__hasOwnProp.call(b, prop))
|
|
11
|
+
__defNormalProp(a, prop, b[prop]);
|
|
12
|
+
if (__getOwnPropSymbols)
|
|
13
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
14
|
+
if (__propIsEnum.call(b, prop))
|
|
15
|
+
__defNormalProp(a, prop, b[prop]);
|
|
16
|
+
}
|
|
17
|
+
return a;
|
|
12
18
|
};
|
|
13
|
-
|
|
14
|
-
var
|
|
15
|
-
|
|
19
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
20
|
+
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
21
|
+
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
22
|
+
}) : x)(function(x) {
|
|
23
|
+
if (typeof require !== "undefined")
|
|
24
|
+
return require.apply(this, arguments);
|
|
25
|
+
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
// src/index.ts
|
|
29
|
+
var _dataurls = require('@readme/data-urls');
|
|
16
30
|
if (!globalThis.Blob) {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
throw new Error('The Blob API is required for this library. https://developer.mozilla.org/en-US/docs/Web/API/Blob');
|
|
23
|
-
}
|
|
31
|
+
try {
|
|
32
|
+
globalThis.Blob = __require("buffer").Blob;
|
|
33
|
+
} catch (e) {
|
|
34
|
+
throw new Error("The Blob API is required for this library. https://developer.mozilla.org/en-US/docs/Web/API/Blob");
|
|
35
|
+
}
|
|
24
36
|
}
|
|
25
37
|
if (!globalThis.File) {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
}
|
|
32
|
-
catch (e) {
|
|
33
|
-
throw new Error('The File API is required for this library. https://developer.mozilla.org/en-US/docs/Web/API/File');
|
|
34
|
-
}
|
|
38
|
+
try {
|
|
39
|
+
globalThis.File = __require("undici").File;
|
|
40
|
+
} catch (e) {
|
|
41
|
+
throw new Error("The File API is required for this library. https://developer.mozilla.org/en-US/docs/Web/API/File");
|
|
42
|
+
}
|
|
35
43
|
}
|
|
36
44
|
function isBrowser() {
|
|
37
|
-
|
|
45
|
+
return typeof window !== "undefined" && typeof document !== "undefined";
|
|
38
46
|
}
|
|
39
47
|
function isBuffer(value) {
|
|
40
|
-
|
|
48
|
+
return typeof Buffer !== "undefined" && Buffer.isBuffer(value);
|
|
41
49
|
}
|
|
42
50
|
function isFile(value) {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
*
|
|
48
|
-
* This object identity crisis does not happen in the browser.
|
|
49
|
-
*/
|
|
50
|
-
return value.constructor.name === 'File';
|
|
51
|
-
}
|
|
52
|
-
return false;
|
|
51
|
+
if (value instanceof File) {
|
|
52
|
+
return value.constructor.name === "File";
|
|
53
|
+
}
|
|
54
|
+
return false;
|
|
53
55
|
}
|
|
54
56
|
function getFileFromSuppliedFiles(filename, files) {
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
return false;
|
|
57
|
+
if (files && filename in files) {
|
|
58
|
+
return files[filename];
|
|
59
|
+
} else if (files && decodeURIComponent(filename) in files) {
|
|
60
|
+
return files[decodeURIComponent(filename)];
|
|
61
|
+
}
|
|
62
|
+
return false;
|
|
62
63
|
}
|
|
63
|
-
function fetchHAR(har, opts) {
|
|
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
|
-
|
|
64
|
+
function fetchHAR(har, opts = {}) {
|
|
65
|
+
var _a, _b, _c, _d, _e;
|
|
66
|
+
if (!har)
|
|
67
|
+
throw new Error("Missing HAR definition");
|
|
68
|
+
if (!har.log || !har.log.entries || !har.log.entries.length)
|
|
69
|
+
throw new Error("Missing log.entries array");
|
|
70
|
+
const { request } = har.log.entries[0];
|
|
71
|
+
const { url } = request;
|
|
72
|
+
let querystring = "";
|
|
73
|
+
let shouldSetDuplex = false;
|
|
74
|
+
const options = __spreadProps(__spreadValues({}, opts.init ? opts.init : {}), {
|
|
75
|
+
method: request.method
|
|
76
|
+
});
|
|
77
|
+
if (!options.headers) {
|
|
78
|
+
options.headers = new Headers();
|
|
79
|
+
} else if (typeof options.headers === "object" && !(options.headers instanceof Headers) && options.headers !== null) {
|
|
80
|
+
options.headers = new Headers(options.headers);
|
|
81
|
+
}
|
|
82
|
+
const headers = options.headers;
|
|
83
|
+
if ("headers" in request && request.headers.length) {
|
|
84
|
+
request.headers.forEach((header) => {
|
|
85
|
+
try {
|
|
86
|
+
return headers.append(header.name, header.value);
|
|
87
|
+
} catch (err) {
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
if ("cookies" in request && request.cookies.length) {
|
|
92
|
+
if (isBrowser()) {
|
|
93
|
+
request.cookies.forEach((cookie) => {
|
|
94
|
+
document.cookie = `${encodeURIComponent(cookie.name)}=${encodeURIComponent(cookie.value)}`;
|
|
95
|
+
});
|
|
96
|
+
options.credentials = "include";
|
|
97
|
+
} else {
|
|
98
|
+
headers.append(
|
|
99
|
+
"cookie",
|
|
100
|
+
request.cookies.map((cookie) => `${encodeURIComponent(cookie.name)}=${encodeURIComponent(cookie.value)}`).join("; ")
|
|
101
|
+
);
|
|
97
102
|
}
|
|
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
|
-
* they won't know what the boundary to split on it.
|
|
149
|
-
*/
|
|
150
|
-
if (headers.has('Content-Type')) {
|
|
151
|
-
headers.delete('Content-Type');
|
|
152
|
-
}
|
|
153
|
-
var form_1 = new FormData();
|
|
154
|
-
request.postData.params.forEach(function (param) {
|
|
155
|
-
if ('fileName' in param) {
|
|
156
|
-
if (opts.files) {
|
|
157
|
-
var fileContents = getFileFromSuppliedFiles(param.fileName, opts.files);
|
|
158
|
-
if (fileContents) {
|
|
159
|
-
// If the file we've got available to us is a Buffer then we need to convert it so
|
|
160
|
-
// that the FormData API can use it.
|
|
161
|
-
if (isBuffer(fileContents)) {
|
|
162
|
-
form_1.append(param.name, new File([fileContents], param.fileName, {
|
|
163
|
-
type: param.contentType || null,
|
|
164
|
-
}), param.fileName);
|
|
165
|
-
return;
|
|
166
|
-
}
|
|
167
|
-
else if (isFile(fileContents)) {
|
|
168
|
-
form_1.append(param.name, fileContents, param.fileName);
|
|
169
|
-
return;
|
|
170
|
-
}
|
|
171
|
-
throw new TypeError('An unknown object has been supplied into the `files` config for use. We only support instances of the File API and Node Buffer objects.');
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
if ('value' in param) {
|
|
175
|
-
var paramBlob = void 0;
|
|
176
|
-
var parsed = (0, data_urls_1.parse)(param.value);
|
|
177
|
-
if (parsed) {
|
|
178
|
-
// If we were able to parse out this data URL we don't need to transform its data
|
|
179
|
-
// into a buffer for `Blob` because that supports data URLs already.
|
|
180
|
-
paramBlob = new Blob([param.value], { type: parsed.contentType || param.contentType || null });
|
|
181
|
-
}
|
|
182
|
-
else {
|
|
183
|
-
paramBlob = new Blob([param.value], { type: param.contentType || null });
|
|
184
|
-
}
|
|
185
|
-
form_1.append(param.name, paramBlob, param.fileName);
|
|
186
|
-
return;
|
|
187
|
-
}
|
|
188
|
-
throw new Error("The supplied HAR has a postData parameter with `fileName`, but neither `value` content within the HAR or any file buffers were supplied with the `files` option. Since this library doesn't have access to the filesystem, it can't fetch that file.");
|
|
189
|
-
}
|
|
190
|
-
form_1.append(param.name, param.value);
|
|
191
|
-
});
|
|
192
|
-
options.body = form_1;
|
|
193
|
-
break;
|
|
194
|
-
default:
|
|
195
|
-
var formBody_1 = {};
|
|
196
|
-
request.postData.params.map(function (param) {
|
|
197
|
-
try {
|
|
198
|
-
formBody_1[param.name] = JSON.parse(param.value);
|
|
199
|
-
}
|
|
200
|
-
catch (e) {
|
|
201
|
-
formBody_1[param.name] = param.value;
|
|
202
|
-
}
|
|
203
|
-
return true;
|
|
204
|
-
});
|
|
205
|
-
options.body = JSON.stringify(formBody_1);
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
else if ((_a = request.postData.text) === null || _a === void 0 ? void 0 : _a.length) {
|
|
209
|
-
// If we've got `files` map content present, and this post data content contains a valid data
|
|
210
|
-
// URL then we can substitute the payload with that file instead of the using data URL.
|
|
211
|
-
if (opts.files) {
|
|
212
|
-
var parsed = (0, data_urls_1.parse)(request.postData.text);
|
|
103
|
+
}
|
|
104
|
+
if ("postData" in request) {
|
|
105
|
+
if (request.postData && "params" in request.postData) {
|
|
106
|
+
if (!("mimeType" in request.postData)) {
|
|
107
|
+
request.postData.mimeType = "application/octet-stream";
|
|
108
|
+
}
|
|
109
|
+
switch (request.postData.mimeType) {
|
|
110
|
+
case "application/x-www-form-urlencoded":
|
|
111
|
+
headers.set("Content-Type", request.postData.mimeType);
|
|
112
|
+
const encodedParams = new URLSearchParams();
|
|
113
|
+
(_a = request.postData.params) == null ? void 0 : _a.forEach((param) => {
|
|
114
|
+
if (param.value)
|
|
115
|
+
encodedParams.set(param.name, param.value);
|
|
116
|
+
});
|
|
117
|
+
options.body = encodedParams.toString();
|
|
118
|
+
break;
|
|
119
|
+
case "multipart/alternative":
|
|
120
|
+
case "multipart/form-data":
|
|
121
|
+
case "multipart/mixed":
|
|
122
|
+
case "multipart/related":
|
|
123
|
+
if (headers.has("Content-Type")) {
|
|
124
|
+
headers.delete("Content-Type");
|
|
125
|
+
}
|
|
126
|
+
const form = new FormData();
|
|
127
|
+
(_b = request.postData.params) == null ? void 0 : _b.forEach((param) => {
|
|
128
|
+
if ("fileName" in param && param.fileName) {
|
|
129
|
+
if (opts.files) {
|
|
130
|
+
const fileContents = getFileFromSuppliedFiles(param.fileName, opts.files);
|
|
131
|
+
if (fileContents) {
|
|
132
|
+
if (isBuffer(fileContents)) {
|
|
133
|
+
form.append(
|
|
134
|
+
param.name,
|
|
135
|
+
new File([fileContents], param.fileName, {
|
|
136
|
+
type: param.contentType || void 0
|
|
137
|
+
}),
|
|
138
|
+
param.fileName
|
|
139
|
+
);
|
|
140
|
+
return;
|
|
141
|
+
} else if (isFile(fileContents)) {
|
|
142
|
+
form.append(param.name, fileContents, param.fileName);
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
throw new TypeError(
|
|
146
|
+
"An unknown object has been supplied into the `files` config for use. We only support instances of the File API and Node Buffer objects."
|
|
147
|
+
);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
if ("value" in param && param.value) {
|
|
151
|
+
let paramBlob;
|
|
152
|
+
const parsed = _dataurls.parse.call(void 0, param.value);
|
|
213
153
|
if (parsed) {
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
if (isBuffer(fileContents)) {
|
|
218
|
-
options.body = fileContents;
|
|
219
|
-
}
|
|
220
|
-
else if (isFile(fileContents)) {
|
|
221
|
-
// `Readable.from` isn't available in browsers but the browser `Request` object can
|
|
222
|
-
// handle `File` objects just fine without us having to mold it into shape.
|
|
223
|
-
if (isBrowser()) {
|
|
224
|
-
options.body = fileContents;
|
|
225
|
-
}
|
|
226
|
-
else {
|
|
227
|
-
// @ts-expect-error "Property 'from' does not exist on type 'typeof Readable'." but it does!
|
|
228
|
-
options.body = readable_stream_1.Readable.from(fileContents.stream());
|
|
229
|
-
shouldSetDuplex = true;
|
|
230
|
-
// Supplying a polyfilled `File` stream into `Request.body` doesn't automatically
|
|
231
|
-
// add `Content-Length`.
|
|
232
|
-
if (!headers.has('content-length')) {
|
|
233
|
-
headers.set('content-length', String(fileContents.size));
|
|
234
|
-
}
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
}
|
|
238
|
-
}
|
|
154
|
+
paramBlob = new Blob([param.value], { type: parsed.contentType || param.contentType || void 0 });
|
|
155
|
+
} else {
|
|
156
|
+
paramBlob = new Blob([param.value], { type: param.contentType || void 0 });
|
|
239
157
|
}
|
|
158
|
+
form.append(param.name, paramBlob, param.fileName);
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
throw new Error(
|
|
162
|
+
"The supplied HAR has a postData parameter with `fileName`, but neither `value` content within the HAR or any file buffers were supplied with the `files` option. Since this library doesn't have access to the filesystem, it can't fetch that file."
|
|
163
|
+
);
|
|
240
164
|
}
|
|
241
|
-
if (
|
|
242
|
-
|
|
165
|
+
if (param.value)
|
|
166
|
+
form.append(param.name, param.value);
|
|
167
|
+
});
|
|
168
|
+
options.body = form;
|
|
169
|
+
break;
|
|
170
|
+
default:
|
|
171
|
+
const formBody = {};
|
|
172
|
+
(_c = request.postData.params) == null ? void 0 : _c.map((param) => {
|
|
173
|
+
try {
|
|
174
|
+
formBody[param.name] = JSON.parse(param.value || "");
|
|
175
|
+
} catch (e) {
|
|
176
|
+
formBody[param.name] = param.value;
|
|
243
177
|
}
|
|
178
|
+
return true;
|
|
179
|
+
});
|
|
180
|
+
options.body = JSON.stringify(formBody);
|
|
181
|
+
}
|
|
182
|
+
} else if ((_e = (_d = request.postData) == null ? void 0 : _d.text) == null ? void 0 : _e.length) {
|
|
183
|
+
if (opts.files) {
|
|
184
|
+
const parsed = _dataurls.parse.call(void 0, request.postData.text);
|
|
185
|
+
if (parsed) {
|
|
186
|
+
if ((parsed == null ? void 0 : parsed.name) && parsed.name in opts.files) {
|
|
187
|
+
const fileContents = getFileFromSuppliedFiles(parsed.name, opts.files);
|
|
188
|
+
if (fileContents) {
|
|
189
|
+
if (isBuffer(fileContents)) {
|
|
190
|
+
options.body = fileContents;
|
|
191
|
+
} else if (isFile(fileContents)) {
|
|
192
|
+
if (isBrowser()) {
|
|
193
|
+
options.body = fileContents;
|
|
194
|
+
} else {
|
|
195
|
+
options.body = fileContents.stream();
|
|
196
|
+
shouldSetDuplex = true;
|
|
197
|
+
if (!headers.has("content-length")) {
|
|
198
|
+
headers.set("content-length", String(fileContents.size));
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
}
|
|
244
204
|
}
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
* As `RequestInit#duplex` isn't supported by any browsers, or even mentioned on MDN, we aren't
|
|
250
|
-
* sending it in browser environments. This work is purely to support Node 18+ and `undici`
|
|
251
|
-
* environments.
|
|
252
|
-
*
|
|
253
|
-
* @see {@link https://github.com/nodejs/node/issues/46221}
|
|
254
|
-
* @see {@link https://github.com/whatwg/fetch/pull/1457}
|
|
255
|
-
* @see {@link https://developer.mozilla.org/en-US/docs/Web/API/Request/Request}
|
|
256
|
-
*/
|
|
257
|
-
if (shouldSetDuplex && !isBrowser()) {
|
|
258
|
-
options.duplex = 'half';
|
|
259
|
-
}
|
|
205
|
+
}
|
|
206
|
+
if (typeof options.body === "undefined") {
|
|
207
|
+
options.body = request.postData.text;
|
|
208
|
+
}
|
|
260
209
|
}
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
var requestURL = url;
|
|
264
|
-
if ('queryString' in request && request.queryString.length) {
|
|
265
|
-
var urlObj = new URL(requestURL);
|
|
266
|
-
var queryParams_1 = Array.from(urlObj.searchParams).map(function (_a) {
|
|
267
|
-
var k = _a[0], v = _a[1];
|
|
268
|
-
return "".concat(k, "=").concat(v);
|
|
269
|
-
});
|
|
270
|
-
request.queryString.forEach(function (q) {
|
|
271
|
-
queryParams_1.push("".concat(q.name, "=").concat(q.value));
|
|
272
|
-
});
|
|
273
|
-
querystring = queryParams_1.join('&');
|
|
274
|
-
// Because anchor hashes before query strings will prevent query strings from being delivered
|
|
275
|
-
// we need to pop them off and re-add them after.
|
|
276
|
-
if (urlObj.hash) {
|
|
277
|
-
var urlWithoutHashes = requestURL.replace(urlObj.hash, '');
|
|
278
|
-
requestURL = "".concat(urlWithoutHashes.split('?')[0]).concat(querystring ? "?".concat(querystring) : '');
|
|
279
|
-
requestURL += urlObj.hash;
|
|
280
|
-
}
|
|
281
|
-
else {
|
|
282
|
-
requestURL = "".concat(requestURL.split('?')[0]).concat(querystring ? "?".concat(querystring) : '');
|
|
283
|
-
}
|
|
210
|
+
if (shouldSetDuplex && !isBrowser()) {
|
|
211
|
+
options.duplex = "half";
|
|
284
212
|
}
|
|
285
|
-
|
|
286
|
-
|
|
213
|
+
}
|
|
214
|
+
let requestURL = url;
|
|
215
|
+
if ("queryString" in request && request.queryString.length) {
|
|
216
|
+
const urlObj = new URL(requestURL);
|
|
217
|
+
const queryParams = Array.from(urlObj.searchParams).map(([k, v]) => `${k}=${v}`);
|
|
218
|
+
request.queryString.forEach((q) => {
|
|
219
|
+
queryParams.push(`${q.name}=${q.value}`);
|
|
220
|
+
});
|
|
221
|
+
querystring = queryParams.join("&");
|
|
222
|
+
if (urlObj.hash) {
|
|
223
|
+
const urlWithoutHashes = requestURL.replace(urlObj.hash, "");
|
|
224
|
+
requestURL = `${urlWithoutHashes.split("?")[0]}${querystring ? `?${querystring}` : ""}`;
|
|
225
|
+
requestURL += urlObj.hash;
|
|
226
|
+
} else {
|
|
227
|
+
requestURL = `${requestURL.split("?")[0]}${querystring ? `?${querystring}` : ""}`;
|
|
287
228
|
}
|
|
288
|
-
|
|
289
|
-
|
|
229
|
+
}
|
|
230
|
+
if (opts.userAgent) {
|
|
231
|
+
headers.append("User-Agent", opts.userAgent);
|
|
232
|
+
}
|
|
233
|
+
options.headers = headers;
|
|
234
|
+
return fetch(requestURL, options);
|
|
290
235
|
}
|
|
236
|
+
|
|
237
|
+
|
|
291
238
|
exports.default = fetchHAR;
|
|
239
|
+
|
|
240
|
+
module.exports = exports.default//# sourceMappingURL=index.js.map
|