acacode / swagger-typescript-api

Generate the API Client for Fetch or Axios from an OpenAPI Specification
MIT License
3.1k stars 344 forks source link

Typescript error with Axios v.1.2.2 #477

Open xAl3xFx opened 1 year ago

xAl3xFx commented 1 year ago

There is a typescript error with axios v.1.2.2

After using npx swagger-typescript-api --axios -p ./swagger.json -o ./ -n myApi.ts the following error occurs: image

After downgrading axios version to 1.2.0 the error disappears.

Tested with typescript version: 2.7.4 && 2.9.4

Thanks for reading, hope to get this issue fixed so future versions of axios will be supported.

michalb-goflink commented 1 year ago

I have the same issue. v1.2.1 is OK, while v1.2.2 is not.

rjwalters commented 1 year ago

As a temporary solution you can ignore the error by forcing headers: to any in the template http-client.ejs (in two places)


headers: {
          ...((method && this.instance.defaults.headers[method.toLowerCase() as keyof HeadersDefaults]) || {}),
          ...(params1.headers || {}),
          ...((params2 && params2.headers) || {}),
        },
        } as any,
jdimmerman commented 1 year ago

This is now fixed in Axios 1.2.3

rjwalters commented 1 year ago

Upgrading to Axios 1.2.3 results in a bunch of other issues... I'm not confident in these changes even though it seems to work and satisfies the error checker. I'm going to have to lock at Axios 1.2.2 until this tool is updated or I find an alternative code generator for axios.

+++ b/src/lib/api/api.ts
@@ -1047,13 +1047,17 @@ import type {
   AxiosResponse,
   HeadersDefaults,
   ResponseType,
+  AxiosRequestHeaders,
 } from 'axios';
 import axios from 'axios';

 export type QueryParamsType = Record<string | number, any>;

 export interface FullRequestParams
-  extends Omit<AxiosRequestConfig, 'data' | 'params' | 'url' | 'responseType'> {
+  extends Omit<
+    AxiosRequestConfig,
+    'data' | 'params' | 'url' | 'responseType' | 'headers'
+  > {
   /** set parameter to `true` for call `securityWorker` for this request */
   secure?: boolean;
   /** request path */
@@ -1066,18 +1070,24 @@ export interface FullRequestParams
   format?: ResponseType;
   /** request body */
   body?: unknown;
+  /** headers */
+  headers?: AxiosRequestHeaders;
 }

-export type RequestParams = Omit<
-  FullRequestParams,
-  'body' | 'method' | 'query' | 'path'
->;
-
+export interface RequestParams
+  extends Omit<
+    FullRequestParams,
+    'body' | 'method' | 'query' | 'path' | 'headers'
+  > {
+  headers?: any;
+}
 export interface ApiConfig<SecurityDataType = unknown>
-  extends Omit<AxiosRequestConfig, 'data' | 'cancelToken'> {
+  extends Omit<AxiosRequestConfig, 'data' | 'cancelToken' | 'headers'> {
+  headers?: AxiosRequestHeaders;
   securityWorker?: (
     securityData: SecurityDataType | null
   ) => Promise<AxiosRequestConfig | void> | AxiosRequestConfig | void;
+
   secure?: boolean;
   format?: ResponseType;
 }
@@ -1091,6 +1101,7 @@ export enum ContentType {

 export class HttpClient<SecurityDataType = unknown> {
   public instance: AxiosInstance;
+  public headers?: AxiosRequestHeaders;
   private securityData: SecurityDataType | null = null;
   private securityWorker?: ApiConfig<SecurityDataType>['securityWorker'];
   private secure?: boolean;
@@ -1110,6 +1121,7 @@ export class HttpClient<SecurityDataType = unknown> {
     this.secure = secure;
     this.format = format;
     this.securityWorker = securityWorker;
+    this.headers = axiosConfig.headers;
   }

   public setSecurityData = (data: SecurityDataType | null) => {
@@ -1134,7 +1146,7 @@ export class HttpClient<SecurityDataType = unknown> {
           {}),
         ...(params1.headers || {}),
         ...((params2 && params2.headers) || {}),
-      } as any,
+      } as AxiosRequestHeaders,
     };
   }

@@ -1206,7 +1218,7 @@ export class HttpClient<SecurityDataType = unknown> {
         ...(type && type !== ContentType.FormData
           ? { 'Content-Type': type }
           : {}),
-      } as any,
+      } as AxiosRequestHeaders,
       params: query,
       responseType: responseFormat,
       data: body,
Deftunk commented 1 year ago

Adding this resolution in your package.json should fix this issue. { "resolutions": { "axios": "1.2.1" } } Axios 1.2.3 request a mandatory headers in the typing, it seems to be a dirty temporary fix. At least it is definitely not compliant with swagger-typescript-api

rjwalters commented 1 year ago

axios 1.2.6 seems to work with only the two as any changes to the template as in 1.2.2

rcbevans commented 1 year ago

This error is typescript warning you that you have multiple version of axios being imported and the types aren't guaranteed to be compatible.

This library has a peer dependency of axios 1.1.3, mixing versions may work, but you may also run into subtle issues.