How to handle api request with window

32 views Asked by At

I have my development environment (localhost) and then my production environment (something.com). I wanted to make my code more flexible, so I do not have to update my useFetch requests to my backend each time I change environments, so I use the following snippet to get the base URI:

      const getProducts = async () => {
    isLoading.value = true;
    
    return await useFetch(window.location.origin + '/assets/json/data.json', {
      onRequestError({ request, options, error }) {
        console.log(error);
        isLoading.value = false;
      },
      onResponse({ request, response, options }) {
        let pricing = new Map(response._data['Regency Pricing'].map(o => [o['Part Num'], o]))
        products.value = response._data['Product Info'].map(o => ({ ...pricing.get(o['Pricing Reference Name']), ...o }))
        setFilters();
        isLoading.value = false;
      },
      onResponseError({ request, response, options }) {
        console.log(request.error);
        isLoading.value = false;
      }
    });
  };

Using window however seems to draw the following error:

[nitro] [unhandledRejection] ReferenceError: window is not defined

The code works, but this pollutes my terminal window where I try to watch for errors. I just keep getting this, and I am not sure how I should be handling the base URI to make this work better without those warnings/errors.

0

There are 0 answers