1 | 'use strict';
|
2 |
|
3 | const httpProxyMiddleware = require('http-proxy-middleware');
|
4 |
|
5 | function getProxyEntries(proxyOptions, defaults) {
|
6 | const applyDefaults = (opts) => ({...defaults, ...opts});
|
7 | const normalizeTarget = (input) => typeof input === "object" ? input : {target: input};
|
8 | const proxyEntries = [];
|
9 | if (!proxyOptions) {
|
10 | return proxyEntries;
|
11 | }
|
12 | if (!Array.isArray(proxyOptions)) {
|
13 | for (const key in proxyOptions) {
|
14 | proxyEntries.push({
|
15 | context: key,
|
16 | options: applyDefaults(normalizeTarget(proxyOptions[key]))
|
17 | });
|
18 | }
|
19 | return proxyEntries;
|
20 | }
|
21 | for (const input of proxyOptions) {
|
22 | if (Array.isArray(input)) {
|
23 | proxyEntries.push({
|
24 | context: input[0],
|
25 | options: applyDefaults(normalizeTarget(input[1]))
|
26 | });
|
27 | } else {
|
28 | proxyEntries.push({
|
29 | context: input,
|
30 | options: applyDefaults()
|
31 | });
|
32 | }
|
33 | }
|
34 | return proxyEntries;
|
35 | }
|
36 |
|
37 | const proxyModule = function(options2) {
|
38 | const nuxt = this.nuxt;
|
39 | if (!nuxt.options.server || !nuxt.options.proxy) {
|
40 | return;
|
41 | }
|
42 | const defaults = {
|
43 | changeOrigin: true,
|
44 | ws: true,
|
45 | ...options2
|
46 | };
|
47 | const proxyEntries = getProxyEntries(nuxt.options.proxy, defaults);
|
48 | for (const proxyEntry of proxyEntries) {
|
49 | this.addServerMiddleware({
|
50 | prefix: false,
|
51 | handler: httpProxyMiddleware.createProxyMiddleware(proxyEntry.context, proxyEntry.options)
|
52 | });
|
53 | }
|
54 | };
|
55 | proxyModule.meta = require("../package.json");
|
56 |
|
57 | module.exports = proxyModule;
|