Skip to content

I read almost all the open questions, maybe this article can provide some solutions for them. #1338

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wll8 opened this issue Jun 14, 2022 · 0 comments

Comments

@wll8
Copy link
Contributor

wll8 commented Jun 14, 2022

Thanks json-server, you are great. I gradually created the mockm project in the process of using, maybe it can provide some help for the following problems:

read and write db

Read or change it from custom api or json-server api.

module.exports = (util) => {
  return {
    api: {
      'get /test/db' (req, res) {
        const db = global.config._db // Object wrapped by _.chain(db.json)
        const data = db.getState() // whole json data
        data.user[0].t1 = 111
        db.write() // call write to store changes
        data.user[0].t2 = 222
        db.get(`user`).set(`[0].t3`, 333).value() // You can use lodash chain operations, you need to call .value() after the operation is complete
        res.json(data.user[0])
      },
    },
    db: {
      user: [
        {
          name: `ace`,
        },
      ],
    },
  }
}

Intercept requests and responses

Just create the same api and you can intercept it.

module.exports = (util) => {
  return {
    api: {
      // For example, permission checks can be created at all entry points
      'use /' (req, res, next) { 
        if(req.headers.token === undefined) {
          res.status(403).json({msg: `No permission`})
        } else {
          next()
        }
      },
      // Intercepts any requests sent to db.json, and responses from db.josn
      'patch /books/:id' (req, res, next) { // intercept config.db
        req.body.a = `111` // Modify the data passed in by the user
        next()
        res.mm.resHandleJsonApi = async (arg) => {
          arg.data.a = `222` // Modify the response, it will not be stored in db.json
          return arg.resHandleJsonApi(arg)
        }
      },
    },
    db: {
      books: [
        {
          id: 1,
          title: `css`,
        },
      ],
    },
  }
}

route rewrite

Acts on static file addresses, addresses generated by db.json, and custom api addresses.

module.exports = (util) => {
  return {
    route: {
      '/test/db/api/*': '/$1', // /test/db/api/books/1 => /books/1
    },
  }
}

upload files

const path = require(`path`)

module.exports = (util) => {
  return {
    api: {
      async '/upload'(req, res) {
        const multiparty = await util.toolObj.generate.initPackge(`multiparty`) // or require(`multiparty`)
        const form = new multiparty.Form({
          uploadDir: `${__dirname}/upload/`,
        })
        form.parse(req, async (err, fields = [], files) => {
          const file = files.file[0]
          let url = `http://127.0.0.1:${global.config.port}/file/${path.parse(file.path).base}`
          // Can also be saved to db.json: global.config._db.
          res.json({url})
        })
      },
    },
    // Support for multiple static directories, and histroy mode
    static: {
      path: `/file`,
      fileDir: `./upload`,
    },
  }
}

how to use

Sorry, as I'm not familiar with English, there is currently no documentation in English, but the tool is simple and should be easy to use.

# Check the node version, currently mockm supports node v10.12.0 and above
node -v

# Install
npm i -g mockm

# Run it with the sample configuration
mockm --config 

# Browser open http://127.0.0.1:9005/#/apiStudio/

The above command will generate common configuration, modify mm.config.js and try it out.

The following documents can be helpful to you:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant