plugin-utils.ts 1.4 KB

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