1 | const Proxy = require('http-proxy-middleware')
|
2 | const consola = require('consola')
|
3 |
|
4 | const logProvider = consola.withScope('nuxt:proxy')
|
5 |
|
6 |
|
7 | logProvider.info = logProvider.debug
|
8 |
|
9 | module.exports = function nuxtProxy (options) {
|
10 | if (!this.options.proxy) {
|
11 |
|
12 | return
|
13 | }
|
14 |
|
15 | this.nuxt.hook('generate:before', () => {
|
16 | consola.warn('This module does not work in generated mode')
|
17 | })
|
18 |
|
19 |
|
20 | const defaults = Object.assign(
|
21 | {
|
22 |
|
23 | changeOrigin: true,
|
24 |
|
25 | ws: true,
|
26 |
|
27 | logProvider: () => logProvider
|
28 | },
|
29 | options
|
30 | )
|
31 |
|
32 | delete defaults.src
|
33 |
|
34 |
|
35 | const applyDefaults = o => Object.assign({}, defaults, o)
|
36 | const normalizeTarget = o => (typeof o === 'object' ? o : { target: o })
|
37 |
|
38 | const proxy = []
|
39 | if (Array.isArray(this.options.proxy)) {
|
40 |
|
41 | this.options.proxy.forEach(p => {
|
42 | if (Array.isArray(p)) {
|
43 | proxy.push([p[0], applyDefaults(normalizeTarget(p[1]))])
|
44 | } else {
|
45 | proxy.push([p, applyDefaults()])
|
46 | }
|
47 | })
|
48 | } else {
|
49 |
|
50 | Object.keys(this.options.proxy).forEach(context => {
|
51 | proxy.push([
|
52 | context,
|
53 | applyDefaults(normalizeTarget(this.options.proxy[context]))
|
54 | ])
|
55 | })
|
56 | }
|
57 |
|
58 |
|
59 | proxy.forEach(args => {
|
60 |
|
61 | const middleware = Proxy.apply(undefined, args)
|
62 | middleware.prefix = false
|
63 | this.options.serverMiddleware.push(middleware)
|
64 | })
|
65 | }
|
66 |
|
67 | module.exports.meta = require('../package.json')
|