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

simple-mock-webpack-plugin

v0.0.2

Published

Binds a mocking service to your webpack-dev-server

Downloads

1

Readme

simple-mock-webpack-plugin

中文文档

Binds a local service to your webpack-dev-server for mocking data.

Features

  • Config of mocking supports hot reloading
  • No more new listening port, no more CORS
  • Mocks APIs in a succinct and simple way
  • Stores data in the service(in memory), like a database
  • Uses mockjs to support data mocking, learn more about nuysoft/Mock

P.S.: This repo is called simple-mock-webpack-plugin, but it isn't a stantard webpack-plugin because of its usage. 😂

Installation

Yarn

yarn add -D simple-mock-webpack-plugin

NPM

npm i -D simple-mock-webpack-plugin

Getting Started

1. Sets devServer.before in webpack.config.js

const { buildBefore } = require('simple-mock-webpack-plugin')

module.exports = {
    devServer: {
        before: buildBefore()
    }
}

2. Create a config file named mock.js in the same directory

module.exports = {
    apis: [
        {
            url: '/test',
            template: {
                code: '@integer(100,600)',
                msg: 'ok'
            }
        },
        {
            url: '/test-promise',
            template: (mock) => {
                return new Promise((res) => {
                    setTimeout(() => {
                        res(mock({
                            code: '@integer(100,600)',
                            msg: 'ok'
                        }))
                    }, 3000)
                })
            }
        },
        {
            url: '/count',
            template(mock, { state, request, response, Mock }) {
                state.counter = (state.counter || 0) + 1
                return {
                    data: state.counter
                }
            }
        },
        {
            url: '/get-count-number',
            template(mock, { state, request, response, Mock }) {
                return {
                    count: state.counter || 0
                }
            }
        },
    ]
}

3. Have a try

const _fetch = url => fetch(url).then(resp => resp.json()).then(console.log)

_fetch('/test') // {code: 264, msg: "ok"}
_fetch('/count') // {data: 1}
_fetch('/count') // {data: 2}

Options

| Name | Type | Required | Default | Description | |------------- |-------------- |------ |------------- |--------------------------------------- | | configPath | string | No | ./mock.js | The path of config file | | log | boolean | No | true | Whether prints logs | | before | Function | No | null | Customized before | | reloadDelay | number | No | 300 | Dealy of service's reloading |

Example

module.exports = {
    devServer: {
        before: buildBefore({ configPath: path.resolve(__dirname, 'my_mock.js') })
    }
}

Configuration of Mocking

Learn more about the configuration at examples.

  • MockConfig

| Name | Type | Required | Default | Description | |-------- |--------------- |---------- |-------- |----------------------------------- | | prefix | string | No | / | The prefix of all APIs | | delay | boolean | No | 0 | Dealy of the reponse for all APIs | | apis | MockAPI[] | Yes | | The configuration of APIs |

  • MockAPI

| Name | Type | Required | Default | Description | |---------- |----------------------------- |-------- |-------- |------------------------------ | | url | string | Yes | | Path relative to the prefix | | method | string | No | all | The method of API | | template | object \| MockAPIHandler | Yes | | The tempalte of response. It will be regared as mockjs template if it is an object.(Learn more at Syntax). You can custmize reponse if you set it as a function. |

  • MockAPIHandler
(mock: MockjsMock, options: ExtraOptions) => object|Promise<object>
  • ExtraOptions

| Name | Type | Default | Description | |---------- |-------------------- |-------- |----------------------------------------------------------------------------- | | state | object | {} | Temporary states in the service. It will not be affected from config changing | | request | Express.Request | | Express Request | | response | Express.Response | | Express Response | | Mock | Mock.Mockjs | | Mock = require('mockjs') |

License

MIT