Comments (7)
- To proxy or not, perhaps accepting a function for the
proxy
property (like yourmatch
function) would help? - Overriding the
render
function in a definition allows you to send any response you want. - You can group definitions any way you want. No need to put them in
get/
directory, the definition should have e.g.method: 'GET'
.
from dyson.
If you implement it on the proxy
option, that will let you control proxying. If you implement it as its own option, it will allow that and much more without significantly more effort.
It is not obvious to me how you suggest to use the render
method, and I think it would be even less obvious to many dyson users. My suggestion is to offer the user a dedicated option to control the matching, which would be DRY (by avoiding to duplicate the same kind of matching code), and would unequivocally express the intention.
I think my example better illustrates my need than my poor wording. The question to proxy or not is peripheral to me, as it would be elegantly addressed in the move. The real need for me is to be able to split multiple definitions for the same route. If two configs in different files match the same route, I cannot know which one will be processed first and so a "default" config could respond first.
I am not sure it would be possible to hack the matching logic into the render
method, since the response may already be in the tube at this point. It could probably be done by overriding the callback
method, but then you've got no more dyson to generate the response. Maybe there's a clean solution that I don't see?
Before adding the match
option in my fork, the best I could find to do it was a big switch
or if
block in the template
function.
from dyson.
Thanks for your input, @rixo.
- I'm thinking about supporting extra middleware, both on the global and definition level.
- Somehow have more control over the middlewares being applied or not (e.g. to bypass default middlewares such as "response" and "render").
- Combine the non-proxy and proxy flows into one middleware chain.
I'll give this a shot soon, but definitely interested in your thoughts.
from dyson.
Totally agree. I have been fiddling with some pretty advanced usages around dyson, and all the rough edges I hit would have been prevented if everything had been a middleware. Here are some examples:
- Validating client request and stubbed response against a blueprint API spec (global middleware -- notice that, like the proxy, would need access to the request's body).
- Recording client requests to the mock server, in order to replay them later to the actual server (in testing, again, global middleware).
- Piloting this polymorph stack in javascript (e.g. gulp), by chaining middlewares, saving the trouble to build a proxy chain over HTTP.
- Plugging dyson behind
gulp watch
for smooth work flow. Not really a good example because I have since realized that I could simply restart dyson from the gulp... I don't know why I wanted to keep it in the same process as gulp, but had the idea been legitimate, it has proved really difficult. In essence, I've had to expose registerServices and cherry pick what I needed in bootstrap. - And, in effect, that would make it quite easy to implement the current issue on a vanilla dyson stack... Provided dyson exposes the config currently being processed to the req (see 2nd code example there).
My suggestions to go down that road would be:
- Make the bootstrap process only single middleware chain. The encapsulation that follows would have saved me from copy/pasting some dyson code in one of my examples.
- Don't make
listen
part of this stack, move it to the cli wrapper, so that we can importdyson.js
as "the whole package" middleware. - As you say, make the proxy decision a middleware in this stack.
- Do rename "callback" to "response", as it seems you have inadvertently done in your last comment.
- If you allow extra middleware by config, you should probably distinguish "before" and "after" middlewares. From my examples, it seems a pretty common need.
- And finally, for fine grained control over the middleware stack, I would say "don't bother"... Just make all the middlewares public (kind of already the case with
require
). Since at this point there will be no intelligence left in the bootstrap, just gluing code, I don't see any problem building one's custom stack by copying what is done in the bootstrap. The worst that can happen is to have to give this a bit of easy maintenance if dyson later changes its middleware stack. I don't think adding options for this advanced use case would worth the trouble.
from dyson.
Hi @rixo, I've just pushed a change to master including docs (5f6d662) that should allow to handle some use cases more easily. Happy to hear your feedback/suggestion if it still matters to you.
from dyson.
Hi @webpro ! Indeed, your change addresses several of the use cases in my last comment by letting use dyson as a whole as a middleware, without having to duplicate part of its code anymore.
I think you may even get a cleaner separation of concerns by removing the dependency of dyson's "services" on express app this way:
var configs = dyson.getConfigurations(options);
var routes = dyson.registerServices(options, configs);
var app = express().use(routes);
Also, it doesn't seem that the configs could be useful alone by themselves so why not encapsulate the loader and defaults logic into registerServices and save the trouble of exposing getConfigurations as API? In the end that would allow to use dyson with this very common middleware pattern:
// registerServices would be renamed to something like dyson.middleware(...)
// or even dyson(...)?
var app = express().use(dyson.registerServices(options)).listen(3000);
I've started to explore in this direction, if you want to have a look (that's not tested in a real app, only against the unit tests).
Unfortunately regarding the present issue that won't help, because the middleware stack we need to hook into is the one inside the registerServices method. A simple solution would be to have an extra middleware just before config.callback
that can be set in the route config (we can't use config.callback since replacing it loses the response generation logic, which really is dyson's heart).
from dyson.
I'm going to close this issue, as it's a year old now. Feel free to re-open or create a new ticket.
from dyson.
Related Issues (20)
- Default options not working HOT 7
- How to set options.strict to false HOT 2
- Hi. It is possible to use with webpack-dev-server hooks? HOT 1
- Objects being mutated within template function HOT 3
- Demo site is down HOT 3
- Explicit versions prevent fixing npm vulnerabilities HOT 2
- Query Params HOT 2
- Request is not proxied when form-data is used
- Add support for TypeScript files
- json file and js file in same folder causes problems HOT 2
- Maximum call stack size exceeded HOT 1
- How do I simulate a file upload? HOT 3
- dyson dependencies fail npm security audit HOT 3
- Return return express server instead of express HOT 2
- Sending custom status clears body HOT 5
- Documentation on what params are returned HOT 9
- Recording Mocks HOT 4
- 404 not found HOT 2
- Content-Type is converted to lowercase HOT 1
- Dyson dependencies fail npm security audit - lodash needs updating HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from dyson.