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

vcr.js-next

v0.9.3

Published

Mock server with Proxy and Record support inspired by ruby VCR.

Downloads

443

Readme

VCR.js CircleCI Dependency Status

Mock server with Proxy and Record support inspired by ruby VCR.

tl;dr

yarn add vcr.js
mkdir -p fixtures/users
echo '{"users": ["Tim", "Tom"]}' > ./fixtures/users/GET.default.json
yarn vcr -- -f ./fixtures

Now you can hit localhost:8100/users and get your JSON!

Terminal options

Use --help to get all the possible options:

yarn vcr -- --help

Output:

yarn vcr -- --fixturesDir [./fixtures]

Options:
  -h, --help         Show help                                         [boolean]
  -f, --fixturesDir  Directory where to load fixtures    [default: "./fixtures"]
  -p, --proxy        URL to real API
  -r, --record       Record proxied responses to fixtures dir          [boolean]
  --port                                                         [default: 8100]

Examples:
  -f ./fixtures -p https://ur.l/base -r  Load fixtures from directory, proxy not
                                         found fixtures to ur.l/base and success
                                         responses record back to fixtures
                                         directory

Resolving fixtures

When you hit the VCR.js server with a URL (e.g. GET http://localhost:8100/api/v1/users), the URL is translated to the path of the fixture file, consisting of a relative path to a directory and a file name, in this case {fixturesDir}/api/v1/users/GET.default.(json|js).

In general form:

{fixturesDir}/{endpointPath}/{method}.{variant}.(json|js)

With url query parameters (in variant name params must be sorted alphabetically, also encoded by encodeURIComponent):

{fixturesDir}/{endpointPath}/{method}.param1=value&param2=value.(json|js)

Example:

When accessing http://localhost:8100/api/v1/users?page=1&limit=10 it will ty to look for fixture:

{fixturesDir}/{endpointPath}/GET.limit=10&page=1.(json|js)

Dynamic route params

To match an endpoint with dynamic params, use {dynamicParam} as the directory name. If you would like to get the same response from both GET /users/1 and GET /users/42, create a file with the name {fixturesDir}/users/{id}/GET.default.json and you can reuse your fixture file for all users! As a bonus you can access these params in fixtures and customize the response by using a .js fixture (described below).

Custom responses of a single endpoint - Variants

What if you wanted to customize the response of a single endpoint? Just set a variants cookies with a list of desired variants separated by comma as the value, e.g. /api/v1/users/{id}/GET.variantName,/api/v1/projects/POST.otherVariant Stub server will find the corresponding cookie variant matching the request path and provide you with the correct fixture.

or you can also visit /variants in browsers and use /variants?add=XXX to add multiple variants to existing setup, /variants?set=XXX to override variants and set given values, /variants?clear=t to remove all variants from cookie

What fixture types are handled?

Currently supported fixture types are .json and .js. JS fixtures are basically handlers as you know them from expressjs/node. A simple template for a .js fixture:

module.exports = (req, res, next) => {
  res.status(400).json({error: 'Bad request :D'});
};

Proxy mode - fixture recording

If you specify a -p or --proxy URL, the stub server will look for local fixtures and if no fixture is found, it will proxy the request to the 'real API', streaming the response back and optionally saving it as a fixture.

Proxy + record mode

Together with proxy option you can add -r or --record. This will enable saving fixtures from a proxy locally on the disc. For example after running yarn vcr -- -f ./fixtures -p https://ap.io/ -r with an empty fixtures folder and hitting GET /users, response from https://ap.io/users is streamed to the client and is also saved in fixtures/users/GET.default.json.

Custom variants for recording

If you want to save fixtures from proxy under a custom variant, just set the record_fixture_variant cookie with any word you want as the value. With the record_fixture_variant=blacklistedUser cookie the recorded fixtures will be saved as {path}/GET.blacklistedUser.json.

Cassettes

If you need completely separated sets of fixtures, set cassette cookie with absolute path to folder containing the fixtures. The same cookie in proxy-record mode will create the folder and save fixtures there. To have colocated cassettes and tests in cypress, you can use const cassette = path.resolve(path.dirname(Cypress.spec.absolute), './fixtures-folder')

Development

yarn test
yarn tslint
yarn start

Made with love by