Skip to content

Add 'injected' HTTP client option #492

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

iansan5653
Copy link

@iansan5653 iansan5653 commented Dec 17, 2020

Adds a new HTTP client option injected which will not generate an HTTP client and will instead add a field to all service classes in which the consumer supplies a request method in the form (options: ApiRequestOptions) => Promise<any>. This would be the most flexible option available, as now anyone would have the power to use any request tools available. They can track and log requests, manage their own authentication, customize behavior to fit their exact needs, and inject a mock/spy method for testing.

Additional changes made to facilitate this:

  • The standard request method exported by all HTTP clients now returns the body directly (Promise<any>) instead of an object containing the body (Promise<ApiResponse>). This makes it easier to create a method as the injected method won't have to return unnecessary information.
  • All services are now standard classes that take the request method as a constructor argument, regardless of which HTTP client is being used. This would be a breaking change, except that when the HTTP client is not injected, the services are exported as pre-instantiated classes already bound to the HTTP client. So in that case, this change is invisible.
  • Added a condition so request.ts is not generated if HTTP client is injected.
  • Updated tests and snapshots.

Resolves issues:

Also may replace / affect PRs #407, #434, #489.

TODO:

  • Update Readme with documentation for new option and how to use

Instead of returning the entire `ApiResult` object in the request method
and then returning the body in the service, the request methods will now
return the response body directly. This makes it simpler to implement a
request method externally.
Rather than import the request method directly, service classes will now
receive it as a constructor argument and save it as a private field. This
makes the constructor classes more generic and testable.

To avoid breaking changes, service class instances are instantiated and
exported from index.ts with PascalCase naming. This allows users to
update with no changes, because the public API does not change
noticeably.
If this option is used, services will be exported as uninstantiated
classes that accept a method that will handle HTTP requests. The
request.ts file will not be generated as it would be unecessary.
@iansan5653 iansan5653 force-pushed the Add-injected-HTTP-client-option branch from d00977e to 921dfeb Compare December 17, 2020 14:12
@iansan5653
Copy link
Author

It looks like CI is failing because the snapshots have different indentation when generated on my machine. I also noticed some missing indentation in the output for a project I'm working on, so I think that may be a separate unrelated bug - maybe related to using Windows?

@iansan5653
Copy link
Author

@ferdikoomen Can you take a look at this PR?

@qqilihq
Copy link
Contributor

qqilihq commented Jan 22, 2021

I'd appreciate some feedback from the maintainer whether this (or something similar to customize the HTTP client) is something which might make it into openapi-typescript-codegen. Been playing around with this yesterday and liked it quite a lot, but we need a functionality to intercept the HTTP calls.

@qqilihq
Copy link
Contributor

qqilihq commented Jan 29, 2021

To give an update here: I was researching some alternatives to this tool in the meantime, and switched to oazapfts! which is rather simplistic, but provides a very simple hook to customize the fetch function. This worked great for our specific case -- maybe it's an option for the described use-cases as well.

@iansan5653
Copy link
Author

Looks like this is the way to go for now: #465 (comment)

@ferdikoomen
Copy link
Owner

@qqilihq @iansan5653 yes the comments on 456 is the way to go: #465

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Option to dependency inject the request function Support for custom request-function
3 participants