gstroup / apimocker

node.js module to run a simple http server for mock service responses.
MIT License
280 stars 81 forks source link

Add suppport for js config files #100

Closed twalker closed 5 years ago

twalker commented 5 years ago

This adds support for config.js files in addition to the existing config.json format.

JSON can become a little verbose, especially when configuring many routes (webServices). JS config files allow the configuration to be organized and, potentially dynamic.

// config.js
const accountRountes = require('./routes/account')
const orderRoutes = require('./routes/order')
module.exports = () => {
  webServices: {
    ...accountRoutes,
    ...orderRoutes
  },
  /* other configuration options... /*
}

The approach taken is to use dynamic require() imports rather than fs.readFileSync, since require imports both js and json files by default.

This is similar to PR #74, which looked good. However this PR: takes advantage of ES6 syntax, adds tests, and clears require's module cache for when /admin/reload is called.

gstroup commented 5 years ago

This looks good. Very useful! I do remember having some problems with the require cache, but that was years ago. Looks like you've solved that now. And the mock-require package looks good for testing too. One thing - can you please add a small explanation in the README? Then I will merge and publish. Thank you very much.

twalker commented 5 years ago

Fantastic! I've updated the documentation to note the option to use JS config files. I don't know why I didn't think to update the documentation in the first place.

By the way, congratulations to you and this module for the 100th Pull requests from 24 contributors!

gstroup commented 5 years ago

Thanks again. I just published v1.1.0 to npm. Wow, 100 PRs and 24 contributors is pretty cool! Big thanks to you and all of the OSS community.