-
-
Notifications
You must be signed in to change notification settings - Fork 542
Add facility to intercept requests and responses in host application. #434
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
Conversation
# Conflicts: # src/templates/core/OpenAPI.hbs # src/templates/core/Result.ts # src/templates/core/request.ts # src/templates/core/requestUsingFetch.ts # src/templates/core/requestUsingXHR.ts # test/__snapshots__/index.spec.js.snap
Codecov Report
@@ Coverage Diff @@
## master #434 +/- ##
==========================================
+ Coverage 93.49% 93.51% +0.02%
==========================================
Files 106 106
Lines 1260 1264 +4
Branches 219 219
==========================================
+ Hits 1178 1182 +4
Misses 82 82
Continue to review full report at Codecov.
|
This is exactly the extension to the project that we also need to implement some custom auth. Thank you @richgoldmd for taking the time to do this. |
@markygab I updated this PR to define the Response type as an implementation-specific type: export interface ResponseHookParams {
url: string;
result: ApiResult;
response?: ResponseImplementation;
}
|
@richgoldmd I 'm working on a @next version that will have a 'bundler' this will load all the external refs before parsing this. In the meantime you can use something like https://github.com/APIDevTools/json-schema-ref-parser to load your spec and combine all references. After this you can send that whole spec to the generator. I will push a @next branch soon so we can add some test cases for your setup. |
This PR is about hooking the http request/response flow - not the schema references.
|
This extension is sorely needed to make the generated code more general in use. A possible extension, would be if you could pass a Implemented here: Cancer-Expert-Now#1 |
This PR seems to implement a common pattern that is universally useful and powerful. |
This PR has still value IMHO, creating your own request method is quite cumbersome maintainance wise, having those hooks would add all the extension points you need while still benefititng from possible upstream fixes. Next it's backwards compatible and doesn't add a lot of complication. |
@richgoldmd I'm ditching this PR in favour of #822 Yes it's more cumbersome to maintain your own client, but it the cleanest approach. One of the roadmap goals is also to get rid of the global OpenAPI object, this would be adding more tech debt in the future. |
Thanks @ferdikoomen - I appreciate your efforts on this library. I agree that PR is an elegant solution. |
@richgoldmd Thanks for the help so far! It's great to see the project being used outside some of my own projects. I really should spend some more time on this (specifically roadmap and documentation) so its easier to contribute. |
Often times when implementing an API client, the need arises to intercept the the outgoing request or incoming response, and act on it in a way that may be globally applied or may not be anticipated in a specific API.
For example, a server may return status information unrelated to the specific api call about session status. A client application could monitor every api call for the potential exception that may indicate such a status, but then it needs to be handled in every instance. This PR enables the addition global hooks before a request is made, and after a response is received (but before it is checked for errors).
This is accomplished by providing optional callbacks in the OpenAPI object:
The request hook, if provided, is called with the url and ApiRequestOptions and returns the same parameters which may have been altered by the hook.
Similarly, the response hook is called with the url, ApiResult, and the actual response object from the underlying http implementation, and returns a potentially altered ApiResult (e.g. a known error condition which has been handled can be reset to 'ok' by returning a status 200 and ok: true). The implementation-specific response object is passed so that it can be queried as needed in the response hook (in my use case to scan for one or more application-specific headers).
Here is a sample response hook implementation which detects and handles a specific header and resets the result to prevent an additional, unneeded exception being raised: