swagger-typescript-api icon indicating copy to clipboard operation
swagger-typescript-api copied to clipboard

Type collisions in Axios HTTP client

Open szymon-99 opened this issue 3 years ago • 10 comments
trafficstars

Right now when setting

httpClientType: "axios"

Inside HTTPClient class there are some type collisions when constructing headers

mergeRequestParams

request

I assume this file is autogenerated from here

szymon-99 avatar Jan 12 '22 10:01 szymon-99

You can use axios with version 0.21.4

"dependencies": {
    "axios": "^0.21.4",

cardinalX avatar Jan 26 '22 09:01 cardinalX

You can use axios with version 0.21.4

"dependencies": {
    "axios": "^0.21.4",

While this works, of course, it's not a real solution. Is there any progress made on this?

SLG avatar Feb 15 '22 14:02 SLG

The problem was introduced with this commit in Axios 0.23.0 by adding explicit type for default headers.

This should not be too hard to fix. I can do the PR, just need some feedback on my initial analysis (due to my limited experience and usage of/with the generator):

  1. mergeRequestParams - I don't understand why this function works with Axios defaults (this.instance.defaults and this.instance.defaults.headers) in the first place. Defaults are set on the instance and will be merged into a request config by Axios itself. So these lines can be safely removed
  2. request function (if (type === ContentType.FormData condition) - little bit harder to make a call as I don't understand the reason for the code and even the PR which introduced it is not clear why it manipulates the headers (almost feels like it was just useful for the contributor himself for his use case). Anyway common, put etc. keys are not allowed on headers key of the request config and must be removed

Any feedback appreciated

Liwoj avatar Mar 09 '22 19:03 Liwoj

Fingers crossed for compatible fix with axios 0.24 🤞

furkanarabaci avatar Mar 26 '22 20:03 furkanarabaci

any update?

stavalfi avatar May 01 '22 11:05 stavalfi

Any update on this? Would love to update axios to latest version.

snufkind avatar May 10 '22 07:05 snufkind

any updates on that? thanks.

inomn avatar Aug 11 '22 08:08 inomn

as workaround, you can remove default headers from the api instance.

const api = new Api();

api.instance.defaults.headers.put = {};
api.instance.defaults.headers.post = {};
api.instance.defaults.headers.patch = {};


api.auth.login()

now, type: ContentType.Json is working properly. I will try to create a PR to fix this behavior (so, type prop will have more prio)

ApacheEx avatar Aug 25 '22 12:08 ApacheEx

Actually, it's already fixed in https://github.com/acacode/swagger-typescript-api/commit/94087eca8ff3d717fec054725c159e8f9f5d9cb8 (upcoming release)

ApacheEx avatar Aug 25 '22 12:08 ApacheEx

I also have a type problem (axios 0.21.4) image

this is used here image

But if you just delete HeadersDefaults everything is fine.

iakovalgaev avatar Sep 01 '22 03:09 iakovalgaev