You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We use a global Axios interceptor instance in /site. As discussed in the ticket linked above, we would like to import our API definitions into external applications, specifically Backstage. In order to avoid configuration collisions or intercepting requests unrelated to Coder, we should scope our instance appropriately and document our usage of Axios.
The text was updated successfully, but these errors were encountered:
Use globalAxios.create to make a new Axios instance that is scoped to the ES module file
Update all our API functions to use the new instance instead
My main worry is testing setup. The above change would solve the issue of collisions with other Axios users, but we'd still have a "global singleton-ish" value, even though it'd be scoped more defensively to Coder-only files.
I've already run into a testing issue when making new tests for Backstage:
importglobalAxiosfrom"axios";constaxiosInstance=globalAxios.create();classCoderClient{constructor(){// Do other stuff before Axios callaxiosInstance.interceptors.request.use(this.interceptAxiosRequest);}interceptAxiosRequest=async()=>{// Pass along class state to each outgoing request. Needs to be defined// as arrow function to avoid loss of `this` context};}test("Test case 1",()=>{constclient1=newCoderClient();});// Even after test 1 finishes, client1 can't be garbage-collected,// because the Axios instance has an interceptor function with// closure over client1. In effect, this means that when test case 2 makes// an API request, the interceptor from client2 will apply first, but then// the interceptor from client1 will also run, likely overwriting the// request with stale datatest("Test case 2",()=>{constclient2=newCoderClient();});
I have some logic in place to make sure the classes clean up after themselves following each test (coderClient.cleanupClient), but it feels flaky/fragile, and it feels like the better option is to increase testing isolation. My main worry, though, is that I don't know if you can do that without rewriting the entire file to use classes instead of functions
Related to coder/backstage-plugins#114
We use a global Axios interceptor instance in
/site
. As discussed in the ticket linked above, we would like to import our API definitions into external applications, specifically Backstage. In order to avoid configuration collisions or intercepting requests unrelated to Coder, we should scope our instance appropriately and document our usage of Axios.The text was updated successfully, but these errors were encountered: