-
Notifications
You must be signed in to change notification settings - Fork 351
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Simplify adapter implementation #154
Conversation
The tests fail because of the ill-formatted commit message. The contributing guides does not mention this so I’ll need some help in making this pass. |
@geigerzaehler thanks for taking the time to open this PR! I have a couple of questions/concerns:
I ask the 2nd question because although some preexisting adapters may look similar, they have small differences that alter return values, how they respond to the original request, formatting responses before responding to the pollyRequest, etc. If we were to implement a Simple Adapter, you would most likely need a |
There are three main reasons for using Polly. It has more features (request recording and replay), better ergonomics when defining fake servers, and it is modular and extensible. (We are not committed to cypress, so we want to be flexible to switch to something else)
Even with better documentation I feel that it would still be harder to implement an adapter than with this proposal. For example you would need to document that the implementer is required to call
I think my proposal addresses this issue. Concrete implementations will extend from
Yes, the hooks always return a Polly request. This is so that I hope this answers all you questions and your concerns. I’m happy to answer more of them. In the meantime I’ll extend this PR with implementations of the puppeteer and node adapters to show that this approach also works well with them. |
9556f6b
to
6ea7ba1
Compare
Ah, ok I see where you are going with this. I think the implementation is starting to look really good but I have some suggestions.
|
The purpose of I have added some documentation for the
That makes a lot of sense. I have changed that |
What I meant was that the hook name is very confusing. What if we use the original hook? async onRequestFinished() {
// Do some custom logic
fakeXhr.respond(...);
return super.onRequestFinished(...arguments);
} Either that or give the hook a more descriptive name: Also, by modifying all the hooks to return the |
I prefer this option since it does not require the implementer to call the super method. I’ll update the PR.
Do you mean that we should omit the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you can also squash your commits to a single one and follow the required conventional commit format, that would make sure the build passes. You can do something like: feat: Simplify adapter implementation
We change the `Adapter` base class so that the concrete adapter implementations become simpler. This also simplifies the documentation for implementing custom adapters.
e8cf8f9
to
e44ae1e
Compare
@offirgolan: Thanks for staying with me on this 🙇. Could you also have a look at the updated documentation for implementing a custom adapter that I updated? I hope the PR is now in a mergable state. |
@geigerzaehler no problem at all! Thank you for seeing this through 😄 |
This is a proof of concept for a simplified
Adapter
class that should make it easier to implement existing and custom adapters. I’d like to get feedback on this proposal and continue working on it if it looks good.Motivation
The motivation behind this is to make it easier to implement custom adapters. This is something I struggled with when trying to write an adapter that integrates with Cypress. The documentation on how to write custom adapters is sparse so I had to dig into the code.
onConnect
,onDisconnect
,onIntercept
,onPassthrough
,onRecord
,onReplay
)onRecord
. If the implementer forgets this, recording will not work.pollyRequest.responds()
in some hooks. Again, if this is omitted, the adapter will break.handleRequest()
. This is not documented, hard to understand, and couples the request hook very tightly.Proposal
The proposal addresses the five problems above by introducing a new
SimpleAdapter
class that can be used to implement adapters. This class requires the implementer to provide only three methods (onConnect
,onDisconnect
, andpassthroughRequest
). Using theSimpleAdapter
allows for smaller and less complex adapter implementations.SimpleAdapter
extends theAdapter
class. The original is retained for backwards compatibility.Todo
SimpleAdapter