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

mock-proxy-autocookie

v0.0.13

Published

local server mock and proxy tool

Downloads

6

Readme

mock-proxy-autocookie

前后端分离项目中的本地mock及远程代理

notice: if use auto proxy cookie, you need install chrome-cookies-secure for your project, because it is peerDependencies

install

npm install mock-proxy-autocookie --save-dev
var mockProxy = require('mock-proxy-autocookie')

define config /xxx/config.js

module.exports = [
  {
    rules: ['^/api/', ^/common-api/], // array,typeof string or regexp
    rules: '^/api/', // string or regexp
    proxyConfig: {
      host: '12.12.12.12',
      port: 8080,
      autoCookie: false, // set true for auto proxy cookie, need project install chrome-cookies-secure
      isHttps: false, // default the same with original
      timeout: 30000, // ms, default 30000ms
      headers: { // set custom headers to proxy server, default proxy original headers
        cookie: 'xxxx'
      },
      redirect: (path) => { // could config redirect path for remote api
        return path
      },
      excludes: [ // when use proxy mode, this apis use local mode
        '^/api/get_index_data/', // string
        /^\/api\/user_info/, // regexp
        (request, proxyConfig) => { // function
          return request.headers.xxx === '/xxxx/' // any logic
        }
      ],
      fillMissingMock: false, // fill missing mock file when lost
      beforeRequest: (params, options) => {
        return [
          {
            _token: 'xxxxx', // add some extra params or reset key:value in params
          },
          {
            agent: false, // set some extra options for nodejs http(s).request`s options
            auth: 'xxxx'
          }
        ]
      },
      afterResponse: (path, responseStr, { request, response }) => responseStr
    },
    mockConfig: {
      path: 'mock', // project`s mock dir name, default 'mock'
      ext: '.js',
      fillMissingMock: boolean || object || (apiPath, params) => (promise<object> | object)
    }
  }
]

if you use express server, you can use it like here:

var app = express()
var config = require('/xxx/config')

app.use(mockProxy(config));

app.use(mockProxy(
  '/xxx/config.js' // if set the config path as first param,the change is immediate effect when modify config
));

app.use(mockProxy(
  config,
  '/xxx/xxx/personal_path_config.js' // optional,prevent modification conflicts, could set the second param as self config, add this config file to .gitignore file
));

app.use(mockProxy(
  '/xxx/config.js',
  '/xxx/personal_path_config.js',
  profile // profile default is "Default", to check your profile, look at ~/Library/Application Support/Google/Chrome/${profile}/Cookies
));

for example,a api like '/common-api/get_user_info', you can define a js file at ${project}/mock/common-api/get_user_info.js, it`s content like

function (params) {
    return {
        err_no: 0,
        err_msg: '',
        sleep: 1000, // mock 1 second delay
        data: {
            name: 'zhangsan'
        }
    }
}

or

{
    err_no: 0,
    err_msg: '',
    data: {
        name: 'zhangsan'
    }
}

if you want to cache mock status by context, you can do it like this:

let times = 0
return function (params) { // this 'return' is required
  return {
    code: xxx,
    data: {
      times: times++ // this can cache prev value
    }
  }
}

for example another, a api like '/api/a/b/c', you can define a js file at ${project}/mock/api/a_b_c.js if you use gulp-connect server, you can use it like here:

var connect = require('gulp-connect');
var config = require('/xxx/config');
connect.server({
    host: host,
    port: port,
    root: ['/'],
    middleware: function(connect, opt) {
        return [
            mockProxy(config || '/xxx/config')  // if set a path of config, config is immediate effect
        ];
    }
});

if you use webpack-dev-server, you can use it like here on webpack.config.js:

var config = require('/xxx/config');
devServer: {
  contentBase: '/dist',
  port: 8888,
  historyApiFallback: true,
  inline: true,
  before: function(app) {
    app.use(mockProxy(config || '/xxx/config')) // if set a path of config, config is immediate effect
  }
}

if you look at all of apis at this project, input 'https?:{host}/show-apis', need has mock file and meta about api description