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

mockwave

v1.0.0

Published

API proxy with mocking support

Downloads

6

Readme

image

Mockwave

Have you ever had to delay your UI development to wait for a backend team to implement the APIs you need?

Mockwave lets you create mock API endpoints while seamlessly proxying all other requests to the real API, combining the flexibility of mocking with the reliability of a live backend.

While you wait for the API team to implement the new APIs you need for your UI development, you can create temporary endpoints that return mock or test data.

Mockwave is still in an early stage. I hope you find it useful but please feel free to open feature requests!

Getting started

Install Mockwave in your project:

npm install mockwave

Creating mock APIs

Mockwave uses Fastify under the hood for mock API routes. Routes are defined using Fastify handler functions. This gives you the full power of the Fastify framework. Your mock APIs can return static data or contain logic.

Mock APIs are defined in a JavaScript file. The convention is for the file name to end with .mock.js, but this is just a convention and is not required.

The file should have a default export which is a function. The function takes one argument, server, which is the underlying Fastify instance. From here, you can define your own mock endpoints and their data or behavior.

See the Fastify documentation for more details on adding routes.

Example

Here is an example mock file:

export default function myMock(app) {
    app.get('/api/test', (request, reply) => {
        reply
            .code(200)
            .send({ hello: 'world' });
    });
}

Using multiple mock files

You can split up your mocks into multiple .mock.js files. Just specify them as a space-separated list on the command line when you run Mockwave.

Running Mockwave

Once you have your mocks defined in a mock file, you can start Mockwave. You'll need the file name of the mock as well as the URL you want to proxy other requests to.

The structure of the command is:

npx mockwave -t <proxy target> <mock file name>

Assuming you want to proxy to https://httpbin.org and your mock file is called myMock.mock.js, the command is as follows:

npx mockwave -t https://httpbin.org myMock.mock.js

By default, Mockwave listens on port 8000. If you need a different port, you can specify the -p option and enter a different port number.

Now, when you send a request to http://localhost:8000, if it matches one of your mock routes, your mock will handle it. Otherwise, the request will be proxied to https://httpbin.org.