Prechádzať zdrojové kódy

refactor(core): Export createProxyHandler from core

Michael Bromley 6 rokov pred
rodič
commit
886cc7244f

+ 0 - 2
packages/admin-ui-plugin/package.json

@@ -15,7 +15,6 @@
   "devDependencies": {
     "@types/express": "^4.0.39",
     "@types/fs-extra": "^5.0.4",
-    "@types/http-proxy-middleware": "^0.19.2",
     "@vendure/common": "~0.1.0",
     "@vendure/core": ">=0.1.0-alpha.18",
     "express": "^4.16.4",
@@ -23,7 +22,6 @@
   },
   "dependencies": {
     "fs-extra": "^7.0.1",
-    "http-proxy-middleware": "^0.19.1",
     "typescript": "^3.3.4000"
   },
   "peerDependencies": {

+ 3 - 26
packages/admin-ui-plugin/src/plugin.ts

@@ -1,9 +1,8 @@
 import { AdminUiConfig } from '@vendure/common/lib/shared-types';
-import { InjectorFn, VendureConfig, VendurePlugin } from '@vendure/core';
+import { createProxyHandler, InjectorFn, VendureConfig, VendurePlugin } from '@vendure/core';
 import express from 'express';
 import fs from 'fs-extra';
 import { Server } from 'http';
-import proxy from 'http-proxy-middleware';
 import path from 'path';
 
 /**
@@ -49,7 +48,8 @@ export interface AdminUiOptions {
  * @description
  * This plugin starts a static server for the Admin UI app, and proxies it via the `/admin/` path of the main Vendure server.
  *
- * The Admin UI allows you to administer all aspects of your store, from inventory management to order tracking. It is the tool used by store administrators on a day-to-day basis for the management of the store.
+ * The Admin UI allows you to administer all aspects of your store, from inventory management to order tracking. It is the tool used by
+ * store administrators on a day-to-day basis for the management of the store.
  *
  * ## Installation
  *
@@ -131,26 +131,3 @@ export class AdminUiPlugin implements VendurePlugin {
         throw new Error(`AdminUiPlugin: admin-ui app not found`);
     }
 }
-
-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.
- */
-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',
-    });
-}

+ 0 - 2
packages/asset-server-plugin/package.json

@@ -14,7 +14,6 @@
   "devDependencies": {
     "@types/express": "^4.0.39",
     "@types/fs-extra": "^5.0.4",
-    "@types/http-proxy-middleware": "^0.19.2",
     "@types/sharp": "^0.22.1",
     "@vendure/common": "~0.1.0",
     "@vendure/core": ">=0.1.0-alpha.18",
@@ -23,7 +22,6 @@
   },
   "dependencies": {
     "fs-extra": "^7.0.1",
-    "http-proxy-middleware": "^0.19.1",
     "sharp": "^0.22.0",
     "typescript": "^3.3.4000"
   },

+ 4 - 26
packages/asset-server-plugin/src/plugin.ts

@@ -1,7 +1,6 @@
-import { AssetStorageStrategy, InjectorFn, LocalAssetStorageStrategy, VendureConfig, VendurePlugin } from '@vendure/core';
+import { AssetStorageStrategy, createProxyHandler, InjectorFn, LocalAssetStorageStrategy, VendureConfig, VendurePlugin } from '@vendure/core';
 import express, { NextFunction, Request, Response } from 'express';
 import { Server } from 'http';
-import proxy from 'http-proxy-middleware';
 import path from 'path';
 
 import { SharpAssetPreviewStrategy } from './sharp-asset-preview-strategy';
@@ -212,7 +211,9 @@ export class AssetServerPlugin implements VendurePlugin {
     }
 
     onClose(): Promise<void> {
-        return new Promise(resolve => { this.server.close(() => resolve()); });
+        return new Promise(resolve => {
+            this.server.close(() => resolve());
+        });
     }
 
     private createAssetStorageStrategy() {
@@ -292,26 +293,3 @@ export class AssetServerPlugin implements VendurePlugin {
         return `${baseName}${suffix}${ext}`;
     }
 }
-
-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.
- */
-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',
-    });
-}

+ 1 - 0
packages/core/package.json

@@ -54,6 +54,7 @@
     "graphql-tag": "^2.10.0",
     "graphql-tools": "^4.0.3",
     "graphql-type-json": "^0.2.1",
+    "http-proxy-middleware": "^0.19.1",
     "i18next": "^13.1.0",
     "i18next-express-middleware": "^1.7.1",
     "i18next-icu": "^1.0.1",

+ 1 - 0
packages/core/src/plugin/index.ts

@@ -1 +1,2 @@
 export * from './default-search-plugin/default-search-plugin';
+export { createProxyHandler, ProxyOptions } from './plugin-utils';

+ 3 - 2
packages/core/src/plugin/plugin-utils.ts

@@ -10,8 +10,9 @@ export interface ProxyOptions {
 }
 
 /**
- * Configures the proxy middleware which will be passed to the main Vendure server. This
- * will proxy all asset requests to the dedicated asset server.
+ * Creates a proxy middleware which proxies the given route to the given port.
+ * Useful for plugins which start their own servers but should be accessible
+ * via the main Vendure url.
  */
 export function createProxyHandler(options: ProxyOptions, logging: boolean) {
     const route = options.route.charAt(0) === '/' ? options.route : '/' + options.route;

+ 1 - 1
yarn.lock

@@ -1223,7 +1223,7 @@
     "@types/node" "*"
     "@types/range-parser" "*"
 
-"@types/express@*", "@types/express@4.16.1", "@types/express@^4.0.39":
+"@types/express@*", "@types/express@4.16.1", "@types/express@^4.0.39", "@types/express@^4.16.1":
   version "4.16.1"
   resolved "https://registry.yarnpkg.com/@types/express/-/express-4.16.1.tgz#d756bd1a85c34d87eaf44c888bad27ba8a4b7cf0"
   integrity sha512-V0clmJow23WeyblmACoxbHBu2JKlE5TiIme6Lem14FnPW9gsttyHtk6wq7njcdIWH1njAaFgR8gW09lgY98gQg==