Skip to content

Axios Interceptors #226

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

Closed
indapublic opened this issue Apr 5, 2021 · 10 comments · Fixed by #245
Closed

Axios Interceptors #226

indapublic opened this issue Apr 5, 2021 · 10 comments · Fixed by #245
Assignees

Comments

@indapublic
Copy link

Hello. Thanks for your great library.
Do you have good examples how to use axios interceptors (e.g. refresh token) for generated apis?

@marko-hologram
Copy link

Hello. Thanks for your great library.
Do you have good examples how to use axios interceptors (e.g. refresh token) for generated apis?

I think you currently cannot add axios interceptors since you don't have access to the actual axios instance that is created by code generated by this library (at least I didn't manage to do it).

@orouz
Copy link

orouz commented Apr 14, 2021

not a great solution, but you can override the instance property on HttpClient to use your own.

i think it makes sense to pass an actual instance instead of an instance config.

@indapublic
Copy link
Author

@orouz I just saw recent release with customFetch feature 🤔

@guhyeon
Copy link
Contributor

guhyeon commented Apr 19, 2021

  1. change httpclient instance to public
    image

const api = new Api(); api.instance.interceptors.response.use(...)

@marko-hologram
Copy link

1. change httpclient instance to public
   ![image](https://user-images.githubusercontent.com/9765614/115196381-ccb45200-a12a-11eb-8a2f-41e8cb3ebce0.png)

const api = new Api(); api.instance.interceptors.response.use(...)

Wouldn't this change be reset back when you use swagger-typescript-api again since the file would be generated again?
You would have to manually update it back to be public. Not that much of a hassle, but something to keep in mind.

@guhyeon
Copy link
Contributor

guhyeon commented Apr 19, 2021

@marko-hologram, You are right. it is always reseted, unless you can use custom templates, successively.

I'm trying to use custom templates, but it is not work well.
so, I'm now change manually every generating. 🤔

@guhyeon
Copy link
Contributor

guhyeon commented Apr 19, 2021

I just successed using template.

make directory "templates" and
make "http-client.eta" file in "templates" directory
and copy https://github.com/acacode/swagger-typescript-api/blob/next/templates/base/http-clients/axios-http-client.eta
and paste into "http-client.eta"
and change private instance: AxiosInstance; => instance: AxiosInstance;

image

and type below in terminal
npx swagger-typescript-api --templates ./templates --axios

@marko-hologram
Copy link

I just successed using template.

make directory "templates" and
make "http-client.eta" file in "templates" directory
and copy https://github.com/acacode/swagger-typescript-api/blob/next/templates/base/http-clients/axios-http-client.eta
and paste into "http-client.eta"
and change private instance: AxiosInstance; => instance: AxiosInstance;

image

and type below in terminal
npx swagger-typescript-api --templates ./templates --axios

Oh yeah I didn't think about using custom templates for this. I used them for something else but forgot it can also be applied here. Good catch!

Unfortunately this means you will have to keep this template updated as swagger-typescript-api gets updated just so that you get all the latest code.

But at least there is a way to expose this AxiosInstance after all.

@js2me
Copy link
Member

js2me commented Apr 23, 2021

I'll make instance public property

@js2me js2me linked a pull request Apr 23, 2021 that will close this issue
@js2me js2me self-assigned this Apr 23, 2021
@js2me js2me mentioned this issue Apr 29, 2021
@himanshs
Copy link

I just successed using template.

make directory "templates" and make "http-client.eta" file in "templates" directory and copy https://github.com/acacode/swagger-typescript-api/blob/next/templates/base/http-clients/axios-http-client.eta and paste into "http-client.eta" and change private instance: AxiosInstance; => instance: AxiosInstance;

image

and type below in terminal npx swagger-typescript-api --templates ./templates --axios

Can we somehow inject the interceptor in the client kit itself as it is required for all api call? It not good idea to write the same interceptor code in all apis.

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 a pull request may close this issue.

6 participants