/
plugin-utils.ts
42 lines (37 loc) · 1.38 KB
/
plugin-utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import proxy from 'http-proxy-middleware';
import { notNullOrUndefined } from '../../../shared/shared-utils';
import { APIExtensionDefinition, VendurePlugin } from '../config';
export interface ProxyOptions {
route: string;
port: number;
hostname?: string;
}
/**
* Configures the proxy middleware which will be passed to the main Vendure server. This
* will proxy all asset requests to the dedicated asset server.
*/
export function createProxyHandler(options: ProxyOptions, logging: boolean) {
const route = options.route.charAt(0) === '/' ? options.route : '/' + options.route;
const proxyHostname = options.hostname || 'localhost';
return proxy({
// TODO: how do we detect https?
target: `http://${proxyHostname}:${options.port}`,
pathRewrite: {
[`^${route}`]: `/`,
},
logLevel: logging ? 'info' : 'silent',
});
}
/**
* Given an array of VendurePlugins, returns a flattened array of all APIExtensionDefinitions.
*/
export function getPluginAPIExtensions(
plugins: VendurePlugin[],
apiType: 'shop' | 'admin',
): APIExtensionDefinition[] {
const extensions =
apiType === 'shop'
? plugins.map(p => (p.extendShopAPI ? p.extendShopAPI() : undefined))
: plugins.map(p => (p.extendAdminAPI ? p.extendAdminAPI() : undefined));
return extensions.filter(notNullOrUndefined);
}