mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-09 11:12:52 +00:00
feat: add possibility to configure retrying of requests
This commit is contained in:
30
src/api.ts
30
src/api.ts
@@ -61,14 +61,33 @@ export interface ApiRequestOptions {
|
||||
* HTTP verb to use for the request
|
||||
*/
|
||||
method?: 'DELETE' | 'GET' | 'POST' | 'PUT';
|
||||
|
||||
/**
|
||||
* Whether or not to retry on any error
|
||||
*/
|
||||
retryOnAnyError?: boolean;
|
||||
|
||||
/**
|
||||
* Amount of tries
|
||||
*/
|
||||
tries?: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* GitLab API get issues options
|
||||
*/
|
||||
export interface ApiGetIssuesOptions {
|
||||
/**
|
||||
* Filter issues by group ID
|
||||
*/
|
||||
groupId?: number;
|
||||
/**
|
||||
* Filter issues by milestone
|
||||
*/
|
||||
milestone?: 'Backlog' | 'No Milestone';
|
||||
/**
|
||||
* Filter issues by state
|
||||
*/
|
||||
state?: IssueState;
|
||||
}
|
||||
|
||||
@@ -446,8 +465,11 @@ export class Api {
|
||||
// remove leading slash
|
||||
url = url.replace(/^\/+/g, '');
|
||||
|
||||
const options: ApiRequestOptions = {
|
||||
const options: Required<ApiRequestOptions> = {
|
||||
data: undefined,
|
||||
method: 'GET',
|
||||
retryOnAnyError: false,
|
||||
tries: 5,
|
||||
..._options,
|
||||
};
|
||||
|
||||
@@ -472,7 +494,7 @@ export class Api {
|
||||
let body;
|
||||
let tries = 0;
|
||||
|
||||
while (typeof body === 'undefined' && tries++ < 5) {
|
||||
while (typeof body === 'undefined' && tries++ < options.tries) {
|
||||
try {
|
||||
const requestUrl = url + concatenator + 'page=' + currentPage + '&per_page=100';
|
||||
|
||||
@@ -493,7 +515,7 @@ export class Api {
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
if (error.error === 'GitLab is not responding') {
|
||||
if (error.error.includes('not responding') || options.retryOnAnyError) {
|
||||
const seconds = 5;
|
||||
|
||||
logger.warn(`GitLab was not responding. Waiting ${seconds}s and retrying...`);
|
||||
@@ -511,7 +533,7 @@ export class Api {
|
||||
return;
|
||||
}
|
||||
|
||||
if (Array.isArray(apiResult) && currentPage > 1) {
|
||||
if (typeof apiResult !== 'undefined' && Array.isArray(apiResult) && currentPage > 1) {
|
||||
// add items to previously fetched items
|
||||
apiResult = apiResult.concat(body);
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user