Skip to content

Commit 95ae2ba

Browse files
committed
fix: timeout parameter being respected
1 parent f04c595 commit 95ae2ba

File tree

2 files changed

+28
-15
lines changed

2 files changed

+28
-15
lines changed

packages/vetch/__tests__/vetch.test.ts

+12-9
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,7 @@ describe('option configuration', () => {
104104
});
105105

106106
test('should default to application/json', async () => {
107-
nock(url)
108-
.matchHeader('accept', 'application/json')
109-
.get('/')
110-
.reply(200, {});
107+
nock(url).matchHeader('accept', 'application/json').get('/').reply(200, {});
111108
const res = await request({ url });
112109
expect(res.data).toEqual({});
113110
});
@@ -122,19 +119,25 @@ describe('option configuration', () => {
122119
test('should allow for our custom user agent', async () => {
123120
const options = {
124121
reqheaders: {
125-
'user-agent': (val) => {
126-
return /^@vonage\/server-sdk\/[\d].[\d].[\d].* node\/.*$/.test(
127-
val,
128-
);
122+
'user-agent': (val: string) => {
123+
return /^@vonage\/server-sdk\/[\d].[\d].[\d].* node\/.*$/.test(val);
129124
},
130125
},
131126
};
132127

133128
nock(url, options).get('/').reply(200);
134129
const inst = new Vetch();
135-
const res = await inst.request({ url });
130+
await inst.request({ url });
136131
expect(nock.isDone()).toBeTruthy();
137132
});
133+
134+
test('should timeout', async () => {
135+
nock(url).get('/').delayConnection(5).reply(200);
136+
const inst = new Vetch({ timeout: 1 });
137+
await expect(inst.request({ url })).rejects.toThrow(
138+
'network timeout at: https://just-testing.com/',
139+
);
140+
});
138141
});
139142

140143
describe('data handling', () => {

packages/vetch/lib/vetch.ts

+16-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { stringify } from 'querystring';
33
import merge from 'lodash.merge';
44
import http from 'http';
55
import https from 'https';
6-
import URL from 'url';
76
import { VetchError } from './types/vetchError';
87
import { Headers } from './interfaces/headers';
98
import { VetchResponse } from './interfaces/vetchResponse';
@@ -26,9 +25,20 @@ export class Vetch {
2625
private async _defaultAdapter<T>(
2726
opts: VetchOptions,
2827
): Promise<VetchResponse<T>> {
29-
const res = await fetch(opts.url, opts);
30-
const data = await this.getResponseData(opts, res);
31-
return this.createResponse(opts, res, data);
28+
const { timeout } = opts;
29+
let timeoutId = null;
30+
if (timeout) {
31+
const controller = new AbortController();
32+
timeoutId = setTimeout(() => controller.abort(), timeout);
33+
}
34+
35+
try {
36+
const res = await fetch(opts.url, opts);
37+
const data = await this.getResponseData(opts, res);
38+
return this.createResponse(opts, res, data);
39+
} finally {
40+
clearTimeout(timeoutId);
41+
}
3242
}
3343

3444
async request<T>(opts: VetchOptions = {}): Promise<VetchResponse<T>> {
@@ -116,10 +126,10 @@ export class Vetch {
116126

117127
// Allow a custom timeout to be used
118128
const httpAgent = new http.Agent({
119-
timeout: this.defaults.timeout,
129+
timeout: opts.timeout,
120130
});
121131
const httpsAgent = new https.Agent({
122-
timeout: this.defaults.timeout,
132+
timeout: opts.timeout,
123133
});
124134
opts.agent = (parsedUrl: URL): https.Agent | http.Agent => {
125135
if (parsedUrl.protocol === 'http:') {

0 commit comments

Comments
 (0)