All Projects → exogen → node-fetch-har

exogen / node-fetch-har

Licence: MIT license
Generate HAR entries for requests made with node-fetch

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to node-fetch-har

re-frame-fetch-fx
js/fetch Effect Handler for re-frame
Stars: ✭ 24 (+4.35%)
Mutual labels:  fetch, http-requests, fetch-api
Node Fetch
A light-weight module that brings the Fetch API to Node.js
Stars: ✭ 7,176 (+31100%)
Mutual labels:  fetch, fetch-api, node-fetch
Cross Fetch
Universal WHATWG Fetch API for Node, Browsers and React Native.
Stars: ✭ 1,063 (+4521.74%)
Mutual labels:  fetch, fetch-api
React Ufo
🛸 react-ufo - A simple React hook to help you with data fetching 🛸
Stars: ✭ 85 (+269.57%)
Mutual labels:  fetch, fetch-api
Zl Fetch
A library that makes the Fetch API a breeze
Stars: ✭ 186 (+708.7%)
Mutual labels:  fetch, fetch-api
React Fetching Library
Simple and powerful API client for react 👍 Use hooks or FACCs to fetch data in easy way. No dependencies! Just react under the hood.
Stars: ✭ 561 (+2339.13%)
Mutual labels:  fetch, ssr
Create Request
Apply interceptors to `fetch` and create a custom request function.
Stars: ✭ 34 (+47.83%)
Mutual labels:  fetch, fetch-api
Use Http
🐶 React hook for making isomorphic http requests
Stars: ✭ 2,066 (+8882.61%)
Mutual labels:  fetch, ssr
Ky Universal
Use Ky in both Node.js and browsers
Stars: ✭ 421 (+1730.43%)
Mutual labels:  fetch, ssr
node-http-client
🔌 A light-weight, performant, composable blueprint for writing consistent and re-usable Node.js HTTP clients
Stars: ✭ 21 (-8.7%)
Mutual labels:  fetch, node-fetch
Next Blog
基于react(ssr)服务端框架next.js和antd-design搭建的个人博客
Stars: ✭ 214 (+830.43%)
Mutual labels:  fetch, ssr
fetch
A fetch API polyfill for React Native with text streaming support.
Stars: ✭ 27 (+17.39%)
Mutual labels:  fetch, fetch-api
Fetch Suspense
A React hook compatible with React 16.6's Suspense component.
Stars: ✭ 479 (+1982.61%)
Mutual labels:  fetch, fetch-api
Frisbee
🐕 Modern fetch-based alternative to axios/superagent/request. Great for React Native.
Stars: ✭ 1,038 (+4413.04%)
Mutual labels:  fetch, fetch-api
Fetch Examples
A repository of Fetch examples. See https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API for the corresponding documentation.
Stars: ✭ 431 (+1773.91%)
Mutual labels:  fetch, fetch-api
Php Fetch
A simple, type-safe, zero dependency port of the javascript fetch WebApi for PHP
Stars: ✭ 95 (+313.04%)
Mutual labels:  fetch, fetch-api
bestfetch
fetch ⭐️caching ⭐️deduplication
Stars: ✭ 44 (+91.3%)
Mutual labels:  fetch, fetch-api
node-fetch-cookies
node-fetch wrapper that adds support for cookie-jars
Stars: ✭ 15 (-34.78%)
Mutual labels:  fetch, node-fetch
React Fetch Hook
React hook for conveniently use Fetch API
Stars: ✭ 285 (+1139.13%)
Mutual labels:  fetch, fetch-api
Fetch Ponyfill
WHATWG fetch ponyfill
Stars: ✭ 209 (+808.7%)
Mutual labels:  fetch, fetch-api

node-fetch-har

npm Travis Coveralls

A Fetch API wrapper that records HAR logs for server requests made with node-fetch. You can then expose this data to get visibility into what’s happening on the server.

See also the companion project for Next.js integration, next-fetch-har.

Demo

Status

STABLE

Due to the wide variety seen in HTTP requests, please test thoroughly with your application and file an issue if you find any problems.

Warning

⚠️ HAR files can contain sensitive information like cookies or passwords. Since this library is for capturing what happens on the server, this is especially important because it is information that users can’t normally acces in their own browser. Be careful about sharing this data. If you provide a way to expose it, ensure it is only enabled for superusers or in secure environments.

Usage

The withHar function takes a base Fetch implementation such as node-fetch and returns a new one that records HAR entries:

