chore: add timeout mechanism for fetch calls (#3649)

This commit is contained in:
Kunal Kundu
2020-05-12 19:21:13 +05:30
committed by GitHub
parent 334304ed52
commit 3e34e52440
11 changed files with 66 additions and 28 deletions

View File

@ -32,6 +32,7 @@ describe('github API', () => {
Authorization: 'Bearer token',
'Content-Type': 'application/json; charset=utf-8',
},
signal: expect.any(AbortSignal),
});
});

View File

@ -211,30 +211,31 @@ export default class GitGateway implements Implementation {
gitlab_enabled: gitlabEnabled,
bitbucket_enabled: bitbucketEnabled,
roles,
} = await fetch(`${this.gatewayUrl}/settings`, {
headers: { Authorization: `Bearer ${token}` },
}).then(async res => {
const contentType = res.headers.get('Content-Type') || '';
if (!contentType.includes('application/json') && !contentType.includes('text/json')) {
throw new APIError(
`Your Git Gateway backend is not returning valid settings. Please make sure it is enabled.`,
res.status,
'Git Gateway',
);
}
} = await unsentRequest
.fetchWithTimeout(`${this.gatewayUrl}/settings`, {
headers: { Authorization: `Bearer ${token}` },
})
.then(async res => {
const contentType = res.headers.get('Content-Type') || '';
if (!contentType.includes('application/json') && !contentType.includes('text/json')) {
throw new APIError(
`Your Git Gateway backend is not returning valid settings. Please make sure it is enabled.`,
res.status,
'Git Gateway',
);
}
const body = await res.json();
const body = await res.json();
if (!res.ok) {
throw new APIError(
`Git Gateway Error: ${body.message ? body.message : body}`,
res.status,
'Git Gateway',
);
}
if (!res.ok) {
throw new APIError(
`Git Gateway Error: ${body.message ? body.message : body}`,
res.status,
'Git Gateway',
);
}
return body;
});
return body;
});
this.acceptRoles = roles;
if (githubEnabled) {
this.backendType = 'github';

View File

@ -1,6 +1,6 @@
import { flow, fromPairs, map } from 'lodash/fp';
import minimatch from 'minimatch';
import { ApiRequest, PointerFile } from 'netlify-cms-lib-util';
import { ApiRequest, PointerFile, unsentRequest } from 'netlify-cms-lib-util';
type MakeAuthorizedRequest = (req: ApiRequest) => Promise<Response>;
@ -96,7 +96,7 @@ const getResourceUploadURLs = async (
};
const uploadBlob = (uploadURL: string, blob: Blob) =>
fetch(uploadURL, {
unsentRequest.fetchWithTimeout(uploadURL, {
method: 'PUT',
body: blob,
});