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

@consolelabs/mochi-mock

v0.1.1

Published

<p align="center"> <img src="https://github.com/consolelabs/mochi.js/assets/25856620/86864905-d19b-4992-abe9-43111670e0b2" alt="mochi pose 04" width="40%" /> </p>

Downloads

18

Readme

Mochi Mock

This package is used to mock json file normally returned by backend apis

🌈 Installation

// npm
npm i @consolelabs/mochi-mock

// yarn
yarn add @consolelabs/mochi-mock

// pnpm
pnpm add @consolelabs/mochi-mock

🚀 Usage

1. Sync data

First, sync the mock data to your local project, this should clone/overwrite the remote data mock repo

// npm or yarn
npx mochi-mock -i

// pnpm
pnpm exec mochi-mock -i

2. Spy request

import spyRequest from "@consolelabs/mochi-mock";
// or
const spyRequest = require("@consolelabs/mochi-mock");

// ensure that mocker only runs in development
if (process.env.NODE_ENV === "development") {
  spyRequest();
}

3. Mock response

Method 1 (recommended, requires wretch package)

Register a new addon for your wretch instance

import { MockAddon } from "@consolelabs/mochi-mock";
import wretch from "@consolelabs";

const api = wretch().addon(MockAddon);

// somewhere else in your business logic
const data = api
  .mock("path/to/json/mock/file", { query: "optional" })
  .get()
  .json();

Method 2

The other way is to attach a custom header called X-Mochi-Mock with the json file as the header's value (this is also how the mocker works under the hood)

❓ FAQ

Do I need to follow some kind of format in my mock json file?

No, just use regular objects/list at the root, the response returned by the mocker will be converted to a format that is the same from a real api

Does the mocker support query string?

Yes, and there are also some reserved keys like page and size that are used for pagination. Anyother key/value pair is treated as filtering value, for example, this file:

[
  {
    "id": 1,
    "name": "john"
  },
  {
    "id": 2,
    "name": "ben"
  }
]

mocking this file with query string ?name=john will only return list with 1 element

🤝 Contributing

If you happen to change the mock data, kindly submit a PR with the changes so the team can review it