import { withHar } from "node-fetch-har";
import nodeFetch from "node-fetch";

const fetch = withHar(nodeFetch);

Individual HAR entries can then accessed on the response object:

fetch("https://httpstat.us/200").then(response => {
  console.log(response.harEntry);
  return response;
});

Or by configuring withHar with an onHarEntry callback:

const fetch = withHar(nodeFetch, {
  onHarEntry: entry => console.log(entry)
});

You can also customize onHarEntry for individual requests:

const fetch = withHar(nodeFetch);

fetch("https://httpstat.us/200", {
  onHarEntry: entry => console.log(entry)
});

To disable HAR tracking for individual requests, set the har option to false:

fetch("https://httpstat.us/200", { har: false }).then(response => {
  console.log(response.harEntry); // Should be undefined.
  return response;
});

The above options will give you individual HAR entries. It’s likely that you’ll want to collect multiple requests into a single HAR log. For example, all API calls made while rendering a single page. Use the createHarLog function to generate a complete HAR object that can hold multiple entries.

You can pass the resulting object via the har option and entries will automatically be added to it:

import { withHar, createHarLog } from "node-fetch-har";
import nodeFetch from "node-fetch";

async function run() {
  const har = createHarLog();
  const fetch = withHar(nodeFetch, { har });

  await Promise.all([
    fetch("https://httpstat.us/200"),
    fetch("https://httpstat.us/200"),
    fetch("https://httpstat.us/200")
  ]);

  console.log(har);
}

You can also call createHarLog with an array of entries, if you’ve already collected them in a different way:

const har = createHarLog(entries);

…with Isomorphic Fetch

When using “universal” libraries like cross-fetch, isomorphic-fetch, or isomorphic-unfetch, make sure you only import this library and wrap the Fetch instance on the server. Not only does this library require built-in Node modules, but it’s unnecessary in the browser anyway, since you can already spy on requests (and export HAR logs) via the Network tab.

The following example assumes your bundler (e.g. webpack) is configured to strip out conditional branches based on process.browser.

import baseFetch from "isomorphic-unfetch";

let fetch = baseFetch;

if (!process.browser) {
  const { withHar } = require("node-fetch-har");
  fetch = withHar(baseFetch);
}

Redirects

Due to redirects, it is possible for a single fetch call to result in multiple HTTP requests. As you might expect, multiple HAR entries will be recorded as well.

With the Fetch API’s redirect option in follow mode (the default), calls will transparently follow redirects; that is, you get the response for the final, redirected request. Likewise, the harEntry property of the response will correspond with that final request.

To get the HAR entries for the redirects, use the har or onHarEntry options (described above). The redirects will be appended to the log and reported with onHarEntry in addition to the final entry, in the order that they were made.

Request Body

If there is no Content-Type header specified in the request, then postData will not be populated since we would not be able to populate the required mimeType field.

Additionally, params will only be populated if the Content-Type is exactly application/x-www-form-urlencoded. If it is anything else (including multipart/form-data) then text will be populated instead.

There may be limited support for exotic request body encodings.

Custom Agent

This library works by using the custom agent option supported by node-fetch. However, it should still work if you pass your own custom agent as well. The provided agent instance will have its addRequest method instrumented with the necessary HAR tracking behavior. This behavior will be skipped if the request does not originate from a Fetch instance returned by withHar.

Page Info

The second argument to createHarLog allows you to add some initial page info:

const har = createHarLog([], { title: "My Page" });

If you have additional pages within a single log, you’ll have to add them yourself:

har.log.pages.push({ title: "2nd Page" });

If not provided, a default page will be created with an ID of page_1. By default, all HAR entries will reference this page. To customize the page that entries reference, use the harPageRef option to withHar:

const fetch = withHar(nodeFetch, { har, harPageRef: "page_2" });

Or use the harPageRef option to fetch for individual requests:

await fetch(url, { harPageRef: "page_2" });

Examples

See the demo for an example of exposing an SSR HAR log from Next.js.

Run the demo like so:

$ cd demo
$ yarn
$ yarn start

TODO

  • More tests for different response types, protocols (HTTP/2), encodings, etc.

How does it work?

node-fetch supports a custom agent option. This can be used to capture very detailed information about the request all the way down to the socket level if desired. This library only uses it in a very simplistic way, to capture a few key timestamps and metadata like the HTTP version.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].