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

igsem-mock-fetch-api

v1.2.7

Published

Assign to window.fetch to create a wrapper over the Fetch API, if mockData are supplied it will fake the call to your API and return a response with fake data: window.fetch = Fetch({url:{...data}})

Downloads

7

Readme

##Mocking Fetch API

Description

Simple wrapper around Fetch API, it accepts a json object of urls mapped to mock response data. If the url is called with standard fetch, it will return the mock data without hitting your backend. This way you can easily start developing your frontend and create a json tree of fake data to implement for your backend without the need of a REST API. Check the test files for examples.

!Note: query params are ignored eg www.url.tld?search=whatever will be resolved to www.url.tld and return standard data.

HTTP Methods

Default method is GET, if config is provided, PATCH, POST, PUT will return the Resource sent with a generated id and response code 201

Common use case

Imagine you have a React app in development. You just received the screen which needs to be implemented but the Backend team is behind. You know how the data should look like and you don't want to write unnecessary code just for mocking the BE. In this case the only thing you need to do is define the url of the back end in your mock data and a json with a body key to hold the response. You will still write your calls as normally without worrying about mocking because the mock-api will just wrap your regular Fetch API. You just need to have a if statement at the enrty point of your app which will init the fake fetch like:

use_mock_api && window.fetch = Fetch(mockdata);

Another advantage is that in the mockData you will build up exactly the structure you require and you just pass it to the BE team..I need this :)

Simulating slow connection

// simulate 3 second latency
window.mock_fetch_timeout = 3000;

Installation

yarn add igsem-mock-fetch-api
npm i --save-dev igsem-mock-fetch-api

Instructions

In your index.js file just import the script and assign it to the global window passing in the mock data

import Fetch from 'igsem-mock-fetch-api';

window.fetch = Fetch(mockdata);

example of mockData:

const mockData = 
{
    'http://test.com': {
        'body': "some text",
        'status_code': 201
    },   
    'http://test2.com': {
        'body': "some text"
    },
    'http://test3.com': {
        'body': "some text",
        'status_code': 404
    }
};

Requirements

The library uses ES6 features, therefore is meant to be used only with webpack and Babel. It is based on functional programming and uses rambda for compose and curry. Type hinting is done via Flow.

Forking

If you need any feature just let me know :). If you fork the project, there is a prepublish script which will build, compile, commit, add tag, patch npm and push to git script if you run npm publish :)