Giter Club home page Giter Club logo

mocor's Introduction

Mocor

πŸ‘‰ δΈ­ζ–‡ζ–‡ζ‘£

BUILD ELEGANT MOCK API WITH ELEGANT API DOCUMENTATION.

fff


Mocor is the most convenient API mocking tool.

  • Extremely easy to use.
  • Automatically generates API documentation.
  • Works even better when combined with AirCode.

Getting started

  1. Can be integrated locally with Koa:
const Koa = require('koa');
const {bodyParser} = require("@koa/bodyparser");

const {Mock} = require('mocor');

const app = new Koa();

const mock = new Mock();

mock.use({
  hello: 'mocor',
}, {
  allowMethods: ['GET','POST']
});

app
  .use(bodyParser())
  .use(async (ctx) => {
    const params = ctx.method === 'GET' ? ctx.query : ctx.request.body;
    const result = await mock.execute(params, ctx);
    if(ctx.response.headers['content-type'] === 'application/json') {
      ctx.body = JSON.stringify(result);
    } else {
      ctx.body = result;
    }
  });

app.listen(3000);

Accessing http://localhost:3000 will allow you to see the API page.

  1. With AirCode (recommended):
// @see https://docs.aircode.io/guide/functions/
const aircode = require('aircode');
const {Mock} = require('mocor');

const mock = new Mock('hello', 'This is a demo api generated by mocor.');

mock.post({
  message: 'Hi, Mocor',
});

module.exports = mock.compile();

Data Mocking API

Mocor provides powerful API generation helper functions to generate various types of data. Here are the built-in helper functions in Mocor:

  • randomFloat(from = 0, to = 1)
    • Generates a random floating-point number within a specified range.
  • randomInteger(from = 0, to = 1000)
    • Generates a random integer within a specified range.
  • randomString(len = 16)
    • Generates a random string with a specified length.
  • randomUUID()
    • Generates a random UUID string.
  • randomDate(from = new Date(0), to = new Date())
    • Generates a random date within a specified time range.
  • repeat(schema, min = 3, max = min)
    • Repeats a specified schema a certain number of times and generates a list.
  • join(list, joint = '')
    • Joins a list of elements into a string with a specified separator.

These helper functions can be used within Mocor to generate dynamic data for your API endpoints.

Example 1

The following example generates 5 to 10 student records, including their names, IDs, and grades.

// @see https://docs.aircode.io/guide/functions/
const aircode = require('aircode');
const {Mock, repeat, randomInteger, randomUUID} = require('mocor');

const mock = new Mock('random list', 'This is a demo api generated by mocor.');

const genName = () => {
  let i = 0;
  return () => `student${i++}`;
}

mock.use(repeat({
  name: genName(),
  score: randomInteger(0, 101),
  id: randomUUID(),
}, 5, 10), {
  allowMethods: ['GET', 'POST'],
});

module.exports = mock.compile();
  • randomLatinLetter()
    • Generates a random Latin (English) letter.
  • randomLatinWord(minLetters = 3, maxLatters = 12)
    • Generates a word with a specified range of letter counts.
  • randomLatinParagraph(minWords = 10, maxWords = 40)
    • Generates a paragraph consisting of a certain number of words.
  • randomLatinArticle(minParagraph = 3, maxParagraph = 10)
    • Generates an article with a certain number of paragraphs.
  • randomChineseName()
    • Generates a random Chinese name.
  • randomChineseParagraph()
    • Generates a random Chinese paragraph.
  • randomChineseArticle(min = 200, max = 800)
    • Generates a random Chinese article with a specific number of characters.

Example 2

The following example generates Chinese student information:

// @see https://docs.aircode.io/guide/functions/
const aircode = require('aircode');
const {Mock, repeat, randomInteger, randomUUID, randomChineseName, randomChineseArticle} = require('mocor');

const mock = new Mock('chinese students', 'This is a demo api generated by mocor.');

mock.use(repeat({
  name: randomChineseName(),
  scores: repeat(randomInteger(50, 101), 5),
  id: randomUUID(),
  comment: randomChineseArticle(),
}, 5, 10), {
  allowMethods: ['GET', 'POST'],
});

module.exports = mock.compile();
  • param(name, type = 'any', defaultValue = null, info = ' ')
    • Generates data based on the passed parameter.
  • randomPick(...list)
    • Retrieves a random value from an array with equal probability.
  • randomPickWeight(...list)
    • Randomly retrieves a value based on the weight assigned to each item.
  • poll(...list)
    • Cycles through the values in an array.

Example 3

The following example returns different data based on different parameters:

// @see https://docs.aircode.io/guide/functions/
const aircode = require('aircode');
const {Mock, param} = require('mocor');

const mock = new Mock('params', 'This is a demo api generated by mocor.');

mock.post((args) => {
  const type = param('type', 'string', 'odd', 'get numbers even or odd')(args);
  if(type === 'odd') return [1, 3, 5, 7, 9];
  return [0, 2, 4, 6, 8];
});

module.exports = mock.compile();

Example 4

The following example simulates 32 pagination data entries and returns different data items based on the value of the page parameter:

// @see https://docs.aircode.io/guide/functions/
const aircode = require('aircode');
const {Mock, param, repeat, randomChineseName, randomInteger} = require('mocor');

const mock = new Mock('params', 'This is a demo api generated by mocor.');

mock.post((args) => {
  const total = 32;
  const pn = param('pn', 'number', 1, 'the page number')(args);
  const n = Math.min(10, Math.max(0, total - (pn - 1) * 10));
  return repeat({
    name: randomChineseName(),
    score: randomInteger(),
  }, n);
});

module.exports = mock.compile();

Example 5

The following example simulates an error with a certain probability:

// @see https://docs.aircode.io/guide/functions/
const aircode = require('aircode');
const {Mock, repeat, randomChineseName, randomInteger} = require('mocor');

const mock = new Mock('Random error', 'This is a demo api generated by mocor.');

mock.post(({context}) => {
  if(Math.random() > 0.5) {
    context.status(500);
    return {
      error: 'fatal error',
    }
  }
  return repeat({
    name: randomChineseName(),
    score: randomInteger(),
  }, 5);
});

module.exports = mock.compile();

Advanced Usage

If you want to debug using mock data first and then switch to the production environment seamlessly in AirCode, you can follow these steps:

First, create a mock environment cloud function named student-mock.js in AirCode.

// @see https://docs.aircode.io/guide/functions/
const aircode = require('aircode');
const {Mock, repeat, randomInteger, randomUUID, randomChineseName, randomChineseArticle} = require('mocor');

const mock = new Mock('chinese students', 'This is a demo api generated by mocor.');

mock.use(repeat({
  name: randomChineseName(),
  scores: repeat(randomInteger(50, 101), 5),
  id: randomUUID(),
  comment: randomChineseArticle(),
}, 5, 10), {
  allowMethods: ['GET', 'POST'],
});

module.exports = mock.compile();

Then, create the production environment file student.js using the following code:

// @see https://docs.aircode.io/guide/functions/
const aircode = require('aircode');
const {mocking} = require('mocor');

module.exports = mocking(require('./student-mock.js'),
  async function (params, context) {
    console.log('Received params:', params);
    return {
      message: 'Hi, AirCode.',
    };
  });

So accessing the production API can be done by setting the HTTP request header x-motor to a value of 1, which will allow you to use mock data. If the x-motor header is not set, the API will return real data.

Meanwhile, you can still access the API documentation through the cloud function that generates mock data.

mocor's People

Contributors

akira-cn avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.