|
1 | 1 | import { accessTokenHelper } from '.'; |
2 | 2 | import RequestOptions from './RequestOptions'; |
3 | 3 |
|
| 4 | +const REPEAT_TRIES_COUNT = 10; |
| 5 | +const REPEAT_INTERVAL_MS = 3_000; |
| 6 | + |
4 | 7 | const handleOrThrowMessageIfResponseError = async ( |
5 | 8 | url: string, |
6 | 9 | response: Response, |
@@ -33,114 +36,205 @@ const handleOrThrowMessageIfResponseError = async ( |
33 | 36 | } |
34 | 37 | }; |
35 | 38 |
|
36 | | -const makeRequest = async (url: string, optionsWrapper: RequestOptions): Promise<Response> => { |
37 | | - const response = await fetch(url, optionsWrapper.toRequestInit()); |
38 | | - await handleOrThrowMessageIfResponseError(url, response); |
39 | | - return response; |
| 39 | +const makeRequest = async ( |
| 40 | + url: string, |
| 41 | + optionsWrapper: RequestOptions, |
| 42 | + currentTry = 0, |
| 43 | +): Promise<Response> => { |
| 44 | + try { |
| 45 | + const response = await fetch(url, optionsWrapper.toRequestInit()); |
| 46 | + await handleOrThrowMessageIfResponseError(url, response); |
| 47 | + return response; |
| 48 | + } catch (e) { |
| 49 | + if (currentTry < REPEAT_TRIES_COUNT) { |
| 50 | + await new Promise((resolve) => setTimeout(resolve, REPEAT_INTERVAL_MS)); |
| 51 | + return makeRequest(url, optionsWrapper, currentTry + 1); |
| 52 | + } |
| 53 | + |
| 54 | + throw e; |
| 55 | + } |
40 | 56 | }; |
41 | 57 |
|
42 | 58 | export const apiHelper = { |
43 | | - fetchPostJson: async <T>(url: string, requestOptions?: RequestOptions): Promise<T> => { |
| 59 | + fetchPostJson: async <T>( |
| 60 | + url: string, |
| 61 | + requestOptions?: RequestOptions, |
| 62 | + isRetryOnError = false, |
| 63 | + ): Promise<T> => { |
44 | 64 | const optionsWrapper = (requestOptions ?? new RequestOptions()) |
45 | 65 | .setMethod('POST') |
46 | 66 | .addHeader('Content-Type', 'application/json') |
47 | 67 | .addHeader('Access-Control-Allow-Methods', 'POST') |
48 | 68 | .addHeader('Accept', 'application/json') |
49 | 69 | .addHeader('Authorization', accessTokenHelper.getAccessToken()); |
50 | 70 |
|
51 | | - const response = await makeRequest(url, optionsWrapper); |
| 71 | + const response = await makeRequest( |
| 72 | + url, |
| 73 | + optionsWrapper, |
| 74 | + isRetryOnError ? 0 : REPEAT_TRIES_COUNT, |
| 75 | + ); |
52 | 76 |
|
53 | 77 | return response.json(); |
54 | 78 | }, |
55 | 79 |
|
56 | | - fetchPostRaw: async (url: string, requestOptions?: RequestOptions): Promise<string> => { |
| 80 | + fetchPostRaw: async ( |
| 81 | + url: string, |
| 82 | + requestOptions?: RequestOptions, |
| 83 | + isRetryOnError = false, |
| 84 | + ): Promise<string> => { |
57 | 85 | const optionsWrapper = (requestOptions ?? new RequestOptions()) |
58 | 86 | .setMethod('POST') |
59 | 87 | .addHeader('Content-Type', 'application/json') |
60 | 88 | .addHeader('Access-Control-Allow-Methods', 'POST') |
61 | 89 | .addHeader('Accept', 'application/json') |
62 | 90 | .addHeader('Authorization', accessTokenHelper.getAccessToken()); |
63 | 91 |
|
64 | | - const response = await makeRequest(url, optionsWrapper); |
| 92 | + const response = await makeRequest( |
| 93 | + url, |
| 94 | + optionsWrapper, |
| 95 | + isRetryOnError ? 0 : REPEAT_TRIES_COUNT, |
| 96 | + ); |
65 | 97 |
|
66 | 98 | return response.text(); |
67 | 99 | }, |
68 | 100 |
|
69 | | - fetchPostBlob: async (url: string, requestOptions?: RequestOptions): Promise<Blob> => { |
| 101 | + fetchPostBlob: async ( |
| 102 | + url: string, |
| 103 | + requestOptions?: RequestOptions, |
| 104 | + isRetryOnError = false, |
| 105 | + ): Promise<Blob> => { |
70 | 106 | const optionsWrapper = (requestOptions ?? new RequestOptions()) |
71 | 107 | .setMethod('POST') |
72 | 108 | .addHeader('Content-Type', 'application/json') |
73 | 109 | .addHeader('Access-Control-Allow-Methods', 'POST') |
74 | 110 | .addHeader('Authorization', accessTokenHelper.getAccessToken()); |
75 | 111 |
|
76 | | - const response = await makeRequest(url, optionsWrapper); |
| 112 | + const response = await makeRequest( |
| 113 | + url, |
| 114 | + optionsWrapper, |
| 115 | + isRetryOnError ? 0 : REPEAT_TRIES_COUNT, |
| 116 | + ); |
77 | 117 |
|
78 | 118 | return response.blob(); |
79 | 119 | }, |
80 | 120 |
|
81 | | - fetchGetJson: async <T>(url: string, requestOptions?: RequestOptions): Promise<T> => { |
| 121 | + fetchGetJson: async <T>( |
| 122 | + url: string, |
| 123 | + requestOptions?: RequestOptions, |
| 124 | + isRetryOnError = false, |
| 125 | + ): Promise<T> => { |
82 | 126 | const optionsWrapper = (requestOptions ?? new RequestOptions()) |
83 | 127 | .addHeader('Content-Type', 'application/json') |
84 | 128 | .addHeader('Access-Control-Allow-Methods', 'GET') |
85 | 129 | .addHeader('Accept', 'application/json') |
86 | 130 | .addHeader('Authorization', accessTokenHelper.getAccessToken()); |
87 | 131 |
|
88 | | - const response = await makeRequest(url, optionsWrapper); |
| 132 | + const response = await makeRequest( |
| 133 | + url, |
| 134 | + optionsWrapper, |
| 135 | + isRetryOnError ? 0 : REPEAT_TRIES_COUNT, |
| 136 | + ); |
| 137 | + |
89 | 138 | return response.json(); |
90 | 139 | }, |
91 | 140 |
|
92 | | - fetchGetRaw: async (url: string, requestOptions?: RequestOptions): Promise<string> => { |
| 141 | + fetchGetRaw: async ( |
| 142 | + url: string, |
| 143 | + requestOptions?: RequestOptions, |
| 144 | + isRetryOnError = false, |
| 145 | + ): Promise<string> => { |
93 | 146 | const optionsWrapper = (requestOptions ?? new RequestOptions()) |
94 | 147 | .addHeader('Content-Type', 'application/json') |
95 | 148 | .addHeader('Access-Control-Allow-Methods', 'GET') |
96 | 149 | .addHeader('Accept', 'application/json') |
97 | 150 | .addHeader('Authorization', accessTokenHelper.getAccessToken()); |
98 | 151 |
|
99 | | - const response = await makeRequest(url, optionsWrapper); |
| 152 | + const response = await makeRequest( |
| 153 | + url, |
| 154 | + optionsWrapper, |
| 155 | + isRetryOnError ? 0 : REPEAT_TRIES_COUNT, |
| 156 | + ); |
| 157 | + |
100 | 158 | return response.text(); |
101 | 159 | }, |
102 | 160 |
|
103 | | - fetchGetBlob: async (url: string, requestOptions?: RequestOptions): Promise<Blob> => { |
| 161 | + fetchGetBlob: async ( |
| 162 | + url: string, |
| 163 | + requestOptions?: RequestOptions, |
| 164 | + isRetryOnError = false, |
| 165 | + ): Promise<Blob> => { |
104 | 166 | const optionsWrapper = (requestOptions ?? new RequestOptions()) |
105 | 167 | .addHeader('Content-Type', 'application/json') |
106 | 168 | .addHeader('Access-Control-Allow-Methods', 'GET') |
107 | 169 | .addHeader('Authorization', accessTokenHelper.getAccessToken()); |
108 | 170 |
|
109 | | - const response = await makeRequest(url, optionsWrapper); |
| 171 | + const response = await makeRequest( |
| 172 | + url, |
| 173 | + optionsWrapper, |
| 174 | + isRetryOnError ? 0 : REPEAT_TRIES_COUNT, |
| 175 | + ); |
| 176 | + |
110 | 177 | return response.blob(); |
111 | 178 | }, |
112 | 179 |
|
113 | | - fetchPutJson: async <T>(url: string, requestOptions?: RequestOptions): Promise<T> => { |
| 180 | + fetchPutJson: async <T>( |
| 181 | + url: string, |
| 182 | + requestOptions?: RequestOptions, |
| 183 | + isRetryOnError = false, |
| 184 | + ): Promise<T> => { |
114 | 185 | const optionsWrapper = (requestOptions ?? new RequestOptions()) |
115 | 186 | .setMethod('PUT') |
116 | 187 | .addHeader('Content-Type', 'application/json') |
117 | 188 | .addHeader('Access-Control-Allow-Methods', 'PUT') |
118 | 189 | .addHeader('Accept', 'application/json') |
119 | 190 | .addHeader('Authorization', accessTokenHelper.getAccessToken()); |
120 | 191 |
|
121 | | - const response = await makeRequest(url, optionsWrapper); |
| 192 | + const response = await makeRequest( |
| 193 | + url, |
| 194 | + optionsWrapper, |
| 195 | + isRetryOnError ? 0 : REPEAT_TRIES_COUNT, |
| 196 | + ); |
| 197 | + |
122 | 198 | return response.json(); |
123 | 199 | }, |
124 | 200 |
|
125 | | - fetchDeleteJson: async <T>(url: string, requestOptions?: RequestOptions): Promise<T> => { |
| 201 | + fetchDeleteJson: async <T>( |
| 202 | + url: string, |
| 203 | + requestOptions?: RequestOptions, |
| 204 | + isRetryOnError = false, |
| 205 | + ): Promise<T> => { |
126 | 206 | const optionsWrapper = (requestOptions ?? new RequestOptions()) |
127 | 207 | .setMethod('DELETE') |
128 | 208 | .addHeader('Access-Control-Allow-Methods', 'DELETE') |
129 | 209 | .addHeader('Accept', 'application/json') |
130 | 210 | .addHeader('Authorization', accessTokenHelper.getAccessToken()); |
131 | 211 |
|
132 | | - const response = await makeRequest(url, optionsWrapper); |
| 212 | + const response = await makeRequest( |
| 213 | + url, |
| 214 | + optionsWrapper, |
| 215 | + isRetryOnError ? 0 : REPEAT_TRIES_COUNT, |
| 216 | + ); |
| 217 | + |
133 | 218 | return response.json(); |
134 | 219 | }, |
135 | 220 |
|
136 | | - fetchDeleteRaw: async (url: string, requestOptions?: RequestOptions): Promise<string> => { |
| 221 | + fetchDeleteRaw: async ( |
| 222 | + url: string, |
| 223 | + requestOptions?: RequestOptions, |
| 224 | + isRetryOnError = false, |
| 225 | + ): Promise<string> => { |
137 | 226 | const optionsWrapper = (requestOptions ?? new RequestOptions()) |
138 | 227 | .setMethod('DELETE') |
139 | 228 | .addHeader('Access-Control-Allow-Methods', 'DELETE') |
140 | 229 | .addHeader('Accept', 'application/json') |
141 | 230 | .addHeader('Authorization', accessTokenHelper.getAccessToken()); |
142 | 231 |
|
143 | | - const response = await makeRequest(url, optionsWrapper); |
| 232 | + const response = await makeRequest( |
| 233 | + url, |
| 234 | + optionsWrapper, |
| 235 | + isRetryOnError ? 0 : REPEAT_TRIES_COUNT, |
| 236 | + ); |
| 237 | + |
144 | 238 | return response.text(); |
145 | 239 | }, |
146 | 240 | }; |
0 commit comments