api-mid-mock v3.0.1
api-mid-mock
The api mock middleware that allows to serve local files as responses to intercepted api calls. Ideally feats to webpack-dev-server like Angular proxy server.
features
š„ Built in support for hot mocks replacement
š Quickly and easily configure the API via JSON
š Or using the mock files structure
Installation
npm install api-mid-mock --save-devBypass function
Call APIMocker() function to get the pre-configured bypass function.
There is object with four parameters, that you can pass to:
path- specifies path to config file or all mocks root folder, by default./mocksuseFiles- boolean flag that allows adding the mock files to configuration, by defaultfalseacceptOnlyJSON- boolean flag to skip loading route from browser application by checking Accept header, by defaulttruewatch- boolean flag to disable thefs.watchthat implements the hot mocks replacement (can be helpful on systems wherefs.watchis broken), by defaulttrue
Files based usage
Put your JSON files in folder, for example ./mocks:
./mocks
/api
/users.json
/goods.jsonAnd add proxy configuration proxy.config.js (for Angular application):
const APIMocker = require('api-mid-mock')
module.exports = [{
context: () => true,
target: 'https://your.domain',
bypass: APIMocker({path: './mocks', useFiles: true}),
}]Or put it into devServer.proxy section into your webpack config.
That's it!
When you'll perform requests to /api/users or /api/goods,
the proxy server will intercept it and return JSON from existed files.
All other requests will be passed to your back-end.
To distinguish different HTTP methods, just call the files with according prefix:
./mocks/api
/get.users.json
/post.users.jsonConfig based usage
The same as previous, but put to ./mocks folder the index.js file, that returns an object
describes the configuration:
module.exports = {
'get /api/users': require('./api/get.users.json'),
'post /api/users': require('./api/post.users.json'),
'/api/goods': [
{id: 1, price: 100.00, title: 'Sample'},
{id: 2, price: 99.99, title: 'Worse'}
],
'/api/goods/:id': function(req, res) {
const {id} = req.params
if (id === '1' || id === '2') {
res.end(JSON.stringify({status: 'Found!'}))
} else {
res.statusCode = 400
res.end('Bad Request')
}
}
}The key string format
method /path/:param/*?query=valueMethod
The method is case-insensitive. It can be one of HTTP methods like GET or POST.
Or it can be ANY or * or just omit it to match with any request methods.
Path
The path describes the part or URL that can contain wildcards and parameters.
Wildcard * in a path matches any substring.
Params allows you to deal with dynamics URLs and you can get an according value from the req.params.
Query params
It is possible to filter requests by query parameters.
You can use only parameter name to check if it exists: ?param.
Or check full coincidence for a name and value: ?param=value.
Responses
There are three types of responses: strings, objects and function.
The strings will be returned as is.
The objects will be translated to string by JSON.stringify and returned with the Content-type: application/json header.
The functions
The functions can fully manage the request and response with according parameters.
If you need to pass the request to back-end just return null or undefined from the function.
But there is one difference with webpack-dev-server bypass function.
You cannot return the path to the file to return it as response.
Please, use require or other ways to get the response content and return the content.
To end the request without body, return true from function.
Chokidar usage
Because of fs.watch is broken on some systems, you can use chokidar to watch files changes.
Here is the example how to wrap APIMocker with chokidar in webpack.config.js:
let mocker = () => {}
chokidar.watch('./mocks').on('all', () => {
mocker = APIMocker({path: './mocks', watch: false})
})
module.exports = [{
context: () => true,
target: 'https://your.domain',
bypass: (req, res, next) => mocker(req, res, next),
}]Node.js example
You can run server with mock data without webpack, with node.js only.
Here is the example of server.js file:
const http = require('http')
const url = require('url')
const APIMock = require('api-mid-mock')
const mocked = APIMock({ path: './index.js', acceptOnlyJSON: false })
const requestListener = function (req, res) {
req.accepts = () => req.headers.accept?.split(',').map((s) => s.replace(/;.+$/, '')) || []
const parsedUrl = url.parse(req.url, true)
req.path = parsedUrl.path.replace(parsedUrl.search, '')
req.query = parsedUrl.query
req.params = {}
mocked(req, res, () => {}).then((result) => {
if (!result) {
res.writeHead(400)
res.end('Please, try supported requests')
}
})
}
const server = http.createServer(requestListener)
server.listen(80)To start the server just run node server.js.
The small helper to get the request body:
function body(req) {
return new Promise((resolve) => {
let body = ''
req.on('data', (chunk) => {
body += chunk.toString()
})
req.on('end', () => {
try {
body = JSON.parse(body)
} catch {}
resolve(body)
})
})
}use it with await:
const data = await body(req)