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

storybook-mock-rest

v1.2.2

Published

Manage and mock your endpoints via Storybook panel

Downloads

29

Readme

Storybook Rest Endpoint Mock

latest-version npm-version

Manage and mock your endpoints via Storybook panel

Currently supports axios library.

What you can do?

Mock panel is a storybook addon to help mocking and managing mocked endpoints on a UI.

Features

  • Realistic data generating for response data
  • Adding delay to response
  • Changing response type dynamicly
  • Mocking different response types
  • Mocked requests and responses shown in console

Usage

Defining response schema

You can both define your schema props to generate dynamic data or to show directly static data.

  • To define dynamic data, start with "$" sign such as $name.firstName
  • Arrays in schema
    • To get n number of array items add {{repeat(n)}} to your first item and your array's schema as second item

** We are using FakerJS behind, you can check FakerJS's Github to check their APIs.

{
  "name": "$name.firstName",
  "age": 33,
  "scores": [
    {
      "point": "$random.number"
    }
  ],
  "places": [
    "{{repeat(2)}}",
    {
      "name": "$address.city"
    }
  ]
}

Above example will generate

{
  "name": "Murat",
  "age": 33,
  "scores": [
    {
      "point": 5
    }
  ],
  "places": [
    {
      "name": "Istanbul"
    },
    {
      "name": "Ankara"
    }
  ]
}

Defining endpoints

  • Static endpoint: /users/list
  • For dynamic endpoints that has dynamic values
    • /users//list?page=&limit=5

You can use asterix char in your endpoints to define that field is dynamic. Above second endpoint will match;

  • /users/admin/list?page=1&limite=5
  • /users/banned/list?page=2&limite=5

Installation

  yarn add storybook-mock-rest -D

Configuration

Step 1

Add to your storybook's addon config

import 'storybook-mock-rest/register';

Step 2

Add a mockify.js file under .storybook folder

const mockConfig = () => {
  const config = {
    // path where your mocks will be saved
    mockPath: './mocks',
  };

  return config;
};

module.exports = {
  mockConfig,
};

Step 3

Bind your axios client to our mock api.

import { bindMock } from 'storybook-mock-rest';

const axiosClient = axios.create(); // implementation can be changed according to your needs.

bindMock(axiosClient);

Step 4

In your story file, add a mockPanel parameter. Type is the unique identifier that will map your story with mock panel.

export default {
  title: 'My Component',
  component: ListComponent,
  parameters: {
    mockPanel: {
      type: 'list-component',
    },
  },
};

Step 5

You should run a mock server behind to generate dynamic datas. It should be start while you start your storybook. So you can add a simple script to your package.json file to run it.

{
  "scripts": {
    "mock-server": "node ./node_modules/storybook-mock-rest/dist/server.js"
  }
}

NOTES

  • You can see your requests and their responses on your console

Contribution

All contributions are welcome.

For bugs please visit issues page