💡Aha!

background worker에서 Axios 사용하기

Chrome 확장 기능 만드는 도중 Axios에서 리퀘스트 오류가 터졌다.

1TypeError: t is not a function

소스 맵을 켜서 오류 나는 곳을 확인했더니, dispatchRequestadapter 부분에서 오류가 나는 것 같더라.

@vespaiach/axios-fetch-adapter
I'm going to adopt PWA to my web applications and those web apps are heavily using Axios with the default XMLHTTPRequest adapter. Hence, I have to switch to Fetch adapter; However, Axios doesn't have an adapter for fetch API for now. So I write one to use while waiting for an offical one from Axios.
@vespaiach/axios-fetch-adapter
https://www.npmjs.com/package/@vespaiach/axios-fetch-adapter
@vespaiach/axios-fetch-adapter
1const instance = axios.create({
2 ...
3 adapter: fetchAdapter
4});

Axios에서 XMLHTTPRequest 대신 fetch를 사용할 수 있도록 도와주는 Adapter 라이브러리를 사용하면 해결된다.

여담

나는 기존에 axios를 사용하는 라이브러리가 있어서 이렇게 해결했지만, 새로 만드는 것이라면 Axios를 버리는 것도 과감히 고려해보자.

최근엔 ky 인기가 상당한 것 같더라.

2024 Dohyun Jung.
Made with ☕️.