0
0

axios-retryでリトライ機能を追加

Last updated at Posted at 2023-12-23

経緯

axios使用したAPIでリトライ機能を実装することになり調べた結果
Nuxt.jsにデフォルトで入ってたaxios-retryが使いやすいと思われたため使用した。

サンプルソース

import axiosRetry from 'axios-retry';

axios.get('http://sample.jp/')

axiosRetry(axios, { 
    // ここでリトライ回数を設定
    retries: 10,
    retryCondition: () => true,
    retryDelay: (retryCount) => {
      return retryCount * 1000
    }
});

補足

retryConditionをtrueにするとHTTPステータス4XX,5XXでリトライする。
retryConditionをfalseにすると5XXのみでリトライするがPOSTのみfalseにしても4XXでリトライする。

下記GitHub参考
https://github.com/softonic/axios-retry

※はまりポイントだったので補足で説明した。

おわりに

axiosRetryの項目を数行書くだけでリトライ機能が実装出来るのでお手軽でした。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0