Categories
JavaScript Answers

How to use Axios Interceptors retry original request and access original promise with JavaScript?

Sometimes, we want to use Axios Interceptors retry original request and access original promise with JavaScript.

In this article, we’ll look at how to use Axios Interceptors retry original request and access original promise with JavaScript.

How to use Axios Interceptors retry original request and access original promise with JavaScript?

To use Axios Interceptors retry original request and access original promise with JavaScript, we can retry the original request after we get the refresh token in the error handler function.

For instance, we write

const refreshToken = async (store) => {
  if (store.state.auth.isRefreshing) {
    return store.state.auth.refreshingCall;
  }
  store.commit("auth/setRefreshingState", true);
  const {
    data: { token },
  } = await Axios.get("get token");
  store.commit("auth/setToken", token);
  store.commit("auth/setRefreshingState", false);
  store.commit("auth/setRefreshingCall", undefined);
  store.commit("auth/setRefreshingCall", refreshingCall);
  return Promise.resolve(true);
};

Axios.interceptors.response.use(
  (response) => response,
  async (error) => {
    const status = error.response ? error.response.status : null;

    if (status === 401) {
      await refreshToken(store);
      error.config.headers["Authorization"] =
        "Bearer " + store.state.auth.token;
      error.config.baseURL = undefined;
      await Axios.request(error.config);
    }

    return Promise.reject(error);
  }
);

to define the refreshToken function.

It it, we make a get request with Axios.get to get the refresh token from token.

Then in the error handler that we call Axios.interceptors.response.use with, we check if the response status is 401.

If it is, then we call refreshToken to get the refresh token.

And then we set the token as the value of error.config.headers["Authorization"].

Then we call Axios.request to make the request with the error.config.

We then call Promise.reject to reject the promise.

Conclusion

To use Axios Interceptors retry original request and access original promise with JavaScript, we can retry the original request after we get the refresh token in the error handler function.

Categories
JavaScript Answers

How to make array indexOf() case insensitive with JavaScript?

Sometimes, we want to make array indexOf() case insensitive with JavaScript.

In this article, we’ll look at how to make array indexOf() case insensitive with JavaScript.

How to make array indexOf() case insensitive with JavaScript?

To make array indexOf() case insensitive with JavaScript, we can use the array findIndex method.

For instance, we write

const array = ["I", "hAve", "theSe", "ITEMs"];
const index = array.findIndex((item) => q.toLowerCase() === item.toLowerCase());

to call array.findIndex with a callback that converts the item in array to lower case with toLowerCase.

And we compare it with value q after converting it to lower case.

Then the index of the array item that matches q in a case insensitive manner is returned.

Conclusion

To make array indexOf() case insensitive with JavaScript, we can use the array findIndex method.

Categories
JavaScript Answers

How to split string in JavaScript by line breaks?

Sometimes, we want to split string in JavaScript by line breaks.

In this article, we’ll look at how to split string in JavaScript by line breaks.

How to split string in JavaScript by line breaks?

To split string in JavaScript by line breaks, we call the string split method with a regex.

For instance, we write

const array = str.split(/\r?\n/);

to call str.split with a regex that matches \r\n or \n to split str into an array with either as separators.

Conclusion

To split string in JavaScript by line breaks, we call the string split method with a regex.

Categories
JavaScript Answers

How to iterate over set elements with JavaScript?

Sometimes, we want to iterate over set elements with JavaScript.

In this article, we’ll look at how to iterate over set elements with JavaScript.

How to iterate over set elements with JavaScript?

To iterate over set elements with JavaScript, we can use the for-of loop.

For instance, we write

const a = new Set();
a.add("Hello");

for (const key of a) {
  console.log(key);
}

to create a set with the Set constructor.

We add an entry into it with add.

Then we use a for-of loop to loop through the entries of a.

We get the entry being looped through from key.

Conclusion

To iterate over set elements with JavaScript, we can use the for-of loop.

Categories
JavaScript Answers

How to get the day of the month in JavaScript?

Sometimes, we want to get the day of the month in JavaScript.

In this article, we’ll look at how to get the day of the month in JavaScript.

How to get the day of the month in JavaScript?

To get the day of the month in JavaScript, we use the getDate method.

For instance, we write

const date = new Date().getDate();

to create a new Date object with the current date and time.

Then we use getDate to get the day of the month.

Conclusion

To get the day of the month in JavaScript, we use the getDate method.