main.mjs
import axios from 'axios';
import axiosRetry from 'axios-retry';
var foo = null;
axiosRetry(
axios,
{
retries: 2,
retryCondition: (error) => {
return error.response.status === 429 || axiosRetry.isNetworkOrIdempotentRequestError(error);
},
retryDelay: (retryCount, error) => {
console.log("A");
if (error.response.status === 429 && 'retry-after' in error.response.headers) {
console.log("B");
const retryAfter = parseInt(error.response.headers['retry-after']);
console.log("C");
if (typeof(retryAfter) === 'number') {
console.log("D");
return (retryAfter * 1000) + 1;
}
}
console.log("DEFAULT");
return axiosRetry.exponentialDelay(retryCount, error);
// return retryCount * 1000;
// return axiosRetry.exponentialDelay * 2;
},
},
);
try {
const resp = await axios.get('http://localhost:8000/500');
console.log('OK');
console.log(resp.status);
} catch {
console.log('NG');
}
// console.log(foo);
// on
console.log(foo);
APIはapisproutで用意した。以下はapisprouの設定。
openapi: 3.0.0
info:
version: 1.0.0
title: example
paths:
"/200":
get:
summary: testing
responses:
"200":
description: bad
content:
application/json:
schema:
type: object
properties:
user_id:
type: string
example: 1234567
"/429":
get:
summary: testing
responses:
"429":
description: bad
headers:
Retry-After:
schema:
type: string
example: "2"
content:
application/json:
schema:
type: object
properties:
user_id:
type: string
example: 1
"/500":
get:
summary: testing
responses:
"500":
description: bad
content:
application/json:
schema:
type: object
properties:
user_id:
type: string
example: 1234567
リクエストを確認する。
GET http://localhost:8000/429
{
"user_id": 1
}
// GET http://localhost:8000/429
// HTTP/1.1 429 Too Many Requests
// Access-Control-Allow-Origin: *
// Content-Type: application/json
// Retry-After: 2
// Date: Tue, 07 Dec 2021 01:03:44 GMT
// Content-Length: 18
// Request duration: 0.004449s