feat: support setting timeout and disable by default (#51)

master
Alessandro Giordo 2019-05-29 23:14:54 +01:00 committed by Pooya Parsa
parent fba27f23ac
commit 53287ddf80
6 changed files with 45 additions and 9 deletions

View File

@ -110,6 +110,30 @@ By default, number of retries will be **2 times**, if `retry` value is set to `t
http: {
retry: 1
}
```
### `serverTimeout`
* Default: `false`
Sets the timeout for the server requests in milliseconds.
```js
http: {
serverTimeout: 2000
}
```
### `clientTimeout`
* Default: `false`
Sets the timeout for the client requests in milliseconds.
```js
http: {
clientTimeout: 5000
}
```
### `proxyHeaders`

View File

@ -39,6 +39,8 @@ function httpModule(_moduleOptions) {
proxyHeadersIgnore: ['accept', 'host', 'cf-ray', 'cf-connecting-ip'],
proxy: false,
retry: false,
serverTimeout: false,
clientTimeout: false,
https: false,
...moduleOptions
}

View File

@ -10,7 +10,6 @@ class HTTP {
this._defaults = {
hooks: {},
headers: _headers,
retry: 0,
...defaults
}
@ -104,6 +103,7 @@ export default (ctx, inject) => {
// Defaults
const defaults = {
retry: <%= parseInt(options.retry) %>,
timeout: process.server ? <%= parseInt(options.serverTimeout) %> : <%= parseInt(options.clientTimeout) %>,
prefixUrl,
headers: {}
}

View File

@ -1,6 +1,6 @@
const { setupMockNuxt } = require('./_utils')
describe('empty config', () => {
describe('defaults', () => {
let nuxt
beforeAll(async () => {
@ -9,11 +9,13 @@ describe('empty config', () => {
})
})
test('preset baseURL and browserBaseURL', () => {
test('should render template with defaults', () => {
expect(nuxt.moduleContainer.addTemplate).toBeDefined()
const call = nuxt.moduleContainer.addTemplate.mock.calls.find(args => args[0].src.includes('plugin.js'))
const options = call[0].options
expect(options.baseURL.toString()).toBe('http://localhost:3000/')
expect(options.browserBaseURL.toString()).toBe('http://localhost:3000/')
expect(options.baseURL).toBe('http://localhost:3000/')
expect(options.browserBaseURL).toBe('http://localhost:3000/')
expect(options.clientTimeout).toBe(false)
expect(options.serverTimeout).toBe(false)
})
})

View File

@ -15,8 +15,11 @@ module.exports = {
],
http: {
prefix: '/test_api',
serverTimeout: 10000,
clientTimeout: 25000,
proxy: true,
retry: 1
retry: 1,
https: true
},
build: {
terser: false

View File

@ -1,21 +1,26 @@
const { setupMockNuxt } = require('./_utils')
describe('browserBaseURL', () => {
describe('with-config', () => {
let nuxt
beforeAll(async () => {
nuxt = await setupMockNuxt({
http: {
browserBaseURL: '/test_api'
browserBaseURL: '/test_api',
retry: true,
serverTimeout: 10000,
clientTimeout: 25000
}
})
})
test('browserBaseURL', () => {
test('should render template with provided config', () => {
expect(nuxt.moduleContainer.addTemplate).toBeDefined()
const call = nuxt.moduleContainer.addTemplate.mock.calls.find(args => args[0].src.includes('plugin.js'))
const options = call[0].options
expect(options.baseURL).toBe('http://localhost:3000/')
expect(options.browserBaseURL).toBe('/test_api')
expect(options.clientTimeout).toBe(10000)
expect(options.serverTimeout).toBe(25000)
})
})