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

mockster

v1.0.2

Published

A library for mocking window.fetch

Downloads

3

Readme

Mockster

Mockster on NPM Mockster on TravisCI

A library for mocking fetch responses.

Install

npm i --save mockster
import mockster from 'mockster';

Note: window.fetch is automatically mocked when including this library. It's a good idea to only include mocks in a mock build so this usually isn't an issue. If you need them the library also exports fetchHook and fetchUnhook, as well as fetchUnmock(url, method).

Tip: You can include mocks in a webpack build with no extra config. Create a mocks/index.js file at your root and run npm start -- mocks.

Usage

Supported methods: delete, get, patch, post, put.

mockster.get('/film/:title?rating=:rating', (url, params) => {
  // params.title = 'batman'
  // params.rating = '8'
  return { something: 'to return' };
});

fetch('/film/batman?rating=8');

You can also return error responses:

mockster.post('/project/create', (url, params, options) => {
  if (options.body.name === 'Farting investigation') {
    return {
      status: 409,
      body: 'That project already exists',
    };
  }

  // An object without a `body` property is considered to be the body,
  // and the status will be 200 'OK'.
  return {
    ...options.body,
    id: '3250235H6',
  };
});

fetch('/project/create', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    name: 'Hubot',
  }),
});

You can also return a Promise if you want to delay the response or do async work:

mockster.get('/hello', () =>
  new Promise((resolve) => {
    setTimeout(() => resolve({ hello: 'world' }), 300);
  }));

Parameters

Parameters are avaiable in the second arguments (params). URLs support params, splats, and optional segments.

| Example | Description | | --------------- | -------- | | :name | a parameter to capture from the route up to /, ?, or end of string | | *splat | a splat to capture from the route up to ? or end of string | | () | Optional group that doesn't have to be part of the query. Can contain nested optional groups, params, and splats | anything else | free form literals |

Some examples:

  • /some/(optional/):thing
  • /users/:id/comments/:comment/rating/:rating
  • /*a/foo/*b
  • /books/*section/:title
  • /books?author=:author&subject=:subject

Matching based on options

It is also possible to match based on options as well as the URL. A deep equal will be performed on the options you wish to compare:

mockster.post('project/create', (url, params, options) => {
  return 'your response';
}, {
  body: {
    name: 'Billy Bob',
  }
});

Not all options will pass an equality check (for example FormData), to this effect options can also be a function:

mockster.post('project/create', (url, params, options) => {
  return 'your response';
}, options => options.body.name.startsWith('Billy'));