1

I am currently trying to work with the Atlassian Jira rest API. In order to not get a CORS error I go through the recommended route of not sending the request from the browser but proxy it through my express server.

Now as I am doing this, all I receive back in the app is a pending promise. I assume that I have not correctly resolved it at one point but I cant figure out where.

API Handler sending the request to the proxy:

const baseURL = `${apiConfig}/jiraproxy`;

export const testConnection = integration => {
  return fetch(`${baseURL}/get`, {
    method: "POST",
    headers: { "content-type": "application/json" },
    body: JSON.stringify(integration)
  })
    .then(handleResponse)
    .catch(handleError);
};

Jira Proxy Endpoint on the Express Server

const baseURL = `rest/api/3/dashboard`;

router.post("/get", (req, res) => {
  fetch(req.body.link + baseURL, {
    method: "GET",
    headers: { Accept: "application/json" },
    auth: {
      username: req.body.credentials.username,
      password: req.body.credentials.token
    }
  })
    .then(handleResponse)
    .catch(handleError);
});

handleResponse & handle Error Methods:

async function handleResponse(response) {
  if (response.ok) {
    return response.json();
  }
  if (response.status === 400) {
    const error = await response.text();
    throw new Error(error);
  }
  throw new Error("Network response was not ok.");
}

function handleError(error) {
  // eslint-disable-next-line no-console
  console.error(`API call failed. ${error}`);
  throw error;
}

Goal: Send the request of sending a request to the proxy and return the resonse of the proxy as the return of the initial "testConction" method.

Error: No errors thrown, but the response received in the Browser is a pending promise.

Stefan Januschke
  • 322
  • 4
  • 20
  • 1
    How could this ever work? Your `router.post()` never sends any sort of response to the client. There's no `res.send()` or `res.json()` in your `router.post()` handler. – jfriend00 Jan 17 '20 at 05:39
  • Also, are you aware that `handleResponse()` is `async`? That means it ALWAYS returns a promise. So, any result you're trying to get out of it is going to be the resolved value of the promise that it returns. You don't show what you're trying to accomplish with `handleResponse()` at all. All, it seems to do is check for errors and call `response.json()`, but you never do anything with that result in your `router.post()` handler. – jfriend00 Jan 17 '20 at 05:41
  • Alright I should probably sleep now - thanks for the gotcha! – Stefan Januschke Jan 17 '20 at 06:14
  • Answered the question myself for clarity, – Stefan Januschke Jan 17 '20 at 06:17

1 Answers1

1

Change to the Jira Proxy router fixed it. Thanks to @jfriend00.

router.post("/get", (req, res) => {
  return fetch(req.body.link + baseURL, {
    method: "GET",
    headers: { Accept: "application/json" },
    auth: {
      username: req.body.credentials.username,
      password: req.body.credentials.token
    }
  })
     // This is the part that changed
    .then(response => handleResponse(response))
    .then(jiraResponse => res.status(200).json(jiraResponse))
    .catch(handleError);
});
Stefan Januschke
  • 322
  • 4
  • 20
  • 1
    FYI, you can change this `.then(jiraResponse => res.status(200).json(jiraResponse))` to this `.then(res.json.bind(res))`. No need to set the status to 200 (that's the default) and you can just let the `.then()` handler call `res.json()` for you. – jfriend00 Jan 17 '20 at 06:33
  • Also, if you really just want to proxy the actual response, you can use something like the `request()` library and just `.pipe()` the response back. You don't even have to parse the response yourself, just pass it on thru. See https://stackoverflow.com/questions/20351637/how-to-create-a-simple-http-proxy-in-node-js for an example. – jfriend00 Jan 17 '20 at 07:05