master
Pooya Parsa 2017-08-13 18:17:38 +04:30
parent 4e4efec85c
commit 5e919381eb
2 changed files with 14 additions and 12 deletions

View File

@ -3,13 +3,14 @@ const path = require('path')
const { hostname } = require('os')
const { URL } = require('whatwg-url')
const port = process.env.PORT || process.env.npm_package_config_nuxt_port || 3000
let host = process.env.HOST || process.env.npm_package_config_nuxt_host || 'localhost'
if (host === '0.0.0.0') {
host = hostname()
}
module.exports = function nuxtAxios (moduleOptions) {
const port = process.env.PORT || process.env.npm_package_config_nuxt_port || 3000
let host = process.env.HOST || process.env.npm_package_config_nuxt_host || 'localhost'
/* istanbul ignore if */
if (host === '0.0.0.0') {
host = hostname()
}
// Apply defaults
const defaults = {
baseURL: `http://${host}:${port}/api`,
@ -23,10 +24,12 @@ module.exports = function nuxtAxios (moduleOptions) {
const options = Object.assign({}, defaults, this.options.axios, moduleOptions)
// Override env
/* istanbul ignore if */
if (process.env.API_URL) {
options.baseURL = process.env.API_URL
}
/* istanbul ignore if */
if (process.env.API_URL_BROWSER) {
options.browserBaseURL = process.env.API_URL_BROWSER
}

View File

@ -1,10 +1,11 @@
process.env.PORT = '5060'
jasmine.DEFAULT_TIMEOUT_INTERVAL = 20000
process.env.PORT = process.env.PORT || 5060
process.env.NODE_ENV = 'production'
const { Nuxt, Builder } = require('nuxt')
const axios = require('axios')
const config = require('./fixture/nuxt.config')
process.env.NODE_ENV = 'production'
const url = path => `http://localhost:${process.env.PORT}${path}`
describe('axios module', () => {
@ -12,8 +13,6 @@ describe('axios module', () => {
let addTemplate
beforeAll(async () => {
jasmine.DEFAULT_TIMEOUT_INTERVAL = 20000
config.modules.unshift(function () {
addTemplate = this.addTemplate = jest.fn(this.addTemplate)
})
@ -31,7 +30,7 @@ describe('axios module', () => {
let call = addTemplate.mock.calls.find(args => args[0].src.includes('plugin.template.js'))
expect(call).toBeDefined()
let options = call[0].options
expect(options.baseURL.toString()).toBe('http://localhost:5060/test_api')
expect(options.baseURL.toString()).toBe(`http://localhost:${process.env.PORT}/test_api`)
expect(options.browserBaseURL.toString()).toBe('/test_api')
})