11.1.0 • Published 6 months ago

fetch-har v11.1.0

Weekly downloads
1,398
License
ISC
Repository
github
Last release
6 months ago

fetch-har

Make a fetch request from a HAR definition.

CI npm.io License

npm.io

Features

  • Supports Node 18+
  • Natively works in all browsers that support fetch without having to use any polyfils.
  • Tested across Chrome, Safari, Firefox on Mac, Windows, and Linux.
  • Requests can be mocked with msw or fetch-mock (though the latter does not appear to be maintained).

Installation

npm install --save fetch-har

Usage

import fetchHAR from 'fetch-har';
// const fetchHAR = require('fetch-har');

const har = {
  log: {
    entries: [
      {
        request: {
          headers: [
            {
              name: 'Authorization',
              value: 'Bearer api-key',
            },
            {
              name: 'Content-Type',
              value: 'application/json',
            },
          ],
          queryString: [
            { name: 'a', value: 1 },
            { name: 'b', value: 2 },
          ],
          postData: {
            mimeType: 'application/json',
            text: '{"id":8,"category":{"id":6,"name":"name"},"name":"name"}',
          },
          method: 'POST',
          url: 'http://httpbin.org/post',
        },
      },
    ],
  },
};

fetchHAR(har)
  .then(res => res.json())
  .then(console.log);

API

Options

userAgent

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.

await fetchHAR(har, { userAgent: 'my-client/1.0' });
files

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. It supports Node file buffers and the File API.

await fetchHAR(har, {
  files: {
    'owlbert.png': await fs.readFile('./owlbert.png'),
    'file.txt': document.querySelector('#some-file-input').files[0],
  },
});

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.

init

This optional argument lets you supply any option that's available to supply to the Request constructor.

await fetchHAR(har, {
  init: {
    headers: new Headers({
      'x-custom-header': 'buster',
    }),
  },
});

❗ Note that if you supply body or credentials to this option they may be overridden by what your HAR requires.

11.1.0

6 months ago

10.0.0

8 months ago

11.0.0

8 months ago

11.0.1

8 months ago

9.0.0

11 months ago

8.1.5

1 year ago

8.1.4

1 year ago

8.1.3

2 years ago

8.1.0

2 years ago

8.1.2

2 years ago

8.0.3

2 years ago

8.1.1

2 years ago

8.0.2

2 years ago

7.0.0

2 years ago

8.0.1

2 years ago

8.0.0

2 years ago

5.0.5

2 years ago

5.0.4

2 years ago

6.1.0

2 years ago

6.0.1

2 years ago

6.0.0

2 years ago

6.0.2

2 years ago

5.0.3

3 years ago

5.0.2

3 years ago

5.0.1

3 years ago

5.0.0

3 years ago

4.0.3

3 years ago

4.0.2

4 years ago

4.0.1

4 years ago

4.0.0

4 years ago

3.0.2

4 years ago

3.0.1

4 years ago

3.0.0

4 years ago

2.3.2

4 years ago

2.3.1

4 years ago

2.3.0

4 years ago

2.2.1

4 years ago

2.2.0

4 years ago

2.1.0

4 years ago

1.1.0

4 years ago

2.0.0

7 years ago

1.0.0

7 years ago