Skip to content

Commit 53287dd

Browse files
bovas85Pooya Parsa
authored andcommitted
feat: support setting timeout and disable by default (#51)
1 parent fba27f2 commit 53287dd

6 files changed

Lines changed: 45 additions & 9 deletions

File tree

docs/api/readme.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,30 @@ By default, number of retries will be **2 times**, if `retry` value is set to `t
110110
http: {
111111
retry: 1
112112
}
113+
114+
```
115+
### `serverTimeout`
116+
117+
* Default: `false`
118+
119+
Sets the timeout for the server requests in milliseconds.
120+
121+
```js
122+
http: {
123+
serverTimeout: 2000
124+
}
125+
```
126+
127+
### `clientTimeout`
128+
129+
* Default: `false`
130+
131+
Sets the timeout for the client requests in milliseconds.
132+
133+
```js
134+
http: {
135+
clientTimeout: 5000
136+
}
113137
```
114138

115139
### `proxyHeaders`

lib/module.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ function httpModule(_moduleOptions) {
3939
proxyHeadersIgnore: ['accept', 'host', 'cf-ray', 'cf-connecting-ip'],
4040
proxy: false,
4141
retry: false,
42+
serverTimeout: false,
43+
clientTimeout: false,
4244
https: false,
4345
...moduleOptions
4446
}

lib/plugin.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ class HTTP {
1010
this._defaults = {
1111
hooks: {},
1212
headers: _headers,
13-
retry: 0,
1413
...defaults
1514
}
1615

@@ -104,6 +103,7 @@ export default (ctx, inject) => {
104103
// Defaults
105104
const defaults = {
106105
retry: <%= parseInt(options.retry) %>,
106+
timeout: process.server ? <%= parseInt(options.serverTimeout) %> : <%= parseInt(options.clientTimeout) %>,
107107
prefixUrl,
108108
headers: {}
109109
}
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const { setupMockNuxt } = require('./_utils')
22

3-
describe('empty config', () => {
3+
describe('defaults', () => {
44
let nuxt
55

66
beforeAll(async () => {
@@ -9,11 +9,13 @@ describe('empty config', () => {
99
})
1010
})
1111

12-
test('preset baseURL and browserBaseURL', () => {
12+
test('should render template with defaults', () => {
1313
expect(nuxt.moduleContainer.addTemplate).toBeDefined()
1414
const call = nuxt.moduleContainer.addTemplate.mock.calls.find(args => args[0].src.includes('plugin.js'))
1515
const options = call[0].options
16-
expect(options.baseURL.toString()).toBe('http://localhost:3000/')
17-
expect(options.browserBaseURL.toString()).toBe('http://localhost:3000/')
16+
expect(options.baseURL).toBe('http://localhost:3000/')
17+
expect(options.browserBaseURL).toBe('http://localhost:3000/')
18+
expect(options.clientTimeout).toBe(false)
19+
expect(options.serverTimeout).toBe(false)
1820
})
1921
})

test/fixture/nuxt.config.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@ module.exports = {
1515
],
1616
http: {
1717
prefix: '/test_api',
18+
serverTimeout: 10000,
19+
clientTimeout: 25000,
1820
proxy: true,
19-
retry: 1
21+
retry: 1,
22+
https: true
2023
},
2124
build: {
2225
terser: false
Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,26 @@
11
const { setupMockNuxt } = require('./_utils')
22

3-
describe('browserBaseURL', () => {
3+
describe('with-config', () => {
44
let nuxt
55

66
beforeAll(async () => {
77
nuxt = await setupMockNuxt({
88
http: {
9-
browserBaseURL: '/test_api'
9+
browserBaseURL: '/test_api',
10+
retry: true,
11+
serverTimeout: 10000,
12+
clientTimeout: 25000
1013
}
1114
})
1215
})
1316

14-
test('browserBaseURL', () => {
17+
test('should render template with provided config', () => {
1518
expect(nuxt.moduleContainer.addTemplate).toBeDefined()
1619
const call = nuxt.moduleContainer.addTemplate.mock.calls.find(args => args[0].src.includes('plugin.js'))
1720
const options = call[0].options
1821
expect(options.baseURL).toBe('http://localhost:3000/')
1922
expect(options.browserBaseURL).toBe('/test_api')
23+
expect(options.clientTimeout).toBe(10000)
24+
expect(options.serverTimeout).toBe(25000)
2025
})
2126
})

0 commit comments

Comments
 (0)