Proxy not working for server side calls?

Anima-t3d
0
Anima-t3d
commented 7 months ago

I'm using proxy in nuxtServerInit and asyncData, but it does not seem to proxy the request properly? Everywhere else the proxy works just fine.

Currently using a workaround: Not use proxy for the base api, then only do the base api requests in nuxtServerInit and asyncData (e.g. the login and authentication. Specific api e.g. for task microservice api will be called elsewhere).

Is there something I am missing?

0
Anima-t3d
0
Anima-t3d
commented 6 months ago

For now I ditched proxy mod and am using on request interceptor (in axios plugin) to rewrite url:

// ---- START - ROUTING OF REQUESTS - START ----
    // API calls need to start or be pre-fixed with /core/ or /admin/
    const coreAPIRegEx = /^\/core\//;
    const taskAPIRegEx = /^\/admin\//;

    if (coreAPIRegEx.test(config.url)) {
      config.url = getDestinationPath(config.url);
      config.baseURL = API_ENDPOINT;
    }

    if (taskAPIRegEx.test(config.url)) {
      config.url = getDestinationPath(config.url);
      config.baseURL = TASK_API_ENDPOINT;
    }
    // ---- END - ROUTING OF REQUESTS - END ----

getDestinationPath:

function getDestinationPath(url) {
  // First is empty string, second is the prefix, so remove from destination
  const cleanedURL = url.split('/').slice(2).join('/');
  return '/' + cleanedURL;
}

Usage in api:

getTaskById(id) {
    if (!id) {
      throw 'Invalid id parameter';
    }

    return axios.get(`/admin/tasks/${id}`);
  },

/admin will be filtered out, but used to update baseURL in the request

0
Informations
Question โ€ข Unresolved
#c13 - Created 7 months ago