UNPKG

3.58 kBJavaScriptView Raw
1const path = require('path')
2const consola = require('consola')
3const defu = require('defu')
4
5const logger = consola.withScope('nuxt:axios')
6
7function axiosModule (_moduleOptions) {
8 const { nuxt } = this
9
10 // Combine options
11 const moduleOptions = {
12 ...nuxt.options.axios,
13 ..._moduleOptions,
14 ...(nuxt.options.runtimeConfig && nuxt.options.runtimeConfig.axios)
15 }
16
17 // Default port
18 const defaultPort =
19 process.env.API_PORT ||
20 moduleOptions.port ||
21 process.env.PORT ||
22 process.env.npm_package_config_nuxt_port ||
23 (this.options.server && this.options.server.port) ||
24 3000
25
26 // Default host
27 let defaultHost =
28 process.env.API_HOST ||
29 moduleOptions.host ||
30 process.env.HOST ||
31 process.env.npm_package_config_nuxt_host ||
32 (this.options.server && this.options.server.host) ||
33 'localhost'
34
35 /* istanbul ignore if */
36 if (defaultHost === '0.0.0.0') {
37 defaultHost = 'localhost'
38 }
39
40 // Default prefix
41 const prefix = process.env.API_PREFIX || moduleOptions.prefix || '/'
42
43 // HTTPS
44 const https = Boolean(this.options.server && this.options.server.https)
45
46 // Headers
47 const headers = {
48 common: {
49 Accept: 'application/json, text/plain, */*'
50 },
51 delete: {},
52 get: {},
53 head: {},
54 post: {},
55 put: {},
56 patch: {}
57 }
58
59 // Support baseUrl alternative
60 if (moduleOptions.baseUrl) {
61 moduleOptions.baseURL = moduleOptions.baseUrl
62 delete moduleOptions.baseUrl
63 }
64 if (moduleOptions.browserBaseUrl) {
65 moduleOptions.browserBaseURL = moduleOptions.browserBaseUrl
66 delete moduleOptions.browserBaseUrl
67 }
68
69 // Apply defaults
70 const options = defu(moduleOptions, {
71 baseURL: `http://${defaultHost}:${defaultPort}${prefix}`,
72 browserBaseURL: undefined,
73 credentials: false,
74 debug: false,
75 progress: true,
76 proxyHeaders: true,
77 proxyHeadersIgnore: [
78 'accept',
79 'cf-connecting-ip',
80 'cf-ray',
81 'content-length',
82 'content-md5',
83 'content-type',
84 'host',
85 'x-forwarded-host',
86 'x-forwarded-port',
87 'x-forwarded-proto'
88 ],
89 proxy: false,
90 retry: false,
91 https,
92 headers
93 })
94
95 // ENV overrides
96
97 /* istanbul ignore if */
98 if (process.env.API_URL) {
99 options.baseURL = process.env.API_URL
100 }
101
102 /* istanbul ignore if */
103 if (process.env.API_URL_BROWSER) {
104 options.browserBaseURL = process.env.API_URL_BROWSER
105 }
106
107 // Default browserBaseURL
108 if (typeof options.browserBaseURL === 'undefined') {
109 options.browserBaseURL = options.proxy ? prefix : options.baseURL
110 }
111
112 // Normalize options
113 if (options.retry === true) {
114 options.retry = {}
115 }
116
117 // Convert http:// to https:// if https option is on
118 if (options.https === true) {
119 const https = s => s.replace('http://', 'https://')
120 options.baseURL = https(options.baseURL)
121 options.browserBaseURL = https(options.browserBaseURL)
122 }
123
124 // globalName
125 options.globalName = this.nuxt.options.globalName || 'nuxt'
126
127 // Register plugin
128 this.addPlugin({
129 src: path.resolve(__dirname, 'plugin.js'),
130 fileName: 'axios.js',
131 options
132 })
133
134 // Proxy integration
135 if (options.proxy) {
136 this.requireModule([
137 '@nuxtjs/proxy',
138 typeof options.proxy === 'object' ? options.proxy : {}
139 ])
140 }
141
142 // Set _AXIOS_BASE_URL_ for dynamic SSR baseURL
143 process.env._AXIOS_BASE_URL_ = options.baseURL
144
145 logger.debug(`baseURL: ${options.baseURL}`)
146 logger.debug(`browserBaseURL: ${options.browserBaseURL}`)
147}
148
149module.exports = axiosModule
150module.exports.meta = require('../package.json')