npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@simplenode/vcr

v1.3.0

Published

📼 Record, play and replay your back-end HTTP calls

Downloads

16

Readme

VCR 📼

Record, save and replay HTTP requests to one or more back-end APIs. Useful for building up stubbed data for your integration tests. Configuring VCR is easy and creating recorded requests is as simple as browsing your application.

Cached requests are stored locally as HTTP Archive Format (HAR) files, so they can be easily inspected, modified and replayed with lots of other applications.

⏱ Getting Started

Install the library:

npm i @simplenode/vcr -D

Once the library has been installed, create a .vcrrc.js file in your project and add the back-end APIs you use to the file:

module.exports = {
  // snapshots are stored under this directory, relative to the root
  // of your project
  snapshotsDir: './__vcr__',
  // you can define one or more back-end APIs:
  servers: [
    {
      // a recognisable name for the API
      name: 'Example API',
      // the URL to the destination that VCR is proxying.
      //
      // note: pathnames are not currently supported in the origin. Pathnames should be passed
      // through to the proxy from the caller.
      origin: 'https://example.com',
      // a port to serve the proxy on
      port: 56845,
      // (OPTIONAL) the host to bind the server to
      host: 'localhost',
      options: {
        // ignore specfic headers when generating the signature for a request.
        // This is particularly useful if your application uses different values
        // for specific headers in each request, such as a request ID.
        ignoreHeaders: ['x-dynamic-correlation-id'],
        // ignore query parameters and strip them from the signature for a request.
        // similar to ignore headers above, but for request query parameters.
        ignoreQueryParameters: ['ts'],
      },
    },
  ],
};

The library uses cosmiconfig, so configuration files can be created in a number of different languages and locations.

After configuring your APIs, you now need to alter any environment variables that set the location of your back-end APIs. For example:

BACK_END_API=https://example.com/path/to/location

Would become:

BACK_END_API=http://localhost:56845

You're now ready to begin recording requests to your back-end APIs. Use vcr record from the command line to start the proxying requests.

Once VCR starts, simply browse your application as normal. You should start to see requests being proxied to your back-end APIs, multiple identical requests will be served from the cached HAR files, whilst new requests are proxied and cached.

As an example, using curl http://localhost:56845/products?a=1 would create a HAR file in your project with the path ./__vcr__/example.com/path/to/location/products/3456ygfdw34.....56ygf.har.

Cypress

VCR can easily be integrated with Cypress, simply add the following to the top of your Cypress plugins file (cypress/plugins/index.js):

require('vcr/cypress');

When Cypress starts, VCR will automatically start using the configuration file you've provided.

⏺ Record HTTP Requests

When recording, requests are proxied to your back-end application. Load and "use" your application as normal; each request will be cached locally as HAR files and identical requests will be served from the cached HAR file.

vcr record

⏯ Play HTTP Requests

When using VCR in play mode, requests are not proxied to your back-end application, any requests that are not cached return a status code 501 (Not Implemented).

vcr play || vcr

🔀 Replay HTTP Requests

Once the cached requests become "stale" and you need to refresh the data, simply use reply and it will update each cached HAR file by sending a request to your back-end application.

vcr replay