Skip to content

Commit 5604567

Browse files
committed
Wrap fetch with timeout
1 parent 46e1619 commit 5604567

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export async function get(url: string, timeout: number = 30000) {
2+
const c = new AbortController();
3+
const id = setTimeout(() => c.abort(), timeout);
4+
const res = await fetch(url, { signal: c.signal });
5+
clearTimeout(id);
6+
return res;
7+
}

netlify/edge-functions/common/share-api.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { APIDefinitions } from "./definition.ts";
2+
import * as Http from "./http";
23

34
type APIProject = {
45
owner: { handle: string };
@@ -85,7 +86,7 @@ const ShareAPI = {
8586
getProfile: async (handle: string): Promise<APIProfile> => {
8687
const url = `${ShareAPI.baseURL}/users/${apiHandle(handle)}`;
8788

88-
return fetch(url).then(async (resp) => {
89+
return Http.get(url).then(async (resp) => {
8990
if (!resp.ok) {
9091
throw await error(url, resp);
9192
}
@@ -100,7 +101,7 @@ const ShareAPI = {
100101
): Promise<APIProject> => {
101102
const url = ShareAPI.projectBaseUrl(handle, projectSlug);
102103

103-
return fetch(url).then(async (resp) => {
104+
return Http.get(url).then(async (resp) => {
104105
if (!resp.ok) {
105106
throw await error(url, resp);
106107
}
@@ -119,7 +120,7 @@ const ShareAPI = {
119120
projectSlug,
120121
`/contributions/${contribRef}`,
121122
);
122-
return fetch(url).then(async (resp) => {
123+
return Http.get(url).then(async (resp) => {
123124
if (!resp.ok) {
124125
throw await error(url, resp);
125126
}
@@ -138,7 +139,7 @@ const ShareAPI = {
138139
projectSlug,
139140
`/tickets/${ticketRef}`,
140141
);
141-
return fetch(url).then(async (resp) => {
142+
return Http.get(url).then(async (resp) => {
142143
if (!resp.ok) {
143144
throw await error(url, resp);
144145
}
@@ -158,7 +159,7 @@ const ShareAPI = {
158159
`/releases/${version}`,
159160
);
160161

161-
return fetch(url).then(async (resp) => {
162+
return Http.get(url).then(async (resp) => {
162163
if (!resp.ok) {
163164
throw await error(url, resp);
164165
}
@@ -186,7 +187,7 @@ const ShareAPI = {
186187
url = mkUrl(branchRef);
187188
}
188189

189-
return fetch(url).then(async (resp) => {
190+
return Http.get(url).then(async (resp) => {
190191
if (!resp.ok) {
191192
throw await error(url, resp);
192193
}

0 commit comments

Comments
 (0)