| @@ -7,9 +7,9 @@ | ||
| 7 | 7 | private controller: AbortController | null; |
| 8 | 8 | private readonly headers: {'X-WP-Nonce': string; 'Content-Type': string}; |
| 9 | 9 | private readonly swrOptions; |
| 10 | 10 | |
| 11 | - constructor({apiNonce, apiRoot, preload = null}) { | |
| 11 | + constructor({apiNonce, apiRoot, preload = null, swrConfig = {}}) { | |
| 12 | 12 | this.controller = null; |
| 13 | 13 | this.apiRoot = apiRoot; |
| 14 | 14 | this.headers = { |
| 15 | 15 | 'Content-Type': 'application/json', |
| @@ -16,8 +16,9 @@ | ||
| 16 | 16 | 'X-WP-Nonce': apiNonce, |
| 17 | 17 | }; |
| 18 | 18 | this.swrOptions = { |
| 19 | 19 | use: [lagData], |
| 20 | + ...swrConfig, | |
| 20 | 21 | onErrorRetry: (error, key, config, revalidate, {retryCount}) => { |
| 21 | 22 | //don't retry if we cancelled the initial request |
| 22 | 23 | if (error.name == 'AbortError') return; |
| 23 | 24 | if (retryCount >= 5) return; |
| @@ -41,9 +42,11 @@ | ||
| 41 | 42 | signal: signal, |
| 42 | 43 | headers: this.headers, |
| 43 | 44 | }).then((res) => { |
| 44 | 45 | if (!res.ok) { |
| 45 | - throw new Error(); | |
| 46 | + return res.text().then((errorMessage) => { | |
| 47 | + throw new Error(errorMessage); | |
| 48 | + }); | |
| 46 | 49 | } |
| 47 | 50 | return res.json(); |
| 48 | 51 | }); |
| 49 | 52 | }; |
| @@ -66,8 +69,17 @@ | ||
| 66 | 69 | testMode, |
| 67 | 70 | ...filters, |
| 68 | 71 | }, |
| 69 | 72 | this.fetcher, |
| 73 | + this.swrOptions | |
| 74 | + ); | |
| 75 | + return {data, error, mutate, isValidating}; | |
| 76 | + }; | |
| 77 | + | |
| 78 | + useStats = (testMode) => { | |
| 79 | + const {data, error, mutate, isValidating} = useSWR( | |
| 80 | + {testMode}, | |
| 81 | + (params) => this.fetchWithArgs('/stats', params, 'GET'), | |
| 70 | 82 | this.swrOptions |
| 71 | 83 | ); |
| 72 | 84 | return {data, error, mutate, isValidating}; |
| 73 | 85 | }; |