@@ -19,8 +19,14 @@ import {
1919 UpdateRequest ,
2020 DeleteRequest ,
2121} from './types' ;
22- import { API_VERSION , BASE_DOMAIN } from './utils/constants' ;
22+ import {
23+ API_VERSION ,
24+ BASE_DOMAIN ,
25+ MAX_RETRY_COUNT ,
26+ MIN_TIMEOUT_MS ,
27+ } from './utils/constants' ;
2328import { generateFetchClient } from './lib/fetch' ;
29+ import retry from 'async-retry' ;
2430
2531/**
2632 * Initialize SDK Client
@@ -29,6 +35,7 @@ export const createClient = ({
2935 serviceDomain,
3036 apiKey,
3137 customFetch,
38+ retry : retryOption ,
3239} : MicroCMSClient ) => {
3340 if ( ! serviceDomain || ! apiKey ) {
3441 throw new Error ( 'parameter is required (check serviceDomain and apiKey)' ) ;
@@ -55,53 +62,87 @@ export const createClient = ({
5562 customBody,
5663 } : MakeRequest ) => {
5764 const fetchClient = generateFetchClient ( apiKey , customFetch ) ;
58-
5965 const queryString = parseQuery ( queries ) ;
6066 const url = `${ baseUrl } /${ endpoint } ${ contentId ? `/${ contentId } ` : '' } ${
6167 queryString ? `?${ queryString } ` : ''
6268 } `;
6369
64- try {
65- const response = await fetchClient ( url , {
66- method : method || 'GET' ,
67- headers : customHeaders ,
68- body : customBody ,
69- } ) ;
70-
71- if ( ! response . ok ) {
72- const message = await ( async ( ) => {
73- // Enclose `response.json()` in a try since it may throw an error
74- // Only return the `message` if there is a `message`
75- try {
76- const { message } = await response . json ( ) ;
77- return message ?? null ;
78- } catch ( _ ) {
79- return null ;
80- }
81- } ) ( ) ;
82- return Promise . reject (
83- new Error (
84- `fetch API response status: ${ response . status } ${
85- message ? `\n message is \`${ message } \`` : ''
86- } `
87- )
88- ) ;
70+ const getMessageFromResponse = async ( response : Response ) => {
71+ // Enclose `response.json()` in a try since it may throw an error
72+ // Only return the `message` if there is a `message`
73+ try {
74+ const { message } = await response . json ( ) ;
75+ return message ?? null ;
76+ } catch ( _ ) {
77+ return null ;
8978 }
79+ } ;
9080
91- if ( method === 'DELETE' ) return ;
81+ return await retry (
82+ async ( bail ) => {
83+ try {
84+ const response = await fetchClient ( url , {
85+ method : method || 'GET' ,
86+ headers : customHeaders ,
87+ body : customBody ,
88+ } ) ;
9289
93- return response . json ( ) ;
94- } catch ( error ) {
95- if ( error . data ) {
96- throw error . data ;
97- }
90+ // If a status code in the 400 range other than 429 is returned, do not retry.
91+ if (
92+ response . status !== 429 &&
93+ response . status >= 400 &&
94+ response . status < 500
95+ ) {
96+ const message = await getMessageFromResponse ( response ) ;
9897
99- if ( error . response ?. data ) {
100- throw error . response . data ;
101- }
98+ return bail (
99+ new Error (
100+ `fetch API response status: ${ response . status } ${
101+ message ? `\n message is \`${ message } \`` : ''
102+ } `
103+ )
104+ ) ;
105+ }
102106
103- return Promise . reject ( new Error ( `Network Error.\n Details: ${ error } ` ) ) ;
104- }
107+ // If the response fails with any other status code, retry until the set number of attempts is reached.
108+ if ( ! response . ok ) {
109+ const message = await getMessageFromResponse ( response ) ;
110+
111+ return Promise . reject (
112+ new Error (
113+ `fetch API response status: ${ response . status } ${
114+ message ? `\n message is \`${ message } \`` : ''
115+ } `
116+ )
117+ ) ;
118+ }
119+
120+ if ( method === 'DELETE' ) return ;
121+
122+ return response . json ( ) ;
123+ } catch ( error ) {
124+ if ( error . data ) {
125+ throw error . data ;
126+ }
127+
128+ if ( error . response ?. data ) {
129+ throw error . response . data ;
130+ }
131+
132+ return Promise . reject (
133+ new Error ( `Network Error.\n Details: ${ error } ` )
134+ ) ;
135+ }
136+ } ,
137+ {
138+ retries : retryOption ? MAX_RETRY_COUNT : 0 ,
139+ onRetry : ( err , num ) => {
140+ console . log ( err ) ;
141+ console . log ( `Waiting for retry (${ num } /${ MAX_RETRY_COUNT } )` ) ;
142+ } ,
143+ minTimeout : MIN_TIMEOUT_MS ,
144+ }
145+ ) ;
105146 } ;
106147
107148 /**
0 commit comments