gstroup / apimocker

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

accept callback function for user requests as a parameter for apimock #39

Open asnov opened 8 years ago

asnov commented 8 years ago

Hi @gstroup ,

I use apimock like this:

var ApiMocker = require("apimocker");
var apiMocker = ApiMocker.createServer(options)
    .setConfigFile(__dirname + '/config.json')
    .start(port, function() {/* callback for establish connection */});

Now I faced the task to use TimeStamp in responses because of the strict security restrictions of the .Net WCF library.

Is it possible to add response callback function to apimock?

tedvanderveen commented 8 years ago

+1

gstroup commented 8 years ago

I think this is a good idea in general. @ztsmith suggested that it might be a better idea to expose the express middleware, then you could add a middleware function to do whatever you like. Would that solve your problem?? Let me know what you think.

asnov commented 8 years ago

Yes, that would solve my task. How do you think to expose the express middleware?

gstroup commented 8 years ago

Please take a look at the latest version 0.4.9. I added support for custom middleware functions, that should solve this problem. Please let me know what you think! @tedvanderveen @asnov @ztsmith

asnov commented 8 years ago

Great! It is not so cute as to add a separate method to use chaining like this:

ApiMocker.createServer().setConfigFile().addmiddleware().start();

but it is works. It solves my task. Thank you!

ztsmith commented 8 years ago

Cool!

This makes me think.... if the config file was a JS file, we could define custom functions and middleware and specify them in the config. I think this would unlock an endless array of configuration options.

gstroup commented 8 years ago

@asnov - yes, I thought of adding a simple "addMiddleware()" function, but I decided to expose the entire array. This way, people have full control over the order of middleware functions. But, it would probably make sense to add a simple "addMiddleware()" function as well.

@ztsmith - I was also thinking how it would be great to define custom functions right in the config file. But I wasn't sure how the config file would parse then... we'd need to do some more testing on that. But I like the idea.

tedvanderveen commented 8 years ago

@gstroup what is the status of this? Thanks!

gstroup commented 8 years ago

@tedvanderveen At the moment, the entire "middlewares" array is exposed, so you can add a customer middleware function anywhere in the pipeline. https://github.com/gstroup/apimocker#adding-custom-middleware Does that meet your needs?

Or would you prefer to have a simple addMiddleware() function instead? That would require a small code change.