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: { http: {
retry: 1 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` ### `proxyHeaders`

View File

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

View File

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

View File

@ -1,6 +1,6 @@
const { setupMockNuxt } = require('./_utils') const { setupMockNuxt } = require('./_utils')
describe('empty config', () => { describe('defaults', () => {
let nuxt let nuxt
beforeAll(async () => { 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() expect(nuxt.moduleContainer.addTemplate).toBeDefined()
const call = nuxt.moduleContainer.addTemplate.mock.calls.find(args => args[0].src.includes('plugin.js')) const call = nuxt.moduleContainer.addTemplate.mock.calls.find(args => args[0].src.includes('plugin.js'))
const options = call[0].options const options = call[0].options
expect(options.baseURL.toString()).toBe('http://localhost:3000/') expect(options.baseURL).toBe('http://localhost:3000/')
expect(options.browserBaseURL.toString()).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: { http: {
prefix: '/test_api', prefix: '/test_api',
serverTimeout: 10000,
clientTimeout: 25000,
proxy: true, proxy: true,
retry: 1 retry: 1,
https: true
}, },
build: { build: {
terser: false terser: false

View File

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