fetch-har 9.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/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2020, ReadMe
1
+ Copyright (c) 2023, ReadMe
2
2
 
3
3
  Permission to use, copy, modify, and/or distribute this software for any purpose
4
4
  with or without fee is hereby granted, provided that the above copyright notice
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
  [![CI](https://github.com/readmeio/fetch-har/workflows/CI/badge.svg)](https://github.com/readmeio/fetch-har/)
@@ -9,10 +10,10 @@ Make a [fetch](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) reque
9
10
 
10
11
  ## Features
11
12
 
12
- - Supports Node 14+ (including the native `fetch` implementation in Node 18!).
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 [nock](https://npm.im/nock) or [fetch-mock](https://npm.im/fetch-mock).
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,14 +22,10 @@ npm install --save fetch-har
21
22
  ```
22
23
 
23
24
  ## Usage
24
- ```js
25
- require('isomorphic-fetch');
26
25
 
27
- // If executing from an environment that doesn't normally provide `fetch()` we'll automatically
28
- // polyfill in the `Blob`, `File`, and `FormData` APIs with the optional `formdata-node` package
29
- // (provided you've installed it).
30
- const fetchHAR = require('fetch-har').default;
31
- // import fetchHAR from 'fetch-har'); // Or if you're in an ESM codebase.
26
+ ```js
27
+ import fetchHAR from 'fetch-har';
28
+ // const fetchHAR = require('fetch-har');
32
29
 
33
30
  const har = {
34
31
  log: {
@@ -67,14 +64,11 @@ fetchHAR(har)
67
64
  ```
68
65
 
69
66
  ### API
70
- If you are executing `fetch-har` in a browser environment that supports the [FormData API](https://developer.mozilla.org/en-US/docs/Web/API/FormData) then you don't need to do anything. If you arent, however, you'll need to polyfill it.
71
-
72
- Unfortunately the most popular NPM package [form-data](https://npm.im/form-data) ships with a [non-spec compliant API](https://github.com/form-data/form-data/issues/124), and for this we don't recommend you use it, as if you use `fetch-har` to upload files it may not work.
73
-
74
- Though we recommend either [formdata-node](https://npm.im/formdata-node) or [formdata-polyfill](https://npm.im/formdata-polyfill) we prefer [formdata-node](https://npm.im/formdata-node) right now as it's CJS-compatible.
75
67
 
76
68
  #### Options
69
+
77
70
  ##### userAgent
71
+
78
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.
79
73
 
80
74
  ```js
@@ -82,33 +76,22 @@ await fetchHAR(har, { userAgent: 'my-client/1.0' });
82
76
  ```
83
77
 
84
78
  ##### files
79
+
85
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.
86
81
 
87
82
  ```js
88
- await fetchHAR(har, { files: {
89
- 'owlbert.png': await fs.readFile('./owlbert.png'),
90
- 'file.txt': document.querySelector('#some-file-input').files[0],
91
- } });
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
+ });
92
89
  ```
93
90
 
94
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.
95
92
 
96
- ##### multipartEncoder
97
- > ❗ If you are using `fetch-har` in Node you may need this option to execute `multipart/form-data` requests!
98
-
99
- If you are running `fetch-har` within a Node environment and you're using `node-fetch@2`, or another `fetch` polyfill that does not support a spec-compliant `FormData` API, you will need to specify an encoder that will transform your `FormData` object into something that can be used with [Request.body](https://developer.mozilla.org/en-US/docs/Web/API/Request/body).
100
-
101
- We recommend [form-data-encoder](https://npm.im/form-data-encoder).
102
-
103
- ```js
104
- const { FormDataEncoder } = require('form-data-encoder');
105
-
106
- await fetchHAR(har, { multipartEncoder: FormDataEncoder });
107
- ```
108
-
109
- You do **not**, and shouldn't, need to use this option in browser environments.
110
-
111
93
  ##### init
94
+
112
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).
113
96
 
114
97
  ```js
@@ -118,7 +101,7 @@ await fetchHAR(har, {
118
101
  'x-custom-header': 'buster',
119
102
  }),
120
103
  },
121
- })
104
+ });
122
105
  ```
123
106
 
124
107
  > ❗ Note that if you supply `body` or `credentials` to this option they may be overridden by what your HAR requires.
@@ -0,0 +1,6 @@
1
+ import { FetchHAROptions } from './types.mjs';
2
+ import { Har } from 'har-format';
3
+
4
+ declare function fetchHAR(har: Har, opts?: FetchHAROptions): Promise<Response>;
5
+
6
+ export { fetchHAR as default };
package/dist/index.d.ts CHANGED
@@ -1,22 +1,6 @@
1
- /// <reference types="node" />
2
- import type { Har } from 'har-format';
3
- interface RequestInitWithDuplex extends RequestInit {
4
- /**
5
- * `RequestInit#duplex` does not yet exist in the TS `lib.dom.d.ts` definition yet the native
6
- * fetch implementation in Node 18+, `undici`, requires it for certain POST payloads.
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
- userAgent?: string;
17
- files?: Record<string, Blob | Buffer>;
18
- multipartEncoder?: any;
19
- init?: RequestInitWithDuplex;
20
- }
21
- export default function fetchHAR(har: Har, opts?: FetchHAROptions): Promise<Response>;
22
- 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;