Skip to content
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

Add asResponse option to HttpService methods #52434

Merged
merged 3 commits into from
Dec 11, 2019

Conversation

joshdover
Copy link
Contributor

@joshdover joshdover commented Dec 6, 2019

Summary

This refactors the client-side HTTP service with a few minor changes:

  • Pulls apart different parts of the logic into multiple files
  • Adds an optional type parameter to fetch methods to support typesafe response bodies
  • Adds a asResponse option to return a more detailed response object rather than just the response body.

This refactoring does not change any tests in order to be sure that the functionality was not broken. In the future, tests could be broken out for different parts of the functionality (interceptors, fetching, etc.).

In order to support the asResponse option without breaking the API, I had to overload the HttpHandler type. This causes some unexpected type errors with Jest mocks as it does not recognize the return type can be two different types. If anyone has an ideas on how to fix this, please let me know, but it does not appear to be supported well.

This is not so much a problem for any code that uses the built in mocks that Core provides. I only had to manually cast in a few spots where custom mocks were used.

This change paves the way for #43970 where we can add methods to the IHttpResponse interface for adding Kibana-specific functionality to responses.

Checklist

Use strikethroughs to remove checklist items you don't feel are applicable to this PR.

For maintainers

@joshdover joshdover added Team:Core Core services & architecture: plugins, logging, config, saved objects, http, ES client, i18n, etc Feature:New Platform release_note:skip Skip the PR/issue when compiling release notes v7.6.0 labels Dec 6, 2019
@joshdover joshdover requested a review from a team December 6, 2019 20:54
@joshdover joshdover requested review from a team as code owners December 6, 2019 20:54
@elasticmachine
Copy link
Contributor

Pinging @elastic/kibana-platform (Team:Platform)


import { IHttpResponse } from './types';

export class HttpResponse<TResponseBody = any> implements IHttpResponse<TResponseBody> {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This object does nothing useful now, but will be extended in #43970

const capabilities = await http.post(url, {
const capabilities = await http.post<Capabilities>(url, {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 I think this is definitely more developer friendly.

Comment on lines +43 to +46
public intercept(interceptor: HttpInterceptor) {
this.interceptors.add(interceptor);
return () => this.interceptors.delete(interceptor);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT: addInterceptor is probably more appropriate. I'm expecting an intercept method on a fetch service to actually intercept a request.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. This is named as such to be compatible with the ui/kfetch module as a drop in replacement. However, I think we could change this one easily since it's only used by a handful of plugins. Let me do that in a separate PR to keep the potential impact of this initial refactor minimal.

src/core/public/saved_objects/saved_objects_client.ts Outdated Show resolved Hide resolved
return () => this.interceptors.delete(interceptor);
}

public removeAllInterceptors() {
Copy link
Contributor

@mshustov mshustov Dec 9, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we still need it? I suspect it's used in tests only. I'd prefer not to make it a part of the public API and remove from http_service contract. Not a blocker for the current PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct, it's only used by tests at this time. I'd like to move the detailed interceptor tests out of the overall HttpService tests and into a test that only exercises this class. At that point, we can remove this completely from the public API.

However, I wanted to make sure not to change the tests too much in this initial refactor so as to avoid regressions.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

However, I wanted to make sure not to change the tests too much in this initial refactor so as to avoid regressions.

💯

@elasticmachine
Copy link
Contributor

💚 Build Succeeded

History

  • 💔 Build #14419 failed 51b855aede8127efea3e4aecfe0a36b74e02764c
  • 💔 Build #14368 failed 1afa08637a077da08343895a6b1e8d1126c4740b
  • 💔 Build #14026 failed 02a500642559425ca5d7c745a2e7382f95289489

To update your PR or re-run it, just comment with:
@elasticmachine merge upstream

@joshdover joshdover merged commit a91e53f into elastic:master Dec 11, 2019
@joshdover joshdover deleted the np/http-asresponse branch December 11, 2019 16:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature:New Platform release_note:skip Skip the PR/issue when compiling release notes Team:Core Core services & architecture: plugins, logging, config, saved objects, http, ES client, i18n, etc v7.6.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants