apimocker icon indicating copy to clipboard operation
apimocker copied to clipboard

accept callback function for user requests as a parameter for apimock

Open asnov opened this issue 9 years ago • 9 comments

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?

asnov avatar Dec 17 '15 21:12 asnov

+1

tedvanderveen avatar Dec 18 '15 10:12 tedvanderveen

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.

gstroup avatar Dec 18 '15 17:12 gstroup

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

asnov avatar Dec 19 '15 15:12 asnov

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

gstroup avatar Dec 22 '15 23:12 gstroup

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!

asnov avatar Dec 23 '15 15:12 asnov

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.

ztsmith avatar Dec 24 '15 21:12 ztsmith

@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.

gstroup avatar Dec 28 '15 18:12 gstroup

@gstroup what is the status of this? Thanks!

tedvanderveen avatar Feb 02 '16 19:02 tedvanderveen

@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.

gstroup avatar Feb 02 '16 19:02 gstroup