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 🙏

© 2025 – Pkg Stats / Ryan Hefner

vite-plugin-local-mock

v0.1.3

Published

vite plugin for local mock

Readme

vite-plugin-local-mock

A lightweight and flexible mock data plugin for Vite, perfect for frontend development without a backend API.

Features

  • 🚀 Simple setup and configuration
  • 🔄 Support for dynamic routes with REST-style parameters
  • 📊 Support for dynamic response generation based on request parameters
  • ⏱️ Configurable response delay to simulate network latency
  • 🔧 HTTP method specification for precise request matching

Installation

npm i vite-plugin-local-mock -D
# or
yarn add vite-plugin-local-mock -D
# or
pnpm add vite-plugin-local-mock -D

Setup

Add the plugin to your vite.config.js or vite.config.ts:

import { defineConfig } from 'vite';
import localMock from 'vite-plugin-local-mock';

export default defineConfig({
  plugins: [
    localMock({
      dir: 'mock',
      enable: true,
      pathMapConfig: 'mockMap',
    }),
  ],
});

Configuration Options

| Option | Type | Default | Description | | --------------- | --------- | -------- | ----------------------------------------------------------- | | dir | string | 'mock' | Directory for mock files | | enable | boolean | true | Enable or disable the plugin | | pathMapConfig | string | '' | Filename for path mapping configuration (without extension) |

Usage

Basic Usage

Create a mock file in the mock directory. The filename should match the request path and use the .cjs extension:

// mock/api/user.cjs
module.exports = {
  // Required flag to enable mocking
  __mock: true,

  // Your response data
  code: 0,
  data: {
    name: 'John Doe',
    email: '[email protected]',
    role: 'admin',
  },
};

Dynamic Responses

You can define dynamic responses based on request parameters:

// mock/api/login.cjs
module.exports = (params) => ({
  __mock: true,
  code: 0,
  data: {
    username: params.username || 'guest',
    token: 'mock-token-' + Date.now(),
    loginTime: new Date().toISOString(),
  },
});

REST API Mocking

For RESTful APIs with dynamic parameters, configure a mapping file:

  1. Set the pathMapConfig option in your Vite config:
localMock({
  pathMapConfig: 'mockMap',
});
  1. Create a mockMap.cjs file in your mock directory:
// mock/mockMap.cjs
module.exports = {
  'api/users/:id': {
    method: 'GET',
    path: 'api/user-detail',
  },
  'api/products/:category/:id': {
    path: 'api/product-detail',
  },
};
  1. Create the corresponding mock files:
// mock/api/user-detail.cjs
module.exports = (params) => ({
  __mock: true,
  code: 0,
  data: {
    id: params.id,
    name: `User ${params.id}`,
    email: `user${params.id}@example.com`,
  },
});

HTTP Method Specification

You can specify HTTP methods in your path mapping to handle different request types for the same URL:

// mock/mockMap.cjs
module.exports = {
  'api/users/:id': {
    method: 'GET',
    path: 'api/user-detail',
  },
  'api/users': {
    method: 'POST',
    path: 'api/user-create',
  },
};

If no method is specified, the mock will match any HTTP method.

Response Delay

You can simulate network latency by adding a __delay property to your mock response:

// mock/api/slow-endpoint.cjs
module.exports = {
  __mock: true,
  __delay: 2000, // Delay response by 2 seconds

  code: 0,
  data: {
    message: 'This response was delayed by 2 seconds',
  },
};

You can also use delay with dynamic responses:

// mock/api/dynamic-delay.cjs
module.exports = (params) => ({
  __mock: true,
  __delay: parseInt(params.delay) || 1000, // Use delay from query parameter

  code: 0,
  data: {
    delay: parseInt(params.delay) || 1000,
    timestamp: Date.now(),
  },
});

Examples

GET Request with Query Parameters

Request: GET /api/users?page=1&limit=10

Mock file: mock/api/users.cjs

module.exports = (params) => ({
  __mock: true,
  code: 0,
  data: {
    page: parseInt(params.page) || 1,
    limit: parseInt(params.limit) || 10,
    total: 100,
    users: Array.from({ length: parseInt(params.limit) || 10 }, (_, i) => ({
      id: i + 1 + (parseInt(params.page) - 1 || 0) * (parseInt(params.limit) || 10),
      name: `User ${i + 1}`,
    })),
  },
});

POST Request with Body

Request: POST /api/login with body { "username": "admin", "password": "123456" }

Mock file: mock/api/login.cjs

module.exports = (params) => {
  if (params.username === 'admin' && params.password === '123456') {
    return {
      __mock: true,
      code: 0,
      data: {
        token: 'mock-token-admin',
        username: 'admin',
        role: 'administrator',
      },
    };
  } else {
    return {
      __mock: true,
      code: 1001,
      message: 'Invalid username or password',
    };
  }
};