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

Support axios >= 1.3.0 #124

Closed
simenflatby opened this issue Feb 6, 2023 · 13 comments
Closed

Support axios >= 1.3.0 #124

simenflatby opened this issue Feb 6, 2023 · 13 comments
Labels
good first issue Good for newcomers

Comments

@simenflatby
Copy link

Is your feature request related to a problem? Please describe.
As of axios 1.3.0, there is a breaking change in the use of request interceptors (introduced in axios/axios#5486) that is not compatible with the exported requestLogger of this lib.

Versions

  • axios: 1.3.0
  • axios-logger: 2.6.2

Describe the solution you'd like
Add support for the breaking change so that axios >= 1.3.0 can be used with this lib.

Additional context
To reproduce:

  1. Checkout https://github.com/simenflatby/issue-report_axios-logger_1
  2. npm i
  3. npm run build
mtsmfm added a commit to autifyhq/autify-sdk-js that referenced this issue Feb 14, 2023
axios-logger doesn't support 1.3.0 yet
hg-pyun/axios-logger#124
mtsmfm added a commit to autifyhq/autify-sdk-js that referenced this issue Feb 14, 2023
axios-logger doesn't support 1.3.0 yet
hg-pyun/axios-logger#124
@dan-sherwin
Copy link

Workaround that worked for me (TypeScript) until a proper patch is made:

axios.interceptors.request.use((internalrequest: InternalAxiosRequestConfig) => {
  const headers = internalrequest.headers;
  const request: AxiosRequestConfig = {
    ...internalrequest,
  };
  const logrequest = AxiosLogger.requestLogger(request);
  internalrequest = {
    ...logrequest,
    ...{ headers: headers },
  };
  return internalrequest;
}, AxiosLogger.errorLogger);

@hg-pyun
Copy link
Owner

hg-pyun commented Apr 4, 2023

I think I need to think about 1.3.0. I will think about it so that I can quickly apply for this part. Thank you.

@hg-pyun hg-pyun added the good first issue Good for newcomers label Apr 4, 2023
@tibbe
Copy link

tibbe commented May 16, 2023

1.2.6 doesn't seem to work either:

Argument of type '(request: AxiosRequestConfig<any>, config?: RequestLogConfig | undefined) => AxiosRequestConfig<any>' is not assignable to parameter of type '(value: InternalAxiosRequestConfig<any>) => InternalAxiosRequestConfig<any> | Promise<InternalAxiosRequestConfig<any>>'.

when doing

axiosInstance.interceptors.request.use(
  AxiosLogger.requestLogger,
  AxiosLogger.errorLogger,
);

@hg-pyun
Copy link
Owner

hg-pyun commented Aug 2, 2023

#136

@simenflatby
Copy link
Author

#136

Thanks @hg-pyun 🙏

I have tested this with:

  • axios: 1.3.0 -> 1.4.0
  • axios-logger: 2.6.2 -> 2.7.0
  • typescript: 4.9.5 -> 5.1.6

I still get the same build error.

Steps to reproduce:

  1. Clone https://github.com/simenflatby/issue-report_axios-logger_1
  2. Checkout branch axios-1.4.0-and-axios-logger-2.7.0
  3. npm i
  4. npm run build

@gvanderclay
Copy link

@hg-pyun Can this issue be closed? I tested this on 2.7.1 and I now have no type issues!

@simenflatby
Copy link
Author

simenflatby commented Feb 29, 2024

@hg-pyun Can this issue be closed? I tested this on 2.7.1 and I now have no type issues!

@gvanderclay Still same problem with 2.7.1. Pushed a new branch axios-1.3.0-and-axios-logger-2.7.1 to the repo mentioned in #124 (comment) if you want to have a look.

@gvanderclay
Copy link

@simenflatby, I've tested with axios 1.6.7 and axios-logger 2.7.1 and am no longer encountering the issue. If there's a specific need to ensure compatibility with axios version 1.3.0, the problem seems to have been resolved.

@simenflatby
Copy link
Author

simenflatby commented Mar 1, 2024

@simenflatby, I've tested with axios 1.6.7 and axios-logger 2.7.1 and am no longer encountering the issue. If there's a specific need to ensure compatibility with axios version 1.3.0, the problem seems to have been resolved.

@gvanderclay Thanks for coming back to me. I observe the same problem for 1.6.7. It might ofc. be something wrong with the minimal example I'm testing with, but I do not have the time at the moment to investigate. If you want to have a look, I have pushed a new branch axios-1.6.7-and-axios-logger-2.7.1.

git clone git@github.com:simenflatby/issue-report_axios-logger_1.git
cd issue-report_axios-logger_1
git checkout axios-1.6.7-and-axios-logger-2.7.1
npm ci
npm run build

@gvanderclay
Copy link

@simenflatby Upon revisiting your code, I realized the problem arises when the axiosRequestConfig is being passed through a wrapping function before being used with requestLogger. To resolve the TypeScript error during the build process, you can directly use the requestLogger and responseLogger as interceptors without the additional wrapping function:

import axios, { AxiosInstance, AxiosRequestConfig } from 'axios';
import { requestLogger, responseLogger } from 'axios-logger';

const axiosRequestConfig: AxiosRequestConfig = {};

const axiosInstance: AxiosInstance = axios.create(axiosRequestConfig);

axiosInstance.interceptors.request.use(requestLogger);
axiosInstance.interceptors.response.use(responseLogger);

This should fix the type mismatch error. I apologize for not catching this detail in your initial code example. Please give this a try and let me know if it resolves the issue on your end.

@gvanderclay
Copy link

@simenflatby, I also want to add that if the use of a wrapper function for the axiosRequestConfig is necessary for your use case, you can prevent build errors by using the correct internal type. If you use InternalAxiosRequestConfig instead of AxiosRequestConfig in the wrapper function's signature to match the expected type for the interceptor, the build will succeed:

import axios, {
  AxiosInstance,
  InternalAxiosRequestConfig,
} from 'axios';
import { requestLogger } from 'axios-logger';

const axiosRequestConfig: InternalAxiosRequestConfig = {};

const axiosInstance: AxiosInstance = axios.create(axiosRequestConfig);

const axiosRequestLogger = (
  config: InternalAxiosRequestConfig
): InternalAxiosRequestConfig => requestLogger(config);

axiosInstance.interceptors.request.use(axiosRequestLogger);

Notice that InternalAxiosRequestConfig is used here instead of AxiosRequestConfig. This small change aligns with the expected parameter type for axios interceptors and should eliminate the build error.

@simenflatby
Copy link
Author

@gvanderclay Nice! I remember trying this approach when I first reported the issue, but it did not work then. Forgot about that earlier Today, but I have tested it now and it works. It was fixed as of 2.7.1 (did not work in 2.7.0).

Closing. Fixed by #147.

@gvanderclay
Copy link

@simenflatby Glad I could help!

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

No branches or pull requests

5 participants