Skip to content

zinkey/egg-api-mock

Repository files navigation

egg-api-mock

NPM version build status

An egg plugin for api mock, enabled under local development default.

Install

$ npm i egg-api-mock --save

Usage

Add egg-api-mock as plugin

// {app_root}/config/plugin.js
exports.apiMock = {
  enable: true,
  package: 'egg-api-mock',
};

Configuration

// {app_root}/config/config.default.js
exports.apiMock = {
  dir: 'mock', // default mock dir
  env: [ 'local' ], // enabled under local development default
  glob: '**/*.mock.js', // default glob
};

You can use koa-router like methods in ${dir}/${glob}, default: mock/**/*.mock.js

Example:

mock/1.mock.js

module.exports = {
  "/api/user/:id": (ctx) => {
    ctx.body = ctx.params.id;
  },
  "/api/users": {
    success: true
  },
};

mock/2.mock.js

module.exports = {
  "POST /api/post": (ctx) => {
    ctx.body = ctx.request.body;
  },
};

Then you can request you app:

/api/user/1 : 1
/api/user/2 : 2
/api/users : {success: true}
/api/post : post data

License

MIT

Releases

No releases published

Packages

No packages published